storage

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package storage defines the backend-neutral data access contract. No database driver types cross this boundary.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means no document has the requested id.
	ErrNotFound = errors.New("document not found")
	// ErrVersionMismatch means a conditional write found a different version.
	ErrVersionMismatch = errors.New("document version mismatch")
	// ErrUnavailable means the storage backend could not be reached in time.
	ErrUnavailable = errors.New("storage unavailable")
	// ErrConflict means a write violated a unique index. The concrete error
	// is a *ConflictError.
	ErrConflict = errors.New("unique constraint violated")
)

Functions

This section is empty.

Types

type And

type And []Filter

And matches documents matching every filter; And{} matches everything.

type Condition

type Condition struct {
	Field string
	Op    Operator
	Value any
}

Condition is one filter clause. Field is a dot path in API terms ("id", "_meta.created_at", "address.city"). Values are already type-checked: numbers are int64 or float64, _meta dates are time.Time.

type ConflictError

type ConflictError struct {
	Fields []string // fields of the violated index, in API terms; may be empty
}

ConflictError reports a unique index violation.

func (*ConflictError) Error

func (e *ConflictError) Error() string

func (*ConflictError) Is

func (e *ConflictError) Is(target error) bool

Is makes errors.Is(err, ErrConflict) match.

type Const

type Const bool

Const matches every document (true) or none (false).

type Document

type Document = map[string]any

Document is a stored document in API shape: "id" (string), "_meta" (map with time.Time values) and the user fields. Numbers are int64 or float64, nested objects are Documents or map[string]any, arrays []any.

func Fetch

func Fetch(ctx context.Context, repo Repository, id string, filter Filter) (Document, error)

Fetch returns the document with id if the access filter (nil: none) lets the caller read it, else ErrNotFound. Single reads use it, so a read rule applies to them exactly as it does to lists.

type Filter

type Filter interface {
	// contains filtered or unexported methods
}

Filter is a boolean combination of conditions: a Condition, And, Or, Not or Const.

func Simplify

func Simplify(f Filter) Filter

Simplify folds constants, so the result is a Const or contains none.

type Not

type Not struct{ Filter Filter }

Not matches documents not matching Filter.

type Operator

type Operator string

Operator is a filter operator. Operators are backend-neutral; each storage implementation translates them.

const (
	OpEq          Operator = "$eq"
	OpNe          Operator = "$ne"
	OpGt          Operator = "$gt"
	OpGte         Operator = "$gte"
	OpLt          Operator = "$lt"
	OpLte         Operator = "$lte"
	OpIn          Operator = "$in"      // Value: []any
	OpNin         Operator = "$nin"     // Value: []any
	OpBetween     Operator = "$between" // Value: []any{min, max}, inclusive
	OpIsNull      Operator = "$isNull"  // Value: bool; true matches null or missing
	OpLike        Operator = "$like"    // Value: string; SQL-style % and _ wildcards
	OpILike       Operator = "$ilike"
	OpStartsWith  Operator = "$startsWith"
	OpIStartsWith Operator = "$istartsWith"
	OpEndsWith    Operator = "$endsWith"
	OpIEndsWith   Operator = "$iendsWith"
	OpContains    Operator = "$contains"
	OpIContains   Operator = "$icontains"
)

type Or

type Or []Filter

Or matches documents matching any filter; Or{} matches nothing.

type Page

type Page struct {
	Items   []Document
	HasMore bool
	Total   *int64 // set only when Query.Count is true
}

Page is one page of a list query.

type Query

type Query struct {
	// Filter is the client's where: a tree of conditions combined with
	// And and Or. nil matches everything.
	Filter Filter
	Sort   []SortField // applied in order; ends with "id" as a tiebreaker
	Limit  int
	Skip   int
	Count  bool // also compute the total number of matching documents
	// Access restricts the query to documents the caller may read
	// (from access rules); nil means no restriction.
	Access Filter
}

Query selects a page of documents.

type Repository

type Repository interface {
	// Create stores doc, which must already carry "id" and "_meta".
	Create(ctx context.Context, doc Document) error
	// Get returns the document with the given id, or ErrNotFound.
	Get(ctx context.Context, id string) (Document, error)
	// List returns a page of documents.
	List(ctx context.Context, q Query) (Page, error)
	// Replace overwrites the document with doc["id"] only if its stored
	// _meta.version equals ifVersion (0 means "stored without a version").
	// It returns ErrNotFound or ErrVersionMismatch otherwise.
	Replace(ctx context.Context, doc Document, ifVersion int64) error
	// Delete removes the document with the given id. When ifVersion is not
	// nil the stored version must equal it. It returns ErrNotFound or
	// ErrVersionMismatch otherwise.
	Delete(ctx context.Context, id string, ifVersion *int64) error
}

Repository gives access to the documents of one collection.

type SortField

type SortField struct {
	Field string
	Desc  bool
}

SortField orders results by one field.

Jump to

Keyboard shortcuts

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