Documentation
¶
Index ¶
- Constants
- Variables
- func GormConfig() *gorm.Config
- func OpenPgxPool(ctx context.Context, dsn string, cfg PoolConfig) (*pgxpool.Pool, error)
- type Conn
- func NewConn(ctx context.Context, gormDB *gorm.DB, engine Engine, pool *pgxpool.Pool, ...) (*Conn, error)
- func OpenPostgres(ctx context.Context, dsn string, pool PoolConfig, opts ...Option) (*Conn, error)
- func OpenPostgresWithoutPool(ctx context.Context, dsn string, opts ...Option) (*Conn, error)
- func OpenSqlite(ctx context.Context, dataDir string) (*Conn, error)
- func OpenSqliteFile(ctx context.Context, dataDir, storeFile string) (*Conn, error)
- func (c *Conn) AutoMigrate(models ...any) error
- func (c *Conn) Close() error
- func (c *Conn) DB(tx *Tx) *gorm.DB
- func (c *Conn) Engine() Engine
- func (c *Conn) Pool(tx *Tx) *pgxpool.Pool
- func (c *Conn) RunInTx(ctx context.Context, fn func(tx *Tx) error) error
- func (c *Conn) SetTxMetrics(metrics TxMetrics)
- type Engine
- type LockingStrength
- type Option
- type PoolConfig
- type Tx
- type TxMetrics
Constants ¶
const SqliteFileName = "store.db"
SqliteFileName is the default SQLite database file inside the data directory.
Variables ¶
var DefaultPoolConfig = PoolConfig{ MaxConns: 30, MinConns: 1, MaxConnLifetime: 60 * time.Minute, HealthCheckPeriod: time.Minute, }
Functions ¶
func GormConfig ¶
GormConfig is the configuration every engine is opened with.
func OpenPgxPool ¶ added in v0.82.0
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 ¶
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
OpenPostgresWithoutPool opens a Postgres database through gorm alone, for repositories that never take the pgx read paths.
func OpenSqlite ¶
OpenSqlite opens the SQLite database in dataDir, or the file named by NB_STORE_ENGINE_SQLITE_FILE.
func OpenSqliteFile ¶
OpenSqliteFile opens the SQLite database storeFile, resolved against dataDir when relative. storeFile may carry SQLite URI query parameters.
func (*Conn) AutoMigrate ¶
AutoMigrate creates or updates the tables of the given models.
func (*Conn) DB ¶
DB returns the handle a query must run on: the transaction when tx is set, otherwise the shared connection.
func (*Conn) 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 ¶
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 ¶
SetTxMetrics registers the sink that receives transaction durations.
type LockingStrength ¶
type LockingStrength string
LockingStrength is the row lock a query holds until its transaction ends.
const ( LockingStrengthUpdate LockingStrength = "UPDATE" LockingStrengthNoKeyUpdate LockingStrength = "NO KEY UPDATE" 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
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.