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 ¶
WithInit sets an SQL statement that New executes after connecting. Defaults to none.
func WithLogger ¶
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 ¶
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) Conn ¶
Conn returns a single dedicated connection from the pool. The caller must close it when done.