Skip to content

Latest commit

 

History

History

store

Resgate logo

Storage utilities for Go RES Service
Synchronize Your Clients

License Reference


Package store provides handlers and interfaces for working with database storage.

For more details and comments on the interfaces, see the go.dev reference.

Store interface

A store contains resources of a single type. It can be seen as a row in an Excel sheet or SQL table, or a document in a MongoDB collection.

Any database can be used with a wrapper that implements the following interface:

// Store is a CRUD interface for storing resources of a specific type.
type Store interface {
    Read(id string) ReadTxn
    Write(id string) WriteTxn
    OnChange(func(id string, before, after interface{}))
}

// ReadTxn represents a read transaction.
type ReadTxn interface {
    ID() string
    Close() error
    Exists() bool
    Value() (interface{}, error)
}

// WriteTxn represents a write transaction.
type WriteTxn interface {
    ReadTxn
    Create(interface{}) error
    Update(interface{}) error
    Delete() error
}

QueryStore interface

A query store provides the methods for making queries to an underlying database, and listen for changes that might affect the results.

// QueryStore is an interface for quering the resource in a store.
type QueryStore interface {
    Query(query url.Values) (interface{}, error)
    OnQueryChange(func(QueryChange))
}

// QueryChange represents a change to a resource that may affects queries.
type QueryChange interface {
    ID() string
    Before() interface{}
    After() interface{}
    Events(q url.Values) (events []ResultEvent, reset bool, err error)
}

// ResultEvent represents an event on a query result.
type ResultEvent struct {
    Name string
    Idx int
    Value interface{}
    Changed map[string]interface{}
}

Implementations

Use these examples as inspiration for your database implementation.

Name Description Documentation
mockstore Mock store implementation for testing Reference
badgerstore BadgerDB store implementation Reference