Documentation ¶
Overview ¶
Package middleware provides middleware for the res package:
https://github.com/jirenius/go-res
Middleware can be used for adding handler functions to a res.Handler, to perform tasks such as:
* storing, loading and updating persisted data * synchronize changes between multiple service instances * add additional logging * provide helpers for complex live queries
Currently, only the BadgerDB middleware is created, to demonstrate database persistance.
Usage ¶
Add middleware to a resource:
s.Handle("user.$id", middlware.BadgerDB{DB: db}, )
Index ¶
Examples ¶
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 // 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(). Type interface{} }
BadgerDB provides persistance 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.
Example ¶
package main import ( "github.com/dgraph-io/badger" res "github.com/jirenius/go-res" "github.com/jirenius/go-res/middleware" ) func main() { db := &badger.DB{} // Dummy. Use badger.Open s := res.NewService("directory") s.Handle("user.$id", res.Model, middleware.BadgerDB{DB: db}, /* ... */ ) }
Output:
func (BadgerDB) WithDefault ¶
WithDefault returns a new BadgerDB value with the Default resource value set to i.
Example ¶
package main import ( "github.com/dgraph-io/badger" res "github.com/jirenius/go-res" "github.com/jirenius/go-res/middleware" ) func main() { db := &badger.DB{} // Dummy. Use badger.Open s := res.NewService("directory") badgerDB := middleware.BadgerDB{DB: db} s.Handle("users", res.Collection, // Default to an empty slice of references badgerDB.WithType([]res.Ref{}).WithDefault([]res.Ref{}), /* ... */ ) }
Output:
func (BadgerDB) WithType ¶
WithType returns a new BadgerDB value with the Type value set to v.
Example ¶
package main import ( "github.com/dgraph-io/badger" res "github.com/jirenius/go-res" "github.com/jirenius/go-res/middleware" ) func main() { db := &badger.DB{} // Dummy. Use badger.Open type User struct { ID int `json:"id"` Name string `json:"name"` } s := res.NewService("directory") badgerDB := middleware.BadgerDB{DB: db} s.Handle("user.$id", res.Model, badgerDB.WithType(User{}), res.Set(func(r res.CallRequest) { _ = r.RequireValue().(User) /* ... */ r.OK(nil) }), ) }
Output: