Abstract layer of data storage is described in the package. Also package contains Postgres realization of the abstract layer.
Basic interfaces are described in the package. It describes data and communication with storage. Descriptions of interfaces can be found below.
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.
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.
Interface allows to begin atomic transaction operation. Its declaration is:
type Transactable interface {
BeginTransaction(ctx context.Context) (Transaction, error)
}
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
}
Now only one implementation is avalable. It's Postgres. It can be found here.
Example of usage can be found here