Skip to main content

Update

Update operation executes hook methods in the following sequence:

HookParametersDescription
docPermissions
  1. Mongoose document
  2. global permissions
  • called after Mongoose execute the query
validate
  1. allowed data object
  2. global permissions
  3. context object:
    • originalDocObject
    • originalData
    • currentDoc
    prepare
    1. allowed data object
    2. global permissions
    3. context object:
      • originalDocObject
      • originalData
      • currentDoc
      transform
      1. allowed data object
      2. global permissions
      3. context object:
        • originalDocObject
        • originalData
        • currentDoc
        • preparedData
        • modifiedPaths
      • called before changes saved
      docPermissions
      1. Mongoose document
      2. global permissions
      3. context object:
        • originalDocObject
        • originalData
        • currentDoc
        • preparedData
        • modifiedPaths
      • called after changes saved
      decorate
      1. plain document object
      2. global permissions
      3. context object:
        • originalDocObject
        • originalData
        • currentDoc
        • preparedData
        • modifiedPaths

        Configuration Example

        modelRouter.validate('update', function (dataObject, globalPermissions) {
        return true;
        });

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

        modelRouter.transform(function (doc, globalPermissions) {
        return doc;
        });

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

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

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