Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Remove obsolete "methods" extension
- Move original values from "original" to "_original" not to conflict
with mongoose internals
- Depopulate objects when saving original values
  • Loading branch information
ivank committed Oct 25, 2017
1 parent 864775e commit 823a36f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 118 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-originals",
"version": "1.3.2",
"version": "2.0.0",
"description": "Get original value of mongoose fields",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-originals.git",
Expand All @@ -11,13 +11,14 @@
},
"scripts": {
"test": "node_modules/.bin/mocha --recursive",
"coverage": "node node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- --recursive"
"coverage":
"node node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- --recursive"
},
"devDependencies": {
"dotenv": "^4.0.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mongoose": "^4.11.1"
"mongoose": "^4.12"
},
"dependencies": {
"lodash": "^4.17.4"
Expand Down
26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ function mongooseOriginals(schema, userOptions) {

schema.method("isChanged", function isChanged() {
return (
!this.original ||
!this._original ||
!isEqual(
this.original,
pick(options.fields, this.toObject({ getters: false, transform: false }))
this._original,
pick(options.fields, this.toObject({ depopulate: true, getters: false, transform: false }))
)
);
});

schema.method("setSnapshotOriginal", function setSnapshotOriginal() {
eachMongooseOriginalsPath(this, item => {
item.snapshotOriginal = item.original;
item.snapshotOriginal = item._original;
});
return this;
});
Expand All @@ -47,16 +47,16 @@ function mongooseOriginals(schema, userOptions) {
});

function saveOriginalNamed() {
this.original = {};
this._original = {};
const newValues = this.toObject({ getters: false, transform: false });

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

schema.method("initOriginals", function initOriginals() {
if (this.original === undefined) {
if (this._original === undefined) {
saveOriginalNamed.bind(this)();
}
});
Expand All @@ -70,18 +70,18 @@ function mongooseOriginals(schema, userOptions) {
var _this = this;

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

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

return this.original[name].filter(function(originalItem) {
return this._original[name].filter(function(_originalItem) {
return !_this[name].find(function(item) {
return item._id.equals(originalItem._id);
return item._id.equals(_originalItem._id);
});
});
};
Expand All @@ -90,8 +90,8 @@ function mongooseOriginals(schema, userOptions) {
var _this = this;

return this[name].filter(function(item) {
return _this.original[name].find(function(originalItem) {
return item._id.equals(originalItem._id);
return _this._original[name].find(function(_originalItem) {
return item._id.equals(_originalItem._id);
});
});
};
Expand Down
44 changes: 22 additions & 22 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ describe(
.then(nested => {
assert.equal(nested.setSnapshotOriginal(), nested);

assert.deepEqual(nested.snapshotOriginal, nested.original);
assert.deepEqual(nested.embedded.snapshotOriginal, nested.embedded.original);
assert.deepEqual(nested.snapshotOriginal, nested._original);
assert.deepEqual(nested.embedded.snapshotOriginal, nested.embedded._original);
assert.deepEqual(
nested.embedded.children[0].snapshotOriginal,
nested.embedded.children[0].original
nested.embedded.children[0]._original
);
assert.deepEqual(
nested.embedded.children[1].snapshotOriginal,
nested.embedded.children[1].original
nested.embedded.children[1]._original
);
assert.deepEqual(
nested.children[0].snapshotOriginal,
nested.children[0].original
nested.children[0]._original
);
assert.deepEqual(
nested.children[0].nested.snapshotOriginal,
nested.children[0].nested.original
nested.children[0].nested._original
);
assert.deepEqual(
nested.children[1].snapshotOriginal,
nested.children[1].original
nested.children[1]._original
);
assert.deepEqual(
nested.children[1].nested.snapshotOriginal,
nested.children[1].nested.original
nested.children[1].nested._original
);

assert.deepEqual(nested.snapshotOriginal, { name: "11", email: undefined });
Expand All @@ -86,14 +86,14 @@ describe(
return nested.save();
})
.then(nestedSaved => {
assert.deepEqual(nestedSaved.original, { name: "t1", email: undefined });
assert.deepEqual(nestedSaved.embedded.original, { name: "t2" });
assert.deepEqual(nestedSaved.embedded.children[0].original, { title: "t3" });
assert.deepEqual(nestedSaved.embedded.children[1].original, { title: "t4" });
assert.deepEqual(nestedSaved.children[0].original, { category: "t5" });
assert.deepEqual(nestedSaved.children[0].nested.original, { type: "t6" });
assert.deepEqual(nestedSaved.children[1].original, { category: "t7" });
assert.deepEqual(nestedSaved.children[1].nested.original, { type: "t8" });
assert.deepEqual(nestedSaved._original, { name: "t1", email: undefined });
assert.deepEqual(nestedSaved.embedded._original, { name: "t2" });
assert.deepEqual(nestedSaved.embedded.children[0]._original, { title: "t3" });
assert.deepEqual(nestedSaved.embedded.children[1]._original, { title: "t4" });
assert.deepEqual(nestedSaved.children[0]._original, { category: "t5" });
assert.deepEqual(nestedSaved.children[0].nested._original, { type: "t6" });
assert.deepEqual(nestedSaved.children[1]._original, { category: "t7" });
assert.deepEqual(nestedSaved.children[1].nested._original, { type: "t8" });

assert.deepEqual(nested.snapshotOriginal, { name: "11", email: undefined });
assert.deepEqual(nested.embedded.snapshotOriginal, { name: "22" });
Expand Down Expand Up @@ -131,7 +131,7 @@ describe(
assert.ok(!test.collectionAdded);
});

it("Should be able to save original values", function() {
it("Should be able to save _original values", function() {
const customer = new Customer({
name: "Pesho",
email: "Pesho",
Expand All @@ -155,7 +155,7 @@ describe(
answers: oldCustomer.answers,
};

assert.deepEqual(customer.original, expected);
assert.deepEqual(customer._original, expected);

return customer.save();
})
Expand All @@ -168,7 +168,7 @@ describe(
answers: newCustomer.answers,
};

assert.deepEqual(customer.original, expected);
assert.deepEqual(customer._original, expected);
});
});

Expand Down Expand Up @@ -226,21 +226,21 @@ describe(
it("Should work even with unsaved models, by calling initOriginals", function() {
const subscription = new Subscription({ name: "Basic" });

assert.equal(undefined, subscription.original);
assert.equal(undefined, subscription._original);

subscription.initOriginals();

subscription.discounts.push(subscription.discounts.create({ amount: 20 }));

assert.deepEqual({ discounts: [] }, subscription.original);
assert.deepEqual({ discounts: [] }, subscription._original);

subscription.initOriginals();

subscription.discounts.push(subscription.discounts.create({ amount: 30 }));

assert.deepEqual(
{ discounts: [] },
subscription.original,
subscription._original,
"Should not change initOriginals"
);
});
Expand Down
Loading

0 comments on commit 823a36f

Please sign in to comment.