pgstore

package module
v0.0.0-...-21266fc Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package pgstore keeps collab documents in PostgreSQL.

It implements github.com/go-crdt/collab.Store over a plain *sql.DB and brings no driver of its own, so the caller chooses one — pgx, lib/pq, or a pool wrapped to look like either. That also keeps this package's dependencies to the standard library.

db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
store, err := pgstore.New(db)
if err := store.Migrate(ctx); err != nil { … }
srv := collab.NewServer(collab.Config{Store: store})

A document is stored as one row holding its whole snapshot, which is what the [collab.Store] contract asks for: snapshots are self-contained, so a document restored from one can still serve a participant that has been away. The cost is that saving writes the whole document, so a server holding very large ones should call [collab.Server.Flush] on a timer rather than after every change.

Index

Constants

View Source
const DefaultTable = "collab_documents"

DefaultTable is the table documents are kept in.

Variables

View Source
var ErrInvalidTable = errors.New("pgstore: table name must be a plain identifier")

ErrInvalidTable reports a table name that is not a plain SQL identifier. Table names cannot be passed as query parameters, so they are checked rather than trusted.

Functions

This section is empty.

Types

type Option

type Option func(*Store)

An Option adjusts a Store.

func WithTable

func WithTable(name string) Option

WithTable puts the documents in a table other than DefaultTable. The name must be a plain identifier: a letter or underscore followed by letters, digits or underscores.

type Store

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

A Store keeps documents in one table. It is safe for concurrent use, as *sql.DB is.

func New

func New(db *sql.DB, opts ...Option) (*Store, error)

New returns a store over db.

func (*Store) Documents

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

Documents returns the names of the documents held, ordered, which is what a caller needs to inspect or migrate a store.

func (*Store) Load

func (s *Store) Load(ctx context.Context, document string) ([]byte, error)

Load returns the snapshot for a document, or nil if there is none yet. A document nobody has written is not an error; it is a new document.

func (*Store) Migrate

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

Migrate creates the table if it is not there yet. It is safe to call on every start, and safe to call from several servers at once.

func (*Store) Save

func (s *Store) Save(ctx context.Context, document string, snapshot []byte) error

Save records the current snapshot, replacing any previous one.

Jump to

Keyboard shortcuts

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