├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── lib ├── history-model.js └── mongoose-history.js ├── package.json └── test ├── config └── mongoose.js ├── diffonly.js ├── diffonly_custom_algo.js ├── hooks.js ├── metadata.js ├── mocha.opts ├── model.js └── model ├── post-another-conn.js ├── post-with-index.js ├── post.js ├── post_custom_diff.js ├── post_diff.js └── post_metadata.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .project 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | services: 3 | - mongodb 4 | addons: 5 | apt: 6 | sources: 7 | - mongodb-3.0-precise 8 | packages: 9 | - mongodb-org-server 10 | node_js: 11 | - "4" 12 | - "6" 13 | - "8" 14 | before_script: 15 | - npm install 16 | notifications: 17 | email: false 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Nassor Paulino da Silva 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mongoose History Plugin 2 | 3 | [![Build Status](https://travis-ci.org/nassor/mongoose-history.svg?branch=master)](https://travis-ci.org/nassor/mongoose-history) 4 | 5 | Keeps a history of all changes of a document. 6 | 7 | ## Installation 8 | 9 | ```bash 10 | npm install mongoose-history 11 | ``` 12 | 13 | Or add it to your package.json 14 | 15 | ## Usage 16 | 17 | For starting history of your collection, you need to simply add the mongoose-history plugin: 18 | 19 | ```javascript 20 | var mongoose = require('mongoose') 21 | , mongooseHistory = require('mongoose-history') 22 | , Schema = mongoose.Schema 23 | 24 | var Post = new Schema({ 25 | title: String 26 | , message: String 27 | , updated_for: String 28 | }) 29 | 30 | Post.plugin(mongooseHistory) 31 | ``` 32 | This will generate a log from al your changes on this schema. 33 | 34 | The plugin will create a new collection with format: originalCollectionName + **_history**, in example: __posts_history__. You can also change the name of the collection by setting the configuration customCollectionName: 35 | 36 | ```javascript 37 | var options = {customCollectionName: "post_hst"} 38 | Post.plugin(mongooseHistory, options) 39 | ``` 40 | 41 | The history documents have the format: 42 | 43 | ```javascript 44 | { 45 | _id: ObjectId, 46 | t: Date // when history was made 47 | o: "i" (insert) | "u" (update) | "r" (remove) // what happens with document 48 | d: { // changed document data 49 | _id: ObjectId 50 | , title: String 51 | , message: String 52 | , updated_for: String 53 | } 54 | } 55 | ``` 56 | 57 | ### Indexes 58 | To improve queries perfomance in history collection you can define indexes, for example: 59 | 60 | ```javascript 61 | var options = {indexes: [{'t': -1, 'd._id': 1}]}; 62 | Post.plugin(mongooseHistory, options) 63 | ``` 64 | 65 | ### Send history to another database 66 | You can keep your history collection far away from your primary database or replica set. This can be useful for improve the architecture of your system. 67 | 68 | Just create another connection to the new database and link the reference in __historyConnection__: 69 | 70 | ```javascript 71 | var secondConn = mongoose.createConnection('mongodb://localhost/another_conn'); 72 | var options = {historyConnection: secondConn} 73 | Post.plugin(mongooseHistory, options) 74 | ``` 75 | 76 | ### Store metadata 77 | If you need to store aditionnal data, use the ```metadata``` option 78 | It accepts a collection of objects. The parameters ```key``` and ```value``` are required. 79 | You can specify mongoose options using the parameter ```schema``` (defaults to ```{type: mongoose.Schema.Types.Mixed}```) 80 | ```value``` can be either a String (resolved from the updated object), or a function, sync or async 81 | 82 | ```javascript 83 | var options = { 84 | metadata: [ 85 | {key: 'title', value: 'title'}, 86 | {key: 'titleFunc', value: function(original, newObject){return newObject.title}}, 87 | {key: 'titleAsync', value: function(original, newObject, cb){cb(null, newObject.title)}} 88 | ] 89 | }; 90 | PostSchema.plugin(history,options); 91 | module.exports = mongoose.model('Post_meta', PostSchema); 92 | ``` 93 | 94 | ### Statics 95 | All modules with history plugin have following methods: 96 | 97 | #### Model.historyModel() 98 | Get History Model of Model; 99 | 100 | #### Model.clearHistory() 101 | Clear all History collection; 102 | 103 | ## Development 104 | 105 | ### Testing 106 | 107 | The tests run against a local mongodb installation and use the following databases: `mongoose-history-test` and `mongoose-history-test-second`. 108 | 109 | Custom connection uris can be provided via environment variables for e.g. using a username and password: 110 | ``` 111 | CONNECTION_URI='mongodb://username:password@localhost/mongoose-history-test' SECONDARY_CONNECTION_URI='mongodb://username:password@localhost/mongoose-history-test-second' mocha 112 | ``` 113 | 114 | ### In progress 115 | * Plugin rewriting. 116 | * update, findOneAndUpdate, findOneAndRemove support. 117 | 118 | ## TODO 119 | * **TTL documents** 120 | 121 | ## LICENSE 122 | 123 | Copyright (c) 2013-2016, Nassor Paulino da Silva 124 | All rights reserved. 125 | 126 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 127 | 128 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 129 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 130 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 131 | -------------------------------------------------------------------------------- /lib/history-model.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const mongoose = require('mongoose'); 4 | const historyModels = {}; 5 | 6 | /** 7 | * Create and cache a history mongoose model 8 | * @param {string} collectionName Name of history collection 9 | * @return {mongoose.Model} History Model 10 | */ 11 | module.exports.HistoryModel = function(collectionName, options) { 12 | const indexes = options && options.indexes; 13 | const historyConnection = options && options.historyConnection; 14 | const metadata = options && options.metadata; 15 | 16 | let schemaObject = { 17 | t: {type: Date, required: true}, 18 | o: {type: String, required: true}, 19 | d: {type: mongoose.Schema.Types.Mixed, required: true} 20 | } 21 | if (metadata){ 22 | metadata.forEach((m) =>{ 23 | schemaObject[m.key] = m.schema || {type: mongoose.Schema.Types.Mixed} 24 | }) 25 | } 26 | if (!(collectionName in historyModels)) { 27 | let schema = new mongoose.Schema(schemaObject, { id: true, versionKey: false }); 28 | 29 | if(indexes){ 30 | indexes.forEach(function(idx) { 31 | schema.index(idx); 32 | }); 33 | } 34 | 35 | if(historyConnection) { 36 | historyModels[collectionName] = historyConnection.model(collectionName, schema, collectionName); 37 | } else { 38 | historyModels[collectionName] = mongoose.model(collectionName, schema, collectionName); 39 | } 40 | 41 | } 42 | 43 | return historyModels[collectionName]; 44 | }; 45 | 46 | /** 47 | * Set name of history collection 48 | * @param {string} collectionName history collection name 49 | * @param {string} customCollectionName history collection name defined by user 50 | * @return {string} Collection name of history 51 | */ 52 | module.exports.historyCollectionName = function(collectionName, customCollectionName) { 53 | if(customCollectionName !== undefined) { 54 | return customCollectionName; 55 | } else { 56 | return collectionName + '_history'; 57 | } 58 | }; 59 | -------------------------------------------------------------------------------- /lib/mongoose-history.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const mongoose = require('mongoose'); 3 | const hm = require('./history-model'); 4 | const async = require('async') 5 | 6 | module.exports = function historyPlugin(schema, options) { 7 | const customCollectionName = options && options.customCollectionName; 8 | const customDiffAlgo = options && options.customDiffAlgo; 9 | const diffOnly = options && options.diffOnly; 10 | const metadata = options && options.metadata; 11 | 12 | // Clear all history collection from Schema 13 | schema.statics.historyModel = function() { 14 | return hm.HistoryModel(hm.historyCollectionName(this.collection.name, customCollectionName), options); 15 | }; 16 | 17 | // Clear all history documents from history collection 18 | schema.statics.clearHistory = function(callback) { 19 | const History = hm.HistoryModel(hm.historyCollectionName(this.collection.name, customCollectionName), options); 20 | History.remove({}, function(err) { 21 | callback(err); 22 | }); 23 | }; 24 | 25 | // Save original data 26 | schema.post( 'init', function() { 27 | if (diffOnly){ 28 | this._original = this.toObject(); 29 | } 30 | }); 31 | 32 | function setMetadata(original, d, historyDoc, callback){ 33 | async.each(metadata, (m, cb) => { 34 | if (typeof(m.value) === 'function'){ 35 | if (m.value.length === 3){ 36 | /** async function */ 37 | m.value(original, d, function(err, data){ 38 | if (err) cb(err); 39 | historyDoc[m.key] = data; 40 | cb(); 41 | }) 42 | } else { 43 | historyDoc[m.key] = m.value(original, d); 44 | cb(); 45 | } 46 | } else { 47 | historyDoc[ m.key] = d ? d[ m.value] : null; 48 | cb(); 49 | } 50 | }, callback) 51 | } 52 | 53 | 54 | // Create a copy when insert or update, or a diff log 55 | schema.pre('save', function(next) { 56 | let historyDoc = {}; 57 | 58 | if(diffOnly && !this.isNew) { 59 | var original = this._original; 60 | delete this._original; 61 | var d = this.toObject(); 62 | var diff = {}; 63 | diff['_id'] = d['_id']; 64 | for(var k in d){ 65 | if(customDiffAlgo) { 66 | var customDiff = customDiffAlgo(k, d[k], original[k]); 67 | if(customDiff) { 68 | diff[k] = customDiff.diff; 69 | } 70 | } else { 71 | if(String(d[k]) != String(original[k])){ 72 | diff[k] = d[k]; 73 | } 74 | } 75 | } 76 | 77 | historyDoc = createHistoryDoc(diff, 'u'); 78 | } else { 79 | var d = this.toObject(); 80 | let operation = this.isNew ? 'i' : 'u'; 81 | historyDoc = createHistoryDoc(d, operation); 82 | } 83 | 84 | saveHistoryModel(original, d, historyDoc, this.collection.name, next); 85 | }); 86 | 87 | // Listen on update 88 | schema.pre('update', function(next) { 89 | processUpdate.call(this, next); 90 | }); 91 | 92 | // Listen on updateOne 93 | schema.pre('updateOne', function (next) { 94 | processUpdate.call(this, next); 95 | }); 96 | 97 | // Listen on findOneAndUpdate 98 | schema.pre('findOneAndUpdate', function (next) { 99 | processUpdate.call(this, next); 100 | }); 101 | 102 | // Create a copy on remove 103 | schema.pre('remove', function(next) { 104 | let d = this.toObject(); 105 | let historyDoc = createHistoryDoc(d, 'r'); 106 | 107 | saveHistoryModel(this.toObject(), d, historyDoc, this.collection.name, next); 108 | }); 109 | 110 | // Create a copy on findOneAndRemove 111 | schema.post('findOneAndRemove', function (doc, next) { 112 | processRemove.call(this, doc, next); 113 | }); 114 | 115 | function createHistoryDoc(d, operation) { 116 | const { __v, ...doc } = d; 117 | 118 | let historyDoc = {}; 119 | historyDoc['t'] = new Date(); 120 | historyDoc['o'] = operation; 121 | historyDoc['d'] = doc; 122 | 123 | return historyDoc; 124 | } 125 | 126 | function saveHistoryModel(original, d, historyDoc, collectionName, next) { 127 | if (metadata) { 128 | setMetadata(original, d, historyDoc, (err) => { 129 | if (err) return next(err); 130 | let history = new hm.HistoryModel(hm.historyCollectionName(collectionName, customCollectionName), options)(historyDoc); 131 | history.save(next); 132 | }); 133 | } else { 134 | let history = new hm.HistoryModel(hm.historyCollectionName(collectionName, customCollectionName), options)(historyDoc); 135 | history.save(next); 136 | } 137 | } 138 | 139 | function processUpdate(next) { 140 | let d = this._update.$set || this._update; 141 | let historyDoc = createHistoryDoc(d, 'u'); 142 | 143 | saveHistoryModel(this.toObject, d, historyDoc, this.mongooseCollection.collectionName, next); 144 | } 145 | 146 | function processRemove(doc, next) { 147 | let d = doc.toObject(); 148 | let historyDoc = createHistoryDoc(d, 'r'); 149 | 150 | saveHistoryModel(this.toObject, d, historyDoc, this.mongooseCollection.collectionName, next); 151 | } 152 | 153 | }; 154 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mongoose-history", 3 | "version": "0.8.0", 4 | "description": "Keeps a history of all changes of a document.", 5 | "main": "lib/mongoose-history.js", 6 | "files": [ 7 | "lib/" 8 | ], 9 | "scripts": { 10 | "test": "mocha" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/nassor/mongoose-history" 15 | }, 16 | "keywords": [ 17 | "mongoose", 18 | "history", 19 | "safe", 20 | "cqrs", 21 | "backup" 22 | ], 23 | "author": { 24 | "name": "Nassor Paulino da Silva", 25 | "email": "nassor@gmail.com", 26 | "url": "https://github.com/nassor" 27 | }, 28 | "contributors": [ 29 | { 30 | "name": "Christopher Britz", 31 | "email": "britztopher@gmail.com", 32 | "url": "https://github.com/britztopher" 33 | }, 34 | { 35 | "name": "Allan Nienhuis", 36 | "url": "https://github.com/allannienhuis" 37 | }, 38 | { 39 | "name": "João Manoel Pampanini Filho", 40 | "email": "joao.bontorin@gmail.com", 41 | "url": "https://github.com/jmpf13" 42 | }, 43 | { 44 | "name": "Sebastien Vaucouleur", 45 | "url": "http://vaucouleur.com" 46 | }, 47 | { 48 | "name": "Victor Parmar", 49 | "url": "https://smalldata.tech" 50 | } 51 | ], 52 | "license": "BSD-2-Clause", 53 | "bugs": { 54 | "url": "https://github.com/nassor/mongoose-history/issues" 55 | }, 56 | "dependencies": {}, 57 | "devDependencies": { 58 | "async": "^2.6.0", 59 | "mongoose": "^5.0.0", 60 | "mocha": "2.4.5", 61 | "should": "8.2.2" 62 | }, 63 | "peerDependencies": { 64 | "async": "^2.0.0", 65 | "mongoose": "^4.0.0 || >=5.0.0" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /test/config/mongoose.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | 3 | var connectionUri = process.env.CONNECTION_URI || 'mongodb://localhost/mongoose-history-test'; 4 | mongoose.connect(connectionUri); 5 | 6 | var secondConnectionUri = process.env.SECONDARY_CONNECTION_URI || 'mongodb://localhost/mongoose-history-test-second'; 7 | var secondConn = mongoose.createConnection(secondConnectionUri); 8 | // mongoose.set('debug', true); 9 | 10 | after(function(done) { 11 | mongoose.connection.db.dropDatabase(); 12 | secondConn.db.dropDatabase(); 13 | done(); 14 | }); 15 | -------------------------------------------------------------------------------- /test/diffonly.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var should = require('should') 4 | , Post = require('./model/post_diff') 5 | , HistoryPost = Post.historyModel(); 6 | 7 | require('./config/mongoose'); 8 | 9 | describe('History plugin', function() { 10 | 11 | var post = null; 12 | 13 | function createAndUpdatePostWithHistory(post, callback) { 14 | post.save(function(err) { 15 | if(err) return callback(err); 16 | Post.findOne(function(err, post) { 17 | if(err) return callback(err); 18 | post.updatedFor = 'another_user@test.com'; 19 | post.title = "Title changed"; 20 | post.save(function(err) { 21 | if(err) return callback(err); 22 | HistoryPost.findOne({'d.title': 'Title changed'}, function(err, hpost) { 23 | callback(err, post, hpost); 24 | }); 25 | }); 26 | }); 27 | }); 28 | }; 29 | 30 | var post = null; 31 | 32 | beforeEach(function(done) { 33 | post = new Post({ 34 | updatedFor: 'mail@test.com', 35 | title: 'Title test', 36 | message: 'message lorem ipsum test' 37 | }); 38 | 39 | done(); 40 | }); 41 | 42 | afterEach(function(done) { 43 | Post.remove({}, function(err) { 44 | should.not.exists(err); 45 | Post.clearHistory(function(err) { 46 | should.not.exists(err); 47 | done(); 48 | }); 49 | }); 50 | }); 51 | 52 | it('should keep insert in history', function(done) { 53 | post.save(function(err) { 54 | should.not.exists(err); 55 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 56 | should.not.exists(err); 57 | hpost.o.should.eql('i'); 58 | post.should.have.property('updatedFor', hpost.d.updatedFor); 59 | post.title.should.be.equal(hpost.d.title); 60 | post.should.have.property('message', hpost.d.message); 61 | done(); 62 | }); 63 | }); 64 | }); 65 | 66 | it('should keep just wath changed in update', function(done) { 67 | createAndUpdatePostWithHistory(post, function(err, post, hpost) { 68 | should.not.exists(err); 69 | hpost.o.should.eql('u'); 70 | //post.updatedFor.should.be.equal(hpost.d.updatedFor); 71 | //post.title.should.be.equal(hpost.d.title); 72 | //post.message.should.be.equal(hpost.d.message); 73 | should.not.exists(hpost.d.message); 74 | should.not.exists(hpost.d._v); 75 | //hpost.d.should.not.exist(post.message); 76 | //hpost.d.should.not.exist(post._v); 77 | done(); 78 | }); 79 | }); 80 | 81 | it('should keep remove in history', function(done) { 82 | createAndUpdatePostWithHistory(post, function(err, post, hpost) { 83 | should.not.exists(err); 84 | post.remove(function(err) { 85 | should.not.exists(err); 86 | HistoryPost.find({o: 'r'}).select('d').exec(function(err, historyWithRemove) { 87 | historyWithRemove.should.not.be.empty; 88 | done(); 89 | }); 90 | }); 91 | }); 92 | }); 93 | }); 94 | -------------------------------------------------------------------------------- /test/diffonly_custom_algo.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var should = require('should'), 4 | Post = require('./model/post_custom_diff'), 5 | HistoryPost = Post.historyModel(); 6 | 7 | require('./config/mongoose'); 8 | 9 | describe('History plugin custom diff algo', function() { 10 | 11 | var post = null; 12 | 13 | function createAndUpdatePostWithHistory(post, newTags, callback) { 14 | post.save(function(err) { 15 | if(err) return callback(err); 16 | Post.findOne(function(err, post) { 17 | if(err) return callback(err); 18 | post.updatedFor = 'another_user@test.com'; 19 | post.title = "Title changed"; 20 | post.tags = newTags; 21 | post.save(function(err) { 22 | if(err) return callback(err); 23 | HistoryPost.findOne({'d.title': 'Title changed'}, function(err, hpost) { 24 | should.exists(hpost); 25 | callback(err, post, hpost); 26 | }); 27 | }); 28 | }); 29 | }); 30 | }; 31 | 32 | var post = null; 33 | var defaultTags = ['Brazil', 'France']; 34 | 35 | beforeEach(function(done) { 36 | post = new Post({ 37 | updatedFor: 'mail@test.com', 38 | title: 'Title test', 39 | message: 'message lorem ipsum test', 40 | tags: defaultTags 41 | }); 42 | 43 | done(); 44 | }); 45 | 46 | afterEach(function(done) { 47 | Post.remove({}, function(err) { 48 | should.not.exists(err); 49 | Post.clearHistory(function(err) { 50 | should.not.exists(err); 51 | done(); 52 | }); 53 | }); 54 | }); 55 | 56 | 57 | it('should keep insert in history', function(done) { 58 | post.save(function(err) { 59 | should.not.exists(err); 60 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 61 | should.not.exists(err); 62 | should.exists(hpost); 63 | hpost.o.should.eql('i'); 64 | post.should.have.property('updatedFor', hpost.d.updatedFor); 65 | post.title.should.be.equal(hpost.d.title); 66 | post.should.have.property('message', hpost.d.message); 67 | done(); 68 | }); 69 | }); 70 | }); 71 | 72 | it('should not care about order of tags', function(done) { 73 | should.exists(post); 74 | var newTags = ['France', 'Brazil']; 75 | createAndUpdatePostWithHistory(post, newTags, function(err, post, hpost) { 76 | should.not.exists(err); 77 | should.exists(hpost); 78 | hpost.o.should.eql('u'); 79 | should.not.exists(hpost.d.message); 80 | should.not.exists(hpost.d._v); 81 | should.not.exists(hpost.d.tags); 82 | done(); 83 | }); 84 | }); 85 | 86 | it('should detect null tags', function(done) { 87 | should.exists(post); 88 | var newTags = null; 89 | createAndUpdatePostWithHistory(post, newTags, function(err, post, hpost) { 90 | should.not.exists(err); 91 | should.exists(hpost); 92 | hpost.o.should.eql('u'); 93 | should.not.exists(hpost.d.message); 94 | should.not.exists(hpost.d._v); 95 | should(hpost.d.tags).be.null(); 96 | //console.log('%j', hpost); 97 | done(); 98 | }); 99 | }); 100 | 101 | it('should detect new tags', function(done) { 102 | should.exists(post); 103 | var newTags = ['Brazil', 'France', 'Australia']; 104 | createAndUpdatePostWithHistory(post, newTags, function(err, post, hpost) { 105 | should.not.exists(err); 106 | should.exists(hpost); 107 | hpost.o.should.eql('u'); 108 | should.not.exists(hpost.d.message); 109 | should.not.exists(hpost.d._v); 110 | should.exists(hpost.d.tags); 111 | hpost.d.tags.should.be.instanceof(Array).and.have.lengthOf(3); 112 | hpost.d.tags.should.containEql('Brazil'); 113 | hpost.d.tags.should.containEql('France'); 114 | hpost.d.tags.should.containEql('Australia'); 115 | done(); 116 | }); 117 | }); 118 | 119 | it('should detect removed tags', function(done) { 120 | should.exists(post); 121 | var newTags = ['Brazil']; 122 | createAndUpdatePostWithHistory(post, newTags, function(err, post, hpost) { 123 | should.not.exists(err); 124 | should.exists(hpost); 125 | hpost.o.should.eql('u'); 126 | should.not.exists(hpost.d.message); 127 | should.not.exists(hpost.d._v); 128 | should.exists(hpost.d.tags); 129 | hpost.d.tags.should.be.instanceof(Array).and.have.lengthOf(1); 130 | hpost.d.tags.should.containEql('Brazil'); 131 | done(); 132 | }); 133 | }); 134 | 135 | it('should keep just what changed in update', function(done) { 136 | should.exists(post); 137 | createAndUpdatePostWithHistory(post, defaultTags, function(err, post, hpost) { 138 | should.not.exists(err); 139 | should.exists(hpost); 140 | hpost.o.should.eql('u'); 141 | should.not.exists(hpost.d.message); 142 | should.not.exists(hpost.d.tags); 143 | should.not.exists(hpost.d._v); 144 | done(); 145 | }); 146 | }); 147 | 148 | it('should keep remove in history', function(done) { 149 | should.exists(post); 150 | createAndUpdatePostWithHistory(post, defaultTags, function(err, post, hpost) { 151 | should.not.exists(err); 152 | should.exists(post); 153 | post.remove(function(err) { 154 | should.not.exists(err); 155 | HistoryPost.find({o: 'r'}).select('d').exec(function(err, historyWithRemove) { 156 | historyWithRemove.should.not.be.empty; 157 | done(); 158 | }); 159 | }); 160 | }); 161 | }); 162 | }); 163 | -------------------------------------------------------------------------------- /test/hooks.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var should = require('should') 4 | , Post = require('./model/post') 5 | , HistoryPost = Post.historyModel(); 6 | 7 | require('./config/mongoose'); 8 | 9 | describe('History plugin', function() { 10 | 11 | var post = null; 12 | 13 | function createAndUpdatePostWithHistory(post, callback) { 14 | post.save(function(err) { 15 | if(err) return callback(err); 16 | Post.findOne(function(err, post) { 17 | if(err) return callback(err); 18 | post.updatedFor = 'another_user@test.com'; 19 | post.title = "Title changed"; 20 | post.save(function(err) { 21 | if(err) return callback(err); 22 | HistoryPost.findOne({'d.title': 'Title changed'}, function(err, hpost) { 23 | callback(err, post, hpost); 24 | }); 25 | }); 26 | }); 27 | }); 28 | }; 29 | 30 | function updatePostWithHistory(post, callback) { 31 | post.save(function(err) { 32 | if(err) return callback(err); 33 | 34 | post.title = 'Updated title'; 35 | 36 | Post.update({title: 'Title test'}, post, function(err){ 37 | if(err) return callback(err); 38 | HistoryPost.findOne({'d.title': 'Updated title'}, function(err, hpost) { 39 | callback(err, post, hpost); 40 | }); 41 | }); 42 | }); 43 | }; 44 | 45 | function updateOnePostWithHistory(post, callback) { 46 | post.save(function (err) { 47 | if (err) return callback(err); 48 | 49 | post.title = 'Updated title'; 50 | 51 | Post.updateOne({title: 'Title test'}, post, function (err) { 52 | if (err) return callback(err); 53 | HistoryPost.findOne({'d.title': 'Updated title'}, function (err, hpost) { 54 | callback(err, post, hpost); 55 | }); 56 | }); 57 | }); 58 | }; 59 | 60 | function findOneAndUpdatePostWithHistory(post, callback) { 61 | post.save(function (err) { 62 | if (err) return callback(err); 63 | 64 | post.title = 'Updated title'; 65 | 66 | Post.findOneAndUpdate({title: 'Title test'}, post, function (err) { 67 | if (err) return callback(err); 68 | HistoryPost.findOne({'d.title': 'Updated title'}, function (err, hpost) { 69 | callback(err, post, hpost); 70 | }); 71 | }); 72 | }); 73 | }; 74 | 75 | var post = null; 76 | 77 | beforeEach(function(done) { 78 | post = new Post({ 79 | updatedFor: 'mail@test.com', 80 | title: 'Title test', 81 | message: 'message lorem ipsum test' 82 | }); 83 | 84 | done(); 85 | }); 86 | 87 | afterEach(function(done) { 88 | Post.remove({}, function(err) { 89 | should.not.exists(err); 90 | Post.clearHistory(function(err) { 91 | should.not.exists(err); 92 | done(); 93 | }); 94 | }); 95 | }); 96 | 97 | it('should keep insert in history', function(done) { 98 | post.save(function(err) { 99 | should.not.exists(err); 100 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 101 | should.not.exists(err); 102 | hpost.o.should.eql('i'); 103 | post.should.have.property('updatedFor', hpost.d.updatedFor); 104 | post.title.should.be.equal(hpost.d.title); 105 | post.should.have.property('message', hpost.d.message); 106 | done(); 107 | }); 108 | }); 109 | }); 110 | 111 | it('should keep update in history', function(done) { 112 | createAndUpdatePostWithHistory(post, function(err, post, hpost) { 113 | should.not.exists(err); 114 | hpost.o.should.eql('u'); 115 | post.updatedFor.should.be.equal(hpost.d.updatedFor); 116 | post.title.should.be.equal(hpost.d.title); 117 | post.message.should.be.equal(hpost.d.message); 118 | done(); 119 | }); 120 | }); 121 | 122 | it('should keep update on Model in history', function(done) { 123 | updatePostWithHistory(post, function (err, post, hpost) { 124 | should.not.exists(err); 125 | hpost.o.should.eql('u'); 126 | post.updatedFor.should.be.equal(hpost.d.updatedFor); 127 | post.title.should.be.equal(hpost.d.title); 128 | post.message.should.be.equal(hpost.d.message); 129 | done(); 130 | }) 131 | }); 132 | 133 | it('should keep update on Model in history using updateOne()', function (done) { 134 | updateOnePostWithHistory(post, function (err, post, hpost) { 135 | should.not.exists(err); 136 | hpost.o.should.eql('u'); 137 | post.updatedFor.should.be.equal(hpost.d.updatedFor); 138 | post.title.should.be.equal(hpost.d.title); 139 | post.message.should.be.equal(hpost.d.message); 140 | done(); 141 | }) 142 | }); 143 | 144 | it('should keep update on Model in history using findOneAndUpdate()', function (done) { 145 | findOneAndUpdatePostWithHistory(post, function (err, post, hpost) { 146 | should.not.exists(err); 147 | hpost.o.should.eql('u'); 148 | post.updatedFor.should.be.equal(hpost.d.updatedFor); 149 | post.title.should.be.equal(hpost.d.title); 150 | post.message.should.be.equal(hpost.d.message); 151 | done(); 152 | }) 153 | }); 154 | 155 | it('should keep remove in history', function(done) { 156 | createAndUpdatePostWithHistory(post, function(err, post, hpost) { 157 | should.not.exists(err); 158 | post.remove(function(err) { 159 | should.not.exists(err); 160 | HistoryPost.find({o: 'r'}).select('d').exec(function(err, historyWithRemove) { 161 | historyWithRemove.should.not.be.empty; 162 | done(); 163 | }); 164 | }); 165 | }); 166 | }); 167 | 168 | it('should keep remove in history using findOneAndRemove()', function (done) { 169 | createAndUpdatePostWithHistory(post, function (err, post, hpost) { 170 | should.not.exists(err); 171 | Post.findOneAndRemove({title: post.title}, function (err, doc) { 172 | should.not.exists(err); 173 | HistoryPost.find({o: 'r'}).select('d').exec(function (err, historyWithRemove) { 174 | historyWithRemove.should.not.be.empty; 175 | historyWithRemove.should.be.instanceOf(Array).and.have.lengthOf(1); 176 | done(); 177 | }); 178 | }); 179 | }); 180 | }); 181 | 182 | }); -------------------------------------------------------------------------------- /test/metadata.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var should = require('should') 4 | , Post = require('./model/post_metadata') 5 | , HistoryPost = Post.historyModel(); 6 | 7 | require('./config/mongoose'); 8 | 9 | describe('History plugin with Metadata', function() { 10 | 11 | var post = null; 12 | 13 | function createAndUpdatePostWithHistory(post, callback) { 14 | post.save(function(err) { 15 | if(err) return callback(err); 16 | Post.findOne(function(err, post) { 17 | if(err) return callback(err); 18 | post.updatedFor = 'another_user@test.com'; 19 | post.title = "Title changed"; 20 | post.save(function(err) { 21 | if(err) return callback(err); 22 | HistoryPost.findOne({'d.title': 'Title changed'}, function(err, hpost) { 23 | callback(err, post, hpost); 24 | }); 25 | }); 26 | }); 27 | }); 28 | }; 29 | 30 | var post = null; 31 | 32 | beforeEach(function(done) { 33 | post = new Post({ 34 | updatedFor: 'mail@test.com', 35 | title: 'Title test', 36 | message: 'message lorem ipsum test' 37 | }); 38 | 39 | done(); 40 | }); 41 | 42 | afterEach(function(done) { 43 | Post.remove({}, function(err) { 44 | should.not.exists(err); 45 | Post.clearHistory(function(err) { 46 | should.not.exists(err); 47 | done(); 48 | }); 49 | }); 50 | }); 51 | 52 | it('should set simple field', function(done) { 53 | post.save(function(err) { 54 | should.not.exists(err); 55 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 56 | should.not.exists(err); 57 | hpost.title.should.eql('Title test'); 58 | done(); 59 | }); 60 | }); 61 | }); 62 | 63 | it('should set function field', function(done) { 64 | post.save(function(err) { 65 | should.not.exists(err); 66 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 67 | should.not.exists(err); 68 | hpost.titleFunc.should.eql('Title test'); 69 | done(); 70 | }); 71 | }); 72 | }); 73 | 74 | it('should set async field', function(done) { 75 | post.save(function(err) { 76 | should.not.exists(err); 77 | HistoryPost.findOne({'d.title': 'Title test'}, function(err, hpost) { 78 | should.not.exists(err); 79 | hpost.titleAsync.should.eql('Title test'); 80 | done(); 81 | }); 82 | }); 83 | }); 84 | 85 | }); 86 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require should 2 | -R spec -------------------------------------------------------------------------------- /test/model.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var secondConnectionUri = process.env.SECONDARY_CONNECTION_URI || 'mongodb://localhost/mongoose-history-test-second'; 4 | 5 | var should = require('should') 6 | , hm = require('../lib/history-model') 7 | , Post = require('./model/post-with-index') 8 | , PostAnotherConn = require('./model/post-another-conn') 9 | , PostMetadata = require('./model/post_metadata') 10 | , secondConn = require('mongoose').createConnection(secondConnectionUri); 11 | 12 | require('./config/mongoose'); 13 | 14 | describe('History Model', function() { 15 | describe('historyCoolectionName', function() { 16 | it('should set a collection name', function(done) { 17 | var collectionName = hm.historyCollectionName('original_collection_name', 'defined_by_user_history_collection_name'); 18 | collectionName.should.eql("defined_by_user_history_collection_name"); 19 | done(); 20 | }); 21 | 22 | it('should suffix collection name with \'_history\' by default', function(done) { 23 | var collectionName = hm.historyCollectionName('original_collection_name'); 24 | collectionName.should.eql("original_collection_name_history"); 25 | done(); 26 | }); 27 | }); 28 | 29 | it('should require t(timestamp), o(operation) fields and d(document) field', function(done) { 30 | var HistoryPost = Post.historyModel(); 31 | var history = new HistoryPost(); 32 | history.save(function(err) { 33 | should.exists(err); 34 | history.t = new Date(); 35 | history.save(function(err) { 36 | should.exists(err); 37 | history.o = 'i'; 38 | history.save(function(err) { 39 | should.exists(err); 40 | history.d = {a: 1}; 41 | history.save(function(err) { 42 | should.not.exists(err); 43 | done(); 44 | }); 45 | }); 46 | }); 47 | }); 48 | }); 49 | 50 | it('could have own indexes', function(done) { 51 | var HistoryPost = Post.historyModel(); 52 | HistoryPost.collection.indexInformation({full:true}, function(err, idxInformation) { 53 | 't_1_d._id_1'.should.eql(idxInformation[1].name); 54 | done(); 55 | }); 56 | }); 57 | 58 | it('could have another connection', function(done) { 59 | var post = new PostAnotherConn({ 60 | updatedFor: 'mail@test.com', 61 | title: 'Title test', 62 | message: 'message lorem ipsum test' 63 | }); 64 | 65 | post.save(function(err) { 66 | should.not.exists(err); 67 | secondConn.db.collection('posts_another_conn_history', function(err, hposts) { 68 | should.not.exists(err); 69 | hposts.findOne(function(err, hpost) { 70 | post.should.have.property('updatedFor', hpost.d.updatedFor); 71 | post.title.should.be.equal(hpost.d.title); 72 | post.should.have.property('message', hpost.d.message); 73 | done(); 74 | }); 75 | }); 76 | }); 77 | }); 78 | 79 | it ('could have additionnal metadata fields', function(){ 80 | var HistoryPost = PostMetadata.historyModel(); 81 | HistoryPost.schema.paths.should.have.property('title') 82 | }) 83 | 84 | 85 | }); -------------------------------------------------------------------------------- /test/model/post-another-conn.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose') 2 | , Schema = mongoose.Schema 3 | , history = require('../../lib/mongoose-history'); 4 | 5 | var secondConnectionUri = process.env.SECONDARY_CONNECTION_URI || 'mongodb://localhost/mongoose-history-test-second'; 6 | var secondConn = mongoose.createConnection(secondConnectionUri); 7 | 8 | var PostSchema = new Schema({ 9 | updatedFor: String 10 | , title: String 11 | , message: String 12 | }); 13 | 14 | PostSchema.plugin(history, {historyConnection: secondConn, customCollectionName: 'posts_another_conn_history'}); 15 | 16 | module.exports = mongoose.model('PostAnotherConn', PostSchema); -------------------------------------------------------------------------------- /test/model/post-with-index.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose') 2 | , Schema = mongoose.Schema 3 | , history = require('../../lib/mongoose-history'); 4 | 5 | var PostSchema = new Schema({ 6 | updatedFor: String 7 | , title: String 8 | , message: String 9 | }); 10 | 11 | PostSchema.plugin(history, {indexes: [{'t': 1, 'd._id': 1}], customCollectionName: 'posts_idx_history'}); 12 | 13 | module.exports = mongoose.model('PostWithIdx', PostSchema); -------------------------------------------------------------------------------- /test/model/post.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose') 2 | , Schema = mongoose.Schema 3 | , history = require('../../lib/mongoose-history'); 4 | 5 | var PostSchema = new Schema({ 6 | updatedFor: String 7 | , title: String 8 | , message: String 9 | }); 10 | PostSchema.plugin(history); 11 | 12 | module.exports = mongoose.model('Post', PostSchema); -------------------------------------------------------------------------------- /test/model/post_custom_diff.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'), 2 | Schema = mongoose.Schema, 3 | history = require('../../lib/mongoose-history'); 4 | 5 | var PostSchema = new Schema({ 6 | updatedFor: String, 7 | title: String, 8 | tags: [], 9 | message: String 10 | }); 11 | 12 | 13 | var sortIfArray = function(a) { 14 | if(Array.isArray(a)) { 15 | return a.sort(); 16 | } 17 | return a; 18 | } 19 | 20 | var options = { 21 | diffOnly: true, 22 | customDiffAlgo: function(key, newValue, oldValue) { 23 | var v1 = sortIfArray(oldValue); 24 | var v2 = sortIfArray(newValue); 25 | if(String(v1) != String(v2)){ 26 | return { 27 | diff: newValue 28 | }; 29 | } 30 | // no diff should be recorded for this key 31 | return null; 32 | } 33 | }; 34 | 35 | PostSchema.plugin(history,options); 36 | module.exports = mongoose.model('Post_custom_diff', PostSchema); 37 | -------------------------------------------------------------------------------- /test/model/post_diff.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose') 2 | , Schema = mongoose.Schema 3 | , history = require('../../lib/mongoose-history'); 4 | 5 | var PostSchema = new Schema({ 6 | updatedFor: String 7 | , title: String 8 | , message: String 9 | }); 10 | 11 | var options = {diffOnly: true}; 12 | PostSchema.plugin(history,options); 13 | 14 | module.exports = mongoose.model('Post_metadata', PostSchema); 15 | -------------------------------------------------------------------------------- /test/model/post_metadata.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose') 2 | , Schema = mongoose.Schema 3 | , history = require('../../lib/mongoose-history'); 4 | 5 | var PostSchema = new Schema({ 6 | updatedFor: String 7 | , title: String 8 | , message: String 9 | }); 10 | 11 | var options = { 12 | metadata: [ 13 | {key: 'title', value: 'title'}, 14 | {key: 'titleFunc', value: function(origin, d){return d.title}}, 15 | {key: 'titleAsync', value: function(original, d, cb){cb(null, d.title)}} 16 | ] 17 | }; 18 | PostSchema.plugin(history,options); 19 | 20 | module.exports = mongoose.model('Post_meta', PostSchema); 21 | --------------------------------------------------------------------------------