Context

GORMはContextをサポートしています。メソッド WithContextで使用できます。

Single Session Mode

単一セッションモードは通常、単一の操作を実行するときに使用されます。

db.WithContext(ctx).Find(&users)

Continuous session mode

連続セッションモードは、通常、一連の操作を実行するときに使用されます。例:

tx := db.WithContext(ctx)
tx.First(&user, 1)
tx.Model(&user).Update("role", "admin")

Context in Hooks/Callbacks

You could access the Context object from current Statement, for example:

func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
ctx := tx.Statement.Context
// ...
return
}

Chi Middleware Example

Continuous session mode which might be helpful when handling API requests, for example, you can set up *gorm.DB with Timeout Context in middlewares, and then use the *gorm.DB when processing all requests

Following is a Chi middleware example:

func SetDBMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timeoutContext, _ := context.WithTimeout(context.Background(), time.Second)
ctx := context.WithValue(r.Context(), "DB", db.WithContext(timeoutContext))
next.ServeHTTP(w, r.WithContext(ctx))
})
}

r := chi.NewRouter()
r.Use(SetDBMiddleware)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
db, ok := ctx.Value("DB").(*gorm.DB)

var users []User
db.Find(&users)

// lots of db operations
})

r.Get("/user", func(w http.ResponseWriter, r *http.Request) {
db, ok := ctx.Value("DB").(*gorm.DB)

var user User
db.First(&user)

// lots of db operations
})

NOTE Set Context with WithContext is goroutine-safe, refer Session for details

Logger

Logger accepts Context too, you can use it for log tracking, refer Logger for details