context.go

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