postgres

package module
v1.2.84 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 20 Imported by: 5

README

Postgres

Postgres ORM.

Install

go get github.com/aacfactory/fns-contrib/databases/postgres

Usage

Deploy
app.Deply(postgres.New())
Config

See SQL.

Register driver

github.com/lib/pq and github.com/jackc/pgx are all supported.
Note, when use pgx, then don't enable statements in config.

import (
    _ "github.com/lib/pq"
)
Register dialect

Add import in deploy src file.

import (
	_ "github.com/aacfactory/fns-contrib/databases/postgres"
)
Define struct

See DAC.

Switch package

Use github.com/aacfactory/fns-contrib/databases/postgres insteadof github.com/aacfactory/fns-contrib/databases/sql/dac.

entry, err = postgres.Insert[Table](ctx, entry) // insteadof dac
Code generator in fn

Add annotation code writer

generates.New(generates.WithAnnotations(postgres.FAG()...))

Use @postgres:transaction annotation. params are readonly and isolation.

  • readonly: set the transaction to be readonly.
  • isolation: use spec isolation. default is use isolation of config.
    • ReadCommitted
    • ReadUncommitted
    • WriteCommitted
    • RepeatableRead
    • Snapshot
    • Serializable
    • Linearizable
// @fn some
// ... some func use transaction
// @postgres:transaction
func some(ctx context.Context, param Param) (result Result, err error) {
	// ...
	return
}

Use @postgres:use annotation to switch datasource service. param is service name and mark it before @postgres:transaction.

