postgres

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package postgres implements PostgreSQL storage. It is powered by sqlx and goqu.

`sqlx` is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

goqu is a query builder.

Index

Constants

View Source
const Name = "postgres"

Name of the storage.

Variables

This section is empty.

Functions

func Get

func Get() storage.IStorage

Get returns a setup storage, or set it up.

func Set

func Set(s storage.IStorage)

Set sets the storage, primarily used for testing.

func ToSQLString

func ToSQLString(s customsort.Sort) (string, error)

ToSQLString converts the `s` to a format that can be used in a SQL query.

NOTE: Keywords in SQL are case-insensitive for the most popular DBMSs. The computer doesn't care whether you write SELECT, select, sELeCt, ASC, AsC, or asc.

Types

type Config

type Config struct {
	DataSourceName string `json:"dataSourceName" validate:"required"`
	DriverName     string `json:"driverName"     validate:"required"`
}

Config is the postgres configuration.

type Postgres

type Postgres struct {
	*storage.Storage

	// Client is the postgres client.
	Client *sqlx.DB `json:"-" validate:"required,structonly"`

	// Config is the postgres configuration.
	Config *Config `json:"-"`

	// Target allows to set a static target. If it is empty, the target will be
	// dynamic - the one set at the operation (count, create, delete, etc) time.
	// Depending on the storage, target is a collection, a table, a bucket, etc.
	// For ElasticSearch, for example it doesn't have a concept of a database -
	// the target then is the index. Due to different cases of ElasticSearch
	// usage, the target can be static or dynamic - defined at the index time,
	// for example: log-{YYYY}-{MM}. For Redis, it isn't used at all.
	Target string `json:"-" validate:"omitempty,gt=0"`
	// contains filtered or unexported fields
}

Postgres storage definition.

func New

func New(ctx context.Context, dataSource string) (*Postgres, error)

New creates a new postgres storage.

func (*Postgres) BulkCreate

func (p *Postgres) BulkCreate(
	ctx context.Context,
	target string,
	items []storage.BulkItem,
	prm *create.Create,
	options ...storage.Func[*create.Create],
) (storage.BulkResult, error)

BulkCreate creates data with parameterized, multi-row INSERT statements.

func (*Postgres) Close

func (p *Postgres) Close() error

Close releases the resources held by the storage.

func (*Postgres) Count

func (p *Postgres) Count(ctx context.Context, target string, prm *count.Count, options ...storage.Func[*count.Count]) (int64, error)

Count returns the number of items in the storage.

func (*Postgres) Create

func (p *Postgres) Create(ctx context.Context, id, target string, v any, prm *create.Create, options ...storage.Func[*create.Create]) (string, error)

Create data.

NOTE: Not all storages returns the ID, neither all storages requires `id` to be set. You are better off setting the ID yourself.

func (*Postgres) Delete

func (p *Postgres) Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...storage.Func[*delete.Delete]) error

Delete removes data.

func (*Postgres) GetClient

func (p *Postgres) GetClient() any

GetClient returns the client.

func (*Postgres) List

func (p *Postgres) List(ctx context.Context, target string, v any, prm *list.List, options ...storage.Func[*list.List]) error

List data.

Search is executed as the SQL query. Sort, Limit, Offset, and Fields are not translated into SQL; include them in Search when needed.

func (*Postgres) Retrieve

func (p *Postgres) Retrieve(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, options ...storage.Func[*retrieve.Retrieve]) error

Retrieve data.

func (*Postgres) Update

func (p *Postgres) Update(ctx context.Context, id, target string, v any, prm *update.Update, options ...storage.Func[*update.Update]) error

Update data.

Jump to

Keyboard shortcuts

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