Documentation ¶
Index ¶
- type BadgerDB
- type Collection
- type Index
- type IndexQuery
- type IndexSet
- type Model
- func (o Model) RebuildIndexes(pattern string) error
- func (o Model) SetOption(hs *res.Handler)
- func (o Model) WithDefault(i interface{}) Model
- func (o Model) WithIndexSet(idxs *IndexSet) Model
- func (o Model) WithMap(m func(interface{}) (interface{}, error)) Model
- func (o Model) WithType(v interface{}) Model
- type QueryCallback
- type QueryCollection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BadgerDB ¶
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) QueryCollection ¶
func (o BadgerDB) QueryCollection() QueryCollection
QueryCollection returns a middleware builder of type QueryCollection.
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 ¶
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) Listen ¶
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
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 ¶
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 ¶
SetOption sets the res handler options, and implements the res.Option interface.
func (Model) WithDefault ¶
WithDefault returns a new BadgerDB value with the Default resource value set to i.
func (Model) WithIndexSet ¶
WithIndexSet returns a new Model value with the IndexSet set to idxs.
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.