Giter Club home page Giter Club logo

mongoose-revisionist's Introduction

Creates Mongo's oplog-like records for Mongoose operations.

Installation

Use npm: npm install mongoose-revisionist

Operation

Each creation, update or removal will create a Revisionist record with appropriate versioning information. This allows for easy tracking of changes to records through time as well as providing an auditing mechanism.

The Mongoose plugin adds a revision field of type Number to the target schema, which is autoincremented every save. The plugin will also add a collection of revisions that are close to (but not quite exactly) oplog records:

  • ts - Date - the timestamp of the operation, defaults to Date.now.
  • v - Number - the Mongoose-internal version of the document.
  • op - String - the operation type.
  • o - Mixed - depends on operation:
    • create - full account of what is inserted in _$set, ID inclusive.
    • update - full account of what is changed in _$set for changes and additions, _$unset for removals, ID exclusive.
    • remove - ID of the document only.
  • o2 - Mixed - depends on operation:
    • create - omitted as it is not needed
    • update - ID of the document being updated
    • remove - omitted as it is not needed
  • revision - what revision of the document the change was for.
  • referenceId - the ID of the document the change is for.

Usage

Use as you would any Mongoose plugin:

var mongoose = require('mongoose'),
    Revisionist = require('mongoose-revisionist'),
    schema = new mongoose.Schema({ ... });
    schema.plugin(Revisionist.plugin);

If more help is needed, have a look in tests/models.

Options:

The Revisionist plugin also takes an options hash using the plugin syntax:

schema.plugin(Revisionist.plugin, options);
  • modelName - the model name of where revisions get saved. Defaults to Revisionist.

Instance Methods

.versions(cb)

Gets the valid versions for a given Mongoose document. Calls back with an array of integers.

.getVersion([versionNumber|date|'current', ] cb)

Gets a specific version of a given Mongoose document. The first argument of the call can be a version number, a date (to get by date), "current" to get the current version, or not specified, also to get the current version. Calls back with the collapsed version as it would have appeared at that number/date.

.diff(versionNumber|date, versionNumber|date, cb)

Gets the difference in versions or between dates specified. Calls back with an object with three hashes: added, removed, updated. Each hash has member paths denoting they were changed, as well as what the value was changed from and to along with the version the change happened in. For example:

{ 
    added: {},
    updated: { 
        name: { 
            from: 'bar', 
            to: 'baz', 
            revision: 4 
        } 
    },
    removed: {}
}

... would denote that the name field was updated from bar to baz in the 4th revision.

Static Methods

Each of the above instance methods are also available as static methods on the Mongoose model. The only additional parameter required is a valid Mongo ID as the first parameter. For example, instance.diff() would have the signature Model.diff(id, versionNumber|date, versionNumber| date, cb).

mongoose-revisionist's People

Contributors

rosshinkley avatar

Stargazers

Alexandros Sigalas avatar Zen Li avatar Simon Carter avatar Nikolai Udalov avatar  avatar Paul Zerkel avatar

Watchers

James Cloos avatar  avatar Simon Carter avatar

mongoose-revisionist's Issues

getting error in pre save hook

Hi,

I am trying this in mongoose version: 4.2.3. I am getting error in following line:

var revisionistModel = connection.model(options.modelName, revisionist.schema);

Error:

TypeError: Undefined typeundefinedatpaths.ts Did you try nesting Schemas? You can only nest using refs or arrays.

Let me know if any other details are needed.

memory leak detected

Hey @rosshinkley,

I've found some error as your current version of mongoose-revisionist does not work.

(node) warning: possible EventEmitter memory leak detected. 11 uncaughtException listeners added. Use emitter.setMaxListeners() to increase limit.
Trace

    at process.addListener (events.js:239:17)
    at Object.module.exports.exports [as setup] (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/tests/util/setup.js:12:13)
    at Object.module.exports.exports.setUp (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/tests/methods/getVersionByDate.js:13:18)
    at /Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:260:35
    at /Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:458:21
    at /Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:163:13
    at iterate (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:123:13)
    at async.forEachSeries (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:139:9)
    at _asyncMap (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:162:9)
    at Object.mapSeries (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:152:23)
    at Object.async.series (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/deps/async.js:456:19)
    at Object.<anonymous> (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:264:22)
    at Object.<anonymous> (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:228:19)
    at Object.<anonymous> (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:236:16)
    at Object.<anonymous> (/Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:236:16)
    at /Users/bastianwegge/dev/prototypes/mongoose-revisionist/node_modules/nodeunit/lib/core.js:236:16

Any questions? Feel free to @ me ๐Ÿ‘

./audit required

I tried installing and running this as per your documentation but getting the following error:

Cannot find module './audit'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.