pool

package
v1.95.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTx added in v1.95.0

func ContextWithTx(ctx context.Context, tx *gorm.DB) context.Context

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

func TxFromContext(ctx context.Context) *gorm.DB

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

type Connection struct {
	DSN      string
	ReadOnly bool
}

Connection represents a single database connection configuration.

type Option

type Option func(*Options)

Option configures database connection settings.

func WithConnection

func WithConnection(dsn string, readOnly bool) Option

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

func WithInsertBatchSize(insertBatchSize int) Option

WithInsertBatchSize returns an Option to configure the database connection insert batch size.

func WithMaxIdle

func WithMaxIdle(maxIdle int) Option

WithMaxIdle returns an Option to configure the database connection max idle connections.

func WithMaxLifetime

func WithMaxLifetime(maxLifetime time.Duration) Option

WithMaxLifetime returns an Option to configure the database connection max lifetime.

func WithMaxOpen

func WithMaxOpen(maxOpen int) Option

WithMaxOpen returns an Option to configure the database connection max open connections.

func WithPreferSimpleProtocol

func WithPreferSimpleProtocol(preferSimpleProtocol bool) Option

WithPreferSimpleProtocol returns an Option to configure the database connection prefer simple protocol.

func WithPreparedStatements added in v1.64.2

func WithPreparedStatements(enabled bool) Option

WithPreparedStatements returns an Option to enable or disable the prepared statement cache.

func WithSkipDefaultTransaction

func WithSkipDefaultTransaction(skipDefaultTransaction bool) Option

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)
}

func NewPool

func NewPool(_ context.Context) Pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL