database

package
v0.0.0-...-b53ed2c Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Unlicense Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IntegrityConstraintViolationErrorCode when a value is missing or not associated with a valid foreign table.
	IntegrityConstraintViolationErrorCode = "23000"
	// NotNullViolationErrorCode when received an empty or null field.
	NotNullViolationErrorCode = "23502"
	// ForeignKeyViolationErrorCode FK constraint was created with a not valid value.
	ForeignKeyViolationErrorCode = "23503"
	// UniqueViolationErrorCode duplicate key value violates unique constraint.
	UniqueViolationErrorCode = "23505"
	// CheckViolationErrorCode when a value violates a specific requirement in a column.
	CheckViolationErrorCode = "23514"
	// InvalidTextRepresentation when a field is uuid but receive a different pattern.
	InvalidTextRepresentation = "22P02"
)

Variables

View Source
var (
	MasterInstanceType      Master
	ReadReplicaInstanceType ReadReplica

	Nil = database[any, any]{}
)
View Source
var (
	ErrStatementTimeout             = errors.New("statement timeout")
	ErrIntegrityConstraintViolation = errors.New("integrity constraint violation")
	ErrNotNullViolation             = errors.New("not null violation")
	ErrForeignKeyViolation          = errors.New("foreign key violation")
	ErrCheckViolation               = errors.New("check violation")
	ErrUniqueViolation              = errors.New("unique violation")
	ErrInternal                     = errors.New("internal server error")
	ErrNotFound                     = errors.New("it was not found")
	ErrInvalidTextRepresentation    = errors.New("invalid text representation")
	ErrNoRowsAffected               = errors.New("no rows affected")
)

Functions

func GetPGErrorColumn

func GetPGErrorColumn(err error) string

func GetPGErrorConstraint

func GetPGErrorConstraint(err error) string

func HandleAdapterError

func HandleAdapterError(err error) (bool, error)

func HandleResult

func HandleResult(res sql.Result) error

func IsErrNoRows

func IsErrNoRows(err error) bool

func NewDatabaseLogger

func NewDatabaseLogger(before, after logFunction) bun.QueryHook

NewDatabaseLogger returns a new implementation QueryHook to log SQL queries on console.

Types

type Configs

type Configs interface {
	any | PostgresConfig | SQLiteConfig
}

type Database

type Database[C Configs, I InstanceType] interface {
	Client() *bun.DB
	InstanceType() I
	Config() C
	Stop(ctx context.Context) error
}

func New

func New[T Configs, I InstanceType](cfg T, it I) (Database[T, I], error)

New this function initialize a wrapper client over the database connection client.

We are currently working with github.com/uptrace/bun and it can support:

  • PostgreSQL,
  • MySQL,
  • MSSQL,
  • SQLite.

type Hook

type Hook bun.QueryHook

type InstanceType

type InstanceType interface {
	any | Master | ReadReplica
}

type Master

type Master struct{}

type PostgresConfig

type PostgresConfig struct {
	// URI e.g: postgres://pass:user@localhost:5432/my-database?sslmode=disable
	URI   string
	Hooks []Hook
}

type ReadReplica

type ReadReplica struct{}

type SQLiteConfig

type SQLiteConfig struct {
	// URI e.g: file::memory:?cache=shared
	URI   string
	Hooks []Hook
}

Jump to

Keyboard shortcuts

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