pgkit

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	QueryRow(ctx context.Context, query string, args ...any) Row
	Query(ctx context.Context, query string, args ...any) (Rows, error)
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	Close() error
}

DB is an interface for database operations

func NewDB

func NewDB(connectionString string, opts ...DBOption) (DB, error)

NewDB creates a new database connection pool from a connection string. It opens the connection pool and verifies it with a ping. Optional DBOption parameters can be provided to configure connection pooling.

type DBOption

type DBOption func(*dbOptions)

DBOption is a functional option for configuring NewDB

func WithHealthCheckPeriod

func WithHealthCheckPeriod(d time.Duration) DBOption

WithHealthCheckPeriod sets how frequently to check the health of idle connections

func WithMaxConnIdleTime

func WithMaxConnIdleTime(d time.Duration) DBOption

WithMaxConnIdleTime sets the maximum amount of time a connection may be idle

func WithMaxConnLifetime

func WithMaxConnLifetime(d time.Duration) DBOption

WithMaxConnLifetime sets the maximum amount of time a connection may be reused

func WithMaxConns

func WithMaxConns(n int32) DBOption

WithMaxConns sets the maximum number of connections in the pool

func WithMinConns

func WithMinConns(n int32) DBOption

WithMinConns sets the minimum number of connections in the pool

func WithPoolContext

func WithPoolContext(ctx context.Context) DBOption

WithPoolContext sets the context used for creating the connection pool

type FakeDB

type FakeDB struct {
	QueryRowFake func(ctx context.Context, query string, args ...any) Row
	QueryFake    func(ctx context.Context, query string, args ...any) (Rows, error)
	ExecFake     func(ctx context.Context, query string, args ...any) (sql.Result, error)
	CloseFake    func() error
}

func (*FakeDB) Close

func (f *FakeDB) Close() error

func (*FakeDB) Exec

func (f *FakeDB) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*FakeDB) Query

func (f *FakeDB) Query(ctx context.Context, query string, args ...any) (Rows, error)

func (*FakeDB) QueryRow

func (f *FakeDB) QueryRow(ctx context.Context, query string, args ...any) Row

type FakeMigrator

type FakeMigrator struct {
	RunMigrationsFake          func(db DB, dirPath string) error
	RunMigrationsToVersionFake func(db DB, dirPath string, toVersion int) error
	ListMigrationsFake         func(db DB, dirPath string) ([]Migration, error)
}

func (*FakeMigrator) ListMigrations added in v0.12.0

func (f *FakeMigrator) ListMigrations(db DB, dirPath string) ([]Migration, error)

func (*FakeMigrator) RunMigrations

func (f *FakeMigrator) RunMigrations(db DB, dirPath string) error

func (*FakeMigrator) RunMigrationsToVersion added in v0.11.0

func (f *FakeMigrator) RunMigrationsToVersion(db DB, dirPath string, toVersion int) error

type FakeRow

type FakeRow struct {
	ScanFake func(dest ...any) error
}

func (*FakeRow) Scan

func (f *FakeRow) Scan(dest ...any) error

type FakeRows

type FakeRows struct {
	NextFake  func() bool
	ScanFake  func(dest ...any) error
	CloseFake func() error
	ErrFake   func() error
}

func (*FakeRows) Close

func (f *FakeRows) Close() error

func (*FakeRows) Err

func (f *FakeRows) Err() error

func (*FakeRows) Next

func (f *FakeRows) Next() bool

func (*FakeRows) Scan

func (f *FakeRows) Scan(dest ...any) error

type Migration added in v0.11.0

type Migration struct {
	Version     int
	Description string
	Filename    string
	Applied     bool
	AppliedAt   *time.Time
}

Migration represents a database migration file

func ListMigrationsFromDir added in v0.12.0

func ListMigrationsFromDir(dirPath string) ([]Migration, error)

ListMigrationsFromDir returns a list of migrations from a directory without checking applied status

type Migrator

type Migrator interface {
	RunMigrations(db DB, dirPath string) error
	RunMigrationsToVersion(db DB, dirPath string, toVersion int) error
	ListMigrations(db DB, dirPath string) ([]Migration, error)
}

Migrator is an interface for running database migrations

func NewMigrator

func NewMigrator() Migrator

NewMigrator creates a new Migrator

type Row

type Row interface {
	Scan(dest ...any) error
}

Row is an interface for scanning a single row result

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Close() error
	Err() error
}

Rows is an interface for iterating over multiple row results

Jump to

Keyboard shortcuts

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