storage

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: 2 Imported by: 16

README

Storage package

Abstract layer of data storage is described in the package. Also package contains Postgres realization of the abstract layer.

Abstract layer

Basic interfaces are described in the package. It describes data and communication with storage. Descriptions of interfaces can be found below.

Model

Basic data interface. Its declaration is:

type Model interface {
	TableName() string
}

It declares one method TableName which returns name of collection or table where it will be stored.

Table

Interface describes the default way how developer can communicate with data storage. Its declaration is:

type Table[M Model] interface {
	GetByID(ctx context.Context, id uint64) (M, error)
	Save(ctx context.Context, m M) error
	Update(ctx context.Context, m M) error
	List(ctx context.Context, limit, offset uint64, order SortOrder) ([]M, error)
	IsNoRows(err error) bool
}

Interface is generic and allows communication with only one Model. But you can add needed methods to your storage implemention over that interface.

Transactable

Interface allows to begin atomic transaction operation. Its declaration is:

type Transactable interface {
	BeginTransaction(ctx context.Context) (Transaction, error)
}
Transaction

Atomic transaction operation interface. Its declaration is:

type Transaction interface {
	Flush(ctx context.Context) error
	Add(ctx context.Context, model any) error
	Update(ctx context.Context, model any) error
	Rollback(ctx context.Context) error
	BulkSave(ctx context.Context, models []any) error
	Close(ctx context.Context) error
	HandleError(ctx context.Context, err error) error
	Exec(ctx context.Context, query string, params ...any) (int64, error)
	CopyFrom(ctx context.Context, tableName string, data []Copiable) error
	Tx() *bun.Tx
}

Implementations

Now only one implementation is avalable. It's Postgres. It can be found here.

Usage

Example of usage can be found here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparator

type Comparator uint64

Comparator - enum for cursor pagination

const (
	ComparatorEq Comparator = iota
	ComparatorNeq
	ComparatorLt
	ComparatorLte
	ComparatorGt
	ComparatorGte
)

func (Comparator) String

func (c Comparator) String() string

String -

type Copiable

type Copiable interface {
	Columns() []string
	Flat() []any
}

Copiable -

type Model

type Model interface {
	TableName() string
}

Model - general data type interface

type SortOrder

type SortOrder string

SortOrder - asc or desc

const (
	SortOrderAsc  SortOrder = "asc"
	SortOrderDesc SortOrder = "desc"
)

sort orders

type Table

type Table[M Model] interface {
	GetByID(ctx context.Context, id uint64) (M, error)
	Save(ctx context.Context, m M) error
	Update(ctx context.Context, m M) error
	List(ctx context.Context, limit, offset uint64, order SortOrder) ([]M, error)
	CursorList(ctx context.Context, id, limit uint64, order SortOrder, cmp Comparator) ([]M, error)
	LastID(ctx context.Context) (uint64, error)

	IsNoRows(err error) bool
}

Table - interface to communication with one data type (like table, collection, etc)

type Transactable

type Transactable interface {
	BeginTransaction(ctx context.Context) (Transaction, error)
}

Transactable - interface which allows to begin atomic transaction operation

type Transaction

type Transaction interface {
	Flush(ctx context.Context) error
	Add(ctx context.Context, model any) error
	Update(ctx context.Context, model any) error
	Rollback(ctx context.Context) error
	BulkSave(ctx context.Context, models []any) error
	Close(ctx context.Context) error
	HandleError(ctx context.Context, err error) error
	Exec(ctx context.Context, query string, params ...any) (int64, error)
	CopyFrom(ctx context.Context, tableName string, data []Copiable) error
	Tx() *bun.Tx
}

Transaction - atomic transaction operation interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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