postgres

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package postgres is the docstore.Store for PostgreSQL (Cloud SQL, RDS, Aurora, Azure Database for PostgreSQL Flexible Server, self-hosted…) using pgx/v5 and JSONB.

Documents live in <schema>.backyard_docs:

site, collection, id  text COLLATE "C"   (primary key; byte-order sorting)
data                  jsonb
created_at, updated_at timestamptz       (microsecond precision)
created_by, updated_by text COLLATE "C"
version               bigint

with an index on (site, collection, created_at, id) for the default listing order. The schema is created and migrated on start when auto_migrate is set; migrations are serialised across instances with pg_advisory_lock and recorded in <schema>.backyard_schema_version (component "docstore").

Filters, ordering and pagination run in SQL (see query.go) with the type-strict semantics of SPEC §5.2; user input is always bound as parameters. Update is a single atomic statement: data = (data || patch_without_nulls) - keys_to_delete.

Limitation: PostgreSQL cannot store the NUL character (\u0000) in jsonb strings; such documents are rejected with docstore.ErrInvalid.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is a PostgreSQL-backed document store.

func New

func New(ctx context.Context, pool *pgxpool.Pool, schema string, autoMigrate bool) (*Store, error)

New builds a store on an existing pool (which Close leaves open) in schema (default "public"), migrating it when autoMigrate is true.

func Open

func Open(ctx context.Context, cfg config.PostgresConfig) (*Store, error)

Open connects to cfg.DSN (pool size cfg.MaxConns) and prepares the schema: with cfg.AutoMigrate (default true) the schema and tables are created or migrated, otherwise Open fails if they are missing or out of date.

func (*Store) Close

func (s *Store) Close() error

Close closes the pool if Open created it.

func (*Store) Collections

func (s *Store) Collections(ctx context.Context, site string) ([]string, error)

Collections lists the non-empty collections of a site in byte order, using a loose index scan over the primary key.

func (*Store) Count

func (s *Store) Count(ctx context.Context, site string) (int64, error)

Count returns the number of documents of a site.

func (*Store) Create

func (s *Store) Create(ctx context.Context, site, collection, id string, data map[string]any, actor string) (*docstore.Doc, error)

Create inserts a new document; see docstore.Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, site, collection, id string) (bool, error)

Delete removes a document; see docstore.Store.

func (*Store) DeleteIf added in v0.2.0

func (s *Store) DeleteIf(ctx context.Context, site, collection, id string, rev docstore.Rev) (bool, error)

DeleteIf removes a document while it is at rev, in one statement; see docstore.Store.

func (*Store) DeleteSite

func (s *Store) DeleteSite(ctx context.Context, site string) error

DeleteSite removes every document of a site.

func (*Store) Get

func (s *Store) Get(ctx context.Context, site, collection, id string) (*docstore.Doc, error)

Get returns a document; see docstore.Store.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping checks connectivity.

func (*Store) Put

func (s *Store) Put(ctx context.Context, site, collection, id string, data map[string]any, actor string) (*docstore.Doc, error)

Put creates or replaces a document in one statement; see docstore.Store.

func (*Store) Query

func (s *Store) Query(ctx context.Context, site, collection string, q docstore.Query) (*docstore.Page, error)

Query returns matching documents; see docstore.Store.

func (*Store) Update

func (s *Store) Update(ctx context.Context, site, collection, id string, patch map[string]any, actor string) (*docstore.Doc, error)

Update shallow-merges patch into an existing document in a single atomic statement; see docstore.Store.

func (*Store) UpdateIf added in v0.2.0

func (s *Store) UpdateIf(ctx context.Context, site, collection, id string, rev docstore.Rev, patch map[string]any, actor string) (*docstore.Doc, error)

UpdateIf is Update while the document is at rev, checked by the same statement; see docstore.Store.

Jump to

Keyboard shortcuts

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