Skip to content

Commit

Permalink
Add initOriginals workaround, update travis
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank committed Jun 14, 2017
1 parent 3cb88e8 commit e28889d
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 125 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
language: node_js
services: mongodb
addons:
code_climate:
repo_token: 5311945305435ff955a363abf213e2ba6b384f183cf49b5adaacb268fdf8038c
node_js:
- 6
cache:
directories:
- node_modules
script:
- yarn run coverage
after_success:
- npm install -g codeclimate-test-reporter
- codeclimate-test-reporter < coverage/lcov.info
deploy:
provider: npm
email: [email protected]
Expand Down
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2016-2017 Enhancv

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Mongoose Originals
==================
[![Build Status](https://travis-ci.org/enhancv/mongoose-originals.svg?branch=master)](https://travis-ci.org/enhancv/mongoose-originals)
[![Code Climate](https://codeclimate.com/github/enhancv/mongoose-originals/badges/gpa.svg)](https://codeclimate.com/github/enhancv/mongoose-originals)
[![Test Coverage](https://codeclimate.com/github/enhancv/mongoose-originals/badges/coverage.svg)](https://codeclimate.com/github/enhancv/mongoose-originals/coverage)

A mongoose plugin to retrieve original values

Expand Down Expand Up @@ -34,3 +37,17 @@ customer.save();
customer.name = 'new name';
console.log(customer.originals.name);
```

Since mongoose [has some limitations](https://github.com/Automattic/mongoose/issues/3968) originals object will not be available when you create a brand new unsaved object. To work arround that, you'll need to execute the "initOriginals" method.

```
var customer = new Customer({ name: 'test', email: 'example.com' });
customer.initOriginals();
console.log(customer.originals.name);
```

License
-------

Copyright (c) 2016-2017 Enhancv
Licensed under the MIT license.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-originals",
"version": "1.0.1",
"version": "1.1.0",
"description": "Get original value of mongoose fields",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-originals.git",
Expand All @@ -10,11 +10,13 @@
"mongoose": "^4.8"
},
"scripts": {
"test": "node_modules/.bin/mocha --recursive"
"test": "node_modules/.bin/mocha --recursive",
"coverage": "node node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- --recursive"
},
"devDependencies": {
"mongoose": "^4.8",
"dotenv": "^4.0.0",
"mocha": "^3.2.0"
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mongoose": "^4.8"
}
}
40 changes: 23 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
function mongooseOriginals (schema, userOptions) {
function mongooseOriginals(schema, userOptions) {
var options = Object.assign({ methods: true }, userOptions);

if (!options.fields) {
throw new Error('No fields specified for mongoose originals on schema');
throw new Error("No fields specified for mongoose originals on schema");
}

function saveOriginalNamed (item) {

function saveOriginalNamed() {
this.original = {};

options.fields.forEach(name => {
this.original[name] = item.toObject()[name];
this.original[name] = this.toObject()[name];
});
}

schema.post('init', saveOriginalNamed);
schema.post('save', saveOriginalNamed);
function saveConditionalOriginalNamed() {
if (this.original === undefined) {
saveOriginalNamed.bind(this)();
}
}

schema.method("initOriginals", saveConditionalOriginalNamed);
schema.post("init", saveOriginalNamed);
schema.post("save", saveOriginalNamed);

if (options.methods) {
schema.methods.collectionAdded = function collectionAdded (name) {
schema.methods.collectionAdded = function collectionAdded(name) {
var _this = this;

return this[name].filter(function (item) {
return !_this.original[name].find(function (originalItem) {
return this[name].filter(function(item) {
return !_this.original[name].find(function(originalItem) {
return item._id.equals(originalItem._id);
});
});
};

schema.methods.collectionRemoved = function collectionRemoved (name) {
schema.methods.collectionRemoved = function collectionRemoved(name) {
var _this = this;

return this.original[name].filter(function (originalItem) {
return !_this[name].find(function (item) {
return this.original[name].filter(function(originalItem) {
return !_this[name].find(function(item) {
return item._id.equals(originalItem._id);
});
});
};

schema.methods.collectionUpdated = function collectionUpdated (name) {
schema.methods.collectionUpdated = function collectionUpdated(name) {
var _this = this;

return this[name].filter(function (item) {
return _this.original[name].find(function (originalItem) {
return this[name].filter(function(item) {
return _this.original[name].find(function(originalItem) {
return item._id.equals(originalItem._id);
});
});
};
}
}

module.exports = mongooseOriginals
module.exports = mongooseOriginals;
15 changes: 7 additions & 8 deletions test/database.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
"use strict";

const path = require('path');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const path = require("path");
const dotenv = require("dotenv");
const mongoose = require("mongoose");

dotenv.config({ path: path.resolve(__dirname, '../.env') });
dotenv.config({ path: path.resolve(__dirname, "../.env") });
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI);

Expand All @@ -13,9 +13,8 @@ function clearModels(models) {
return Promise.all(removePromises);
}

function database (models, test) {

beforeEach('Clear models', function () {
function database(models, test) {
beforeEach("Clear models", function() {
return clearModels(models);
});

Expand Down
Loading

0 comments on commit e28889d

Please sign in to comment.