storage

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package storage contains an extensible interface for providing persistence to simple applications and other prefab plugins.

Stores provides simple create, read, update, delete, and list operations. Models are represented as structs and should have a `PK() string` method.

Examples:

	prefab.Register(storage.Plugin(memorystore.New()))

 func (m *MyPlugin) Init(r *prefab.Registry) error {
   m.store = r.Get(storage.PluginName)
 }

Index

Constants

View Source
const PluginName = "storage"

PluginName can be used to query the storage plugin.

Variables

View Source
var (
	// Returned when a record does not exist.
	ErrNotFound = errors.NewC("record not found", codes.NotFound)

	// Returned when a record conficts with an existing key.
	ErrAlreadyExists = errors.NewC("primary key already exists", codes.AlreadyExists)

	// Returned when List is called with a non-slice.
	ErrSliceRequired = errors.NewC("pointer slice required", codes.InvalidArgument)

	// Returned when a store can not marshal/unmarshal a model.
	ErrInvalidModel = errors.NewC("invalid model", codes.InvalidArgument)

	// Returned when List is called with a filter and slice of mismatching types.
	ErrTypeMismatch = errors.NewC("type mismatch", codes.InvalidArgument)

	// Returned when a store is passed an uninitialized pointer.
	ErrNilModel = errors.NewC("uninitialized pointer passed as model", codes.InvalidArgument)
)

Functions

func Name

func Name(m any) string

Name returns a pluralied version of the model's name, either derived from the struct or from the `Namer` interface.

func Plugin

func Plugin(impl Store) prefab.Plugin

Plugin wraps a storage implementation for registration.

func ValidateReceiver

func ValidateReceiver(model Model) error

ValidateReceiver returns an error if the model is nil or uninitialized.

Types

type Model

type Model interface {
	// PK returns the primary key that the record is stored under.
	PK() string
}

Model defines the interface for records which want to be persisted to a storage engine.

type ModelInitializer

type ModelInitializer interface {
	// InitModel is called by a plugin or application to initialize a model
	// before it is used. Stores will still work, without initialization, however
	// data will be stored in a shared table.
	InitModel(model Model) error
}

Optional interface that stores can implement in order to support per-model configuration — for example table per model in SQL databases.

type Namer

type Namer interface {
	Name() string
}

Namer allows Models to override how the table-name is determined, for engines which require it.

type StoragePlugin

type StoragePlugin struct {
	Store
}

StoragePlugin exposes a Plugin interface for persisting data.

func (*StoragePlugin) InitModel

func (p *StoragePlugin) InitModel(m Model) error

InitModel can be called by a plugin or application to perform per model initialization. Stores that do not implement ModelInitializer should still function correctly, but may store data in a shared table.

func (*StoragePlugin) Name

func (p *StoragePlugin) Name() string

From prefab.Plugin.

type Store

type Store interface {
	// Create multiple entities.
	Create(models ...Model) error

	// Read a record with the given id.
	Read(id string, model Model) error

	// Update multiple entities.
	Update(models ...Model) error

	// Update or insert multiple entities.
	Upsert(models ...Model) error

	// Delete a record. Only the primary key needs to be populated.
	Delete(model Model) error

	// List populates the slice of models with records that have fields which
	// match the fields of filter. Zero-value fields will be ignored, unless the
	// field is a pointer.
	List(models any, filter Model) error

	// Exists returns true if a record with the given id exists.
	Exists(id string, model Model) (bool, error)
}

Store offers a basic CRUUDLE (Create Read Update Upsert Delete List Exists) interface that allows prefab plugins to persist data.

Directories

Path Synopsis
Package memstore implements storage.Store in a purely in-memory manner.
Package memstore implements storage.Store in a purely in-memory manner.
Package postgres provides a PostgreSQL implementation of storage.Store interface.
Package postgres provides a PostgreSQL implementation of storage.Store interface.
Package sqlite provides a SQLite implementation of storage.Store interface.
Package sqlite provides a SQLite implementation of storage.Store interface.
Package storagetests provides common acceptance tests for storage.Store implementations.
Package storagetests provides common acceptance tests for storage.Store implementations.

Jump to

Keyboard shortcuts

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