utils

package
v0.0.0-...-7cd1858 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func Create[T db.Model](ctx context.Context, db DBQuery, record T, fields ...string) (*T, error)

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

func Delete[T db.Model](ctx context.Context, db DBQuery, whereExpr psql.Expression) (int64, error)

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

func Exists[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID) (bool, error)

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

func Find[T db.Model](ctx context.Context, db DBQuery, uuid uuid.UUID) (*T, error)

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

func FindAll[T db.Model](ctx context.Context, db DBQuery, fields ...string) ([]T, error)

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

func GetColumnsAndValues[T db.Model](s T, tags DBTag) ([]string, []any)

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[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

type DBTag map[string]string

func CompareObjects

func CompareObjects[T db.Model](a, b T, excluded ...string) DBTag

CompareObjects compares two objects and returns the tags for fields having differing values. Any field names listed in `excluded` are ignored.

func GetAllDBTagsFromStruct

func GetAllDBTagsFromStruct[T db.Model](s T) DBTag

GetAllDBTagsFromStruct returns a map of field names to their db tags.

func GetDBTagsFromStructFields

func GetDBTagsFromStructFields[T db.Model](s T, fields ...string) DBTag

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

func GetNonNilDBTagsFromStruct[T db.Model](s T) DBTag

GetNonNilDBTagsFromStruct returns a map of field names to their db tags. Only non-pointer fields or non-nil pointer fields are considered.

func (DBTag) Columns

func (r DBTag) Columns() []any

Columns is used in the Columns method of the SelectBuilder to convert the DBTag to a slice of any.

func (DBTag) Fields

func (r DBTag) Fields() []string

Fields is used get the list of field names from the DBTag map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL