Documentation
¶
Overview ¶
Package r3gopg provides an r3.CRUD[T, ID] driver backed by go-pg v10, a PostgreSQL ORM with a focus on PostgreSQL-specific features.
Driver: github.com/go-pg/pg/v10 Source: https://github.com/go-pg/pg
Supported r3 features:
- Full CRUD (Create, Get, List, Update, Delete)
- Filters, Sorts, Pagination via the r3 SQL dialect
- Preloads via go-pg's Relation() (has-one, has-many, many-to-many)
- IncludeTrashed via go-pg's AllWithDeleted() (requires pg:",soft_delete" tag)
- Restore and HardDelete via the features/softdelete SoftDeleter interface
- Thread-safe default queries (SetDefaultListQuery, SetDefaultGetQuery)
- Raw escape hatch (GoPgRaw) for custom go-pg orm.Query usage
Limitations / notes:
- go-pg is in maintenance mode; the authors recommend migrating to Bun. See: https://github.com/go-pg/pg#bun
- go-pg does NOT wrap database/sql; it uses its own connection pool (*pg.DB). If you need a *sql.DB (e.g. for goose migrations), open a separate connection.
- Model structs must use `pg` struct tags (e.g. `pg:"column_name"`).
- Aggregate / custom-shape queries should use Raw().Scan() into a dedicated struct, since Raw().Find() scans into []T and go-pg rejects unknown columns.
Index ¶
- func NewGoPgQuerier[T any, ID comparable](db *pg.DB, opts ...r3.Option) r3.Querier[T, ID]
- type GoPgCRUD
- func (r *GoPgCRUD[T, ID]) BeginTx(ctx context.Context) (r3.TxCRUD[T, ID], error)
- func (r *GoPgCRUD[T, ID]) Count(ctx context.Context, qarg ...r3.Query) (int64, error)
- func (r *GoPgCRUD[T, ID]) Create(ctx context.Context, entity T) (T, error)
- func (r *GoPgCRUD[T, ID]) Delete(ctx context.Context, id ID) error
- func (r *GoPgCRUD[T, ID]) Get(ctx context.Context, id ID, qarg ...r3.Query) (T, error)
- func (r *GoPgCRUD[T, ID]) HardDelete(ctx context.Context, id ID) error
- func (r *GoPgCRUD[T, ID]) List(ctx context.Context, qarg ...r3.Query) ([]T, int64, error)
- func (r *GoPgCRUD[T, ID]) Patch(ctx context.Context, entity T, fields r3.Fields) (T, error)
- func (r *GoPgCRUD[T, ID]) PgDB() *pg.DB
- func (r *GoPgCRUD[T, ID]) Raw() *GoPgRaw[T, ID]
- func (r *GoPgCRUD[T, ID]) Restore(ctx context.Context, id ID) error
- func (r *GoPgCRUD[T, ID]) Update(ctx context.Context, entity T) (T, error)
- type GoPgRaw
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewGoPgQuerier ¶
NewGoPgQuerier creates a read-only go-pg-based repository. Returns r3.Querier — a compile-time guarantee of read-only access.
Types ¶
type GoPgCRUD ¶
type GoPgCRUD[T any, ID comparable] struct { enginesql.DefaultsManager Config r3.Config // contains filtered or unexported fields }
GoPgCRUD is a CRUD repository based on go-pg's *pg.DB. Internally it uses orm.DB so that it can operate within transactions as well.
func NewGoPgCRUD ¶
NewGoPgCRUD creates a new go-pg-based CRUD repository.
Accepts optional r3.Option values for framework-level configuration.
func (*GoPgCRUD[T, ID]) BeginTx ¶
BeginTx starts a go-pg transaction and returns an r3.TxCRUD scoped to it. The caller must call Commit or Rollback on the returned TxCRUD.
func (*GoPgCRUD[T, ID]) HardDelete ¶
HardDelete permanently removes a record, bypassing go-pg's soft-delete.
func (*GoPgCRUD[T, ID]) PgDB ¶
PgDB returns the underlying *pg.DB for advanced usage. Panics if GoPgCRUD is operating inside a transaction.
type GoPgRaw ¶
GoPgRaw is a go-pg wrapper that allows calling any go-pg query. Is considered to be embedded in GoPgCRUD.
func NewGoPgRaw ¶
func NewGoPgRaw[T any, ID comparable](db orm.DB) *GoPgRaw[T, ID]
NewGoPgRaw creates a new GoPgRaw instance.