pg

package module
v1.0.35 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: MPL-2.0 Imports: 7 Imported by: 0

README

Golang library for simplified database CRUD operations management

Usage examples

Working examples locates here: Plain entity example

One to many example

Documentation

Index

Constants

View Source
const (
	RETURNING_ID = "RETURNING id"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DbApi

type DbApi interface {
	pg_api.SQLExecutor
	pg_api.Transactor
	Pinger

	Close()
}

type DbClient

type DbClient interface {
	Close() error
	Pg() PgDbClient
	RunTransaction(ctx context.Context, txOptions transaction.TxOptions, f pg_api.TransactionalFlow) error
}

func NewDBClient

func NewDBClient(ctx context.Context, dsn string) (DbClient, error)

type IRepository

type IRepository interface {
	Create(ctx context.Context, values ...interface{}) int64
	GetById(ctx context.Context, id int64) any
	GetAll(ctx context.Context) []any
	GetBy(ctx context.Context, where sq.Sqlizer) []any
	Delete(ctx context.Context, id int64) int64
	UpdateCollection(ctx context.Context, fields map[string]interface{}, where sq.Sqlizer) int64
	Update(ctx context.Context, fields map[string]interface{}, id int64) int64
	UpdateReturning(ctx context.Context, builder sq.UpdateBuilder, entityConverter func(row pgx.Row) any) any
}

type Identifiable

type Identifiable interface {
	GetID() int64
}

type PgDbClient

type PgDbClient struct {
	// contains filtered or unexported fields
}

func NewPgDBClient

func NewPgDBClient(ctx context.Context, dsn string) (PgDbClient, error)

func (PgDbClient) API

func (c PgDbClient) API() pg_api.PG

func (PgDbClient) Close

func (c PgDbClient) Close() error

func (PgDbClient) RunTransaction

func (c PgDbClient) RunTransaction(ctx context.Context, txOpts transaction.TxOptions, f pg_api.TransactionalFlow) error

type Pinger

type Pinger interface {
	Ping(ctx context.Context) error
}
type Related interface {
	GetParentID() int64
	PushToParent(parent any)
}

type Relation

type Relation[R any] struct {
	ForeignKey     string
	Repo           Repository[R]
	ParentIdGetter func(R) int64
}

func WrapRelation

func WrapRelation[R any](r Relation[R]) Relation[any]

func (Relation[R]) GetForeignKey

func (r Relation[R]) GetForeignKey() string

func (Relation[R]) GetParentId

func (r Relation[R]) GetParentId(child R) int64

type Repository

type Repository[T any] struct {
	DB            PgDbClient
	InsertBuilder sq.InsertBuilder
	SelectBuilder sq.SelectBuilder
	UpdateBuilder sq.UpdateBuilder
	DeleteBuilder sq.DeleteBuilder
	Converter     func(row pgx.Row) any // type is any to allow generalization
	Relations     []Relation[any]       // the relation type is any because it really any entity
	AddRelated    func(*T, any)
	AddRelation   func(Relation[any])
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository[T any](
	anchor T,
	db DbClient,
	insertBuilder sq.InsertBuilder,
	selectBuilder sq.SelectBuilder,
	updateBuilder sq.UpdateBuilder,
	deleteBuilder sq.DeleteBuilder,
	converter func(row pgx.Row) any) Repository[T]

func WrapRepository

func WrapRepository[R any](repo Repository[R]) Repository[any]

func (Repository[T]) Create

func (repo Repository[T]) Create(ctx context.Context, values ...interface{}) int64

func (Repository[T]) Delete

func (repo Repository[T]) Delete(ctx context.Context, id int64) int64

func (Repository[T]) GetAll

func (repo Repository[T]) GetAll(ctx context.Context) []T

func (Repository[T]) GetBy

func (repo Repository[T]) GetBy(ctx context.Context, where sq.Sqlizer) []T

func (Repository[T]) GetById

func (repo Repository[T]) GetById(ctx context.Context, id int64) T

func (Repository[T]) Update

func (repo Repository[T]) Update(ctx context.Context, fields map[string]interface{}, id int64) int64

func (Repository[T]) UpdateCollection

func (repo Repository[T]) UpdateCollection(ctx context.Context, fields map[string]interface{}, where sq.Sqlizer) int64

func (Repository[T]) UpdateReturning

func (repo Repository[T]) UpdateReturning(ctx context.Context, builder sq.UpdateBuilder) any

func (Repository[T]) UpdateReturningWithExtendedConverter

func (repo Repository[T]) UpdateReturningWithExtendedConverter(ctx context.Context, builder sq.UpdateBuilder, entityConverter func(row pgx.Row) any) any

Directories

Path Synopsis
internal
pkg
test

Jump to

Keyboard shortcuts

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