Documentation ¶
Overview ¶
Package hohin contains interfaces that must be implemented by other packages.
Index ¶
- Variables
- type DB
- type Filter
- func And(value ...Filter) Filter
- func Contains(field string, value string) Filter
- func Eq(field string, value any) Filter
- func Gt(field string, value any) Filter
- func Gte(field string, value any) Filter
- func HasPrefix(field string, value string) Filter
- func HasSuffix(field string, value string) Filter
- func IContains(field string, value string) Filter
- func IEq(field string, value any) Filter
- func IHasPrefix(field string, value string) Filter
- func IHasSuffix(field string, value string) Filter
- func INe(field string, value any) Filter
- func IPWithin(field string, value string) Filter
- func In(field string, value []any) Filter
- func IsNull(field string) Filter
- func Lt(field string, value any) Filter
- func Lte(field string, value any) Filter
- func Ne(field string, value any) Filter
- func Not(value Filter) Filter
- func Or(value ...Filter) Filter
- type IsolationLevel
- type Order
- type Query
- type Repo
- type SimpleDB
- type SimpleRepo
- func (r *SimpleRepo[T]) Add(db SimpleDB, entity T) error
- func (r *SimpleRepo[T]) AddMany(db SimpleDB, entities []T) error
- func (r *SimpleRepo[T]) Clear(db SimpleDB) error
- func (r *SimpleRepo[T]) Count(db SimpleDB, f Filter) (uint64, error)
- func (r *SimpleRepo[T]) CountAll(db SimpleDB) (uint64, error)
- func (r *SimpleRepo[T]) Delete(db SimpleDB, f Filter) error
- func (r *SimpleRepo[T]) Exists(db SimpleDB, f Filter) (bool, error)
- func (r *SimpleRepo[T]) Get(db SimpleDB, f Filter) (T, error)
- func (r *SimpleRepo[T]) GetFirst(db SimpleDB, q Query) (T, error)
- func (r *SimpleRepo[T]) GetForUpdate(db SimpleDB, f Filter) (T, error)
- func (r *SimpleRepo[T]) GetMany(db SimpleDB, q Query) ([]T, error)
- func (r *SimpleRepo[T]) Update(db SimpleDB, f Filter, entity T) error
Constants ¶
This section is empty.
Variables ¶
var NotFound error = errors.New("object not found")
NotFound is returned when an entity cannot be found in the repository.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB interface { // Transaction executes a given function within a transaction. // If the function returns an error then the transaction rolls back. Transaction(context.Context, func(context.Context, DB) error) error // Tx is similar to Transaction but requires to choose an isolation level. Tx(context.Context, IsolationLevel, func(context.Context, DB) error) error // Simple returns the DB wrapped into an object with a simplified interface. Simple() SimpleDB }
DB is a wrapper around a database connection.
type Filter ¶
type Filter struct { Field string // name of an entity field Operation operations.Operation // comparison operation Value any // value to compare with a field }
Filter is an object used for filtering entities before getting them from a repository.
func Gte ¶
Gte creates a filter to find entities whose field value is greater than or equal to a given one.
func HasPrefix ¶
HasPrefix creates a filter to find entities whose field value starts with a given value.
func HasSuffix ¶
HasSuffix creates a filter to find entities whose field value ends with a given value.
func IContains ¶
IContains creates a case-insensitive filter to find entities whose field value has a given value.
func IEq ¶
IEq creates a case-insensitive filter to find entities whose field value is equal to a given one.
func IHasPrefix ¶
IHasPrefix creates a case-insensitive filter to find entities whose field value starts with a given value.
func IHasSuffix ¶
IHasSuffix creates a case-insensitive filter to find entities whose field value ends with a given value.
func INe ¶
INe creates a case-insensitive filter to find entities whose field value is not equal to a given one.
func IPWithin ¶
IPWithin creates a filter to find entities whose field is an IP address contained within a given subnet.
func Lte ¶
Lte creates a filter to find entities whose field value is less than or equal to a given one.
type IsolationLevel ¶
type IsolationLevel int
IsolationLevel defines an isolation level of a database transaction.
const ( DefaultIsolation IsolationLevel = iota ReadUncommitted ReadCommitted RepeatableRead Serializable )
type Order ¶
type Order struct { Field string // field to order by Desc bool // defines if ordering must be descending or not }
Order describes how entities retrieved from a repository must be ordered.
type Query ¶
type Query struct { Filter Filter // filter to search entities Limit int // maximum number of entities to retrieve Offset int // result offset Order []Order // order of entities }
Query contains a Filter and additional options.
type Repo ¶
type Repo[T any] interface { // Get finds an entity and returns it. Get(context.Context, DB, Filter) (T, error) // GetForUpdate finds an entity and locks it for update. GetForUpdate(context.Context, DB, Filter) (T, error) // GetMany finds and returns several entities. GetMany(context.Context, DB, Query) ([]T, error) // GetFirst finds and returns the first entity matching given criteria. GetFirst(context.Context, DB, Query) (T, error) // Add saves a new entity to the repository. Add(context.Context, DB, T) error // AddMany saves several entities to the repository. AddMany(context.Context, DB, []T) error // Update saves an updated entity. Update(context.Context, DB, Filter, T) error // Delete removes entities matching a given filter. Delete(context.Context, DB, Filter) error // Exists checks if there is an entity matching a given filter. Exists(context.Context, DB, Filter) (bool, error) // Count returns a number of entities matching a given filter. Count(context.Context, DB, Filter) (uint64, error) // CountAll returns a number of all entities in the repository. CountAll(context.Context, DB) (uint64, error) // Clear removes all entities from the repository. Clear(context.Context, DB) error // Simple returns the repository wrapped into an object with a simplified interface. Simple() SimpleRepo[T] }
Repo is a repository of entities. It saves entities to the database and loads or deletes them from it.
type SimpleDB ¶
type SimpleDB struct {
// contains filtered or unexported fields
}
SimpleDB is a wrapper around a DB for a case when there is no need of passing a Context to DB's methods.
func (*SimpleDB) Transaction ¶
Transaction executes a given function within a transaction. If the function returns an error then the transaction rolls back.
type SimpleRepo ¶
type SimpleRepo[T any] struct { // contains filtered or unexported fields }
SimpleRepo is a wrapper around a Repo for a case when there is no need of passing a Context to Repo's methods.
func NewSimpleRepo ¶
func NewSimpleRepo[T any](r Repo[T]) SimpleRepo[T]
NewSimpleRepo creates a new SimpleRepo.
func (*SimpleRepo[T]) Add ¶
func (r *SimpleRepo[T]) Add(db SimpleDB, entity T) error
Add saves a new entity to the repository.
func (*SimpleRepo[T]) AddMany ¶
func (r *SimpleRepo[T]) AddMany(db SimpleDB, entities []T) error
AddMany saves several entities to the repository.
func (*SimpleRepo[T]) Clear ¶
func (r *SimpleRepo[T]) Clear(db SimpleDB) error
Clear removes all entities from the repository.
func (*SimpleRepo[T]) Count ¶
func (r *SimpleRepo[T]) Count(db SimpleDB, f Filter) (uint64, error)
Count returns a number of entities matching a given filter.
func (*SimpleRepo[T]) CountAll ¶
func (r *SimpleRepo[T]) CountAll(db SimpleDB) (uint64, error)
CountAll returns a number of all entities in the repository.
func (*SimpleRepo[T]) Delete ¶
func (r *SimpleRepo[T]) Delete(db SimpleDB, f Filter) error
Delete removes entities matching a given filter.
func (*SimpleRepo[T]) Exists ¶
func (r *SimpleRepo[T]) Exists(db SimpleDB, f Filter) (bool, error)
Exists checks if there is an entity matching a given filter.
func (*SimpleRepo[T]) Get ¶
func (r *SimpleRepo[T]) Get(db SimpleDB, f Filter) (T, error)
Get finds an entity and returns it.
func (*SimpleRepo[T]) GetFirst ¶
func (r *SimpleRepo[T]) GetFirst(db SimpleDB, q Query) (T, error)
GetFirst finds and returns the first entity matching given criteria.
func (*SimpleRepo[T]) GetForUpdate ¶
func (r *SimpleRepo[T]) GetForUpdate(db SimpleDB, f Filter) (T, error)
GetForUpdate finds an entity and locks it for update.
Directories ¶
Path | Synopsis |
---|---|
Package clickhouse contains implementations of hohin interfaces for ClickHouse.
|
Package clickhouse contains implementations of hohin interfaces for ClickHouse. |
Package maps contains useful functions to work with maps.
|
Package maps contains useful functions to work with maps. |
Package mem contains in-memory implementations of hohin interfaces.
|
Package mem contains in-memory implementations of hohin interfaces. |
Package mysql contains implementations of hohin interfaces for MySQL.
|
Package mysql contains implementations of hohin interfaces for MySQL. |
Package operations contains types of operations that can be used for filtering entities.
|
Package operations contains types of operations that can be used for filtering entities. |
Package pg contains implementations of hohin interfaces for PostgreSQL.
|
Package pg contains implementations of hohin interfaces for PostgreSQL. |
Package sqldb contains a very primitive SQL builder.
|
Package sqldb contains a very primitive SQL builder. |
Package sqlite3 contains implementations of hohin interfaces for SQLite3.
|
Package sqlite3 contains implementations of hohin interfaces for SQLite3. |