UNPKG

mongoose-plugin-diff

Version:

Mongoose.js plugin report document modification differences.

67 lines (53 loc) 2.08 kB
'use strict'; const _ = require('lodash'); /** * @module mongoose-plugin-diff * @example ```js const diffPlugin = require('mongoose-plugin-diff'); const schema = Schema({...}); schema.plugin(diffPlugin[, OPTIONS]); ``` */ module.exports = diffPlugin; function diffPlugin(schema, options) { /** * @param {object} [options] * @param {string} [options.optionKey=diff] - the path options key to mark paths for inclusion in monitoring for modification. If no paths are provided or tagged, document modification is monitored. * @param {array} [options.paths] - the paths paths for monitoring for modification. If no paths are provided or tagged, document modification is monitored. * @param {string} [options.snapShotPath=__snapShot] - the path to store snap shot properties for capturing original values. * @param {string} [options.methodName=getDiff] - the method name for creating an object with the original values for modified properties. */ options = _.merge({ optionKey: 'diff', paths: undefined, snapShotPath: '__snapShot', methodName: 'getDiff' }, options); const paths = options.paths || Object.keys(schema.paths).filter(path => { const schemaType = schema.path(path); return _.get(schemaType, `options.${options.optionKey}`); }); const setSnapShotFn = _.partial(setSnapShot, options.snapShotPath, paths); schema.post('init', setSnapShotFn); schema.post('save', setSnapShotFn); const getDiffFn = _.partial(getDiff, options.snapShotPath, paths); schema.method(options.methodName, getDiffFn); } function setSnapShot(snapShotPath, paths, doc) { let snapShot = doc.toObject({ depopulate: true }); if (paths.length) { snapShot = _.pick(snapShot, paths); } _.set(doc, snapShotPath, snapShot); } function getDiff(snapShotPath, paths) { const doc = this; const modifiedPaths = paths.length ? paths.filter(path => doc.isModified(path)) : doc.modifiedPaths().filter(modPath => doc.isDirectModified(modPath)); return _.pick( _.get(doc, snapShotPath, {}), modifiedPaths ); }