resbadger

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 8 Imported by: 0

README

Resgate logo

BadgerDB Middleware for Go RES Service
Synchronize Your Clients

License Reference Status


This package is deprecated, please use the store package together with the badgerstore package instead.

The store interface provides superior structure for building services that scales well.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadgerDB

type BadgerDB struct {
	// BadgerDB database
	DB *badger.DB
}

BadgerDB provides persistence to BadgerDB for the res Handlers.

It will set the GetResource and Apply* handlers to load, store, and update the resources in the database, using the resource ID as key value.

func (BadgerDB) Collection

func (o BadgerDB) Collection() Collection

Collection returns a middleware builder of type Collection.

func (BadgerDB) Model

func (o BadgerDB) Model() Model

Model returns a middleware builder of type Model.

func (BadgerDB) QueryCollection

func (o BadgerDB) QueryCollection() QueryCollection

QueryCollection returns a middleware builder of type QueryCollection.

func (BadgerDB) WithDB

func (o BadgerDB) WithDB(db *badger.DB) BadgerDB

WithDB returns a new BadgerDB value with the DB set to db.

type Collection

type Collection struct {
	// BadgerDB middleware
	BadgerDB BadgerDB
	// Default resource value if not found in database.
	// Will return res.ErrNotFound if not set.
	Default interface{}
	// Type used to marshal into when calling r.Value() or r.RequireValue().
	// Defaults to []interface{} if not set.
	Type interface{}
}

Collection represents a collection that is stored in the badger DB by its resource ID.

func (Collection) SetOption

func (o Collection) SetOption(hs *res.Handler)

SetOption sets the res handler options, and implements the res.Option interface.

func (Collection) WithDefault

func (o Collection) WithDefault(i interface{}) Collection

WithDefault returns a new BadgerDB value with the Default resource value set to i.

func (Collection) WithType

func (o Collection) WithType(v interface{}) Collection

WithType returns a new Collection value with the Type value set to v.

type Index

type Index struct {
	// Index name
	Name string
	// Key callback is called with a resource item of the type defined by Type,
	// and should return the string to use as index value.
	// It does not have to be unique.
	//
	// Example index by Country and lower case Name on a user model:
	// 	func(v interface{}) {
	// 		user := v.(UserModel)
	// 		return []byte(user.Country + "_" + strings.ToLower(user.Name))
	// 	}
	Key func(interface{}) []byte
}

Index defines an index used for a resource.

When used on Model resource, an index entry will be added for each model entry. An index entry will have no value (nil), and the key will have the following structure:

<Name>:<Key>?<RID>

Where: * <Name> is the name of the Index (so keep it rather short) * <Key> is the index value as returned from the Key callback * <RID> is the resource ID of the indexed model

type IndexQuery

type IndexQuery struct {
	// Index used
	Index Index
	// KeyPrefix to match against the index key
	KeyPrefix []byte
	// FilterKeys for keys in the query collection. May be nil.
	FilterKeys func(key []byte) bool
	// Offset from which item to start.
	Offset int
	// Limit how many items to read. Negative means unlimited.
	Limit int
	// Reverse flag to tell if order is reversed
	Reverse bool
}

IndexQuery represents a query towards an index.

func (*IndexQuery) FetchCollection

func (iq *IndexQuery) FetchCollection(db *badger.DB) ([]res.Ref, error)

FetchCollection fetches a collection of resource references based on the query.

type IndexSet

type IndexSet struct {
	// List of indexes
	Indexes []Index
	// contains filtered or unexported fields
}

IndexSet represents a set of indexes for a model resource.

func (*IndexSet) GetIndex

func (i *IndexSet) GetIndex(name string) (Index, error)

GetIndex returns an index by name, or an error if not found.

func (*IndexSet) Listen

func (i *IndexSet) Listen(cb func(r res.Resource, before, after interface{}))

Listen adds a callback listening to the changes that have affected one or more index entries.

The model before value will be nil if the model was created, or if previously not indexed. The model after value will be nil if the model was deleted, or if no longer indexed.

func (*IndexSet) ListenIndex added in v0.3.0

func (i *IndexSet) ListenIndex(name string, cb func(r res.Resource, before, after interface{}))

ListenIndex adds a callback listening to the changes of a specific index.

The model before value will be nil if the model was created, or if previously not indexed. The model after value will be nil if the model was deleted, or if no longer indexed.

type Model

type Model struct {
	// BadgerDB middleware
	BadgerDB BadgerDB
	// Default resource value if not found in database.
	// Will return res.ErrNotFound if not set.
	Default interface{}
	// Type used to marshal into when calling r.Value() or r.RequireValue().
	// Defaults to map[string]interface{} if not set.
	Type interface{}
	// IndexSet defines a set of indexes to be created for the model.
	IndexSet *IndexSet
	// Map defines a map callback to transform the model when
	// responding to get requests.
	Map func(interface{}) (interface{}, error)
}

Model represents a model that is stored in the badger DB by its resource ID.

func (Model) RebuildIndexes

func (o Model) RebuildIndexes(pattern string) error

RebuildIndexes drops existing indexes and creates new entries for the models with the given resource pattern.

The resource pattern should be the full pattern, including any service name. It may contain $tags, or end with a full wildcard (>).

test.model.$id
test.resource.>

func (Model) SetOption

func (o Model) SetOption(hs *res.Handler)

SetOption sets the res handler options, and implements the res.Option interface.

func (Model) WithDefault

func (o Model) WithDefault(i interface{}) Model

WithDefault returns a new BadgerDB value with the Default resource value set to i.

func (Model) WithIndexSet

func (o Model) WithIndexSet(idxs *IndexSet) Model

WithIndexSet returns a new Model value with the IndexSet set to idxs.

func (Model) WithMap added in v0.3.0

func (o Model) WithMap(m func(interface{}) (interface{}, error)) Model

WithMap returns a new Model value with the Map set to m.

The m callback takes the model value v, with the type being Type, and returns the value to send in response to the get request.

func (Model) WithType

func (o Model) WithType(v interface{}) Model

WithType returns a new Model value with the Type value set to v.

type QueryCallback

type QueryCallback func(idxs *IndexSet, rname string, params map[string]string, query url.Values) (*IndexQuery, string, error)

QueryCallback is called for each query request. It returns an index query and a normalized query string, or an error.

If the normalized query string is empty, the initial query string is used as normalized query.

type QueryCollection

type QueryCollection struct {
	// BadgerDB middleware
	BadgerDB BadgerDB
	// IndexSet defines a set of indexes to be used with query requests.
	IndexSet *IndexSet
	// QueryCallback takes a query request and returns an IndexQuery used for searching.
	QueryCallback QueryCallback
}

QueryCollection represents a collection of indexed Models that may be queried.

func (QueryCollection) SetOption

func (o QueryCollection) SetOption(hs *res.Handler)

SetOption sets the res handler options, and implements the res.Option interface.

func (QueryCollection) WithIndexSet

func (o QueryCollection) WithIndexSet(idxs *IndexSet) QueryCollection

WithIndexSet returns a new QueryCollection value with the IndexSet set to idxs.

func (QueryCollection) WithQueryCallback

func (o QueryCollection) WithQueryCallback(callback QueryCallback) QueryCollection

WithQueryCallback returns a new QueryCollection value with the QueryCallback set to callback.

Jump to

Keyboard shortcuts

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