Documentation
¶
Index ¶
- func ContextWithTx(ctx context.Context, tx *gorm.DB) context.Context
- func TxFromContext(ctx context.Context) *gorm.DB
- type Connection
- type Option
- func WithConnection(dsn string, readOnly bool) Option
- func WithConnections(connections []Connection) Option
- func WithInsertBatchSize(insertBatchSize int) Option
- func WithMaxIdle(maxIdle int) Option
- func WithMaxLifetime(maxLifetime time.Duration) Option
- func WithMaxOpen(maxOpen int) Option
- func WithPreferSimpleProtocol(preferSimpleProtocol bool) Option
- func WithPreparedStatements(enabled bool) Option
- func WithSkipDefaultTransaction(skipDefaultTransaction bool) Option
- func WithTraceConfig(traceConfig config.ConfigurationDatabaseTracing) Option
- type Options
- type Pool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithTx ¶ added in v1.95.0
ContextWithTx returns a context with the supplied *gorm.DB bound as the request transaction. Public so tests and custom middleware can drive the binding directly; the normal path is WithRequestTx.
func TxFromContext ¶ added in v1.95.0
TxFromContext returns the request-scoped transaction bound by WithRequestTx, or nil if none is bound. Pool.DB honours the binding automatically; this accessor is exposed for code that needs to observe whether a request is already inside a tenancy transaction.
Types ¶
type Connection ¶
Connection represents a single database connection configuration.
type Option ¶
type Option func(*Options)
Option configures database connection settings.
func WithConnection ¶
WithConnection returns an Option to configure the database connection dsn. Multiple calls with the same DSN but different readOnly flags are supported.
func WithConnections ¶
func WithConnections(connections []Connection) Option
WithConnections returns an Option to configure database connections from a slice. Supports adding multiple connections including the same DSN with different readOnly flags.
func WithInsertBatchSize ¶ added in v1.63.3
WithInsertBatchSize returns an Option to configure the database connection insert batch size.
func WithMaxIdle ¶
WithMaxIdle returns an Option to configure the database connection max idle connections.
func WithMaxLifetime ¶
WithMaxLifetime returns an Option to configure the database connection max lifetime.
func WithMaxOpen ¶
WithMaxOpen returns an Option to configure the database connection max open connections.
func WithPreferSimpleProtocol ¶
WithPreferSimpleProtocol returns an Option to configure the database connection prefer simple protocol.
func WithPreparedStatements ¶ added in v1.64.2
WithPreparedStatements returns an Option to enable or disable the prepared statement cache.
func WithSkipDefaultTransaction ¶
WithSkipDefaultTransaction returns an Option to configure the database connection skip default transaction.
func WithTraceConfig ¶
func WithTraceConfig(traceConfig config.ConfigurationDatabaseTracing) Option
WithTraceConfig returns an Option to configure the database connection trace config.
type Options ¶
type Options struct {
Connections []Connection
MaxOpen int
MaxIdle int
MaxLifetime time.Duration
PreferSimpleProtocol bool
SkipDefaultTransaction bool
TraceConfig config.ConfigurationDatabaseTracing
InsertBatchSize int
PreparedStatements bool
}
Options holds Datastore connection configuration.
type Pool ¶
type Pool interface {
DB(ctx context.Context, readOnly bool) *gorm.DB
// WithTenancy runs fn inside a database transaction in which the
// Postgres session variables app.tenant_id and app.partition_id have
// been populated from the auth claims attached to ctx. Combined with
// Row-Level Security policies that consult current_setting() on
// every tenancy-scoped table, this means callers can write SQL that
// makes no mention of tenant_id / partition_id — the database
// enforces isolation transparently.
//
// When ctx carries no claims (system services, migrations) or the
// tenancy-checks-skipped flag is set, the session variables are left
// unset and the RLS policy's NULL-OK branch keeps the row visible.
// Same behaviour as the auto-applied TenancyPartition scope, so the
// contract is consistent between GORM query-builder paths and
// Raw-SQL paths.
//
// Use this for any Raw SQL or multi-table-join report where you do
// not want tenancy logic in the application. For trivially-scoped
// GORM-builder paths (.Model(&X{}).Where(...).Find(...)), the
// auto-applied TenancyPartition scope already filters correctly and
// this helper is unnecessary.
WithTenancy(ctx context.Context, readOnly bool, fn func(tx *gorm.DB) error) error
// WithRequestTx is the per-request middleware primitive. It opens a
// tenancy-scoped transaction the same way WithTenancy does, then
// binds the transaction to a child context (via ContextWithTx) and
// invokes fn with the bound context. Inside fn, callers can use
// pool.DB(ctx, _) and receive the bound transaction transparently —
// application code never references tenant_id or partition_id and
// never wraps individual queries in WithTenancy.
//
// Commits when fn returns nil, rolls back on error. Nesting is
// safe: a nested WithRequestTx reuses the outer transaction
// instead of opening a new one.
WithRequestTx(ctx context.Context, fn func(ctx context.Context) error) error
AddConnection(ctx context.Context, opts ...Option) error
CanMigrate() bool
SaveMigration(ctx context.Context, migrationPatches ...*migration.Patch) error
// Migrate finds missing migrations and records them in the database.
Migrate(ctx context.Context, migrationsDirPath string, migrations ...any) error
Close(ctx context.Context)
}