Documentation
¶
Overview ¶
Package dbx implements a small set of rather opinionated utilities for dealing with databases.
Index ¶
- Constants
- Variables
- func Open(opts ...Option) (*sql.DB, error)
- func OpenSQLX(opts ...Option) (*sqlx.DB, error)
- func ValidSQLIdentifier(s string) bool
- type DriverForFunc
- type EntityQueryRowxFunc
- type ExecFunc
- type NamedExecFunc
- type Option
- type PageRequest
- type PageResponse
- type PaginatedSelectFunc
- type QueryRowxFunc
- type QueryxIteratorFunc
- type Result
- type SelectFunc
Constants ¶
const MaxPageSize = 1000
MaxPageSize is the upper bound on PageRequest.PageSize. Requests exceeding this value are silently clamped.
Variables ¶
var ( ErrDatabaseTooNew = errors.New("database may be newer than migrations available in this binary") ErrNoDSN = errors.New("no data source name given") )
errors
Functions ¶
func ValidSQLIdentifier ¶
ValidSQLIdentifier reports whether s is safe to interpolate as a SQL identifier. Only ASCII letters, digits, and underscores are allowed, and the identifier must not start with a digit.
Types ¶
type DriverForFunc ¶
DriverForFunc returns (driver, migrateDBName, error).
type EntityQueryRowxFunc ¶
EntityQueryRowxFunc is useful for queries that take some entity and return an entity of the same type. For instance when using RETURNING in SQL statement.
func NewEntityQueryRowxFunc ¶
func NewEntityQueryRowxFunc[T any](db *sqlx.DB, stmt string) EntityQueryRowxFunc[T]
NewEntityQueryRowxFunc creates a new EntityQueryRowxFunc
type ExecFunc ¶
ExecFunc is useful for very simple operations like DELETE with positional arguments.
type NamedExecFunc ¶
NamedExecFunc is useful for create, update etc where you send an entity in and just want a Result and error back.
func NewNamedExecFunc ¶
func NewNamedExecFunc[T any](db *sqlx.DB, stmt string) NamedExecFunc[T]
NewNamedExecFunc creates a new NamedExecFunc
type Option ¶
type Option func(*config) error
Option is a configuration option callback type
func WithMigrationDriver ¶
func WithMigrationDriver(sqlDriverName string, migrateName string, create func(*sql.DB) (database.Driver, error)) Option
WithMigrationDriver is provided in case you want to use SQL databases beyond those provided in the default config (sqlite, postgres).
func WithMigrations ¶
WithMigrations sets the migrations filesystem and path within that filesystem. You can either pass an embed.FS or a fs.FS for a OS filesystem path using os.DirFS(path).
func WithPragmas ¶
WithPragmas appends pragmas to the config. Pragmas are executed as raw SQL statements during database opening, so they must be trusted inputs — never pass user-controlled strings.
type PageRequest ¶
type PageRequest struct {
// PageSize is the maximum number of rows to return. Zero means unlimited.
PageSize int
// After is the cursor for forward pagination (exclusive lower bound).
// Mutually exclusive with Before. Only applied when PageSize > 0.
After string
// Before is the cursor for backward pagination (exclusive upper bound).
// Mutually exclusive with After. Only applied when PageSize > 0.
Before string
}
PageRequest carries cursor-based pagination parameters for list operations. A zero PageSize means "return all results" (backward compatible).
After and Before are mutually exclusive: setting both is an error. After requests results after the given cursor (forward pagination), while Before requests results before the given cursor (backward pagination).
Note: After and Before are only applied when PageSize > 0 (i.e. when IsPaginated returns true). Setting a cursor without a page size will return all rows and silently ignore the cursor value.
func (PageRequest) IsPaginated ¶
func (p PageRequest) IsPaginated() bool
IsPaginated returns true when the caller has requested a bounded page.
type PageResponse ¶
type PageResponse struct {
// HasMore is true when additional pages exist beyond this result set.
HasMore bool
// FirstCursor is the cursor of the first item in the returned page.
FirstCursor string
// LastCursor is the cursor of the last item in the returned page.
LastCursor string
}
PageResponse carries pagination metadata returned from list operations.
type PaginatedSelectFunc ¶
type PaginatedSelectFunc[T any] func(ctx context.Context, page PageRequest, args ...any) ([]T, PageResponse, error)
PaginatedSelectFunc performs a cursor-based paginated SELECT query. The args are positional parameters for the base WHERE clause provided at construction time.
func NewPaginatedSelectFunc ¶
func NewPaginatedSelectFunc[T any](db *sqlx.DB, table, orderCol, baseWhere string, cursorFn func(T) string) PaginatedSelectFunc[T]
NewPaginatedSelectFunc creates a PaginatedSelectFunc.
table and orderCol must be valid SQL identifiers (the constructor panics otherwise, consistent with other dbx constructors that panic on invalid input detected at init time).
baseWhere is an optional WHERE clause including the WHERE keyword (e.g. "WHERE tenant_id = ?"). Pass "" to select all rows.
cursorFn extracts a cursor string from a result row for populating PageResponse.FirstCursor and LastCursor.
type QueryRowxFunc ¶
QueryRowxFunc is useful for queries that return one row and takes positional arguments. For instance get operations on a single row.
func NewQueryRowxFunc ¶
func NewQueryRowxFunc[T any](db *sqlx.DB, stmt string) QueryRowxFunc[T]
NewQueryRowxFunc creates a new QueryRowxFunc
type QueryxIteratorFunc ¶
QueryxIteratorFunc is useful for queries that return (potentially) large result sets and you want to be able to stream the result.
func NewQueryxIteratorFunc ¶
func NewQueryxIteratorFunc[T any](db *sqlx.DB, stmt string) QueryxIteratorFunc[T]
NewQueryxIteratorFunc creates a new QueryxIteratorFunc
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result holds the outcome of a mutating operation
func NewResult ¶
NewResult wraps a sql.Result into a dbx.Result, extracting LastInsertId and RowsAffected if available.
func (Result) LastInsertID ¶
LastInsertID returns the last insert ID if available
func (Result) RowsAffected ¶
RowsAffected returns the number of rows affected if available
type SelectFunc ¶
SelectFunc is useful for when you perform selects and you know the result set will be small or at least bounded to acceptable size.
func NewSelectFunc ¶
func NewSelectFunc[T any](db *sqlx.DB, stmt string) SelectFunc[T]
NewSelectFunc creates a new SelectFunc
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dbxtest contains unit tests for dbx
|
Package dbxtest contains unit tests for dbx |
|
examples
|
|
|
open
command
Example: opening a database with migrations and pragmas.
|
Example: opening a database with migrations and pragmas. |
|
paginate
command
Example: cursor-based pagination.
|
Example: cursor-based pagination. |
|
prepared
command
Example: prepared statement functions for CRUD operations.
|
Example: prepared statement functions for CRUD operations. |