r3gopg

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGoPgQuerier

func NewGoPgQuerier[T any, ID comparable](db *pg.DB, opts ...r3.Option) r3.Querier[T, ID]

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

func NewGoPgCRUD[T any, ID comparable](db *pg.DB, opts ...r3.Option) *GoPgCRUD[T, ID]

NewGoPgCRUD creates a new go-pg-based CRUD repository.

Accepts optional r3.Option values for framework-level configuration.

func (*GoPgCRUD[T, ID]) Aggregate added in v0.0.3

func (r *GoPgCRUD[T, ID]) Aggregate(ctx context.Context, qarg ...r3.Query) ([]r3.AggregateRow, error)

Aggregate computes grouped aggregates over the records matching the query. See r3.Aggregator for the query semantics.

func (*GoPgCRUD[T, ID]) BeginTx

func (r *GoPgCRUD[T, ID]) BeginTx(ctx context.Context) (r3.TxCRUD[T, ID], error)

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]) Count

func (r *GoPgCRUD[T, ID]) Count(ctx context.Context, qarg ...r3.Query) (int64, error)

Count returns the number of records matching the query's filters.

func (*GoPgCRUD[T, ID]) Create

func (r *GoPgCRUD[T, ID]) Create(ctx context.Context, entity T) (T, error)

func (*GoPgCRUD[T, ID]) Delete

func (r *GoPgCRUD[T, ID]) Delete(ctx context.Context, id ID) error

func (*GoPgCRUD[T, ID]) Get

func (r *GoPgCRUD[T, ID]) Get(ctx context.Context, id ID, qarg ...r3.Query) (T, error)

func (*GoPgCRUD[T, ID]) HardDelete

func (r *GoPgCRUD[T, ID]) HardDelete(ctx context.Context, id ID) error

HardDelete permanently removes a record, bypassing go-pg's soft-delete.

func (*GoPgCRUD[T, ID]) List

func (r *GoPgCRUD[T, ID]) List(ctx context.Context, qarg ...r3.Query) ([]T, int64, error)

func (*GoPgCRUD[T, ID]) Patch

func (r *GoPgCRUD[T, ID]) Patch(ctx context.Context, entity T, fields r3.Fields) (T, error)

func (*GoPgCRUD[T, ID]) PgDB

func (r *GoPgCRUD[T, ID]) PgDB() *pg.DB

PgDB returns the underlying *pg.DB for advanced usage. Panics if GoPgCRUD is operating inside a transaction.

func (*GoPgCRUD[T, ID]) Raw

func (r *GoPgCRUD[T, ID]) Raw() *GoPgRaw[T, ID]

Raw returns the GoPgRaw escape hatch for custom queries.

func (*GoPgCRUD[T, ID]) Restore

func (r *GoPgCRUD[T, ID]) Restore(ctx context.Context, id ID) error

Restore un-deletes a soft-deleted record by clearing its soft-delete column.

func (*GoPgCRUD[T, ID]) Update

func (r *GoPgCRUD[T, ID]) Update(ctx context.Context, entity T) (T, error)

type GoPgRaw

type GoPgRaw[T any, ID any] struct {
	// contains filtered or unexported fields
}

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.

func (*GoPgRaw[T, ID]) Find

func (r *GoPgRaw[T, ID]) Find(ctx context.Context, cb func(*orm.Query) *orm.Query) ([]T, error)

Find executes a custom query and returns the results. The callback receives a *orm.Query to build a custom query.

func (*GoPgRaw[T, ID]) Scan

func (r *GoPgRaw[T, ID]) Scan(ctx context.Context, cb func(*orm.Query) *orm.Query, dest any) error

Scan executes a custom query and scans results into the provided destination.

Jump to

Keyboard shortcuts

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