Skip to main content

Create

Create operation executes hook methods in the following sequence:

HookParametersDescription
validate
  1. allowed data object
  2. global permissions
  3. context object:
    • originalData
    prepare
    1. allowed data object
    2. global permissions
    3. context object:
      • originalData
      docPermissions
      1. Mongoose document
      2. global permissions
      3. context object:
        • originalData
        • preparedData
      • skips if option includePermissions set to false
      • called after a Mongoose document created
      decorate
      1. plain document object
      2. global permissions
      3. context object:
        • originalData
        • preparedData

        Configuration Example

        modelRouter.validate('create', function (dataObject, globalPermissions) {
        const hasName = !!dataObject.name;
        return hasName;
        });

        modelRouter.prepare('create', function (dataObject, globalPermissions) {
        dataObject.name = dataObject.name.trim();
        return dataObject;
        });

        modelRouter.docPermissions('create', function (doc, globalPermissions) {
        const isMe = String(doc._id) === String(this.user._id);

        return {
        'edit.name': globalPermissions.isAdmin || isMe,
        'edit.role': globalPermissions.isAdmin,
        };
        });

        modelRouter.decorate('create', function (docObject, globalPermissions) {
        docObject.decoratedBy = 'egose';
        return docObject;
        });