dbx

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 9 Imported by: 4

Documentation

Index

Constants

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

View Source
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")
)
View Source
var DefaultLimits = []int{5, 10, 25, 50}

DefaultLimits is a list of default limits.

Functions

func CheckPassword

func CheckPassword(password []byte, hashedPassword []byte) error

CheckPassword checks if the provided password is correct or not.

func HashPassword

func HashPassword(password []byte) ([]byte, error)

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.

func RowsPtr added in v0.7.8

func RowsPtr[T any](rows []T) []*T

RowsPtr is a function that returns the rows as pointers.

func SearchScope added in v0.7.10

func SearchScope[T any](pagination *Results[T]) func(db *gorm.DB) *gorm.DB

SearchScope is a function that returns a scope that searches the given fields.

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

func NewPaginated[T any]() *Paginated[T]

NewPaginated returns a new Paginated struct.

func (*Paginated[T]) GetLimit added in v0.7.8

func (p *Paginated[T]) GetLimit() int

GetLimit returns the limit.

func (*Paginated[T]) GetOffset added in v0.7.8

func (p *Paginated[T]) GetOffset() int

GetOffset returns the page.

func (*Paginated[T]) GetSearch added in v0.7.8

func (p *Paginated[T]) GetSearch() string

GetSearch returns the search.

func (*Paginated[T]) GetSort added in v0.7.8

func (p *Paginated[T]) GetSort() string

GetSort returns the sort.

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

type ReadTxFactory[R any] func(*gorm.DB) (R, error)

ReadTxFactory is a function that creates a new instance of Datastore.

type ReadWriteTxFactory

type ReadWriteTxFactory[W any] func(*gorm.DB) (W, error)

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

func NewResults[T any]() *Results[T]

NewResults returns a new Results struct.

func (*Results[T]) GetLen added in v0.7.8

func (p *Results[T]) GetLen() int

GetLen returns the length of the rows.

func (*Results[T]) GetLimit added in v0.7.8

func (p *Results[T]) GetLimit() int

GetLimit returns the limit.

func (*Results[T]) GetOffset added in v0.7.8

func (p *Results[T]) GetOffset() int

GetOffset returns the page.

func (*Results[T]) GetRows added in v0.7.8

func (p *Results[T]) GetRows() []*T

GetRows returns the rows as pointers.

func (*Results[T]) GetSearch added in v0.7.8

func (p *Results[T]) GetSearch() string

GetSearch returns the search.

func (*Results[T]) GetSort added in v0.7.8

func (p *Results[T]) GetSort() string

GetSort returns the sort.

func (*Results[T]) GetTotalPages added in v0.7.8

func (p *Results[T]) GetTotalPages() int

GetTotalPages returns the total pages.

func (*Results[T]) GetTotalRows added in v0.7.8

func (p *Results[T]) GetTotalRows() int

GetTotalRows returns the total rows.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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