tinypostgres

package
v1.0.56 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tinypostgres provides utilities for handling Postgres.

Index

Constants

View Source
const (
	// ErrUnknown is returned for all errors with unknown cause.
	ErrUnknown = iota

	// ErrUniqueViolation is returned for unique constraint violations.
	ErrUniqueViolation

	// ErrNotNullViolation is returned for non-null constraint violations.
	ErrNotNullViolation

	// ErrRecordNotFound is returned when query is expected to return a single record, but none are returned.
	ErrRecordNotFound

	// ErrInvalidText is returned when you try assigned invalid value to types such as uuid.
	ErrInvalidText
)

Variables

This section is empty.

Functions

func Dial added in v1.0.15

func Dial(url string, opts ...Opt) (*gorm.DB, error)

Dial creates a connection to Postgres, and returns *gorm.DB instance.

Types

type Config

type Config struct {
	// Verbose specifies whether to log all executed queries.
	Verbose bool

	// PoolMaxOpen is the maximum number of open connections to the database (default: 10).
	PoolMaxOpen int

	// PoolMaxIdle is the maximum number of connections in the idle connection pool (default: 5).
	PoolMaxIdle int

	// PoolMaxLifetime is the maximum amount of time a connection may be reused (default: 1h).
	PoolMaxLifetime time.Duration

	// PoolMaxIdleTime is the maximum amount of time a connection may be idle (default: 30m).
	PoolMaxIdleTime time.Duration
	// contains filtered or unexported fields
}

Config holds a configuration for Dial.

type Error

type Error struct {
	// Err is the wrapped error.
	Err error

	// Code is the error code.
	Code int

	// Constraint is the name of violated constraint.
	Constraint string

	// TableName is the name of the table.
	TableName string

	// ColumnName is the name of the column.
	ColumnName string

	// Message is the message returned by the DB.
	Message string
}

Error represents a wrapped query error.

func TranslateError

func TranslateError(err error) *Error

TranslateError tries to cast given error instance to PgError and extract enough information to build Error instance.

type Opt

type Opt = func(*Config)

Opt is an option to be specified to Dial.

func GormOpt

func GormOpt(gormOpt func(*gorm.Config)) Opt

GormOpt adds an option to modify the default gorm.Config.

func PoolMaxIdle

func PoolMaxIdle(poolMaxIdle int) Opt

PoolMaxIdle is the maximum number of connections in the idle connection pool.

func PoolMaxIdleTime

func PoolMaxIdleTime(poolMaxIdleTime time.Duration) Opt

PoolMaxIdleTime is the maximum amount of time a connection may be idle.

func PoolMaxLifetime

func PoolMaxLifetime(poolMaxLifetime time.Duration) Opt

PoolMaxLifetime is the maximum amount of time a connection may be reused.

func PoolMaxOpen

func PoolMaxOpen(poolMaxOpen int) Opt

PoolMaxOpen is the maximum number of open connections to the database.

func Verbose

func Verbose() Opt

Verbose tells client to log all executed queries.

Jump to

Keyboard shortcuts

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