db

package
v0.83.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause, AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const SqliteFileName = "store.db"

SqliteFileName is the default SQLite database file inside the data directory.

Variables

View Source
var DefaultPoolConfig = PoolConfig{
	MaxConns:          30,
	MinConns:          1,
	MaxConnLifetime:   60 * time.Minute,
	HealthCheckPeriod: time.Minute,
}

Functions

func GormConfig

func GormConfig() *gorm.Config

GormConfig is the configuration every engine is opened with.

func OpenPgxPool added in v0.82.0

func OpenPgxPool(ctx context.Context, dsn string, cfg PoolConfig) (*pgxpool.Pool, error)

OpenPgxPool opens a pgx pool to dsn sized by cfg, applying the configured timeouts to every session, and verifies it with a ping.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn is a database connection the repositories run on: one gorm handle, the pgx pool of a Postgres deployment and the engine they talk to.

func NewConn

func NewConn(ctx context.Context, gormDB *gorm.DB, engine Engine, pool *pgxpool.Pool, opts ...Option) (*Conn, error)

NewConn takes ownership of an open gorm handle and pool once it returns without error, applying the connection limits and transaction timeout configured through the environment and opts.

func OpenPostgres

func OpenPostgres(ctx context.Context, dsn string, pool PoolConfig, opts ...Option) (*Conn, error)

OpenPostgres opens a Postgres database through gorm and a pgx pool sized by pool, applying the configured timeouts to every session.

func OpenPostgresWithoutPool added in v0.82.0

func OpenPostgresWithoutPool(ctx context.Context, dsn string, opts ...Option) (*Conn, error)

OpenPostgresWithoutPool opens a Postgres database through gorm alone, for repositories that never take the pgx read paths.

func OpenSqlite

func OpenSqlite(ctx context.Context, dataDir string) (*Conn, error)

OpenSqlite opens the SQLite database in dataDir, or the file named by NB_STORE_ENGINE_SQLITE_FILE.

func OpenSqliteFile

func OpenSqliteFile(ctx context.Context, dataDir, storeFile string) (*Conn, error)

OpenSqliteFile opens the SQLite database storeFile, resolved against dataDir when relative. storeFile may carry SQLite URI query parameters.

func (*Conn) AutoMigrate

func (c *Conn) AutoMigrate(models ...any) error

AutoMigrate creates or updates the tables of the given models.

func (*Conn) Close

func (c *Conn) Close() error

Close releases the gorm connection and the pgx pool.

func (*Conn) DB

func (c *Conn) DB(tx *Tx) *gorm.DB

DB returns the handle a query must run on: the transaction when tx is set, otherwise the shared connection.

func (*Conn) Engine

func (c *Conn) Engine() Engine

func (*Conn) Pool

func (c *Conn) Pool(tx *Tx) *pgxpool.Pool

Pool returns the pgx pool for read paths that bypass gorm. It is nil on engines other than Postgres and inside a transaction, where the pool would not see the uncommitted writes.

func (*Conn) RunInTx

func (c *Conn) RunInTx(ctx context.Context, fn func(tx *Tx) error) error

RunInTx runs fn in one transaction that commits when fn returns nil and rolls back otherwise, bounded by the configured transaction timeout.

func (*Conn) SetTxMetrics

func (c *Conn) SetTxMetrics(metrics TxMetrics)

SetTxMetrics registers the sink that receives transaction durations.

type Engine

type Engine string

Engine identifies the SQL engine behind a Conn.

const (
	SqliteStoreEngine   Engine = "sqlite"
	PostgresStoreEngine Engine = "postgres"
)

type LockingStrength

type LockingStrength string

LockingStrength is the row lock a query holds until its transaction ends.

const (
	LockingStrengthUpdate      LockingStrength = "UPDATE"
	LockingStrengthShare       LockingStrength = "SHARE"
	LockingStrengthNoKeyUpdate LockingStrength = "NO KEY UPDATE"
	LockingStrengthKeyShare    LockingStrength = "KEY SHARE"
	LockingStrengthNone        LockingStrength = "NONE"
)

type Option added in v0.82.0

type Option func(*connOptions)

Option tunes a Conn as it is opened.

func WithMaxOpenConns added in v0.82.0

func WithMaxOpenConns(n int) Option

WithMaxOpenConns sizes the gorm connection pool of this Conn, taking precedence over NB_SQL_MAX_OPEN_CONNS. A pgx pool keeps its PoolConfig size, and SQLite always uses one connection.

type PoolConfig

type PoolConfig struct {
	MaxConns          int32
	MinConns          int32
	MaxConnLifetime   time.Duration
	HealthCheckPeriod time.Duration
}

PoolConfig sizes the pgx pool a Postgres deployment uses for the read paths that bypass gorm.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx is an open transaction handed to repository calls; nil means autocommit.

type TxMetrics

type TxMetrics interface {
	CountTransactionDuration(duration time.Duration)
}

TxMetrics receives the duration of every committed top-level transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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