pgutil

package module
v0.0.0-...-6cf6046 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 12 Imported by: 0

README

Nacelle Postgres Utilities

PkgGoDev Build status Latest release

Postgres utilities for use with nacelle.


Usage

This library creates a sqlx connection wrapped in a nacelle logger. The supplied initializer adds this connection into the nacelle service container under the key db. The initializer will block until a ping succeeds.

func setup(processes nacelle.ProcessContainer, services nacelle.ServiceContainer) error {
    processes.RegisterInitializer(pgutil.NewInitializer())

    // additional setup
    return nil
}

This library uses golang migrate to optionally run migrations on application startup. To configure migrations, supply a source driver to the initializer, as follows.

import (
    _ "github.com/golang-migrate/migrate/v4/source/file"
    "github.com/golang-migrate/migrate/v4/source"
)

func setup(processes nacelle.ProcessContainer, services nacelle.ServiceContainer) error {
    migrationSourceDriver, err := source.Open("file:///migrations")
	if err != nil {
		return err
	}

    processes.RegisterInitializer(pgutil.NewInitializer(
        pgutil.WithMigrationSourceDriver(migrationSourceDriver)
    ))

    // ...
}
Configuration

The default service behavior can be configured by the following environment variables.

Environment Variable Required Default Description
DATABASE_URL yes The connection string of the remote database.
LOG_SQL_QUERIES false Whether or not to log parameterized SQL queries.
MIGRATIONS_TABLE schema_migrations The name of the migrations table.
MIGRATIONS_SCHEMA_NAME default The name of the schema used during migrations.
FAIL_ON_NEWER_MIGRATION_VERSION false If true, fail startup when the database migration version is newer than the known set of migrations.

Documentation

Index

Constants

View Source
const MaxPingAttempts = 15
View Source
const ServiceName = "db"

Variables

View Source
var (
	ErrDoesNotExist  = fmt.Errorf("record does not exist")
	ErrAlreadyExists = fmt.Errorf("record already exists")
)

Functions

func HandleError

func HandleError(err error, description string) error

Types

type Config

type Config struct {
	DatabaseURL                 string `env:"database_url" required:"true"`
	LogSQLQueries               bool   `env:"log_sql_queries" default:"false"`
	MigrationsTable             string `env:"migrations_table"`
	MigrationsSchemaName        string `env:"migrations_schema_name"`
	FailOnNewerMigrationVersion bool   `env:"fail_on_newer_migration_version"`
}

type ConfigFunc

type ConfigFunc func(*options)

ConfigFunc is a function used to configure an initializer.

func WithMigrationSourceDriver

func WithMigrationSourceDriver(sourceDriver source.Driver) ConfigFunc

WithMigrationSourceDriver sets the migration source driver.

type Initializer

type Initializer struct {
	Logger   nacelle.Logger           `service:"logger"`
	Services nacelle.ServiceContainer `service:"services"`
	// contains filtered or unexported fields
}

func NewInitializer

func NewInitializer(configs ...ConfigFunc) *Initializer

func (*Initializer) Init

func (i *Initializer) Init(config nacelle.Config) error

type LoggingDB

type LoggingDB struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

func Dial

func Dial(url string, logger nacelle.Logger) (*LoggingDB, error)

func (*LoggingDB) Beginx

func (db *LoggingDB) Beginx() (*LoggingTx, error)

func (*LoggingDB) Exec

func (db *LoggingDB) Exec(query string, args ...interface{}) (sql.Result, error)

func (*LoggingDB) Query

func (db *LoggingDB) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*LoggingDB) QueryRowx

func (db *LoggingDB) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*LoggingDB) Queryx

func (db *LoggingDB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

type LoggingTx

type LoggingTx struct {
	*sqlx.Tx
	// contains filtered or unexported fields
}

func (*LoggingTx) Exec

func (tx *LoggingTx) Exec(query string, args ...interface{}) (sql.Result, error)

func (*LoggingTx) Query

func (tx *LoggingTx) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*LoggingTx) QueryRowx

func (tx *LoggingTx) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*LoggingTx) Queryx

func (tx *LoggingTx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

type PageMeta

type PageMeta struct {
	Page     int
	PageSize int
}

func (*PageMeta) Limit

func (m *PageMeta) Limit() int

func (*PageMeta) Offset

func (m *PageMeta) Offset() int

type PagedResultMeta

type PagedResultMeta struct {
	NumPages   int `json:"num_pages"`
	NumResults int `json:"num_results"`
}

func PagedSelect

func PagedSelect(
	db *LoggingDB,
	meta *PageMeta,
	baseQuery string,
	target interface{},
	args ...interface{},
) (*PagedResultMeta, error)

Jump to

Keyboard shortcuts

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