Middleware¶
Validate¶
Validate
hooks are called before a new/update document data is processed in prepare
hooks. This method is used to validate write data
and throw an error if not valid; available in create
and update
operations.
userRouter.validate({
create: function (docObject, permissions, context) {
// add create validate logic
const validated = validate(docObject);
return validated;
},
update: function (docObject, permissions, context) {
// add update validate logic
const validated = validate(docObject);
return validated;
},
});
or define individual hooks.
userRouter.validate('create', function (docObject, permissions, context) {
// add create validate logic
const validated = validate(docObject);
return validated;
});
Prepare¶
Prepare
hooks are called before a new document is created or an existing document is updated. This method is used to process raw data passed into the API endpoints; available in create
and update
operations.
userRouter.prepare({
create: function (docObject, permissions, context) {
// add create prepare logic
const processed = process(docObject);
return processed;
},
update: function (docObject, permissions, context) {
// add update prepare logic
const processed = process(docObject);
return processed;
},
});
or define individual hooks.
userRouter.prepare('create', function (docObject, permissions, context) {
// add create prepare logic
const processed = process(docObject);
return processed;
});
Transform¶
Transform
hook is called before an updated document is saved. This method is only available in update
operation.
userRouter.transform(function (doc, permissions, context) {
// add transform logic
const processed = process(doc);
return processed;
});
Decorate¶
Decorate
hooks are called before response data is sent. This method is used to process raw data to apply custom logic before sending the result; available in list
, read
, create
, update
operations.
userRouter.decorate({
list: function (docObject, permissions, context) {
// add list decorate logic
const processed = process(docObject);
return processed;
},
read: function (docObject, permissions, context) {
// add read decorate logic
const processed = process(docObject);
return processed;
},
create: function (docObject, permissions, context) {
// add create decorate logic
const processed = process(docObject);
return processed;
},
update: function (docObject, permissions, context) {
// add update decorate logic
const processed = process(docObject);
return processed;
},
});
or define individual hooks.
userRouter.decorate('list', function (docObject, permissions, context) {
// add list decorate logic
const processed = process(docObject);
return processed;
});
Decorate All¶
Decorate All
hooks are called before response data is sent and after decorate
middleware runs. This method is used to process and filter multiple document objects before sending the result; available in list
operations only.
userRouter.decorateAll(function (docObjects, permissions) {
// add process logic
const processed = process(docObjects);
return processed;
});