sails-nested-blueprint
As of sails 1.0, blueprints no longer support nested creates (passing along associated models during creates). This library brings it back in a non-obstrusive way.
Usage
Blueprint (Easiest)
The blueprint will automatically figure out which model to use just like sails blueprints.
blueprint
exposes a create
, a destroy
, an update
and a count
, so just do this in a controller method:
let blueprint = moduleexports = blueprint ### Blueprint with Works just like the regular blueprint example howerver allows passing an object with options for each of the blueprint methodsCurrently only `delete` supports options such as `cascade` causes any associated record passed in with the main record to be deleted same as `serviceFromReq(req).destroyNested`and `soft` keeps the record but sets a flag `IsDeleted` to true on the affected records ### "Soft Delete" SetupIn order for soft delete to work you have to add a boolean attribute to the model definition or in /config/models ```jslet {blueprint} = require('sails-nested-blueprint')// model using these blueprints will have its `isDeleted` attribute set to true instead of being deleted from the data store.// If associated models are passed with the main record, they will also be soft-deleted since cascade is set to truemodule.exports = blueprintOptions({destroy: {soft: true, cascade: true}})
The count
endpoint allows you to reach to /[model]/count
with a
query to retrieve the number of found elements, instead of the full
JSON object.
Cached Methods
We currently provide a couple of methods that make use of a simple but
extensible caching mechanism. The methods are find
and update
, and
to be able to use them, you'll need to provide a cache
object in the
configuration options passed to
blueprintOptions
. The cache
configuration object will have: a
provider
object containing a get
, a set
, a del
and a keys
functions,
and a prefix
alongsides the provider. See
the following code as an example of how to use these methods on a
Sails controller:
let sails = let redis = let Promise = PromisePromise let client = redislet expiration = 60 // a minutelet keysExpiration = 60 * 30 // 30 minuteslet prefix = 'my-cache' let blueprint = moduleexportsfind = blueprintfindmoduleexportsupdate = blueprintupdate
NOTE caching can be bypassed by returning false on an optional
a keygen
property passed to the options of blueprintOptions
.
This function receives the request object, so it can be personalized
for anything request-related.
Service
You can also use the service directly if you need to perform a nested
create (or destroy) as part of some other operation.
On the service, these functions are called createNested
and destroyNested
.
To perform a soft delete with a service use destroy({soft: true}, record)
or destroySoft(record)
or combine both nested and soft deletion destroyNestedSoft(record)
let service = let sails = let SomeOperation = async { let payload = await // Returns 201 if successful }
You can also use the helper utilities directly in a controller method to recreate what blueprint
does:
let serviceFromReq service = moduleexports = create: async res statusawait
It is much nicer if you use sails-async
- see the index.js source for the implementation