Dirac Middleware
Common express middleware for handling JSON responses for dirac queries. Calling app.use( dm() )
will put a dirac query and options object on each request. Each middleware function modifies those two objects. There are some generic route handlers that will then take the query object and options, pass it along to dirac, and then pass the data back to the client.
Usage
var dirac = ;var dm = ;var app = ; app; app
API
All middleware functions will accept a function as their last parameter to provide custom behavior. They all follow this pattern:
// The rest of the params to the function depend on the helper// they usually have something to do with the functionality of the helper itself// for instance, the param helper passes in the value of the parameter in the URIdm
root( [options] )
The exported value for this module is actually a middleware function to be used within the express app.use
function:
Example:
var dirac = ;var dm = ;var app = ; app;
Parameters:
* [options]
+ envelope - Whether or not the response should enveloped
e.g { data: [/*...*/] } rather than just: [/*...*/]
Default: false
dm.queryObj
The root
function is actually an alias for this function
dm.sort( [default_field], [custom_fn] )
Sort on a field.
Parameters:
* default_field [optional] - The default field to always be applied
* custom_fn [optional] - Custom behavior ( $query, options, sort )
Example:
-- name descending
/api/users?sort=-name
app; // To sort by name descending by defaultapp;
```dm.param( field, [default], [custom_fn] )
Map a query param field to a where condition.
Parameters:
* field [required] - Fieldname to map
* default [optional] - Default value for condition
* custom_fn [optional] - Custom behavior ( $query, options, field_value )
Example:
app; // To Custom condition behaviorapp;
dm.returning( fields, fieldB, fieldC, ... )
Defines what fields the query should return. Either pass in an array of fields or pass in each field individually as arguments.
Example:
app;
Generic Routes
Some of the functions in this module expect to be route handlers. Since we're building up these query objects that just end up passing the data straight from the DB to the client, it makes sense to create generic route handlers.
dm.view( view_name, [dirac_collection], [options] )
Parameters:
* view_name [required] - Name of the view to render
* dirac_collection [optional] - Reference to the dirac collection needing to perform
the query. If none is passed, no data extra data will
be passed to the view
* options [optional] - Object that will be passed to the render function. Can
also be used to specify which dirac.dal method to use
e.g. find, findOne, update, etc...
Example:
// Render user viewapp;
Special Options:
To override error
and not found
behavior:
app;