Documentation ¶
Index ¶
- type DBHolder
- type DBrepository
- func (r *DBrepository[T]) Begin(ctx context.Context) (context.Context, error)
- func (r *DBrepository[T]) Commit(ctx context.Context) error
- func (r *DBrepository[T]) Create(ctx context.Context, value T) error
- func (r *DBrepository[T]) Find(ctx context.Context, v url.Values) (*udatabase.ResourcePage[T], error)
- func (r *DBrepository[T]) FindByID(ctx context.Context, id string, dst any) error
- func (r *DBrepository[T]) FindWithFilters(ctx context.Context, fs ...uormFilters.ValuedFilter[T]) (*udatabase.ResourcePage[T], error)
- func (r *DBrepository[T]) GetDBInstance(ctx context.Context) *gorm.DB
- func (r *DBrepository[T]) HandleSaveOrUpdateError(err error) error
- func (r *DBrepository[T]) IsResourceNotFound(err error) bool
- func (r *DBrepository[T]) ParseFilters(v url.Values) ([]uormFilters.ValuedFilter[T], error)
- func (r *DBrepository[T]) Rollback(ctx context.Context)
- func (r *DBrepository[T]) Save(ctx context.Context, value T) error
- type Filterer
- type FiltererMock
- type TestDBHolder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DBHolder ¶
type DBHolder struct {
// contains filtered or unexported fields
}
func NewDBHolder ¶
Returns a *DBHolder initialized with the provided config. In case the *DBConfig object has zero values, those will be filled with default values.
func (*DBHolder) GetDBInstance ¶
GetDBInstance returns the inner database object *gorm.DB provided by GORM. More on GORM here: https://gorm.io/
func (*DBHolder) RunMigrations ¶
RunMigrations runs SQL migrations found in the folder specified by DBConfig.MigrationsDir
type DBrepository ¶
type DBrepository[T any] struct { // contains filtered or unexported fields }
DBrepository is built on top of GORM to provide easier transaction management as well as methods like Save or Find.
func NewDBRepository ¶
func NewDBRepository[T any](dbHolder *DBHolder, filtersMap map[string]uormFilters.Filter[T]) *DBrepository[T]
NewDBRepository returns a DBrepository. requires a that map will be used in the method Find(context.Context, url.values) to use the filters and sorters provided in the url.values{} parameter. In case the url.values contains a filter that it is not in the filters map, it will return an error.
func (*DBrepository[T]) Begin ¶
Begin opens a new transaction. NOTE: Nested transactions not supported.
func (*DBrepository[T]) Commit ¶
func (r *DBrepository[T]) Commit(ctx context.Context) error
Commit closes and confirms the current transaction.
func (*DBrepository[T]) Create ¶
func (r *DBrepository[T]) Create(ctx context.Context, value T) error
Create is a function that creates the resource in the database.
func (*DBrepository[T]) Find ¶
func (r *DBrepository[T]) Find(ctx context.Context, v url.Values) (*udatabase.ResourcePage[T], error)
Find returns a list of elements matching the provided filters. Usage:
type Resource struct {...} var list []*Resource repository.Find(ctx, url.values{})
resourcePage is of type:
type ResourcePage[T any] struct { Total int64 `json:"total"` Limit int64 `json:"limit"` Offset int64 `json:"offset"` // Resource will be a pointer to the type passed as // dst parameter in Find method. In this example, // *[]*Resource. Resources []T`json:"resources"` }
Filter:
v := url.values{} v.Add("field", "value to use to filter") v.Add("sort", "field") // sort in ascending order v.Add("sort", "-field") // sort in descending order
func (*DBrepository[T]) FindByID ¶
FindByID returns the resource found in the variable dst. Usage:
type Resource struct {...} var obj Resource repository.FindByID(ctx, "an_ID", &obj)
func (*DBrepository[T]) FindWithFilters ¶
func (r *DBrepository[T]) FindWithFilters(ctx context.Context, fs ...uormFilters.ValuedFilter[T]) (*udatabase.ResourcePage[T], error)
func (*DBrepository[T]) GetDBInstance ¶
func (r *DBrepository[T]) GetDBInstance(ctx context.Context) *gorm.DB
func (*DBrepository[T]) HandleSaveOrUpdateError ¶
func (r *DBrepository[T]) HandleSaveOrUpdateError(err error) error
HandleSaveOrUpdateError in case of running an INSERT/UPDATE query, this method provides an easy way of checking if the returned error is nil or if it violates a PRIMARY KEY/UNIQUE constraint.
func (*DBrepository[T]) IsResourceNotFound ¶
func (r *DBrepository[T]) IsResourceNotFound(err error) bool
IsResourceNotFound in case of running custom SELECT queries using *gorm.DB, this method provides an easy way of checking if the error returned is a NotFound or other type.
func (*DBrepository[T]) ParseFilters ¶
func (r *DBrepository[T]) ParseFilters(v url.Values) ([]uormFilters.ValuedFilter[T], error)
func (*DBrepository[T]) Rollback ¶
func (r *DBrepository[T]) Rollback(ctx context.Context)
Rollback cancels the current transaction.
type FiltererMock ¶
func (*FiltererMock[T]) ParseFilters ¶
func (m *FiltererMock[T]) ParseFilters(values url.Values) ([]filters.ValuedFilter[T], error)
type TestDBHolder ¶
type TestDBHolder struct {
*DBHolder
}
func NewTestDBHolder ¶
func NewTestDBHolder(schemaName string) *TestDBHolder
func (*TestDBHolder) Reset ¶
func (d *TestDBHolder) Reset()