postgres

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[M any](query *bun.SelectQuery, field string, arr []M) *bun.SelectQuery

In - adds ANY clause to query:

WHERE field = Any (1,2,3)

If length of array equals 0 condition skips.

func CursorPagination

func CursorPagination(query *bun.SelectQuery, id, limit uint64, order storage.SortOrder, cmp storage.Comparator) *bun.SelectQuery

CursorPagination - adds limit, id where clause and sort to query. Query being like this:

query.Where("id > ?", id).Limit(limit).Order("id ?", order)

func In

func In[M any](query *bun.SelectQuery, field string, arr []M) *bun.SelectQuery

In - adds IN clause to query:

WHERE field IN (1,2,3)

If length of array equals 0 condition skips.

func Pagination

func Pagination(query *bun.SelectQuery, limit, offset uint64, order storage.SortOrder) *bun.SelectQuery

Pagination - adds limit, offset and sort to query. Query being like this:

query.Limit(limit).Offset(offset).Order("id ?", order)

Types

type Init

type Init func(ctx context.Context, conn *database.Bun) error

Init - type of initialization function which called after creating connection to database. For example, can be used for indexes creation.

type Numeric

type Numeric big.Int

Numeric -

func FromInt64

func FromInt64(x int64) *Numeric

func FromString

func FromString(x string) (*Numeric, error)

func NewNumeric

func NewNumeric(x *big.Int) *Numeric

NewNumeric -

func (*Numeric) Add

func (b *Numeric) Add(x *Numeric) *Numeric

func (*Numeric) Div

func (b *Numeric) Div(x *Numeric) *Numeric

func (*Numeric) FromBigInt

func (b *Numeric) FromBigInt(x *big.Int) *Numeric

same as NewBigint()

func (*Numeric) Mul

func (b *Numeric) Mul(x *Numeric) *Numeric

func (*Numeric) Neg

func (b *Numeric) Neg() *Numeric

func (*Numeric) Scan

func (b *Numeric) Scan(value interface{}) error

func (*Numeric) String

func (b *Numeric) String() string

func (*Numeric) Sub

func (b *Numeric) Sub(x *Numeric) *Numeric

func (*Numeric) ToInt64

func (b *Numeric) ToInt64() int64

func (*Numeric) ToUInt64

func (b *Numeric) ToUInt64() uint64

func (*Numeric) Value

func (b *Numeric) Value() (driver.Value, error)

type Storage

type Storage struct {
	Transactable storage.Transactable
	// contains filtered or unexported fields
}

Storage - default storage structure containing Transactable interface and connection to database

func Create

func Create(ctx context.Context, cfg config.Database, init Init) (*Storage, error)

Create - creates storage connection entity

func (*Storage) Close

func (s *Storage) Close() error

Close - closes storage

func (*Storage) Connection

func (s *Storage) Connection() *database.Bun

Connection - returns connection structure

type Table

type Table[M storage.Model] struct {
	// contains filtered or unexported fields
}

Table - Postgres realization of Table interface

func NewTable

func NewTable[M storage.Model](db *database.Bun) *Table[M]

NewTable - creates Table structure

func (*Table[M]) CursorList

func (s *Table[M]) CursorList(ctx context.Context, id, limit uint64, order storage.SortOrder, cmp storage.Comparator) ([]M, error)

CursorList - returns array of rows by cursor pagination

func (*Table[M]) DB

func (s *Table[M]) DB() *bun.DB

DB - returns Postgres connection

func (*Table[M]) GetByID

func (s *Table[M]) GetByID(ctx context.Context, id uint64) (m M, err error)

GetByID - returns row by id

func (*Table[M]) IsNoRows

func (s *Table[M]) IsNoRows(err error) bool

IsNoRows - checks errors is pg.ErrNoRows

func (*Table[M]) LastID

func (s *Table[M]) LastID(ctx context.Context) (uint64, error)

LastID - returns last used id

func (*Table[M]) List

func (s *Table[M]) List(ctx context.Context, limit, offset uint64, order storage.SortOrder) ([]M, error)

List - returns array of rows

func (*Table[M]) Save

func (s *Table[M]) Save(ctx context.Context, m M) error

Save - inserts row to table and returns id.

func (*Table[M]) Update

func (s *Table[M]) Update(ctx context.Context, m M) error

Update - updates table row by primary key.

type Transactable

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

Transactable - realization of Transactable interface for Postgres

func NewTransactable

func NewTransactable(db *database.Bun) *Transactable

NewTransactable - creates Transactable structure

func (*Transactable) BeginTransaction

func (t *Transactable) BeginTransaction(ctx context.Context) (storage.Transaction, error)

BeginTransaction - opens atomic transaction to communication with Postgres

type Transaction

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

Transaction -

func (*Transaction) Add

func (t *Transaction) Add(ctx context.Context, model any) error

Add -

func (*Transaction) BulkSave

func (t *Transaction) BulkSave(ctx context.Context, models []any) error

BulkSave -

func (*Transaction) Close

func (t *Transaction) Close(ctx context.Context) error

Close -

func (*Transaction) CopyFrom

func (t *Transaction) CopyFrom(ctx context.Context, tableName string, data []storage.Copiable) error

CopyFrom -

func (*Transaction) Exec

func (t *Transaction) Exec(ctx context.Context, query string, params ...any) (int64, error)

Exec - executes query and returns the number of affected rows

func (*Transaction) Flush

func (t *Transaction) Flush(ctx context.Context) error

Flush -

func (*Transaction) HandleError

func (t *Transaction) HandleError(ctx context.Context, err error) error

HandleError -

func (*Transaction) Rollback

func (t *Transaction) Rollback(ctx context.Context) error

Rollback -

func (*Transaction) Tx

func (t *Transaction) Tx() *bun.Tx

Tx - returns bun.Tx pointer for custom queries

func (*Transaction) Update

func (t *Transaction) Update(ctx context.Context, model any) error

Update -

Jump to

Keyboard shortcuts

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