Comparing version 2.5.0 to 2.6.0
@@ -49,2 +49,8 @@ var q = require('q'); | ||
definition.properties.__original = { | ||
type: '*', | ||
enumerable: false, | ||
default: null | ||
}; | ||
definition._schema = _buildSchema(definition); | ||
@@ -160,13 +166,19 @@ | ||
var _markAsModified = function (obj, path) { | ||
if (Array.isArray(path)) { | ||
path = path.join('.'); | ||
} | ||
var shouldAdd = true; | ||
obj.__modified = obj.__modified.filter(function (value) { | ||
shouldAdd = shouldAdd && path.indexOf(value + '.', 0) !== 0; | ||
return value.indexOf(path + '.', 0) !== 0; | ||
var _markAsModified = function (obj, paths, original) { | ||
paths.forEach(function(path) { | ||
if (Array.isArray(path)) { | ||
path = path.join('.'); | ||
} | ||
var shouldAdd = true; | ||
obj.__modified = obj.__modified.filter(function (value) { | ||
shouldAdd = shouldAdd && path.indexOf(value + '.', 0) !== 0; | ||
return value.indexOf(path + '.', 0) !== 0; | ||
}); | ||
if (shouldAdd) { | ||
obj.__modified.push(path); | ||
} | ||
}); | ||
if (shouldAdd) { | ||
obj.__modified.push(path); | ||
if(obj._id){ // is not a new object | ||
obj.__original = original.__original || original; | ||
} | ||
@@ -214,18 +226,18 @@ }; | ||
definition.methods.set = function (pathOrObj, value, options) { | ||
var original = this; | ||
return this.mutate(function () { | ||
var paths; | ||
if ('object' === typeof pathOrObj && !Array.isArray(pathOrObj)) { | ||
options = defaults(value, { silent: false }); | ||
assign(this, pathOrObj); | ||
if (!options.silent) { | ||
Object.keys(pathOrObj).forEach(function (item) { | ||
_markAsModified(this, item); | ||
}.bind(this)); | ||
} | ||
paths = Object.keys(pathOrObj); | ||
} else { | ||
options = defaults(options, { silent: false }); | ||
objectPath.set(this, pathOrObj, value); | ||
if (!options.silent) { | ||
_markAsModified(this, pathOrObj); | ||
} | ||
paths = [pathOrObj]; | ||
} | ||
if (!options.silent) { | ||
_markAsModified(this, paths, original); | ||
} | ||
}); | ||
@@ -235,5 +247,6 @@ }; | ||
definition.methods.del = function (path) { | ||
var original = this; | ||
return this.mutate(function () { | ||
objectPath.del(this, path); | ||
_markAsModified(this, path); | ||
_markAsModified(this, [path], original); | ||
}); | ||
@@ -272,2 +285,3 @@ }; | ||
delete json.__modified; | ||
delete json.__original; | ||
if (options.extended) { | ||
@@ -326,8 +340,10 @@ json = EJSON.inflate(json); | ||
var obj = EJSON.inflate(value); | ||
var original = obj.__original; | ||
delete obj.__modified; | ||
delete obj.__original; | ||
var newEtag = etag(JSON.stringify(obj)); | ||
var collection = definition.collection; | ||
if (!this.isNew()) { | ||
var query = { _id: obj._id, _etag: obj._etag }; | ||
obj._etag = newEtag; | ||
var collection = definition.collection; | ||
debug('updating ' + collection + ': ' + JSON.stringify(obj)); | ||
@@ -342,6 +358,7 @@ return db.update(collection, query, obj) | ||
_etag: newEtag, | ||
__modified: [] | ||
__modified: [], | ||
__original: null | ||
}, { silent: true }); | ||
if ('function' === typeof updatedModel.afterSave) { | ||
updatedModel.afterSave(); | ||
updatedModel.afterSave(original); | ||
} | ||
@@ -351,3 +368,2 @@ return updatedModel; | ||
} else { | ||
var collection = definition.collection; | ||
obj._etag = newEtag; | ||
@@ -361,3 +377,3 @@ debug('inserting into ' + collection + ': ' + JSON.stringify(obj)); | ||
if ('function' === typeof newModel.afterSave) { | ||
newModel.afterSave(); | ||
newModel.afterSave(null); | ||
} | ||
@@ -487,2 +503,3 @@ return newModel; | ||
schema = joi.object().type(bson.ObjectId); | ||
break; | ||
case 'any': | ||
@@ -489,0 +506,0 @@ case '*': |
{ | ||
"name": "cosa", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Cosa Models for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1086,2 +1086,3 @@ var chai = require('chai'); | ||
var strSaved = ''; | ||
var checkFunction; | ||
var HookedModel = Model.define({ | ||
@@ -1095,2 +1096,3 @@ name: 'HookedModel', | ||
afterSave: function () { | ||
checkFunction(this, arguments); | ||
strSaved = this.str; | ||
@@ -1102,9 +1104,24 @@ } | ||
expect(model.afterSave).to.exist; | ||
var wasCalled; | ||
checkFunction = function(instance, args){ | ||
expect(instance.str).to.equal('foo'); | ||
expect(args[0]).to.equal(null); | ||
wasCalled = true; | ||
} | ||
model | ||
.save() | ||
.then(function (m) { | ||
expect(strSaved).to.equal('foo'); | ||
expect(wasCalled).to.equal(true); | ||
wasCalled = false; | ||
checkFunction = function(instance, args){ | ||
expect(instance.str).to.equal('bar'); | ||
expect(args[0].str).to.equal('foo'); | ||
wasCalled = true; | ||
} | ||
return m.set('str', 'bar').save(); | ||
}) | ||
.then(function (m) { | ||
expect(wasCalled).to.equal(true); | ||
expect(strSaved).to.equal('bar'); | ||
@@ -1111,0 +1128,0 @@ done(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89960
2576