1package db
2
3import "context"
4
5// ContextKey is the key used to store the database in the context.
6var ContextKey = struct{ string }{"db"}
7
8// FromContext returns the database from the context.
9func FromContext(ctx context.Context) *DB {
10 if db, ok := ctx.Value(ContextKey).(*DB); ok {
11 return db
12 }
13 return nil
14}
15
16// WithContext returns a new context with the database.
17func WithContext(ctx context.Context, db *DB) context.Context {
18 return context.WithValue(ctx, ContextKey, db)
19}