postgres

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgres provides pool based operations on Postgres database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitTx

func CommitTx(ctx context.Context) error

CommitTx commits the transaction stored in the context, if any.

func InTx

func InTx(ctx context.Context) bool

InTx checks if the context has an active transaction.

func RollbackTx

func RollbackTx(ctx context.Context) error

RollbackTx rolls back the transaction stored in the context, if any.

func Tx

func Tx(ctx context.Context) pgx.Tx

Tx retrieves transaction from context.

func WithTx

func WithTx[T any](ctx context.Context, pool *Database, fn func(ctx context.Context) (T, error)) (T, error)

WithTx is a generic helper version of `pool.WithTx` that can be used to return a value from the function as well.

Types

type Database

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

Database is a handle to Postgres database with connection pool support.

func NewDatabase

func NewDatabase(ctx context.Context, p *NewDatabaseParams) (*Database, error)

NewDatabase creates Database.

func (*Database) BeginTx

func (p *Database) BeginTx(ctx context.Context) (context.Context, error)

BeginTx starts a new transaction and returns a new context containing the transaction.

func (*Database) Close

func (p *Database) Close()

Close the Database.

func (*Database) Conn

func (p *Database) Conn(ctx context.Context) (*pgxpool.Conn, error)

Conn attempts to acquire a connection from the pool.

func (*Database) Handle

func (p *Database) Handle() *sql.DB

Handle returns an sql.DB instance for compatibility with database/sql package, acting as a connection handle.

func (*Database) Ping

func (p *Database) Ping(ctx context.Context) error

Ping checks connection to database.

func (*Database) Pool

func (p *Database) Pool() *pgxpool.Pool

Pool returns the underlying pool implementation.

func (*Database) WithTx

func (p *Database) WithTx(ctx context.Context, fn func(ctx context.Context) error) error

WithTx executes the given function within a transaction context, starting a new transaction if one does not already exist.

type Migrator

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

Migrator is for managing migrations.

func NewMigrator

func NewMigrator(pool *Database, fs fs.FS) (*Migrator, error)

NewMigrator creates a new Migrator. fs is assumed to be the root of the migration files that's flat.

func (*Migrator) Close

func (m *Migrator) Close()

Close the Migrator.

func (*Migrator) MigrateDownAll

func (m *Migrator) MigrateDownAll(ctx context.Context) error

MigrateDownAll rolls back all applied migrations in the database.

func (*Migrator) MigrateUpAll

func (m *Migrator) MigrateUpAll(ctx context.Context) error

MigrateUpAll applies all pending migrations to the database.

type NewDatabaseParams

type NewDatabaseParams struct {
	ConnStr    string
	HasPostgis bool
}

NewDatabaseParams of NewDatabase.

Jump to

Keyboard shortcuts

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