rel¶
import "github.com/go-rel/rel"
Overview¶
Package rel contains all rel primary APIs, such as Repository.
Constants¶
const (
// BelongsTo association.
BelongsTo = iota
// HasOne association.
HasOne
// HasMany association.
HasMany
)
Variables¶
var (
// ErrNotFound returned when records not found.
ErrNotFound = NotFoundError{}
// ErrCheckConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrCheckConstraint).
ErrCheckConstraint = ConstraintError{Type: CheckConstraint}
// ErrNotNullConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrNotNullConstraint).
ErrNotNullConstraint = ConstraintError{Type: NotNullConstraint}
// ErrUniqueConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrUniqueConstraint).
ErrUniqueConstraint = ConstraintError{Type: UniqueConstraint}
// ErrPrimaryKeyConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrPrimaryKeyConstraint).
ErrPrimaryKeyConstraint = ConstraintError{Type: PrimaryKeyConstraint}
// ErrForeignKeyConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrForeignKeyConstraint).
ErrForeignKeyConstraint = ConstraintError{Type: ForeignKeyConstraint}
)
var (
// NewSortAsc sorts field with ascending sort.
//
// Deprecated: use SortAsc instead
NewSortAsc = SortAsc
// NewSortDesc sorts field with descending sort.
//
// Deprecated: use SortDesc instead
NewSortDesc = SortDesc
)
var Setf = SetFragment
func DefaultLogger¶
func DefaultLogger(ctx context.Context, op string, message string) func(err error)
func Nullable¶
func Nullable(dest interface{}) interface{}
type Adapter¶
type Adapter interface {
Close() error
Instrumentation(instrumenter Instrumenter)
Ping(ctx context.Context) error
Aggregate(ctx context.Context, query Query, mode string, field string) (int, error)
Query(ctx context.Context, query Query) (Cursor, error)
Insert(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate, onConflict OnConflict) (interface{}, error)
InsertAll(ctx context.Context, query Query, primaryField string, fields []string, bulkMutates []map[string]Mutate, onConflict OnConflict) ([]interface{}, error)
Update(ctx context.Context, query Query, primaryField string, mutates map[string]Mutate) (int, error)
Delete(ctx context.Context, query Query) (int, error)
Exec(ctx context.Context, stmt string, args []interface{}) (int64, int64, error)
Begin(ctx context.Context) (Adapter, error)
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
Apply(ctx context.Context, migration Migration) error
}
type AlterTable¶
type AlterTable struct {
Table
}
func (*AlterTable) DropColumn¶
func (at *AlterTable) DropColumn(name string, options ...ColumnOption)
func (*AlterTable) RenameColumn¶
func (at *AlterTable) RenameColumn(name string, newName string, options ...ColumnOption)
type AssocMutation¶
type AssocMutation struct {
Mutations []Mutation
DeletedIDs []interface{} // This is array of single id, and doesn't support composite primary key.
}
type Association¶
type Association struct {
// contains filtered or unexported fields
}
func (Association) Autoload¶
func (a Association) Autoload() bool
func (Association) Autosave¶
func (a Association) Autosave() bool
func (Association) Collection¶
func (a Association) Collection() (*Collection, bool)
func (Association) Document¶
func (a Association) Document() (*Document, bool)
func (Association) ForeignField¶
func (a Association) ForeignField() string
func (Association) ForeignValue¶
func (a Association) ForeignValue() interface{}
func (Association) IsZero¶
func (a Association) IsZero() bool
func (Association) LazyDocument¶
func (a Association) LazyDocument() (*Document, bool)
func (Association) ReferenceField¶
func (a Association) ReferenceField() string
func (Association) ReferenceValue¶
func (a Association) ReferenceValue() interface{}
func (Association) Through¶
func (a Association) Through() string
func (Association) Type¶
func (a Association) Type() AssociationType
type AssociationType¶
type AssociationType uint8
type Cascade¶
type Cascade bool
func (Cascade) Apply¶
func (c Cascade) Apply(doc *Document, mutation *Mutation)
func (Cascade) Build¶
func (c Cascade) Build(query *Query)
func (Cascade) String¶
func (c Cascade) String() string
type ChangeOp¶
type ChangeOp int
const (
// ChangeInvalidOp operation.
ChangeInvalidOp ChangeOp = iota
// ChangeSetOp operation.
ChangeSetOp
// ChangeIncOp operation.
ChangeIncOp
// ChangeFragmentOp operation.
ChangeFragmentOp
)
type Changeset¶
type Changeset struct {
// contains filtered or unexported fields
}
func NewChangeset¶
func NewChangeset(record interface{}) Changeset
func (Changeset) Apply¶
func (c Changeset) Apply(doc *Document, mut *Mutation)
func (Changeset) Changes¶
func (c Changeset) Changes() map[string]interface{}
func (Changeset) FieldChanged¶
func (c Changeset) FieldChanged(field string) bool
type Collection¶
type Collection struct {
// contains filtered or unexported fields
}
func NewCollection¶
func NewCollection(records interface{}, readonly ...bool) *Collection
func (Collection) Add¶
func (c Collection) Add() *Document
func (Collection) Get¶
func (c Collection) Get(index int) *Document
func (Collection) Len¶
func (c Collection) Len() int
func (Collection) PrimaryField¶
func (c Collection) PrimaryField() string
func (Collection) PrimaryFields¶
func (c Collection) PrimaryFields() []string
func (Collection) PrimaryValue¶
func (c Collection) PrimaryValue() interface{}
func (Collection) PrimaryValues¶
func (c Collection) PrimaryValues() []interface{}
func (Collection) ReflectValue¶
func (c Collection) ReflectValue() reflect.Value
func (Collection) Reset¶
func (c Collection) Reset()
func (Collection) Slice¶
func (c Collection) Slice(i, j int) *Collection
func (Collection) Swap¶
func (c Collection) Swap(i, j int)
func (*Collection) Table¶
func (c *Collection) Table() string
func (Collection) Truncate¶
func (c Collection) Truncate(i, j int)
type Column¶
type Column struct {
Op SchemaOp
Name string
Type ColumnType
Rename string
Primary bool
Unique bool
Required bool
Unsigned bool
Limit int
Precision int
Scale int
Default interface{}
Options string
}
type ColumnOption¶
type ColumnOption interface {
// contains filtered or unexported methods
}
func Default¶
func Default(def interface{}) ColumnOption
type ColumnType¶
type ColumnType string
const (
// ID ColumnType.
ID ColumnType = "ID"
// BigID ColumnType.
BigID ColumnType = "BigID"
// Bool ColumnType.
Bool ColumnType = "BOOL"
// SmallInt ColumnType.
SmallInt ColumnType = "SMALLINT"
// Int ColumnType.
Int ColumnType = "INT"
// BigInt ColumnType.
BigInt ColumnType = "BIGINT"
// Float ColumnType.
Float ColumnType = "FLOAT"
// Decimal ColumnType.
Decimal ColumnType = "DECIMAL"
// String ColumnType.
String ColumnType = "STRING"
// Text ColumnType.
Text ColumnType = "TEXT"
// JSON ColumnType that will fallback to Text ColumnType if adapter does not support it.
JSON ColumnType = "JSON"
// Date ColumnType.
Date ColumnType = "DATE"
// DateTime ColumnType.
DateTime ColumnType = "DATETIME"
// Time ColumnType.
Time ColumnType = "TIME"
)
type ConstraintError¶
type ConstraintError struct {
Key string
Type ConstraintType
Err error
}
func (ConstraintError) Error¶
func (ce ConstraintError) Error() string
func (ConstraintError) Is¶
func (ce ConstraintError) Is(target error) bool
func (ConstraintError) Unwrap¶
func (ce ConstraintError) Unwrap() error
type ConstraintType¶
type ConstraintType int8
const (
// CheckConstraint error type.
CheckConstraint ConstraintType = iota
// NotNullConstraint error type.1
NotNullConstraint
// UniqueConstraint error type.1
UniqueConstraint
// PrimaryKeyConstraint error type.1
PrimaryKeyConstraint
// ForeignKeyConstraint error type.1
ForeignKeyConstraint
)
func (ConstraintType) String¶
func (ct ConstraintType) String() string
type Cursor¶
type Cursor interface {
Close() error
Fields() ([]string, error)
Next() bool
Scan(...interface{}) error
NopScanner() interface{} // TODO: conflict with manual scanners interface
}
type Do¶
type Do func(context.Context, Repository) error
type Document¶
type Document struct {
// contains filtered or unexported fields
}
func NewDocument¶
func NewDocument(record interface{}, readonly ...bool) *Document
func (*Document) Add¶
func (d *Document) Add() *Document
func (Document) Association¶
func (d Document) Association(name string) Association
func (Document) BelongsTo¶
func (d Document) BelongsTo() []string
func (Document) Fields¶
func (d Document) Fields() []string
func (Document) Flag¶
func (d Document) Flag(flag DocumentFlag) bool
func (*Document) Get¶
func (d *Document) Get(index int) *Document
func (Document) HasMany¶
func (d Document) HasMany() []string
func (Document) HasOne¶
func (d Document) HasOne() []string
func (Document) Index¶
func (d Document) Index() map[string][]int
func (*Document) Len¶
func (d *Document) Len() int
func (Document) Persisted¶
func (d Document) Persisted() bool
func (Document) Preload¶
func (d Document) Preload() []string
func (Document) PrimaryField¶
func (d Document) PrimaryField() string
func (Document) PrimaryFields¶
func (d Document) PrimaryFields() []string
func (Document) PrimaryValue¶
func (d Document) PrimaryValue() interface{}
func (Document) PrimaryValues¶
func (d Document) PrimaryValues() []interface{}
func (Document) ReflectValue¶
func (d Document) ReflectValue() reflect.Value
func (Document) Reset¶
func (d Document) Reset()
func (Document) Scanners¶
func (d Document) Scanners(fields []string) []interface{}
func (Document) SetValue¶
func (d Document) SetValue(field string, value interface{}) bool
func (Document) Table¶
func (d Document) Table() string
func (Document) Type¶
func (d Document) Type(field string) (reflect.Type, bool)
func (Document) Value¶
func (d Document) Value(field string) (interface{}, bool)
type DocumentFlag¶
type DocumentFlag int8
const (
// Invalid flag.
Invalid DocumentFlag = 1 << iota
// HasCreatedAt flag.
HasCreatedAt
// HasUpdatedAt flag.
HasUpdatedAt
// HasDeletedAt flag.
HasDeletedAt
// HasDeleted flag.
HasDeleted
// Versioning
HasVersioning
)
func (DocumentFlag) Is¶
func (df DocumentFlag) Is(flag DocumentFlag) bool
type ErrorFunc¶
type ErrorFunc func(error) error
func (ErrorFunc) Apply¶
func (ef ErrorFunc) Apply(doc *Document, mutation *Mutation)
type FilterOp¶
type FilterOp int
const (
// FilterAndOp is filter type for and operator.
FilterAndOp FilterOp = iota
// FilterOrOp is filter type for or operator.
FilterOrOp
// FilterNotOp is filter type for not operator.
FilterNotOp
// FilterEqOp is filter type for equal comparison.
FilterEqOp
// FilterNeOp is filter type for not equal comparison.
FilterNeOp
// FilterLtOp is filter type for less than comparison.
FilterLtOp
// FilterLteOp is filter type for less than or equal comparison.
FilterLteOp
// FilterGtOp is filter type for greater than comparison.
FilterGtOp
// FilterGteOp is filter type for greter than or equal comparison.
FilterGteOp
// FilterNilOp is filter type for nil check.
FilterNilOp
// FilterNotNilOp is filter type for not nil check.
FilterNotNilOp
// FilterInOp is filter type for inclusion comparison.
FilterInOp
// FilterNinOp is filter type for not inclusion comparison.
FilterNinOp
// FilterLikeOp is filter type for like comparison.
FilterLikeOp
// FilterNotLikeOp is filter type for not like comparison.
FilterNotLikeOp
// FilterFragmentOp is filter type for custom filter.
FilterFragmentOp
)
func (FilterOp) String¶
func (fo FilterOp) String() string
type FilterQuery¶
type FilterQuery struct {
Type FilterOp
Field string
Value interface{}
Inner []FilterQuery
}
func And¶
func And(inner ...FilterQuery) FilterQuery
func Eq¶
func Eq(field string, value interface{}) FilterQuery
func FilterFragment¶
func FilterFragment(expr string, values ...interface{}) FilterQuery
func Gt¶
func Gt(field string, value interface{}) FilterQuery
func Gte¶
func Gte(field string, value interface{}) FilterQuery
func In¶
func In(field string, values ...interface{}) FilterQuery
func InInt¶
func InInt(field string, values []int) FilterQuery
func InString¶
func InString(field string, values []string) FilterQuery
func InUint¶
func InUint(field string, values []uint) FilterQuery
func Like¶
func Like(field string, pattern string) FilterQuery
func Lt¶
func Lt(field string, value interface{}) FilterQuery
func Lte¶
func Lte(field string, value interface{}) FilterQuery
func Ne¶
func Ne(field string, value interface{}) FilterQuery
func Nil¶
func Nil(field string) FilterQuery
func Nin¶
func Nin(field string, values ...interface{}) FilterQuery
func NinInt¶
func NinInt(field string, values []int) FilterQuery
func NinString¶
func NinString(field string, values []string) FilterQuery
func NinUint¶
func NinUint(field string, values []uint) FilterQuery
func Not¶
func Not(inner ...FilterQuery) FilterQuery
func NotLike¶
func NotLike(field string, pattern string) FilterQuery
func NotNil¶
func NotNil(field string) FilterQuery
func Or¶
func Or(inner ...FilterQuery) FilterQuery
func (FilterQuery) And¶
func (fq FilterQuery) And(filters ...FilterQuery) FilterQuery
func (FilterQuery) AndEq¶
func (fq FilterQuery) AndEq(field string, value interface{}) FilterQuery
func (FilterQuery) AndFragment¶
func (fq FilterQuery) AndFragment(expr string, values ...interface{}) FilterQuery
func (FilterQuery) AndGt¶
func (fq FilterQuery) AndGt(field string, value interface{}) FilterQuery
func (FilterQuery) AndGte¶
func (fq FilterQuery) AndGte(field string, value interface{}) FilterQuery
func (FilterQuery) AndIn¶
func (fq FilterQuery) AndIn(field string, values ...interface{}) FilterQuery
func (FilterQuery) AndLike¶
func (fq FilterQuery) AndLike(field string, pattern string) FilterQuery
func (FilterQuery) AndLt¶
func (fq FilterQuery) AndLt(field string, value interface{}) FilterQuery
func (FilterQuery) AndLte¶
func (fq FilterQuery) AndLte(field string, value interface{}) FilterQuery
func (FilterQuery) AndNe¶
func (fq FilterQuery) AndNe(field string, value interface{}) FilterQuery
func (FilterQuery) AndNil¶
func (fq FilterQuery) AndNil(field string) FilterQuery
func (FilterQuery) AndNin¶
func (fq FilterQuery) AndNin(field string, values ...interface{}) FilterQuery
func (FilterQuery) AndNotLike¶
func (fq FilterQuery) AndNotLike(field string, pattern string) FilterQuery
func (FilterQuery) AndNotNil¶
func (fq FilterQuery) AndNotNil(field string) FilterQuery
func (FilterQuery) Build¶
func (fq FilterQuery) Build(query *Query)
func (FilterQuery) None¶
func (fq FilterQuery) None() bool
func (FilterQuery) Or¶
func (fq FilterQuery) Or(filter ...FilterQuery) FilterQuery
func (FilterQuery) OrEq¶
func (fq FilterQuery) OrEq(field string, value interface{}) FilterQuery
func (FilterQuery) OrFragment¶
func (fq FilterQuery) OrFragment(expr string, values ...interface{}) FilterQuery
func (FilterQuery) OrGt¶
func (fq FilterQuery) OrGt(field string, value interface{}) FilterQuery
func (FilterQuery) OrGte¶
func (fq FilterQuery) OrGte(field string, value interface{}) FilterQuery
func (FilterQuery) OrIn¶
func (fq FilterQuery) OrIn(field string, values ...interface{}) FilterQuery
func (FilterQuery) OrLike¶
func (fq FilterQuery) OrLike(field string, pattern string) FilterQuery
func (FilterQuery) OrLt¶
func (fq FilterQuery) OrLt(field string, value interface{}) FilterQuery
func (FilterQuery) OrLte¶
func (fq FilterQuery) OrLte(field string, value interface{}) FilterQuery
func (FilterQuery) OrNe¶
func (fq FilterQuery) OrNe(field string, value interface{}) FilterQuery
func (FilterQuery) OrNil¶
func (fq FilterQuery) OrNil(field string) FilterQuery
func (FilterQuery) OrNin¶
func (fq FilterQuery) OrNin(field string, values ...interface{}) FilterQuery
func (FilterQuery) OrNotLike¶
func (fq FilterQuery) OrNotLike(field string, pattern string) FilterQuery
func (FilterQuery) OrNotNil¶
func (fq FilterQuery) OrNotNil(field string) FilterQuery
func (FilterQuery) String¶
func (fq FilterQuery) String() string
type ForeignKeyReference¶
type ForeignKeyReference struct {
Table string
Columns []string
OnDelete string
OnUpdate string
}
type GroupQuery¶
type GroupQuery struct {
Fields []string
Filter FilterQuery
}
func NewGroup¶
func NewGroup(fields ...string) GroupQuery
func (GroupQuery) Build¶
func (gq GroupQuery) Build(query *Query)
func (GroupQuery) Having¶
func (gq GroupQuery) Having(filters ...FilterQuery) GroupQuery
func (GroupQuery) OrHaving¶
func (gq GroupQuery) OrHaving(filters ...FilterQuery) GroupQuery
func (GroupQuery) OrWhere¶
func (gq GroupQuery) OrWhere(filters ...FilterQuery) GroupQuery
func (GroupQuery) Where¶
func (gq GroupQuery) Where(filters ...FilterQuery) GroupQuery
type Index¶
type Index struct {
Op SchemaOp
Table string
Name string
Unique bool
Columns []string
Optional bool
Filter FilterQuery
Options string
}
type IndexOption¶
type IndexOption interface {
// contains filtered or unexported methods
}
type Instrumenter¶
type Instrumenter func(ctx context.Context, op string, message string) func(err error)
func (Instrumenter) Observe¶
func (i Instrumenter) Observe(ctx context.Context, op string, message string) func(err error)
type Iterator¶
type Iterator interface {
io.Closer
Next(record interface{}) error
}
type IteratorOption¶
type IteratorOption interface {
// contains filtered or unexported methods
}
func BatchSize¶
func BatchSize(size int) IteratorOption
func Finish¶
func Finish(id ...interface{}) IteratorOption
func Start¶
func Start(id ...interface{}) IteratorOption
type JoinQuery¶
type JoinQuery struct {
Mode string
Table string
From string
To string
Filter FilterQuery
Arguments []interface{}
}
func NewFullJoin¶
func NewFullJoin(table string, filter ...FilterQuery) JoinQuery
func NewFullJoinOn¶
func NewFullJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery
func NewInnerJoin¶
func NewInnerJoin(table string, filter ...FilterQuery) JoinQuery
func NewInnerJoinOn¶
func NewInnerJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery
func NewJoin¶
func NewJoin(table string, filter ...FilterQuery) JoinQuery
func NewJoinFragment¶
func NewJoinFragment(expr string, args ...interface{}) JoinQuery
func NewJoinOn¶
func NewJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery
func NewJoinWith¶
func NewJoinWith(mode string, table string, from string, to string, filter ...FilterQuery) JoinQuery
func NewLeftJoin¶
func NewLeftJoin(table string, filter ...FilterQuery) JoinQuery
func NewLeftJoinOn¶
func NewLeftJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery
func NewRightJoin¶
func NewRightJoin(table string, filter ...FilterQuery) JoinQuery
func NewRightJoinOn¶
func NewRightJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery
func (JoinQuery) Build¶
func (jq JoinQuery) Build(query *Query)
type Key¶
type Key struct {
Op SchemaOp
Name string
Type KeyType
Columns []string
Rename string
Reference ForeignKeyReference
Options string
}
type KeyOption¶
type KeyOption interface {
// contains filtered or unexported methods
}
type KeyType¶
type KeyType string
const (
// PrimaryKey KeyType.
PrimaryKey KeyType = "PRIMARY KEY"
// ForeignKey KeyType.
ForeignKey KeyType = "FOREIGN KEY"
// UniqueKey KeyType.
UniqueKey = "UNIQUE"
)
type Limit¶
type Limit int
func (Limit) Build¶
func (l Limit) Build(query *Query)
type Lock¶
type Lock string
func ForUpdate¶
func ForUpdate() Lock
func (Lock) Build¶
func (l Lock) Build(query *Query)
type Map¶
type Map map[string]interface{}
func (Map) Apply¶
func (m Map) Apply(doc *Document, mutation *Mutation)
func (Map) String¶
func (m Map) String() string
type Migration¶
type Migration interface {
// contains filtered or unexported methods
}
type Mutate¶
type Mutate struct {
Type ChangeOp
Field string
Value interface{}
}
func Dec¶
func Dec(field string) Mutate
func DecBy¶
func DecBy(field string, n int) Mutate
func Inc¶
func Inc(field string) Mutate
func IncBy¶
func IncBy(field string, n int) Mutate
func Set¶
func Set(field string, value interface{}) Mutate
func SetFragment¶
func SetFragment(raw string, args ...interface{}) Mutate
func (Mutate) Apply¶
func (m Mutate) Apply(doc *Document, mutation *Mutation)
func (Mutate) String¶
func (m Mutate) String() string
type Mutation¶
type Mutation struct {
Mutates map[string]Mutate
Assoc map[string]AssocMutation
OnConflict OnConflict
Unscoped Unscoped
Reload Reload
Cascade Cascade
ErrorFunc ErrorFunc
}
func Apply¶
func Apply(doc *Document, mutators ...Mutator) Mutation
func (*Mutation) Add¶
func (m *Mutation) Add(mut Mutate)
func (*Mutation) IsAssocEmpty¶
func (m *Mutation) IsAssocEmpty() bool
func (*Mutation) IsEmpty¶
func (m *Mutation) IsEmpty() bool
func (*Mutation) IsMutatesEmpty¶
func (m *Mutation) IsMutatesEmpty() bool
func (*Mutation) SetAssoc¶
func (m *Mutation) SetAssoc(field string, muts ...Mutation)
func (*Mutation) SetDeletedIDs¶
func (m *Mutation) SetDeletedIDs(field string, ids []interface{})
type Mutator¶
type Mutator interface {
Apply(doc *Document, mutation *Mutation)
}
type Name¶
type Name string
type NotFoundError¶
type NotFoundError struct{}
func (NotFoundError) Error¶
func (nfe NotFoundError) Error() string
func (NotFoundError) Is¶
func (nfe NotFoundError) Is(target error) bool
type NowFunc¶
type NowFunc func() time.Time
var (
Now NowFunc = func() time.Time {
return time.Now().Truncate(time.Second)
}
)
type Offset¶
type Offset int
func (Offset) Build¶
func (o Offset) Build(query *Query)
type OnConflict¶
type OnConflict struct {
Keys []string
Ignore bool
Replace bool
Fragment string
FragmentArgs []interface{}
}
func OnConflictFragment¶
func OnConflictFragment(sql string, args ...interface{}) OnConflict
This will add custom sql after ON CONFLICT, example: ON CONFLICT [FRAGMENT]
func OnConflictIgnore¶
func OnConflictIgnore() OnConflict
func OnConflictKeyIgnore¶
func OnConflictKeyIgnore(key string) OnConflict
Specifying key is not supported by all database and may be ignored.
func OnConflictKeyReplace¶
func OnConflictKeyReplace(key string) OnConflict
Specifying key is not supported by all database and may be ignored.
func OnConflictKeysIgnore¶
func OnConflictKeysIgnore(keys []string) OnConflict
Specifying key is not supported by all database and may be ignored.
func OnConflictKeysReplace¶
func OnConflictKeysReplace(keys []string) OnConflict
Specifying key is not supported by all database and may be ignored.
func OnConflictReplace¶
func OnConflictReplace() OnConflict
func (OnConflict) Apply¶
func (ocm OnConflict) Apply(doc *Document, mutation *Mutation)
type OnDelete¶
type OnDelete string
type OnUpdate¶
type OnUpdate string
type Optional¶
type Optional bool
type Options¶
type Options string
type Precision¶
type Precision int
type Preload¶
type Preload string
func (Preload) Build¶
func (p Preload) Build(query *Query)
type Primary¶
type Primary bool
type Querier¶
type Querier interface {
Build(*Query)
}
type Query¶
type Query struct {
Table string
SelectQuery SelectQuery
JoinQuery []JoinQuery
WhereQuery FilterQuery
GroupQuery GroupQuery
SortQuery []SortQuery
OffsetQuery Offset
LimitQuery Limit
LockQuery Lock
SQLQuery SQLQuery
UnscopedQuery Unscoped
ReloadQuery Reload
CascadeQuery Cascade
PreloadQuery []string
UsePrimaryDb bool
// contains filtered or unexported fields
}
func Build¶
func Build(table string, queriers ...Querier) Query
func From¶
func From(table string) Query
func Join¶
func Join(table string, filter ...FilterQuery) Query
func JoinOn¶
func JoinOn(table string, from string, to string, filter ...FilterQuery) Query
func JoinWith¶
func JoinWith(mode string, table string, from string, to string, filter ...FilterQuery) Query
func Joinf¶
func Joinf(expr string, args ...interface{}) Query
func Select¶
func Select(fields ...string) Query
func UsePrimary¶
func UsePrimary() Query
func Where¶
func Where(filters ...FilterQuery) Query
func (Query) Build¶
func (q Query) Build(query *Query)
func (Query) Cascade¶
func (q Query) Cascade(c bool) Query
func (Query) Distinct¶
func (q Query) Distinct() Query
func (Query) From¶
func (q Query) From(table string) Query
func (Query) Group¶
func (q Query) Group(fields ...string) Query
func (Query) Having¶
func (q Query) Having(filters ...FilterQuery) Query
func (Query) Havingf¶
func (q Query) Havingf(expr string, args ...interface{}) Query
func (Query) Join¶
func (q Query) Join(table string, filter ...FilterQuery) Query
func (Query) JoinOn¶
func (q Query) JoinOn(table string, from string, to string, filter ...FilterQuery) Query
func (Query) JoinWith¶
func (q Query) JoinWith(mode string, table string, from string, to string, filter ...FilterQuery) Query
func (Query) Joinf¶
func (q Query) Joinf(expr string, args ...interface{}) Query
func (Query) Limit¶
func (q Query) Limit(limit int) Query
func (Query) Lock¶
func (q Query) Lock(lock string) Query
func (Query) Offset¶
func (q Query) Offset(offset int) Query
func (Query) OrHaving¶
func (q Query) OrHaving(filters ...FilterQuery) Query
func (Query) OrHavingf¶
func (q Query) OrHavingf(expr string, args ...interface{}) Query
func (Query) OrWhere¶
func (q Query) OrWhere(filters ...FilterQuery) Query
func (Query) OrWheref¶
func (q Query) OrWheref(expr string, args ...interface{}) Query
func (Query) Preload¶
func (q Query) Preload(field string) Query
func (Query) Reload¶
func (q Query) Reload() Query
func (Query) Select¶
func (q Query) Select(fields ...string) Query
func (Query) Sort¶
func (q Query) Sort(fields ...string) Query
func (Query) SortAsc¶
func (q Query) SortAsc(fields ...string) Query
func (Query) SortDesc¶
func (q Query) SortDesc(fields ...string) Query
func (Query) String¶
func (q Query) String() string
func (Query) Unscoped¶
func (q Query) Unscoped() Query
func (Query) UsePrimary¶
func (q Query) UsePrimary() Query
func (Query) Where¶
func (q Query) Where(filters ...FilterQuery) Query
func (Query) Wheref¶
func (q Query) Wheref(expr string, args ...interface{}) Query
type Raw¶
type Raw string
type Reload¶
type Reload bool
func (Reload) Apply¶
func (r Reload) Apply(doc *Document, mutation *Mutation)
func (Reload) Build¶
func (r Reload) Build(query *Query)
type Repository¶
type Repository interface {
// Adapter used in this repository.
Adapter(ctx context.Context) Adapter
// Instrumentation defines callback to be used as instrumenter.
Instrumentation(instrumenter Instrumenter)
// Ping database.
Ping(ctx context.Context) error
// Iterate through a collection of records from database in batches.
// This function returns iterator that can be used to loop all records.
// Limit, Offset and Sort query is automatically ignored.
Iterate(ctx context.Context, query Query, option ...IteratorOption) Iterator
// Aggregate over the given field.
// Supported aggregate: count, sum, avg, max, min.
// Any select, group, offset, limit and sort query will be ignored automatically.
// If complex aggregation is needed, consider using All instead.
Aggregate(ctx context.Context, query Query, aggregate string, field string) (int, error)
// MustAggregate over the given field.
// Supported aggregate: count, sum, avg, max, min.
// Any select, group, offset, limit and sort query will be ignored automatically.
// If complex aggregation is needed, consider using All instead.
// It'll panic if any error eccured.
MustAggregate(ctx context.Context, query Query, aggregate string, field string) int
// Count records that match the query.
Count(ctx context.Context, collection string, queriers ...Querier) (int, error)
// MustCount records that match the query.
// It'll panic if any error eccured.
MustCount(ctx context.Context, collection string, queriers ...Querier) int
// Find a record that match the query.
// If no result found, it'll return not found error.
Find(ctx context.Context, record interface{}, queriers ...Querier) error
// MustFind a record that match the query.
// If no result found, it'll panic.
MustFind(ctx context.Context, record interface{}, queriers ...Querier)
// FindAll records that match the query.
FindAll(ctx context.Context, records interface{}, queriers ...Querier) error
// MustFindAll records that match the query.
// It'll panic if any error eccured.
MustFindAll(ctx context.Context, records interface{}, queriers ...Querier)
// FindAndCountAll records that match the query.
// This is a convenient method that combines FindAll and Count. It's useful when dealing with queries related to pagination.
// Limit and Offset property will be ignored when performing count query.
FindAndCountAll(ctx context.Context, records interface{}, queriers ...Querier) (int, error)
// MustFindAndCountAll records that match the query.
// This is a convenient method that combines FindAll and Count. It's useful when dealing with queries related to pagination.
// Limit and Offset property will be ignored when performing count query.
// It'll panic if any error eccured.
MustFindAndCountAll(ctx context.Context, records interface{}, queriers ...Querier) int
// Insert a record to database.
Insert(ctx context.Context, record interface{}, mutators ...Mutator) error
// MustInsert an record to database.
// It'll panic if any error occurred.
MustInsert(ctx context.Context, record interface{}, mutators ...Mutator)
// InsertAll records.
// Does not supports application cascade insert.
InsertAll(ctx context.Context, records interface{}, mutators ...Mutator) error
// MustInsertAll records.
// It'll panic if any error occurred.
// Does not supports application cascade insert.
MustInsertAll(ctx context.Context, records interface{}, mutators ...Mutator)
// Update a record in database.
// It'll panic if any error occurred.
Update(ctx context.Context, record interface{}, mutators ...Mutator) error
// MustUpdate a record in database.
// It'll panic if any error occurred.
MustUpdate(ctx context.Context, record interface{}, mutators ...Mutator)
// UpdateAny records tha match the query.
// Returns number of updated records and error.
UpdateAny(ctx context.Context, query Query, mutates ...Mutate) (int, error)
// MustUpdateAny records that match the query.
// It'll panic if any error occurred.
// Returns number of updated records.
MustUpdateAny(ctx context.Context, query Query, mutates ...Mutate) int
// Delete a record.
Delete(ctx context.Context, record interface{}, mutators ...Mutator) error
// MustDelete a record.
// It'll panic if any error eccured.
MustDelete(ctx context.Context, record interface{}, mutators ...Mutator)
// DeleteAll records.
// Does not supports application cascade delete.
DeleteAll(ctx context.Context, records interface{}) error
// MustDeleteAll records.
// It'll panic if any error occurred.
// Does not supports application cascade delete.
MustDeleteAll(ctx context.Context, records interface{})
// DeleteAny records that match the query.
// Returns number of deleted records and error.
DeleteAny(ctx context.Context, query Query) (int, error)
// MustDeleteAny records that match the query.
// It'll panic if any error eccured.
// Returns number of updated records.
MustDeleteAny(ctx context.Context, query Query) int
// Preload association with given query.
// This function can accepts either a struct or a slice of structs.
// If association is already loaded, this will do nothing.
// To force preloading even though association is already loaeded, add `Reload(true)` as query.
Preload(ctx context.Context, records interface{}, field string, queriers ...Querier) error
// MustPreload association with given query.
// This function can accepts either a struct or a slice of structs.
// It'll panic if any error occurred.
MustPreload(ctx context.Context, records interface{}, field string, queriers ...Querier)
// Exec raw statement.
// Returns last inserted id, rows affected and error.
Exec(ctx context.Context, statement string, args ...interface{}) (int, int, error)
// MustExec raw statement.
// Returns last inserted id, rows affected and error.
MustExec(ctx context.Context, statement string, args ...interface{}) (int, int)
// Transaction performs transaction with given function argument.
// Transaction scope/connection is automatically passed using context.
Transaction(ctx context.Context, fn func(ctx context.Context) error) error
}
func New¶
func New(adapter Adapter) Repository
type Required¶
type Required bool
type SQLQuery¶
type SQLQuery struct {
Statement string
Values []interface{}
}
func SQL¶
func SQL(statement string, values ...interface{}) SQLQuery
func (SQLQuery) Build¶
func (sq SQLQuery) Build(query *Query)
func (SQLQuery) String¶
func (sq SQLQuery) String() string
type Scale¶
type Scale int
type Schema¶
type Schema struct {
Migrations []Migration
}
func (*Schema) AddColumn¶
func (s *Schema) AddColumn(table string, name string, typ ColumnType, options ...ColumnOption)
func (*Schema) AlterTable¶
func (s *Schema) AlterTable(name string, fn func(t *AlterTable), options ...TableOption)
func (*Schema) CreateIndex¶
func (s *Schema) CreateIndex(table string, name string, column []string, options ...IndexOption)
func (*Schema) CreateTable¶
func (s *Schema) CreateTable(name string, fn func(t *Table), options ...TableOption)
func (*Schema) CreateTableIfNotExists¶
func (s *Schema) CreateTableIfNotExists(name string, fn func(t *Table), options ...TableOption)
func (*Schema) CreateUniqueIndex¶
func (s *Schema) CreateUniqueIndex(table string, name string, column []string, options ...IndexOption)
func (*Schema) Do¶
func (s *Schema) Do(fn Do)
func (*Schema) DropColumn¶
func (s *Schema) DropColumn(table string, name string, options ...ColumnOption)
func (*Schema) DropIndex¶
func (s *Schema) DropIndex(table string, name string, options ...IndexOption)
func (*Schema) DropTable¶
func (s *Schema) DropTable(name string, options ...TableOption)
func (*Schema) DropTableIfExists¶
func (s *Schema) DropTableIfExists(name string, options ...TableOption)
func (*Schema) Exec¶
func (s *Schema) Exec(raw Raw)
func (*Schema) RenameColumn¶
func (s *Schema) RenameColumn(table string, name string, newName string, options ...ColumnOption)
func (*Schema) RenameTable¶
func (s *Schema) RenameTable(name string, newName string, options ...TableOption)
func (Schema) String¶
func (s Schema) String() string
type SchemaOp¶
type SchemaOp uint8
const (
// SchemaCreate operation.
SchemaCreate SchemaOp = iota
// SchemaAlter operation.
SchemaAlter
// SchemaRename operation.
SchemaRename
// SchemaDrop operation.
SchemaDrop
)
func (SchemaOp) String¶
func (s SchemaOp) String() string
type SelectQuery¶
type SelectQuery struct {
OnlyDistinct bool
Fields []string
}
func NewSelect¶
func NewSelect(fields ...string) SelectQuery
Deprecated: use Select instead
func (SelectQuery) Distinct¶
func (sq SelectQuery) Distinct() SelectQuery
type SortQuery¶
type SortQuery struct {
Field string
Sort int
}
func SortAsc¶
func SortAsc(field string) SortQuery
func SortDesc¶
func SortDesc(field string) SortQuery
func (SortQuery) Asc¶
func (sq SortQuery) Asc() bool
func (SortQuery) Build¶
func (sq SortQuery) Build(query *Query)
func (SortQuery) Desc¶
func (sq SortQuery) Desc() bool
type Structset¶
type Structset struct {
// contains filtered or unexported fields
}
func NewStructset¶
func NewStructset(record interface{}, skipZero bool) Structset
func (Structset) Apply¶
func (s Structset) Apply(doc *Document, mut *Mutation)
type SubQuery¶
type SubQuery struct {
Prefix string
Query Query
}
func All¶
func All(sub Query) SubQuery
Some database may not support this keyword, please consult to your database documentation.
func Any¶
func Any(sub Query) SubQuery
Some database may not support this keyword, please consult to your database documentation.
type Table¶
type Table struct {
Op SchemaOp
Name string
Rename string
Definitions []TableDefinition
Optional bool
Options string
}
func (*Table) BigID¶
func (t *Table) BigID(name string, options ...ColumnOption)
func (*Table) BigInt¶
func (t *Table) BigInt(name string, options ...ColumnOption)
func (*Table) Bool¶
func (t *Table) Bool(name string, options ...ColumnOption)
func (*Table) Column¶
func (t *Table) Column(name string, typ ColumnType, options ...ColumnOption)
func (*Table) Date¶
func (t *Table) Date(name string, options ...ColumnOption)
func (*Table) DateTime¶
func (t *Table) DateTime(name string, options ...ColumnOption)
func (*Table) Decimal¶
func (t *Table) Decimal(name string, options ...ColumnOption)
func (*Table) Float¶
func (t *Table) Float(name string, options ...ColumnOption)
func (*Table) ForeignKey¶
func (t *Table) ForeignKey(column string, refTable string, refColumn string, options ...KeyOption)
func (*Table) Fragment¶
func (t *Table) Fragment(fragment string)
func (*Table) ID¶
func (t *Table) ID(name string, options ...ColumnOption)
func (*Table) Int¶
func (t *Table) Int(name string, options ...ColumnOption)
func (*Table) JSON¶
func (t *Table) JSON(name string, options ...ColumnOption)
func (*Table) PrimaryKey¶
func (t *Table) PrimaryKey(column string, options ...KeyOption)
func (*Table) PrimaryKeys¶
func (t *Table) PrimaryKeys(columns []string, options ...KeyOption)
func (*Table) SmallInt¶
func (t *Table) SmallInt(name string, options ...ColumnOption)
func (*Table) String¶
func (t *Table) String(name string, options ...ColumnOption)
func (*Table) Text¶
func (t *Table) Text(name string, options ...ColumnOption)
func (*Table) Time¶
func (t *Table) Time(name string, options ...ColumnOption)
func (*Table) Unique¶
func (t *Table) Unique(columns []string, options ...KeyOption)
type TableDefinition¶
type TableDefinition interface {
// contains filtered or unexported methods
}
type TableOption¶
type TableOption interface {
// contains filtered or unexported methods
}
type Unique¶
type Unique bool
type Unscoped¶
type Unscoped bool
func (Unscoped) Apply¶
func (u Unscoped) Apply(doc *Document, mutation *Mutation)
func (Unscoped) Build¶
func (u Unscoped) Build(query *Query)
type Unsigned¶
type Unsigned bool
Last update: 2022-06-25