Documentation ¶
Index ¶
- Variables
- func Create[T db.Model](ctx context.Context, db DBQuery, record T, fields ...string) (*T, error)
- func Delete[T db.Model](ctx context.Context, db DBQuery, whereExpr psql.Expression) (int64, error)
- func ExecuteCollectExactlyOneRow[T db.Model](ctx context.Context, db DBQuery, sql string, args []any) (*T, error)
- func ExecuteCollectRows[T db.Model](ctx context.Context, db DBQuery, sql string, args []any) ([]T, error)
- func Exists[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID) (bool, error)
- func Find[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID) (*T, error)
- func FindAll[T db.Model](ctx context.Context, db DBQuery, fields ...string) ([]T, error)
- func GetColumnsAndValues[T db.Model](s T, tags DBTag) ([]string, []any)
- func Search[T db.Model](ctx context.Context, db DBQuery, whereExpr bob.Expression, fields ...string) ([]T, error)
- func Update[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID, record T, fields ...string) (*T, error)
- type DBQuery
- type DBTag
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("record not found")
ErrNotFound represents the error returned by any repository when not record matches the requested criteria
Functions ¶
func Create ¶
Create creates a record of the requested model type. The "record" argument is the record to store in the database. The "fields" argument is a list of columns to store. If no fields are specified only non-nil fields are stored. The stored record is returned on success; otherwise an error is returned.
func Delete ¶
Delete deletes a specific tuple from the database table specified using a custom expression. The `whereExpr` argument is a custom expression to filter the records. The number of rows affected is returned on success; otherwise an error is returned.
func ExecuteCollectExactlyOneRow ¶
func ExecuteCollectExactlyOneRow[T db.Model](ctx context.Context, db DBQuery, sql string, args []any) (*T, error)
ExecuteCollectExactlyOneRow executes a query and collects result using pgx.CollectExactlyOneRow.
func ExecuteCollectRows ¶
func ExecuteCollectRows[T db.Model](ctx context.Context, db DBQuery, sql string, args []any) ([]T, error)
ExecuteCollectRows executes a query and collects result using pgx.CollectRows.
func Exists ¶
Exists checks whether a record exists in the database table specified. The `uuid` argument is the primary key of the record to check.
func Find ¶
Find retrieves a specific tuple from the database table specified. The `uuid` argument is the primary key of the record to retrieve. If no record is found ErrNotFound is returned as an error.
func FindAll ¶
FindAll retrieves all tuples from the database table specified. The `fields` argument is a list of columns to retrieve. If no fields are specified then all columns are fetched. If no records are found then an empty array is returned.
func GetColumnsAndValues ¶
GetColumnsAndValues returns the list of values associated to the field names specified in the tags parameter. Both the columns and the values are returned together to ensure that they are aligned.
func Search ¶
func Search[T db.Model](ctx context.Context, db DBQuery, whereExpr bob.Expression, fields ...string) ([]T, error)
Search retrieves tuples from the database table specified using a custom expression. The `fields` argument is a list of columns to retrieve. If no fields are specified then all columns are fetched. The `whereExpr` argument is a custom expression to filter the records. If no records are found then an empty array is returned.
func Update ¶
func Update[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID, record T, fields ...string) (*T, error)
Update attempts to update a record of the requested model type. The `uuid` argument is the primary key of the record to update. The `record` argument is the record to update in the database. The `fields` argument is a list of columns to update. If no fields are specified only non-nil fields are updated. The updated record is returned on success; otherwise an error is returned.
Types ¶
type DBQuery ¶
type DBQuery interface { Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row }
DBQuery is an abstraction to allow passing either a pool or a transaction query function to any of the utilities.
type DBTag ¶
func CompareObjects ¶
CompareObjects compares two objects and returns the tags for fields having differing values. Any field names listed in `excluded` are ignored.
func GetAllDBTagsFromStruct ¶
GetAllDBTagsFromStruct returns a map of field names to their db tags.
func GetDBTagsFromStructFields ¶
GetDBTagsFromStructFields returns a map of field names to their db tags. It only returns the tags of the fields specified. Non-existent fields are ignored.
func GetNonNilDBTagsFromStruct ¶
GetNonNilDBTagsFromStruct returns a map of field names to their db tags. Only non-pointer fields or non-nil pointer fields are considered.