Comparing version 2.1.0 to 2.2.0
106
lib/model.js
@@ -284,2 +284,6 @@ var q = require('q'); | ||
definition.methods.validate = function (options) { | ||
return this._validate(this.toObject(), options); | ||
}; | ||
definition.methods._validate = function (obj, options) { | ||
var deferred = q.defer(); | ||
@@ -293,3 +297,3 @@ options = defaults(options, { | ||
}); | ||
joi.validate(this.toObject(), definition._schema, options, function (err, value) { | ||
joi.validate(obj, definition._schema, options, function (err, value) { | ||
if (err) { return deferred.reject(err); } | ||
@@ -305,50 +309,54 @@ deferred.resolve(value); | ||
} | ||
var before = 'function' === typeof this.beforeSave ? | ||
q(this.beforeSave()) : q.resolve(); | ||
return before.then(function () { | ||
return this.validate(options) | ||
.then(function (value) { | ||
var obj = this.toJSON({ virtuals: false, extended: true }); | ||
var newEtag = etag(JSON.stringify(obj)); | ||
if (!this.isNew()) { | ||
var query = { _id: obj._id, _etag: obj._etag }; | ||
var set = { _etag: newEtag }; | ||
this.__modified.forEach(function (path) { | ||
set[path] = obj[path]; | ||
}); | ||
var update = { $set: set }; | ||
var collection = definition.collection; | ||
debug('updating ' + collection + ': ' + JSON.stringify(update)); | ||
return db.update(collection, query, update) | ||
.then(function (result) { | ||
if (result.matchCount === 0) { | ||
throw errors.Conflict({ message: 'Document update conflict' }); | ||
} | ||
debug('update into ' + collection + ' successful', result); | ||
var updatedModel = this.set({ | ||
_etag: newEtag, | ||
__modified: [] | ||
}, { silent: true }); | ||
if ('function' === typeof updatedModel.afterSave) { | ||
updatedModel.afterSave(); | ||
} | ||
return updatedModel; | ||
}.bind(this)); | ||
} else { | ||
var collection = definition.collection; | ||
obj._etag = newEtag; | ||
debug('inserting into ' + collection + ': ' + JSON.stringify(obj)); | ||
return db.insert(collection, obj) | ||
.then(function (result) { | ||
debug('insert into ' + collection + ' successful', result); | ||
var obj = Array.isArray(result.ops) ? result.ops[0] : result.ops; | ||
var newModel = _create(obj, definition); | ||
if ('function' === typeof newModel.afterSave) { | ||
newModel.afterSave(); | ||
} | ||
return newModel; | ||
}.bind(this)); | ||
} | ||
}.bind(this)); | ||
}.bind(this)); | ||
var obj = this.toObject(); | ||
var before = q.resolve(obj); | ||
if ('function' === typeof this.beforeSave) { | ||
before = q(this.beforeSave.apply(obj)).then(function () { return obj; }); | ||
} | ||
return before | ||
.then(function (obj) { | ||
return this._validate(obj, options) | ||
}.bind(this)) | ||
.then(function (value) { | ||
var obj = EJSON.inflate(value); | ||
var newEtag = etag(JSON.stringify(obj)); | ||
if (!this.isNew()) { | ||
var query = { _id: obj._id, _etag: obj._etag }; | ||
var set = { _etag: newEtag }; | ||
this.__modified.forEach(function (path) { | ||
set[path] = obj[path]; | ||
}); | ||
var update = { $set: set }; | ||
var collection = definition.collection; | ||
debug('updating ' + collection + ': ' + JSON.stringify(update)); | ||
return db.update(collection, query, update) | ||
.then(function (result) { | ||
if (result.matchCount === 0) { | ||
throw errors.Conflict({ message: 'Document update conflict' }); | ||
} | ||
debug('update into ' + collection + ' successful', result); | ||
var updatedModel = this.set({ | ||
_etag: newEtag, | ||
__modified: [] | ||
}, { silent: true }); | ||
if ('function' === typeof updatedModel.afterSave) { | ||
updatedModel.afterSave(); | ||
} | ||
return updatedModel; | ||
}.bind(this)); | ||
} else { | ||
var collection = definition.collection; | ||
obj._etag = newEtag; | ||
debug('inserting into ' + collection + ': ' + JSON.stringify(obj)); | ||
return db.insert(collection, obj) | ||
.then(function (result) { | ||
debug('insert into ' + collection + ' successful', result); | ||
var obj = Array.isArray(result.ops) ? result.ops[0] : result.ops; | ||
var newModel = _create(obj, definition); | ||
if ('function' === typeof newModel.afterSave) { | ||
newModel.afterSave(); | ||
} | ||
return newModel; | ||
}.bind(this)); | ||
} | ||
}.bind(this)); | ||
}; | ||
@@ -355,0 +363,0 @@ |
{ | ||
"name": "cosa", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Cosa Models for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -975,2 +975,25 @@ var chai = require('chai'); | ||
it('should allow mutating model before saving', function (done) { | ||
var HookedModel = Model.define({ | ||
name: 'HookedModel', | ||
collection: 'mocha_test', | ||
properties: { | ||
str: { type: 'string' } | ||
}, | ||
methods: { | ||
beforeSave: function () { | ||
this.str += ' bar'; | ||
} | ||
} | ||
}); | ||
var model = HookedModel.create({ str: 'foo' }); | ||
expect(model.beforeSave).to.exist; | ||
model.save() | ||
.then(function (model2) { | ||
expect(model2.str).to.equal('foo bar'); | ||
done(); | ||
}) | ||
.done(null, done); | ||
}); | ||
}); | ||
@@ -977,0 +1000,0 @@ |
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
86436
2465