Simple crud service for object definition, validation, basic persistence and collection.
npm install crud-service
var CrudService = require('crud-service')
, save = require('save')
, schema = require('./schema')
, service = new Service('things', save('thing'), schema())
// service now has some slightly inflated CRUD functionality:
// .create() .read() .update() .partialUpdate()
// .delete() .deleteMany() .find() .count()
var service = new CrudService(String: propertyName, Save: collection, Schemata: schema, Object, options)
Create a new crud service that stores entites in the provided collection
. This should be
a save instance with your preferred engine. A
schemata schema is required for validation
service
has the the following CRUD-y methods:
All options
arguments are optional.
service
also has the following method:
This facilitates a pipeline for object manipulation before certain operations.
pre()
can be called multiple times for the same hook
and the processor functions
will be queued up. A processor function has the signature function (entity, cb) {}
,
and should callback with cb(err, entity)
.
A simple example is to maintain a lastUpdated
property on service objects:
function setUpdateTime(entity, cb) {
entity.lastUpdated = new Date()
cb(null, entity)
}
service.pre('update', setUpdateTime)
service.pre('partialUpdate', setUpdateTime)
The available pre hooks are:
create
- after validation, just before persistencecreateValidate
- before validationupdate
- after validation, just before persistenceupdateValidate
- before validationpartialUpdate
- after validation, just before persistencepartialValidate
- before validationdelete
- before deletion
Paul Serby follow me on twitter @serby
Licenced under the New BSD License