baucis-decorator-init
v1.0.1
Published
Initializes certain properties upon creation of a resource. Different from `default` in that it is able to use the `req` object.
Downloads
6
Readme
baucis-decorator-init
Initializes certain properties upon creation of a resource. Different from default
in that it is able to use the req
object.
Install
npm install baucis-decorators baucis-decorator-init --save
Usage
Add the decorator and it will add the functionality to any properties containing an init
field as a function.
Example
controllers/Resource.js
var baucis = require('baucis');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var ResourceProps = {};
ResourceProps.editors = {
type: [{
type: ObjectId,
ref: 'User'
}],
init: function (req, res, next) {
return [req.session.userId];
}
};
var ResourceSchema = new Schema(ResourceProps);
var ResourceModel = mongoose.model('Resource', ResourceSchema);
var ResourceController = baucis.rest('Resource');
var decorators = require('baucis-decorators');
decorators.add.call(ResourceController, [
'baucis-decorator-init'
]);
/**
* Expose controller.
*/
module.exports = ResourceController;