// @fn some
// ... some func use transaction
// @postgres:use postgres1
func some(ctx context.Context, param Param) (result Result, err error) {
	// ...
	return
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ALL added in v1.0.0

func ALL[T Table](ctx context.Context, options ...QueryOption) (entries []T, err error)

func Asc

func Asc(name string) orders.Orders

func Begin added in v1.2.1

func Begin(ctx context.Context, options ...databases.TransactionOption) (err error)

func Between

func Between(field string, left any, right any) conditions.Condition

func Bool added in v1.0.0

func Bool(b bool) conditions.Literal

func Commit added in v1.2.1

func Commit(ctx context.Context) (err error)

func ContainsJsonKey added in v0.12.6

func ContainsJsonKey(field string, key string) conditions.Condition

func ContainsJsonObject added in v0.12.6

func ContainsJsonObject(field string, object json.RawMessage) conditions.Condition

func ContainsJsonObjectOfArray added in v0.12.6

func ContainsJsonObjectOfArray(field string, object json.RawMessage) conditions.Condition

func ContainsJsonObjectsOfArray added in v0.12.6

func ContainsJsonObjectsOfArray(field string, all bool, elements ...any) conditions.Condition

func Count

func Count[T Table](ctx context.Context, cond conditions.Condition) (count int64, err error)

func Delete

func Delete[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func DeleteByCondition added in v1.0.0

func DeleteByCondition[T Table](ctx context.Context, cond conditions.Condition) (affected int64, err error)

func Desc

func Desc(name string) orders.Orders

func Disuse added in v1.2.1

func Disuse(ctx context.Context) context.Context

func Eq

func Eq(field string, expression any) conditions.Condition

func Exist

func Exist[T Table](ctx context.Context, cond conditions.Condition) (has bool, err error)

func FAG added in v1.0.0

func Float added in v1.0.0

func Float(f float32) conditions.Literal

func Float64 added in v1.0.0

func Float64(f float64) conditions.Literal

func Gt added in v1.0.0

func Gt(field string, expression any) conditions.Condition

func Gte added in v1.0.0

func Gte(field string, expression any) conditions.Condition

func In added in v1.0.0

func In(field string, expression ...any) conditions.Condition

func Insert

func Insert[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func InsertMulti added in v1.0.0

func InsertMulti[T Table](ctx context.Context, entries []T) (affected int64, err error)

func InsertOrUpdate

func InsertOrUpdate[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func InsertWhenExist

func InsertWhenExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error)

func InsertWhenNotExist

func InsertWhenNotExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error)

func Int added in v1.0.0

func Int(n int) conditions.Literal

func Int64 added in v1.0.0

func Int64(n int64) conditions.Literal

func Like

func Like(field string, expression string) conditions.Condition

func LikeContains added in v1.0.0

func LikeContains(field string, expression string) conditions.Condition

func LikeLast added in v1.0.0

func LikeLast(field string, expression string) conditions.Condition

func Lit added in v0.12.6

func Lit(v string) conditions.Literal

func LitSubQuery added in v1.0.0

func LitSubQuery(query string) conditions.QueryExpr

func Lt added in v1.0.0

func Lt(field string, expression any) conditions.Condition

func Lte added in v1.0.0

func Lte(field string, expression any) conditions.Condition

func Named added in v1.0.0

func Named(name string, value any) sql.NamedArg

func New added in v1.0.0

func New(options ...sql.Option) services.Service

func NotEq

func NotEq(field string, expression any) conditions.Condition

func NotIn added in v0.11.2

func NotIn(field string, expression ...any) conditions.Condition

func One added in v1.0.0

func One[T Table](ctx context.Context, options ...QueryOption) (entry T, has bool, err error)

func Page

func Page[T Table](ctx context.Context, no int, size int, options ...QueryOption) (page dac.Pager[T], err error)

func Query

func Query[T Table](ctx context.Context, offset int, length int, options ...QueryOption) (entries []T, err error)

func Rollback added in v1.2.1

func Rollback(ctx context.Context)

func String added in v1.0.0

func String(s string) conditions.Literal

func SubQuery added in v0.12.8

func SubQuery(query any, field string, cond conditions.Condition) conditions.QueryExpr

func Time added in v1.0.0

func Time(t time.Time) conditions.Literal

func Tree added in v1.0.0

func Tree[T Table](ctx context.Context, options ...QueryOption) (entry T, err error)

func Trees added in v1.0.0

func Trees[T Table](ctx context.Context, options ...QueryOption) (entries []T, err error)

func Update added in v1.0.0

func Update[T Table](ctx context.Context, entry T) (v T, ok bool, err error)

func UpdateFields added in v1.0.0

func UpdateFields[T Table](ctx context.Context, fields FieldValues, cond conditions.Condition) (affected int64, err error)

func Use added in v1.0.0

func Use(ctx context.Context, endpointName []byte) context.Context

func ViewALL added in v1.2.1

func ViewALL[V View](ctx context.Context, options ...QueryOption) (entries []V, err error)

func ViewOne added in v1.2.1

func ViewOne[V View](ctx context.Context, options ...QueryOption) (entry V, has bool, err error)

func Views added in v1.2.1

func Views[V View](ctx context.Context, offset int, length int, options ...QueryOption) (entries []V, err error)

func WithDatabase added in v1.0.0

func WithDatabase(db databases.Database) sql.Option

func WithName added in v1.0.0

func WithName(name string) sql.Option

func WithTLS added in v1.2.11

func WithTLS(fn RegisterTLSFunc) sql.Option

Types

type FieldValues added in v1.0.0

type FieldValues dac.FieldValues

func Field added in v1.0.0

func Field(name string, value any) FieldValues

func (FieldValues) Field added in v1.0.0

func (fields FieldValues) Field(name string, value any) FieldValues

type QueryOption added in v1.0.0

type QueryOption dac.QueryOption

func Conditions

func Conditions(cond conditions.Condition) QueryOption

func GroupBy added in v1.0.0

func GroupBy(by groups.GroupBy) QueryOption

func Orders

func Orders(order orders.Orders) QueryOption

type QueryOptions added in v1.0.0

type QueryOptions struct {
	dac.QueryOption
}

type RegisterTLSFunc added in v1.2.11

type RegisterTLSFunc func(config *tls.Config) (err error)

type Table

type Table interface {
	dac.Table
}

type View added in v1.2.1

type View interface {
	dac.View
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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