numbering

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package numbering hands out gapless numbers per (tenant, context, name), such as invoice numbers. Next runs in the transaction open in the context, so a rolled-back transaction gives its numbers back.

PgStore needs the identifier_sequences table from docs/postgres/schema.sql. InMemoryStore follows postgres.InMemoryUnitOfWork rollbacks.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTenant is returned when the context carries no Tenant.
	ErrNoTenant = errors.New("numbering: no tenant in context")
	// ErrEmptySequence is returned when the context or name of a Sequence
	// is empty.
	ErrEmptySequence = errors.New("numbering: empty context or name")
)

Functions

This section is empty.

Types

type InMemoryStore

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

InMemoryStore hands out numbers from memory. A rolled-back postgres.InMemoryUnitOfWork gives its numbers back. Unlike PgStore it does not make concurrent transactions wait, so if one rolls back after another has drawn a later number from the same sequence, a gap remains.

func NewInMemoryStore

func NewInMemoryStore() *InMemoryStore

NewInMemoryStore creates an empty InMemoryStore.

func (*InMemoryStore) Next

func (s *InMemoryStore) Next(ctx context.Context, seq Sequence) (int64, error)

Next returns the next number of seq for the Tenant in ctx, starting at 1.

type PgOption

type PgOption func(*PgStore)

PgOption configures a PgStore.

func WithSchema

func WithSchema(schema string) PgOption

WithSchema sets the Postgres schema holding the identifier_sequences table. Defaults to public.

type PgStore

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

PgStore hands out numbers from the identifier_sequences table.

func NewPgStore

func NewPgStore(pool *pgxpool.Pool, opts ...PgOption) *PgStore

NewPgStore creates a PgStore that runs on the transaction in the context, or on pool outside one.

func (*PgStore) Next

func (s *PgStore) Next(ctx context.Context, seq Sequence) (int64, error)

Next returns the next number of seq for the Tenant in ctx, starting at 1. The sequence row stays locked until the surrounding transaction ends, so concurrent callers wait and a rollback leaves no gap. Outside a transaction the number is used up at once.

type Sequence

type Sequence struct {
	Context string
	Name    string
}

Sequence names a gapless range of numbers within a bounded context. The Tenant comes from the context.

Jump to

Keyboard shortcuts

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