datastore

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package datastore is used to interact with a datastore. It has functions to help set up a sql.DB as well as helpers for working with the sql.DB once it's initialized.

Index

Constants

View Source
const (
	// DBHostEnv is the database host environment variable name
	DBHostEnv string = "DB_HOST"
	// DBPortEnv is the database port environment variable name
	DBPortEnv string = "DB_PORT"
	// DBNameEnv is the database name environment variable name
	DBNameEnv string = "DB_NAME"
	// DBUserEnv is the database user environment variable name
	DBUserEnv string = "DB_USER"
	// DBPasswordEnv is the database user password environment variable name
	DBPasswordEnv string = "DB_PASSWORD"
	// DBSearchPathEnv is the database search path environment variable name
	DBSearchPathEnv string = "DB_SEARCH_PATH"
)

Variables

This section is empty.

Functions

func NewNullInt32 added in v0.40.0

func NewNullInt32(i int32) sql.NullInt32

NewNullInt32 returns a null if i == 0, otherwise it returns the int32 which was input.

func NewNullInt64 added in v0.18.0

func NewNullInt64(i int64) sql.NullInt64

NewNullInt64 returns a null if i == 0, otherwise it returns the int64 which was input.

func NewNullString added in v0.18.0

func NewNullString(s string) sql.NullString

NewNullString returns a null if s is empty, otherwise it returns the string which was input

func NewNullTime added in v0.40.0

func NewNullTime(t time.Time) sql.NullTime

NewNullTime returns a null if t is the zero value for time.Time, otherwise it returns the time which was input

func NewPostgreSQLPool added in v0.38.0

func NewPostgreSQLPool(ctx context.Context, dsn PostgreSQLDSN, logger zerolog.Logger) (*pgxpool.Pool, func(), error)

NewPostgreSQLPool returns an open database handle of 0 or more underlying PostgreSQL connections

func ValidatePostgreSQLPool added in v0.39.0

func ValidatePostgreSQLPool(ctx context.Context, pool *pgxpool.Pool, log zerolog.Logger) error

ValidatePostgreSQLPool pings the database and logs the current user and database

Types

type Datastore

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

Datastore is a concrete implementation for a sql database

func NewDatastore added in v0.13.0

func NewDatastore(dbpool *pgxpool.Pool) Datastore

NewDatastore is an initializer for the Datastore struct

func (Datastore) BeginTx

func (ds Datastore) BeginTx(ctx context.Context) (pgx.Tx, error)

BeginTx returns an acquired transaction from the db pool and adds app specific error handling

func (Datastore) CommitTx

func (ds Datastore) CommitTx(ctx context.Context, tx pgx.Tx) error

CommitTx is a wrapper for sql.Tx.Commit in order to expose from the Datastore interface. Proper error handling is also considered.

func (Datastore) Pool added in v0.38.0

func (ds Datastore) Pool() *pgxpool.Pool

Pool returns *pgxpool.Pool from the Datastore struct

func (Datastore) RollbackTx

func (ds Datastore) RollbackTx(ctx context.Context, tx pgx.Tx, err error) error

RollbackTx is a wrapper for sql.Tx.Rollback in order to expose from the Datastore interface. Proper error handling is also considered.

type PostgreSQLDSN added in v0.34.0

type PostgreSQLDSN struct {
	Host       string
	Port       int
	DBName     string
	SearchPath string
	User       string
	Password   string
}

PostgreSQLDSN is a PostgreSQL datasource name

func (PostgreSQLDSN) ConnectionURI added in v0.39.0

func (dsn PostgreSQLDSN) ConnectionURI() string

ConnectionURI returns a formatted PostgreSQL datasource "Keyword/Value Connection String" The general form for a connection URI is: postgresql://[userspec@][hostspec][/dbname][?paramspec] where userspec is

user[:password]

and hostspec is:

[host][:port][,...]

and paramspec is:

name=value[&...]

The URI scheme designator can be either postgresql:// or postgres://. Each of the remaining URI parts is optional. The following examples illustrate valid URI syntax:

postgresql://
postgresql://localhost
postgresql://localhost:5433
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp

func (PostgreSQLDSN) KeywordValueConnectionString added in v0.39.0

func (dsn PostgreSQLDSN) KeywordValueConnectionString() string

KeywordValueConnectionString returns a formatted PostgreSQL datasource "Keyword/Value Connection String"

Directories

Path Synopsis
Package datastoretest provides testing helper functions for the datastore package.
Package datastoretest provides testing helper functions for the datastore package.
Package pingstore enables database health checks through the db Pool Ping method.
Package pingstore enables database health checks through the db Pool Ping method.

Jump to

Keyboard shortcuts

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