Documentation
¶
Index ¶
- Constants
- Variables
- func CheckPassword(password []byte, hashedPassword []byte) error
- func HashPassword(password []byte) ([]byte, error)
- func Paginate[T any](_ interface{}, pagination *Paginated[T], _ *gorm.DB) func(db *gorm.DB) *gorm.DB
- func PaginatedResults[T any](value interface{}, pagination *Results[T], db *gorm.DB) func(db *gorm.DB) *gorm.DB
- func RowsPtr[T any](rows []T) []*T
- func SearchScope[T any](pagination *Results[T]) func(db *gorm.DB) *gorm.DB
- type Database
- type Migrator
- type Paginated
- type QueryError
- type ReadTxFactory
- type ReadWriteTxFactory
- type Results
- func (p *Results[T]) GetLen() int
- func (p *Results[T]) GetLimit() int
- func (p *Results[T]) GetOffset() int
- func (p *Results[T]) GetRows() []*T
- func (p *Results[T]) GetSearch() string
- func (p *Results[T]) GetSort() string
- func (p *Results[T]) GetTotalPages() int
- func (p *Results[T]) GetTotalRows() int
Constants ¶
const ( // SortNone is the no sort order. SortNone = "" // SortAsc is the ascending sort order. SortAsc = "asc" // SortDesc is the descending sort order. SortDesc = "desc" )
Variables ¶
var ( // ErrFailedToHashPassword is returned when the password hashing fails. ErrFailedToHashPassword = errors.New("dbx: failed to hash password") // ErrFailedCheckPassword is returned when the password check fails. ErrFailedCheckPassword = errors.New("dbx: failed to check password") )
var DefaultLimits = []int{5, 10, 25, 50}
DefaultLimits is a list of default limits.
Functions ¶
func CheckPassword ¶
CheckPassword checks if the provided password is correct or not.
func HashPassword ¶
HashPassword returns the bcrypt hash of the password.
func Paginate ¶ added in v0.7.8
func Paginate[T any](_ interface{}, pagination *Paginated[T], _ *gorm.DB) func(db *gorm.DB) *gorm.DB
Paginate returns a function that paginates the results.
func PaginatedResults ¶ added in v0.7.8
func PaginatedResults[T any](value interface{}, pagination *Results[T], db *gorm.DB) func(db *gorm.DB) *gorm.DB
PaginatedResults returns a function that paginates the results.
Types ¶
type Database ¶
type Database[R, W any] interface { // ReadTx starts a read only transaction. ReadTx(context.Context, func(context.Context, R) error) error // ReadWriteTx starts a read write transaction. ReadWriteTx(context.Context, func(context.Context, W) error) error Migrator io.Closer }
Database provides methods for transactional operations.
func NewDatabase ¶
func NewDatabase[R, W any](conn *gorm.DB, r ReadTxFactory[R], rw ReadWriteTxFactory[W]) (Database[R, W], error)
NewDatabase returns a new instance of db.
type Migrator ¶
type Migrator interface {
// Migrate is a method that runs the migration.
Migrate(context.Context, ...any) error
}
Migrator is a method that runs the migration.
type Paginated ¶ added in v0.7.8
type Paginated[T any] struct { // Limit is the number of items to return. Limit int `json:"limit" xml:"limit" form:"limit" query:"limit"` // Offset is the number of items to skip. Offset int `json:"offset" xml:"offset" form:"offset" query:"offset"` // Search is the search term to filter the results. Search string `json:"search,omitempty" xml:"search" form:"search" query:"search"` // Sort is the sorting order. Sort string `json:"sort,omitempty" xml:"sort" form:"sort" query:"sort"` // Value is the value to paginate. Value T `json:"value,omitempty" xml:"value" form:"value" query:"value"` }
Paginated is a struct that contains the properties of a pagination.
func NewPaginated ¶ added in v0.7.10
NewPaginated returns a new Paginated struct.
type QueryError ¶
type QueryError struct {
// Query is the query that caused the error.
Query string
// Err is the error that occurred.
Err error
}
QueryError is an error that occurred while executing a query.
func NewQueryError ¶
func NewQueryError(query string, err error) *QueryError
NewQueryError returns a new QueryError.
func (*QueryError) Error ¶
func (e *QueryError) Error() string
Error implements the error interface.
func (*QueryError) Unwrap ¶
func (e *QueryError) Unwrap() error
Unwrap implements the errors.Wrapper interface.
type ReadTxFactory ¶
ReadTxFactory is a function that creates a new instance of Datastore.
type ReadWriteTxFactory ¶
ReadWriteTxFactory is a function that creates a new instance of Datastore.
type Results ¶ added in v0.7.8
type Results[T any] struct { // Limit is the number of items to return. Limit int `json:"limit" xml:"limit" form:"limit" query:"limit"` // Offset is the number of items to skip. Offset int `json:"offset" xml:"offset" form:"offset" query:"offset"` // Search is the search term to filter the results. Search string `json:"search,omitempty" xml:"search" form:"search" query:"search"` // SearchFields is the search term to filter the results. SearchFields []string `json:"-"` // Sort is the sorting order. Sort string `json:"sort,omitempty" xml:"sort" form:"sort" query:"sort"` // TotalRows is the total number of rows. TotalRows int `json:"total_rows"` // TotalPages is the total number of pages. TotalPages int `json:"total_pages"` // Rows is the items to return. Rows []T `json:"rows" xml:"rows"` }
Results is a struct that contains the results of a query.
func NewResults ¶ added in v0.7.10
NewResults returns a new Results struct.
func (*Results[T]) GetRows ¶ added in v0.7.8
func (p *Results[T]) GetRows() []*T
GetRows returns the rows as pointers.
func (*Results[T]) GetTotalPages ¶ added in v0.7.8
GetTotalPages returns the total pages.
func (*Results[T]) GetTotalRows ¶ added in v0.7.8
GetTotalRows returns the total rows.