pqr

package module
v0.0.0-...-b50264c Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: MIT Imports: 10 Imported by: 0

README

pqr

Another postgres ORM like package. Deeply inspired by reform and squirrel.

Tests GoDoc Go Report

Installation

go get -u github.com/skamenetskiy/pqr

Sample

package main

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/lib/pq"
	"github.com/skamenetskiy/pqr"
)

// Job repository definition.
type Job struct {
	ID         int64      `db:"id,pk"`
	StartedAt  *time.Time `db:"started_at"`
	FinishedAt *time.Time `db:"finished_at"`
	Errored    bool       `db:"errored"`
}

func main() {
	db, err := sql.Open("postgres", "postgres://postgres:password@localhost:5432/postgres?sslmode=disable")
	if err != nil {
		panic(err)
	}

	jobs := pqr.New[Job, int64]("jobs",
		pqr.WithLogger(pqr.DefaultLogger),
		pqr.WithStandardSQL(db),
	)
	if err != nil {
		panic(err)
	}

	// find a single job with id = 11
	job, err := jobs.FindOne(11)
	if err != nil {
		panic(err)
	}

	// update operation
	job.Errored = true
	if err = jobs.Update(pqr.UpdateParams[Job]{
		Condition: pqr.Eq{"id": 11},
		Columns:   []string{"errored"},
		Items:     []*Job{job},
	}); err != nil {
		panic(err)
	}

	// find multiple elements
	jl, err := jobs.Find(pqr.FindParams{
		Condition: pqr.Eq{
			"id": pqr.In[int64]{11, 5, 6},
		},
	})
	fmt.Println(jl)

	// run in transaction
	err = jobs.Transaction(func(tx pqr.Transaction[Job, int64]) error {
		c, e := tx.Count()
		fmt.Println("count", c, e)
		return e
	})
	if err != nil {
		panic(err)
	}
}

How to avoid runtime reflection

Very simple! Your repository struct must implement Valueable and Pointable interfaces. That's it!

Documentation

Index

Constants

View Source
const (
	// NopLogger is a Logger that does nothing. It is used by default,
	// if no logger is provided on initialization.
	NopLogger = nopLogger('🚀')

	// DefaultLogger is a wrapper around slog.Logger.
	DefaultLogger = slogLogger('🚀')
)

Variables

This section is empty.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound is a helper functions to check if the error means that no records were found.

Types

type AfterFinder

type AfterFinder interface {
	AfterFind(ctx context.Context) error
}

type And

type And []Condition

And condition.

type BeforePutter

type BeforePutter interface {
	// BeforePut will be triggered right before "put" query is executed.
	// If an error is returned, the update will be canceled. On batch action,
	// the transaction will be aborted and rolled back.
	BeforePut(context.Context) error
}

type BeforeUpdater

type BeforeUpdater interface {
	// BeforeUpdate will be triggered right before "update" query is executed.
	// If an error is returned, the update will be canceled. On batch action,
	// the transaction will be aborted and rolled back.
	BeforeUpdate(context.Context) error
}

BeforeUpdater interface.

type Condition

type Condition interface {
	// contains filtered or unexported methods
}

Condition interface describes the search condition in Find like methods.

type CountParams

type CountParams struct {
	Condition
}

type DeleteParams

type DeleteParams struct {
	Condition
}

type Eq

type Eq map[string]any

type FindParams

type FindParams struct {
	Condition
	Order
	OrderBy string
	Limit   int64
}

type Gt

type Gt map[string]any

type Gte

type Gte map[string]any

type In

type In[T any] []T

type Key

type Key interface {
	int32 | uint32 | int64 | uint64 | string
}

Key interface represents a list of allowed keys.

type Logger

type Logger interface {
	// BeforeQuery is triggered before the query is executed.
	BeforeQuery(ctx context.Context, query string, args []any)

	// AfterQuery is triggered right after the query was executed.
	AfterQuery(ctx context.Context, query string, args []any, err error, d time.Duration)
}

Logger interface for query logging.

type Lt

type Lt map[string]any

type Lte

type Lte map[string]any

type Nq

type Nq map[string]any

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface is used for initial configuration.

func WithLogger

func WithLogger(l Logger) Option

WithLogger attaches a Logger to Repository on initialization.

func WithSQL

func WithSQL(s SQL) Option

WithSQL attaches SQL connection to Repository on initialization.

func WithStandardSQL

func WithStandardSQL(db *sql.DB) Option

WithStandardSQL attaches sql.DB connection to Repository on initialization.

type Or

type Or []Condition

type Order

type Order string
const (
	OrderNone Order = ""
	OrderASC  Order = "ASC"
	OrderDESC Order = "DESC"
)

type Pointable

type Pointable interface {
	Pointers() []any
}

type Repository

type Repository[T any, K Key] interface {
	// contains filtered or unexported methods
}

Repository interface is the main set of methods to the repository.

func New

func New[T any, K Key](name string, opts ...Option) Repository[T, K]

New is a Repository factory, where name is the database table name and queryLogger is the database connection that implements querier interface.

type Row

type Row interface {
	Err() error
	Scan(...any) error
}

type Rows

type Rows interface {
	Row
	Next() bool
	Close() error
}

type SQL

type SQL interface {
	// contains filtered or unexported methods
}

type Transaction

type Transaction[T any, K Key] interface {
	// contains filtered or unexported methods
}

type Tx

type Tx interface {
	Commit() error
	Rollback() error
	// contains filtered or unexported methods
}

type UpdateParams

type UpdateParams[T any] struct {
	Condition
	Columns []string
	Items   []*T
}

type Valueable

type Valueable interface {
	Values() []any
}

Jump to

Keyboard shortcuts

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