keelpostgres

package module
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 6 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package keelpostgres provides a thin PostgreSQL persistor built on database/sql and the github.com/lib/pq driver.

New opens and pings the database and optionally runs an initialization statement:

p, err := keelpostgres.New(ctx, "postgres://user:pass@localhost:5432/db",
	keelpostgres.WithInit("create table if not exists ..."),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options)

Option configures Options.

func WithInit

func WithInit(v string) Option

WithInit sets an SQL statement that New executes after connecting. Defaults to none.

func WithLogger

func WithLogger(v *zap.Logger) Option

WithLogger sets the logger. Defaults to log.Logger.

type Options

type Options struct {
	// Init is an optional SQL statement executed once after connecting.
	Init string
	// Logger is the logger stored on the [Persistor].
	Logger *zap.Logger
}

Options configures New.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default Options using the global keel logger.

type Persistor

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

Persistor wraps a sql.DB connected to PostgreSQL. It is exported so it can be embedded into types in other packages. Its methods are safe for concurrent use as far as the underlying sql.DB is.

func New

func New(ctx context.Context, dns string, opts ...Option) (*Persistor, error)

New connects to the PostgreSQL database identified by the connection string dns, verifies the connection with a ping and, if configured via WithInit, executes the init statement. It returns an error if the connection string is invalid, the ping fails, or the init statement fails.

func (*Persistor) Close

func (p *Persistor) Close() error

Close closes the underlying database.

func (*Persistor) Conn

func (p *Persistor) Conn(ctx context.Context) (*sql.Conn, error)

Conn returns a single dedicated connection from the pool. The caller must close it when done.

func (*Persistor) DB

func (p *Persistor) DB() *sql.DB

DB returns the underlying sql.DB.

func (*Persistor) Ping

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

Ping verifies the database connection is alive.

func (*Persistor) TableExists

func (p *Persistor) TableExists(ctx context.Context, name string) (bool, error)

TableExists reports whether a table with the given name exists in information_schema.tables.

Jump to

Keyboard shortcuts

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