pgimpl

package
v0.0.0-...-604ed08 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Creator

func Creator[C CreateEntity](db *sql.DB, table string) gimpl.Creator[C]

Creator returns a gimpl.Creator for the specified entity type and table name C must implement the CreateEntity interface

func Finder

func Finder[E Entity](db *sql.DB, table string) gimpl.Finder[E]

Finder returns a gimpl.Finder for the specified entity type and table name E must implement the Entity interface

func FromExpr

func FromExpr(e gimpl.Expr) (squirrel.Sqlizer, error)

FromExpr converts an gimpl.Expr logical/quantified expression tree into a squirrel.Sqlizer fragment and a slice of args that is compatible with postgres.

Conventions implemented:

  • A CondExpr whose Field has no dots (e.g. "status") and is NOT nested beneath a quantifier is treated as a direct column comparison: status = ?
  • A CondExpr whose Field contains dots OR that appears beneath a quantifier context is treated as a JSONB path comparison. The first segment before the first dot (or the quantifier's array element alias) is the JSONB root. Remaining segments become a jsonb path using the '#>' operator.
  • Quantifiers (ANY / ALL) over a jsonb array path are translated into EXISTS / NOT EXISTS with jsonb_array_elements(). Nested quantifiers are supported (aliases are generated q0, q1, ...).
  • IN / NIN for JSON values rely on the jsonb containment operator '<@' Column IN / NIN use '= ANY (?)' / '!= ALL (?)' patterns.

func FromSorts

func FromSorts(s gimpl.Sorts, query squirrel.SelectBuilder) (_ squirrel.SelectBuilder, reverse *squirrel.SelectBuilder, err error)

FromSorts returns the given query with sorting and cursors applied. It also returns a reverse query for fetching the previous first result required for the prev cursor.

func Remover

func Remover[E Entity](db *sql.DB, table string) gimpl.Remover[E]

Remover returns a gimpl.Remover for the specified entity type and table name E must implement the Entity interface

func Saver

func Saver[S SaveEntity](db *sql.DB, table string) gimpl.Saver[S]

Saver returns a gimpl.Saver for the specified entity type and table name S must implement the SaveEntity interface

func TxOpts

func TxOpts(ctx context.Context, db *sql.DB, opts any) (txOpts, error)

func Updater

func Updater[U UpdateEntity](db *sql.DB, table string) gimpl.Updater[U]

Updater returns a gimpl.Updater for the specified entity type and table name U must implement the UpdateEntity interface

Types

type CreateEntity

type CreateEntity interface {
	gimpl.CreateEntity
	// CreatePgColumns returns a slice of column names used for creation operations.
	// Returned values should be static and not vary between calls
	CreatePgColumns() []string
	// CreatePgColumnVals returns a slice of the fields used for writing to the entity during creation operations
	// Returned values should not be dynamic and must correspond to the columns returned by CreatePgColumns
	CreatePgColumnVals() []any
}

CreateEntity is an extension of gimpl.CreateEntity for Postgres creation operations. DO NOT USE POINTER RECEIVERS

type CreateTx

type CreateTx func(context.Context) (Tx, error)

func NewTxCreator

func NewTxCreator(db *sql.DB) CreateTx

type Entity

type Entity interface {
	gimpl.Entity
	// PgColumn returns the column name used for the given field
	PgColumn(field string) string
	// PgColumns returns a slice of column names used for data retrieval.
	// Returned values should be static and not vary between calls
	PgColumns() []string
	// NewWithPgColumnPtrs returns a new entity and a slice of pointers to the fields used for writing to the entity during data retrieval
	// Returned values should not be dynamic and must correspond to the columns returned by PgColumns
	// The reason for creating a new entity is that without pointer receivers, the method cannot modify the original entity
	NewWithPgColumnPtrs() (any, []any)
}

Entity is an extension of gimpl.Entity for Postgres operations. DO NOT USE POINTER RECEIVERS

type SaveEntity

type SaveEntity interface {
	CreateEntity
	UpdateEntity
}

SaveEntity is an extension of gimpl.SaveEntity for Postgres create or update operations. DO NOT USE POINTER RECEIVERS

type Tx

type Tx struct {
	*sql.Tx
}

Tx wraps a sql.Tx to provide helper methods for committing or rolling back.

func NewTx

func NewTx(ctx context.Context, db *sql.DB) (Tx, error)

New begins a new transaction on the given pool. The returned *Tx should be finalized with Finalize to commit or roll back.

func (Tx) Finalize

func (t Tx) Finalize(err error) error

Finalize will commit the transaction if err is nil; otherwise it rolls back. It returns any error encountered during commit or rollback.

type TxOpt

type TxOpt func(*txOpts)

func WithTx

func WithTx(tx Tx) TxOpt

WithTx instructs the repository to use the provided transaction instead of creating a new one. Any error in creating or setting audit parameters must be handled before passing the Tx here.

func WithTxAutoFinalize

func WithTxAutoFinalize() TxOpt

WithTxAutoFinalize instructs the repository to finalize the transaction after the operation completes.

type UpdateEntity

type UpdateEntity interface {
	gimpl.UpdateEntity
	// IdentifyPgColumns returns a slice of column names used to identify unique records for update operations.
	// Returned values should be static and not vary between calls
	IdentifyPgColumns() []string
	// IdentifyPgColumnVals returns a slice of the fields used to identify unique records for update operations
	// Returned values should not be dynamic and must correspond to the columns returned by IdentifyPgColumns
	IdentifyPgColumnVals() []any
	// UpdatePgColumns returns a slice of column names used for update operations.
	// Returned values should be static and not vary between calls
	UpdatePgColumns() []string
	// UpdatePgColumnVals returns a slice of the fields used for writing to the entity during update operations
	// Returned values should not be dynamic and must correspond to the columns returned by UpdatePgColumns
	UpdatePgColumnVals() []any
}

UpdateEntity is an extension of gimpl.UpdateEntity for Postgres update operations. DO NOT USE POINTER RECEIVERS

Jump to

Keyboard shortcuts

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