pgutil

package
v1.1.64 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pgutil 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

This section is empty.

Types

type Client

type Client struct {
	*gorm.DB
}

Client is a wrapper for *gorm.DB providing a handy Close() function.

func DialPostgres

func DialPostgres(opts ...Opt) (*Client, error)

DialPostgres creates a connection to Postgres, and returns Client instance.

func (*Client) Close

func (c *Client) Close()

Close closes a connection to Postgres.

type Config added in v1.1.55

type Config struct {
	// DSN is a string that contains basic connection data.
	DSN string

	// 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 Client.

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 added in v1.1.55

type Opt = func(*Config)

Opt is an option to be specified to DialPostgres.

func DSN added in v1.1.55

func DSN(dsn string) Opt

DSN is a string that contains basic connection data.

func GormOpt added in v1.1.61

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

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

func PoolMaxIdle added in v1.1.55

func PoolMaxIdle(poolMaxIdle int) Opt

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

func PoolMaxIdleTime added in v1.1.55

func PoolMaxIdleTime(poolMaxIdleTime time.Duration) Opt

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

func PoolMaxLifetime added in v1.1.55

func PoolMaxLifetime(poolMaxLifetime time.Duration) Opt

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

func PoolMaxOpen added in v1.1.55

func PoolMaxOpen(poolMaxOpen int) Opt

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

func Verbose added in v1.1.55

func Verbose(verbose bool) Opt

Verbose specifies whether to log all executed queries.

Jump to

Keyboard shortcuts

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