Documentation
¶
Index ¶
- Variables
- func Count(ctx context.Context, db DB, s *query.Scope) (int64, error)
- func Exists(ctx context.Context, db DB, s *query.Scope) (bool, error)
- func New(options ...Option) (*base, error)
- func RefreshModels(ctx context.Context, db DB, mStruct *mapping.ModelStruct, ...) error
- func RunInTransaction(ctx context.Context, db DB, options *query.TxOptions, txFunc TxFunc) error
- type AfterDeleter
- type AfterFinder
- type AfterInserter
- type AfterUpdater
- type BeforeDeleter
- type BeforeInserter
- type BeforeUpdater
- type Builder
- type DB
- type Option
- func WithDefaultRepository(r repository.Repository) Option
- func WithMigrateModels(models ...mapping.Model) Option
- func WithModelMap(m *mapping.ModelMap) Option
- func WithRepositoryModels(r repository.Repository, models ...mapping.Model) Option
- func WithSynchronousConnections() Option
- func WithTimeFunc(timeFunc func() time.Time) Option
- type Options
- type QueryDeleter
- type QueryFinder
- type QueryGetter
- type QueryInserter
- type QueryRefresher
- type QueryRelationAdder
- type QueryRelationClearer
- type QueryRelationSetter
- type QueryUpdater
- type RepositoryMapper
- func (r *RepositoryMapper) GetRepository(model mapping.Model) (repository.Repository, error)
- func (r *RepositoryMapper) GetRepositoryByModelStruct(mStruct *mapping.ModelStruct) (repository.Repository, error)
- func (r *RepositoryMapper) RegisterRepositories(repositories ...repository.Repository)
- func (r *RepositoryMapper) RegisterRepositoryModels() error
- func (r *RepositoryMapper) SetUnmappedModelRepositories() error
- type Tx
- func (t *Tx) AddRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, ...) error
- func (t *Tx) ClearRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField) (int64, error)
- func (t *Tx) Commit() error
- func (t *Tx) Delete(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
- func (t *Tx) DeleteQuery(ctx context.Context, s *query.Scope) (int64, error)
- func (t *Tx) GetRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, ...) ([]mapping.Model, error)
- func (t *Tx) ID() uuid.UUID
- func (t *Tx) IncludeRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, ...) error
- func (t *Tx) Insert(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
- func (t *Tx) InsertQuery(ctx context.Context, q *query.Scope) error
- func (t *Tx) ModelMap() *mapping.ModelMap
- func (t *Tx) Now() time.Time
- func (t *Tx) Options() query.TxOptions
- func (t *Tx) Query(model *mapping.ModelStruct, models ...mapping.Model) Builder
- func (t *Tx) QueryAddRelations(ctx context.Context, s *query.Scope, relationField *mapping.StructField, ...) error
- func (t *Tx) QueryClearRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField) (int64, error)
- func (t *Tx) QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder
- func (t *Tx) QueryFind(ctx context.Context, q *query.Scope) ([]mapping.Model, error)
- func (t *Tx) QueryGet(ctx context.Context, q *query.Scope) (mapping.Model, error)
- func (t *Tx) QueryRefresh(ctx context.Context, q *query.Scope) error
- func (t *Tx) QuerySetRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField, ...) error
- func (t *Tx) Refresh(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
- func (t *Tx) Rollback() error
- func (t *Tx) RollbackSavepoint(name string) error
- func (t *Tx) Savepoint(name string) error
- func (t *Tx) SetRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, ...) error
- func (t *Tx) State() query.TxState
- func (t *Tx) Update(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
- func (t *Tx) UpdateQuery(ctx context.Context, q *query.Scope) (int64, error)
- type TxFunc
Constants ¶
This section is empty.
Variables ¶
var ( ErrDatabase = errors.New("database") // ErrRepository is an error related with the repository. ErrRepository = errors.Wrap(ErrDatabase, "repository") // ErrRepositoryNotFound is an error when repository is not found. ErrRepositoryNotFound = errors.Wrap(ErrRepository, "not found") // ErrRepositoryAlreadyRegistered class of errors when repository is already registered. ErrRepositoryAlreadyRegistered = errors.Wrap(ErrRepository, "already registered") )
Functions ¶
func RefreshModels ¶
func RefreshModels(ctx context.Context, db DB, mStruct *mapping.ModelStruct, models []mapping.Model, fieldSet ...*mapping.StructField) error
RefreshModels refreshes all 'fields' (attributes, foreign keys) for provided input 'models'.
func RunInTransaction ¶ added in v0.17.0
RunInTransaction runs specific function 'txFunc' within a transaction. If an error would return from that function the transaction would be rolled back. Otherwise it commits the changes. If an input 'db' is already within a transaction it would just execute txFunc for given transaction.
Types ¶
type AfterDeleter ¶
AfterDeleter is the interface used as an after delete hook.
type AfterFinder ¶
AfterFinder is the interface used as a after find hook.
type AfterInserter ¶
AfterInserter is the interface that has a method used as a hook after the creation process.
type AfterUpdater ¶
AfterUpdater is the interface used as a after patch hook.
type BeforeDeleter ¶
BeforeDeleter is the interface used as a before delete hook.
type BeforeInserter ¶
BeforeInserter is the interface used for hooks before the creation process.
type BeforeUpdater ¶
BeforeUpdater is the interface used as a before patch hook.
type Builder ¶
type Builder interface {
// Scope returns current query scope.
Scope() *query.Scope
// Err returns error for given query.
Err() error
// Ctx returns current query context.
Ctx() context.Context
// Count returns the number of model instances in the repository matching given query.
Count() (int64, error)
// Insert models into repository. Returns error with class ErrViolationUnique if the model with given primary key already exists.
Insert() error
// update updates query models in the repository. Input models with non zero primary key the function would take
// update these models instances. For a non zero single model with filters it would update all models that match
// given query. In order to update all models in the repository use UpdateAll method.
Update() (int64, error)
// Exists check if input model exists.
Exists() (bool, error)
// Get gets single model that matches given query.
Get() (mapping.Model, error)
// Find gets all models that matches given query. For models with DeletedAt timestamp field, adds the
// filter for not nulls DeletedAt models, unless there is other DeletedAt filter.
Find() ([]mapping.Model, error)
// deleteQuery deletes models matching given query. If the model has DeletedAt timestamp field it would do the
// soft delete - update all models matching given query and set their deleted_at field with current timestamp.
Delete() (int64, error)
// refreshQuery gets all input models and refreshes all selected fields and included relations.
Refresh() error
// Select model fields for the query fieldSet.
Select(fields ...*mapping.StructField) Builder
// Where adds the query 'filter' with provided arguments. The query should be composed of the field name and it's
// operator. In example: 'ID in', 3,4,5 - would result with a filter on primary key field named 'ID' with the
// IN operator and it's value equal to 3,4 or 5.
Where(filter string, arguments ...interface{}) Builder
// Include adds the 'relation' with it's optional fieldset to get for each resulting model to find.
Include(relation *mapping.StructField, relationFieldset ...*mapping.StructField) Builder
// OrderBy sorts the results by the fields in the given order.
OrderBy(fields ...query.Sort) Builder
// Limit sets the maximum number of the results for given query.
Limit(limit int64) Builder
// Offset sets the number of the results to omit in the query.
Offset(offset int64) Builder
// Filter adds the filter field to the given query.
Filter(filter filter.Filter) Builder
// WhereOr creates a OrGroup filter for provided simple filters.
WhereOr(filters ...filter.Simple) Builder
// AddRelations adds the 'relations' models to input models for 'relationField'
AddRelations(relationField *mapping.StructField, relations ...mapping.Model) error
// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
// The relation's foreign key must be allowed to set to null.
SetRelations(relationField *mapping.StructField, relations ...mapping.Model) error
// ClearRelations clears all 'relationField' relations for given input models.
// The relation's foreign key must be allowed to set to null.
RemoveRelations(relationField *mapping.StructField) (int64, error)
// IncludeRelation adds the 'relations' models to input models for 'relationField'
GetRelations(relationField *mapping.StructField, relationFieldSet ...*mapping.StructField) ([]mapping.Model, error)
}
Builder is the interface used to build queries.
type DB ¶
type DB interface {
// Query creates a new query for provided 'model'.
Query(model *mapping.ModelStruct, models ...mapping.Model) Builder
// QueryCtx creates a new query for provided 'model'. The query should take a context on it's
QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder
// Insert inserts provided models into mapped repositories. The DB would use models non zero fields. Provided models
// must have non zero primary key values. The function allows to insert multiple models at once.
Insert(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
// update updates provided models in their mapped repositories. The DB would use models non zero fields. Provided models
// must have non zero primary key values. The function allows to update multiple models at once.
Update(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
// deleteQuery deletes provided models from their mapped repositories. The DB would use models non zero fields. Provided models
// must have non zero primary key values. The function allows to delete multiple model values at once.
Delete(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
// refreshQuery refreshes fields (attributes, foreign keys) for provided models.
Refresh(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
// AddRelations adds the 'relations' models to input models for 'relationField'
AddRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
// The relation's foreign key must be allowed to set to null.
SetRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
// ClearRelations clears all 'relationField' relations for given input models.
// The relation's foreign key must be allowed to set to null.
ClearRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField) (int64, error)
// IncludeRelation includes the relations at the 'relationField' for provided models. An optional relationFieldset might be provided.
IncludeRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) error
// GetRelations gets the 'relatedField' Models for provided the input 'models. An optional relationFieldset might be provided for the relation models.
GetRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) ([]mapping.Model, error)
// Now returns current time used by the database layer.
Now() time.Time
// ModelMap gets related model map.
ModelMap() *mapping.ModelMap
}
DB is the common interface that allows to do the queries.
type Option ¶ added in v0.21.0
type Option func(o *Options)
Option is an option function for the database settings.
func WithDefaultRepository ¶ added in v0.21.0
func WithDefaultRepository(r repository.Repository) Option
WithDefaultRepository sets the default repository for all models without specified repository.
func WithMigrateModels ¶ added in v0.21.0
WithMigrateModels sets tup the models to migrate into database structures.
func WithModelMap ¶ added in v0.21.0
WithModelMap with model map
func WithRepositoryModels ¶ added in v0.21.0
func WithRepositoryModels(r repository.Repository, models ...mapping.Model) Option
WithRepositoryModels maps the repository 'r' to provided 'models'.
func WithSynchronousConnections ¶ added in v0.21.0
func WithSynchronousConnections() Option
WithSynchronousConnections sets the synchronous connections for the db.
func WithTimeFunc ¶ added in v0.21.0
WithTimeFunc sets the function used for the timestamps.
type Options ¶ added in v0.21.0
type Options struct {
// DefaultRepository is the default repository for given controller.
DefaultRepository repository.Repository
// RepositoryModels is the mapping between repository and it's models.
RepositoryModels map[repository.Repository][]mapping.Model
// ModelMap is a mapping for the model's structures.
ModelMap *mapping.ModelMap
// TimeFunc is the time function used by timestamps.
TimeFunc func() time.Time
// SynchronousConnections gets the synchronous connections.
SynchronousConnections bool
// MigrateModels are the models set up for database migration.
MigrateModels []mapping.Model
}
type QueryDeleter ¶
type QueryDeleter interface {
// DeleteQuery deletes the values provided in the query's scope.
DeleteQuery(ctx context.Context, q *query.Scope) (int64, error)
}
QueryDeleter is an interface that allows to delete the model values in the query scope.
type QueryFinder ¶
type QueryFinder interface {
// QueryFind gets the values from the repository with respect to the query filters, sorts, pagination and included values.
// Provided 'ctx' context.Context would be used while querying the repositories.
QueryFind(ctx context.Context, q *query.Scope) ([]mapping.Model, error)
}
QueryFinder is an interface that allows to list results from the query.
type QueryGetter ¶
type QueryGetter interface {
// QueryGet gets single value from the repository taking into account the scope filters and parameters.
QueryGet(ctx context.Context, q *query.Scope) (mapping.Model, error)
}
QueryGetter is an interface that allows to get single result from query.
type QueryInserter ¶
type QueryInserter interface {
// InsertQuery stores the values within the given scope's value repository.
InsertQuery(ctx context.Context, q *query.Scope) error
}
QueryInserter is an interface that allows to store the values in the query scope.
type QueryRefresher ¶
QueryRefresher is an interface that allows to refresh the models in the query scope.
type QueryRelationAdder ¶
type QueryRelationAdder interface {
// AddRelations appends relationship 'relField' 'relationModels' to the scope values.
QueryAddRelations(ctx context.Context, s *query.Scope, relField *mapping.StructField, relationModels ...mapping.Model) error
}
QueryRelationAdder is an interface that allows to get the relations from the provided query results.
type QueryRelationClearer ¶
type QueryRelationClearer interface {
// ClearRelations clears all 'relationField' relations for given query.
// The relation's foreign key must be allowed to set to null.
QueryClearRelations(ctx context.Context, s *query.Scope, relField *mapping.StructField) (int64, error)
}
QueryRelationClearer is an interface that allows to clear the relations for provided query scope.
type QueryRelationSetter ¶
type QueryRelationSetter interface {
// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
// The relation's foreign key must be allowed to set to null.
QuerySetRelations(ctx context.Context, s *query.Scope, relationField *mapping.StructField, relationModels ...mapping.Model) error
}
QueryRelationSetter is an interface that allows to set the query relation for provided query scope.
type QueryUpdater ¶
type QueryUpdater interface {
// UpdateQuery updates the models with selected fields or a single model with provided filters.
UpdateQuery(ctx context.Context, q *query.Scope) (modelsAffected int64, err error)
}
QueryUpdater is an interface that allows to update the values in the query scope.
type RepositoryMapper ¶ added in v0.21.0
type RepositoryMapper struct {
// Repositories are the controller registered repositories.
Repositories map[string]repository.Repository
// DefaultRepository is the default repository for given controller.
DefaultRepository repository.Repository
// ModelRepositories is the mapping between the model and related repositories.
ModelRepositories map[*mapping.ModelStruct]repository.Repository
// ModelMap contains the information about the models.
ModelMap *mapping.ModelMap
}
RepositoryMapper is the database repository mapping structure.
func (*RepositoryMapper) GetRepository ¶ added in v0.21.0
func (r *RepositoryMapper) GetRepository(model mapping.Model) (repository.Repository, error)
GetRepository gets the repository for the provided model.
func (*RepositoryMapper) GetRepositoryByModelStruct ¶ added in v0.21.0
func (r *RepositoryMapper) GetRepositoryByModelStruct(mStruct *mapping.ModelStruct) (repository.Repository, error)
GetRepositoryByModelStruct gets the service by model struct.
func (*RepositoryMapper) RegisterRepositories ¶ added in v0.21.0
func (r *RepositoryMapper) RegisterRepositories(repositories ...repository.Repository)
RegisterRepositories registers provided repositories.
func (*RepositoryMapper) RegisterRepositoryModels ¶ added in v0.21.0
func (r *RepositoryMapper) RegisterRepositoryModels() error
RegisterRepositoryModels registers models in the repositories that implements repository.ModelRegistrar.
func (*RepositoryMapper) SetUnmappedModelRepositories ¶ added in v0.21.0
func (r *RepositoryMapper) SetUnmappedModelRepositories() error
SetUnmappedModelRepositories checks if all models have their repositories mapped.
type Tx ¶
type Tx struct {
Transaction *query.Transaction
// contains filtered or unexported fields
}
Tx is an in-progress transaction orm. A transaction must end with a call to Commit or Rollback. After a call to Commit or Rollback all operations on the transaction fail with an error of class
func Begin ¶
Begin starts new transaction with respect to the transaction context and transaction options with controller 'c'. If the 'db' is already a transaction the function
func (*Tx) AddRelations ¶
func (t *Tx) AddRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
AddRelations implements DB interface.
func (*Tx) ClearRelations ¶
func (t *Tx) ClearRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField) (int64, error)
ClearRelations clears all 'relationField' relations for given input models. The relation's foreign key must be allowed to set to null.
func (*Tx) Delete ¶
func (t *Tx) Delete(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
Delete implements DB interface.
func (*Tx) DeleteQuery ¶
DeleteQuery implements QueryDeleter interface.
func (*Tx) GetRelations ¶
func (t *Tx) GetRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) ([]mapping.Model, error)
GetRelations implements DB interface.
func (*Tx) IncludeRelations ¶
func (t *Tx) IncludeRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) error
IncludeRelations implements DB interface.
func (*Tx) Insert ¶
func (t *Tx) Insert(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
Insert implements DB interface.
func (*Tx) InsertQuery ¶
InsertQuery implements QueryInserter interface.
func (*Tx) Query ¶
Query builds up a new query for given 'model'. The query is executed using transaction context.
func (*Tx) QueryAddRelations ¶
func (t *Tx) QueryAddRelations(ctx context.Context, s *query.Scope, relationField *mapping.StructField, relationModels ...mapping.Model) error
QueryAddRelations implements QueryRelationAdder interface.
func (*Tx) QueryClearRelations ¶
func (t *Tx) QueryClearRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField) (int64, error)
QueryClearRelations implements QueryRelationClearer interface.
func (*Tx) QueryCtx ¶
func (t *Tx) QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder
QueryCtx builds up a new query for given 'model'. The query is executed using transaction context - provided context is used only for Builder purpose.
func (*Tx) QueryRefresh ¶
QueryRefresh implements QueryRefresher interface.
func (*Tx) QuerySetRelations ¶
func (t *Tx) QuerySetRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField, relations ...mapping.Model) error
QuerySetRelations implements QueryRelationSetter interface.
func (*Tx) Refresh ¶
func (t *Tx) Refresh(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
Refresh implements DB interface.
func (*Tx) RollbackSavepoint ¶ added in v0.20.0
RollbackSavepoint rollbacks the transaction savepoint with 'name'.
func (*Tx) SetRelations ¶
func (t *Tx) SetRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
SetRelations clears all 'relationField' for the input models and set their values to the 'relations'. The relation's foreign key must be allowed to set to null.