digger-supplier
A functional approach to REST api's.
A digger supplier is just a function - it accepts req and reply arguments.
req is a plain javascript object representing a HTTP request:
// a request represent a load of item 1234 method:'get' url:'/1234' headers: body:null
reply is a standard node.js callback.
installation
$ npm install digger-supplier --save
usage
A supplier has 4 distinct roles to play.
- select
- append
- save
- remove
These are conceptually mapped onto the REST methods: GET, POST, PUT, DELETE
A supplier is a digger-warehouse - which means you can mount middleware onto a supplier like any other warehouse.
var Supplier = ;var supplier = ; // mount a custom middleware onto the supplier// this is called before the database methodssupplier
The point of a supplier is to connect to a data source on the back and interpret our container queries upon that data.
routing
It is useful to have the same supplier object deal with different backend resources (like files or databases) based upon the route that is used.
Imagine we have a supplier mounted on '/csv' - it is a csv file supplier that has access to a directory '/tmp/mycsvsupplier'.
var folder = '/tmp/mycsvsupplier';var Supplier = ; // we create the supplier with an factory functionvar supplier = // we run the provision function for every request - it decides what specific database table/collection/file to use// based upon the route//// it is basically a renamed .usesupplier
Database Methods
You specify what happens in your supplier when a container is:
- searched for (select)
- added (append)
- updated (save)
- deleted (remove)
var Supplier = ;var supplier = ; /* search the database with a selector and context array */supplier /* add an item to the database */supplier supplier supplier