Documentation
¶
Overview ¶
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{ // the account itself
Columns: []string{"id", "email", "created_at", "nickname"},
Rows: [][]driver.Value{
testdb.Row(int64(1), "ada@example.com", time.Time{}, ""),
},
})
fake.Enqueue(testdb.Result{ // Posts, batch-loaded by Preload (R9.5)
Columns: []string{"id", "user_id", "title"},
Rows: [][]driver.Value{
testdb.Row(int64(10), int64(1), "Hello, Argo"),
},
})
accounts, err := Of[testAccount](db).
Where(accountCol.Email.Eq("ada@example.com")).
Preload(accountRel.Posts).
Find(context.Background())
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(accounts[0].Email, len(accounts[0].Posts), accounts[0].Posts[0].Title)
fake.Enqueue(testdb.Result{ // Create's RETURNING id + the default: created_at column
Columns: []string{"id", "created_at"},
Rows: [][]driver.Value{testdb.Row(int64(2), time.Time{})},
})
row := &testAccount{Email: "grace@example.com"}
if err := Create(context.Background(), db, row); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(row.ID)
Output: ada@example.com 1 Hello, Argo 2
Index ¶
- Variables
- func ByID[T Entity[ID], ID comparable](ctx context.Context, db *DB, id ID) (T, error)
- func Create[T any](ctx context.Context, db *DB, row *T, opts ...WriteOption) error
- func CreateAll[T any](ctx context.Context, db *DB, rows []*T, opts ...WriteOption) error
- func Delete[T Entity[ID], ID comparable](ctx context.Context, db *DB, id ID) error
- func Raw[T any](ctx context.Context, db *DB, sqlText string, args ...any) ([]T, error)
- func Save[T Entity[ID], ID comparable](ctx context.Context, db *DB, row *T) error
- func Update[T Entity[ID], ID comparable](ctx context.Context, db *DB, row *T) error
- type AfterDeleter
- type AfterInserter
- type AfterScanner
- type AfterUpdater
- type AnyCol
- type Assignment
- type BeforeDeleter
- type BeforeInserter
- type BeforeUpdater
- type Col
- func (c Col[T, F]) Asc() OrderTerm[T]
- func (c Col[T, F]) Between(lo, hi F) Predicate
- func (c Col[T, F]) Desc() OrderTerm[T]
- func (c Col[T, F]) Eq(v F) Predicate
- func (c Col[T, F]) Gt(v F) Predicate
- func (c Col[T, F]) Gte(v F) Predicate
- func (c Col[T, F]) In(vs ...F) Predicate
- func (c Col[T, F]) IsNotNull() Predicate
- func (c Col[T, F]) IsNull() Predicate
- func (c Col[T, F]) Lt(v F) Predicate
- func (c Col[T, F]) Lte(v F) Predicate
- func (c Col[T, F]) Ne(v F) Predicate
- func (c Col[T, F]) NotIn(vs ...F) Predicate
- func (c Col[T, F]) Set(v F) Assignment
- func (c Col[T, F]) SetExpr(sqlExpr string, args ...any) Assignment
- type ConflictBuilder
- type DB
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*DB, error)
- func (db *DB) Commit() error
- func (db *DB) Dialect() dialect.Dialect
- func (db *DB) ExecContext(ctx context.Context, op, model, sqlText string, args []any) (sql.Result, error)
- func (db *DB) InTransaction() bool
- func (db *DB) PinnedConn(ctx context.Context, fn func(pinned *DB) error) error
- func (db *DB) QueryContext(ctx context.Context, op, model, sqlText string, args []any) (*sql.Rows, error)
- func (db *DB) Rollback() error
- func (db *DB) Tx(ctx context.Context, fn TransactionFunc) error
- type Entity
- type ErrCheckViolation
- type ErrForeignKeyViolation
- type ErrNotNullViolation
- type ErrSetOperationBlockedByHooks
- type ErrUintOverflow
- type ErrUniqueViolation
- type ErrZeroCompositeKey
- type Model
- type Option
- type OrderTerm
- type Predicate
- func And(preds ...Predicate) Predicate
- func ILike[T any](c Col[T, string], pattern string) Predicate
- func Like[T any](c Col[T, string], pattern string) Predicate
- func Not(p Predicate) Predicate
- func NotLike[T any](c Col[T, string], pattern string) Predicate
- func On[L, R, F any](left Col[L, F], right Col[R, F]) Predicate
- func Or(preds ...Predicate) Predicate
- type PreloadOption
- type Query
- func (q *Query[T]) Count(ctx context.Context) (int64, error)
- func (q *Query[T]) Delete(ctx context.Context) (int64, error)
- func (q *Query[T]) DeleteAll(ctx context.Context) (int64, error)
- func (q *Query[T]) Distinct() *Query[T]
- func (q *Query[T]) Exists(ctx context.Context) (bool, error)
- func (q *Query[T]) Find(ctx context.Context) ([]T, error)
- func (q *Query[T]) First(ctx context.Context) (T, error)
- func (q *Query[T]) ForShare() *Query[T]
- func (q *Query[T]) ForUpdate() *Query[T]
- func (q *Query[T]) GroupBy(cols ...AnyCol[T]) *Query[T]
- func (q *Query[T]) Having(p Predicate) *Query[T]
- func (q *Query[T]) Join[U any](on Predicate) *Query[T]
- func (q *Query[T]) LeftJoin[U any](on Predicate) *Query[T]
- func (q *Query[T]) Limit(n int64) *Query[T]
- func (q *Query[T]) Offset(n int64) *Query[T]
- func (q *Query[T]) Or(p Predicate) *Query[T]
- func (q *Query[T]) Order(terms ...OrderTerm[T]) *Query[T]
- func (q *Query[T]) OrderRaw(fragment string) *Query[T]
- func (q *Query[T]) Preload[U any](rel Rel[T, U], opts ...PreloadOption) *Query[T]
- func (q *Query[T]) Project[U any]() *Query[U]
- func (q *Query[T]) Rows(ctx context.Context) iter.Seq2[T, error]
- func (q *Query[T]) Scopes(scopes ...Scope[T]) *Query[T]
- func (q *Query[T]) Select(cols ...AnyCol[T]) *Query[T]
- func (q *Query[T]) SelectRaw(fragment string) *Query[T]
- func (q *Query[T]) SkipHooks() *Query[T]
- func (q *Query[T]) SkipLocked() *Query[T]
- func (q *Query[T]) Update(ctx context.Context, assignments ...Assignment) (int64, error)
- func (q *Query[T]) Where(p Predicate) *Query[T]
- func (q *Query[T]) WhereRaw(fragment string, args ...any) *Query[T]
- type QueryEvent
- type QueryHook
- type Rel
- type Scope
- type TransactionFunc
- type WriteOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrNoRows = fmt.Errorf("pack: no rows in result set: %w", sql.ErrNoRows)
View Source
var ErrPinnedConnUnsupported = fmt.Errorf("pack: PinnedConn: underlying connection source does not support connection pinning")
Functions ¶
func CreateAll ¶
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{
Columns: []string{"id"},
Rows: [][]driver.Value{testdb.Row(int64(1)), testdb.Row(int64(2))},
})
rows := []*testWidget{{Count: 10}, {Count: 20}}
if err := CreateAll(context.Background(), db, rows); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(rows[0].ID, rows[1].ID)
Output: 1 2
Types ¶
type AfterDeleter ¶
type AfterInserter ¶
type AfterScanner ¶
type AfterUpdater ¶
type Assignment ¶
type Assignment struct {
// contains filtered or unexported fields
}
type BeforeDeleter ¶
type BeforeInserter ¶
type BeforeUpdater ¶
type Col ¶
type Col[T, F any] struct { // contains filtered or unexported fields }
func (Col[T, F]) Set ¶
func (c Col[T, F]) Set(v F) Assignment
type ConflictBuilder ¶
type ConflictBuilder[T any] struct { // contains filtered or unexported fields }
func OnConflict ¶
func OnConflict[T any](cols ...AnyCol[T]) *ConflictBuilder[T]
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{RowsAffected: 1})
row := &testWidget{Count: 5}
err := Create(context.Background(), db, row, OnConflict[testWidget](widgetCol.ID).DoNothing())
fmt.Println(err)
Output: <nil>
func (*ConflictBuilder[T]) DoNothing ¶
func (c *ConflictBuilder[T]) DoNothing() WriteOption
func (*ConflictBuilder[T]) DoUpdate ¶
func (c *ConflictBuilder[T]) DoUpdate(assignments ...Assignment) WriteOption
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) ExecContext ¶
func (*DB) InTransaction ¶
func (*DB) PinnedConn ¶
func (*DB) QueryContext ¶
func (*DB) Tx ¶
func (db *DB) Tx(ctx context.Context, fn TransactionFunc) error
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{
Columns: []string{"id"},
Rows: [][]driver.Value{testdb.Row(int64(1))},
})
err := db.Tx(context.Background(), func(tx *DB) error {
return Create(context.Background(), tx, &testWidget{Count: 1})
})
fmt.Println(err)
Output: <nil>
type Entity ¶
type Entity[ID comparable] interface { PK() ID }
type ErrCheckViolation ¶
type ErrCheckViolation struct {
// contains filtered or unexported fields
}
func (*ErrCheckViolation) Error ¶
func (e *ErrCheckViolation) Error() string
type ErrForeignKeyViolation ¶
type ErrForeignKeyViolation struct {
// contains filtered or unexported fields
}
func (*ErrForeignKeyViolation) Error ¶
func (e *ErrForeignKeyViolation) Error() string
type ErrNotNullViolation ¶
type ErrNotNullViolation struct {
// contains filtered or unexported fields
}
func (*ErrNotNullViolation) Error ¶
func (e *ErrNotNullViolation) Error() string
type ErrSetOperationBlockedByHooks ¶
func (*ErrSetOperationBlockedByHooks) Error ¶
func (e *ErrSetOperationBlockedByHooks) Error() string
type ErrUintOverflow ¶
func (*ErrUintOverflow) Error ¶
func (e *ErrUintOverflow) Error() string
type ErrUniqueViolation ¶
type ErrUniqueViolation struct {
// contains filtered or unexported fields
}
func (*ErrUniqueViolation) Error ¶
func (e *ErrUniqueViolation) Error() string
type ErrZeroCompositeKey ¶
type ErrZeroCompositeKey struct {
Model string
}
func (*ErrZeroCompositeKey) Error ¶
func (e *ErrZeroCompositeKey) Error() string
type Model ¶
type Model[ID comparable] struct { ID ID `db:"id,pk,auto_increment"` }
type Option ¶
type Option func(o *dbOptions)
func WithErrorDecoder ¶
func WithErrorDecoder(d driver.ErrorDecoder) Option
func WithQueryHook ¶
func WithQueryHookArgs ¶
func WithQueryHookArgs() Option
type PreloadOption ¶
type PreloadOption interface {
// contains filtered or unexported methods
}
func Preload ¶
func Preload[T, U any](rel Rel[T, U], opts ...PreloadOption) PreloadOption
func Where ¶
func Where(p Predicate) PreloadOption
type Query ¶
type Query[T any] struct { // contains filtered or unexported fields }
func (*Query[T]) Preload ¶
func (q *Query[T]) Preload[U any](rel Rel[T, U], opts ...PreloadOption) *Query[T]
func (*Query[T]) Rows ¶
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{
Columns: []string{"id", "name"},
Rows: [][]driver.Value{
testdb.Row(int64(1), "a"),
testdb.Row(int64(2), "b"),
},
})
for row, err := range Of[testHookedItem](db).Rows(context.Background()) {
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(row.ID, row.Name)
}
Output: 1 a 2 b
func (*Query[T]) Scopes ¶
Example ¶
fake := testdb.New()
db := Open(fake.Open(), pgdialect.New())
fake.Enqueue(testdb.Result{
Columns: []string{"id", "email", "created_at", "nickname"},
Rows: [][]driver.Value{
testdb.Row(int64(1), "ada@example.com", time.Time{}, ""),
},
})
accounts, err := Of[testAccount](db).
Scopes(adultAccounts, withAccountEmail("ada@example.com")).
Find(context.Background())
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(accounts[0].Email)
Output: ada@example.com
func (*Query[T]) SkipLocked ¶
type QueryEvent ¶
type QueryHook ¶
type QueryHook interface {
BeforeQuery(ctx context.Context, ev *QueryEvent) context.Context
AfterQuery(ctx context.Context, ev *QueryEvent)
}
type Rel ¶
type Rel[T, U any] struct { // contains filtered or unexported fields }
func RelationOne ¶
type TransactionFunc ¶
type WriteOption ¶
type WriteOption interface {
// contains filtered or unexported methods
}
func WithBatchSize ¶
func WithBatchSize(n int) WriteOption
Source Files
¶
Click to show internal directories.
Click to hide internal directories.