package controllers import ( "context" "git.golem15.com/golem15/summercms/modules/cabana" "gorm.io/gorm" ) // requestDB is the database handle a hook or scope should read through. Inside // an admin write it is the transaction the write runs in, so the hook sees the // write's snapshot and does not take a second pool connection while the // transaction holds row locks. Outside a transaction (the list) it is the // application pool from the booted app. It is nil when neither exists. func requestDB(ctx context.Context, pool func() *gorm.DB) *gorm.DB { if tx, ok := cabana.TxFromContext(ctx); ok { return tx } if pool == nil { return nil } return pool() }