Documentation ¶
Index ¶
- type DBHolder
- type DBrepository
- func (r *DBrepository[T]) ApplyFilters(db Querier, query string, v url.Values) (queryResult string, args []any, limit, offset int64, err error)
- func (r *DBrepository[T]) Begin(ctx context.Context) (context.Context, error)
- func (r *DBrepository[T]) Commit(ctx context.Context) error
- func (r *DBrepository[T]) GetContext(ctx context.Context, db Querier, dst T, query string, v url.Values) (T, error)
- func (r *DBrepository[T]) GetDBInstance() *sqlx.DB
- func (r *DBrepository[T]) GetTransaction(ctx context.Context) *sqlx.Tx
- func (r *DBrepository[T]) HandleSaveOrUpdateError(res sql.Result, err error) error
- func (r *DBrepository[T]) HandleSearchError(err error) error
- func (r *DBrepository[T]) Rollback(ctx context.Context)
- func (r *DBrepository[T]) SelectContext(ctx context.Context, db Querier, query string, v url.Values) (rp *udatabase.ResourcePage[T], err error)
- type Querier
- 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 *sqlx.DB provided by sqlx. More on sqlx here: https://github.com/jmoiron/sqlx
func (*DBHolder) MapperFunc ¶
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 sqlx to provide easier transaction management as well as methods for error handling.
func NewDBRepository ¶
func NewDBRepository[T any](dbHolder *DBHolder, filtersMap map[string]usqlFilters.Filter, sorters map[string]usqlFilters.Sorter) *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]) ApplyFilters ¶
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]) GetContext ¶
func (*DBrepository[T]) GetDBInstance ¶
func (r *DBrepository[T]) GetDBInstance() *sqlx.DB
func (*DBrepository[T]) GetTransaction ¶
func (r *DBrepository[T]) GetTransaction(ctx context.Context) *sqlx.Tx
func (*DBrepository[T]) HandleSaveOrUpdateError ¶
func (r *DBrepository[T]) HandleSaveOrUpdateError(res sql.Result, 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]) HandleSearchError ¶
func (r *DBrepository[T]) HandleSearchError(err error) error
IsResourceNotFound in case of running SELECT queries using *sqlx.DB/*sqlx.Tx, this method provides an easy way of checking if the error returned is a NotFound or other type.
func (*DBrepository[T]) Rollback ¶
func (r *DBrepository[T]) Rollback(ctx context.Context)
Rollback cancels the current transaction.
func (*DBrepository[T]) SelectContext ¶
func (r *DBrepository[T]) SelectContext(ctx context.Context, db Querier, query string, v url.Values) (rp *udatabase.ResourcePage[T], err error)
type TestDBHolder ¶
type TestDBHolder struct {
*DBHolder
}
func NewTestDBHolder ¶
func NewTestDBHolder(schemaName string) *TestDBHolder
func (*TestDBHolder) Reset ¶
func (d *TestDBHolder) Reset()