Documentation
¶
Index ¶
- Variables
- type Condition
- type ConditionGroup
- type DB
- func (db *DB) AddDateFilters(query *bun.SelectQuery, filters []DateFilter) *bun.SelectQuery
- func (db *DB) CreateExtensions(extensions []string) error
- func (db *DB) CreateTables(models []interface{}) error
- func (db *DB) QueryBuilder(groups []ConditionGroup) (query string, queryArgs []interface{}, err error)
- func (db *DB) QueryCount(ctx context.Context, model interface{}, query string, ...) (int, error)
- func (db *DB) RegisterModels(models []interface{}) error
- func (db *DB) ResetTables(models []interface{}) error
- func (db *DB) RollbackLastMigration(migrations *migrate.Migrations) error
- func (db *DB) RunMigrations(migrations *migrate.Migrations) error
- type DBModel
- type DateFilter
- type LogicalOperator
- type Operator
- type Option
- type PaginableModel
Constants ¶
This section is empty.
Variables ¶
var ( EqualOperator = Operator{"="} GreaterThanOperator = Operator{">"} LessThanOperator = Operator{"<"} GreaterThanOrEqualOperator = Operator{">="} LessThanOrEqualOperator = Operator{"<="} NotEqualOperator = Operator{"!="} InOperator = Operator{"IN"} NotInOperator = Operator{"NOT IN"} LikeOperator = Operator{"LIKE"} ILikeOperator = Operator{"ILIKE"} )
var ( AndOperator = LogicalOperator{"AND"} OrOperator = LogicalOperator{"OR"} )
Functions ¶
This section is empty.
Types ¶
type Condition ¶
type Condition struct {
Column string
Comparison Operator
LogOp LogicalOperator
Value interface{}
}
Condition values are only safe with trusted input: Column is interpolated raw into SQL (to support expressions like CONCAT(...) or unaccent(...)), so it must come from code, never from client input. Only Value is bound as a parameter.
Slice values become "column IN (?)" or "NOT IN (?)". For compatibility, = and != are accepted as aliases of IN and NOT IN. An empty or nil slice is skipped like any other zero value. Elements must be strings, ints, uints wider than a byte, floats, bools, or types implementing driver.Valuer such as uuid.UUID. Byte slices and types bun expands as raw SQL, such as bun.Safe, are rejected.
type ConditionGroup ¶
type ConditionGroup struct {
Conditions []Condition
LogOp LogicalOperator
}
type DB ¶
func (*DB) AddDateFilters ¶
func (db *DB) AddDateFilters(query *bun.SelectQuery, filters []DateFilter) *bun.SelectQuery
func (*DB) CreateExtensions ¶
CreateExtensions - Creates a database extension if it doesn't already exist. Extensions are a Postgres concept, so requesting any on another dialect is an error rather than a silent no-op.
func (*DB) CreateTables ¶
CreateTables - Creates the database schema if it doesn't already exist
func (*DB) QueryBuilder ¶
func (db *DB) QueryBuilder(groups []ConditionGroup) (query string, queryArgs []interface{}, err error)
QueryBuilder builds a WHERE clause and its arguments from condition groups.
The builder is frozen and slated for deprecation. It will not gain new operators or value types. New queries should use bun's Where, WhereOr, WhereGroup and bun.List directly. A formal deprecation notice will follow in a later release.
func (*DB) QueryCount ¶
func (*DB) RegisterModels ¶
RegisterModels - Registers many-to-many relationship
func (*DB) ResetTables ¶
ResetTables - Drops and recreates the database schema
func (*DB) RollbackLastMigration ¶
func (db *DB) RollbackLastMigration(migrations *migrate.Migrations) error
RollbackLastMigration - Rollbacks the last migration
func (*DB) RunMigrations ¶
func (db *DB) RunMigrations(migrations *migrate.Migrations) error
RunMigrations - Executes all pending migrations
type DBModel ¶
type DBModel interface {
// Validate checks the model for logical and business-rule correctness.
Validate() error
// Insert validates and inserts the model into the DB.
Insert(ctx context.Context, db bun.IDB) error
// Update validates and updates the model in the DB.
Update(ctx context.Context, db bun.IDB) error
// Delete removes the model from the DB.
Delete(ctx context.Context, db bun.IDB) error
}
DBModel defines the minimal interface for a model that can be persisted.
type DateFilter ¶
type DateFilter struct {
DateColumn string `json:"date_column"`
FromDate string `json:"from_date"`
ToDate string `json:"to_date"`
FromDateUnix int64 `json:"-"`
ToDateUnix int64 `json:"-"`
}
func (*DateFilter) ParseToUnix ¶
func (df *DateFilter) ParseToUnix(validDBColumns []string) error
type LogicalOperator ¶
type LogicalOperator struct {
Value string
}
func (LogicalOperator) String ¶
func (s LogicalOperator) String() string
type Option ¶
func WithExtensions ¶
func WithRegistrableModels ¶
func WithRegistrableModels(models []interface{}) Option
type PaginableModel ¶
type PaginableModel interface {
// GetCursor returns a stable, opaque value representing the record’s
// position in the sorted result set.
GetCursor() string
// GetSortField returns the DB column used for ordering in pagination queries.
GetSortField() string
// GetSortValue returns the value of the sort field for this record.
GetSortValue() interface{}
// GetUniqueField returns the DB column that uniquely identifies this record.
GetUniqueField() string
// GetUniqueValue returns the value of the unique field for this record.
GetUniqueValue() interface{}
}
PaginableModel defines the minimal interface for cursor-based pagination.