Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

experimental validateAnd* mapping and namespacing

  • Loading branch information...
commit ab1ac413de1d4c4f5d1edb9c51f998f07ad6a1ad 1 parent 4a53c15
@masylum authored
View
62 lib/model.js
@@ -106,26 +106,46 @@ module.exports = function (db, collection_name) {
*/
MODEL.validateAndInsert = function (document, options, callback) {
// options are optional
- if (typeof options === "function") {
+ if (typeof options === 'function') {
callback = options;
options = {};
} else {
options = options || {};
}
- var namespace = options.namespace ? ':' + options.namespace : '';
+ var fn = {method: 'insert', namespacing: false, mapping: false}
+ , args = [document];
+
+ if (options.namespace) {
+ fn.namespace = options.namespace;
+ }
+
+ if (MODEL.namespaces && MODEL.namespaces[options.namespace]) {
+ // has side effects, alters args
+ MODEL.collection_proxy.namespacer.filter(MODEL.namespaces, options.namespace, 'insert', args);
+ }
+
+ if (MODEL.maps) {
+ // has side effects, alters args
+ MODEL.collection_proxy.mapper.map(MODEL.maps, 'insert', args);
+ }
MODEL.validate({}, document, function (error, validator) {
- if (validator.hasErrors()) {
- callback(error, validator);
- } else {
- MODEL.mongo('insert' + namespace, document, function (error, documents) {
+ if (error) return callback(error);
+ if (validator.hasErrors()) return callback(null, validator);
+
+ args.push(function cb(error, documents) {
+ if (error) {
+ return callback(error);
+ } else {
if (documents) {
validator.updated_document = documents[0];
}
callback(error, validator);
- });
- }
+ }
+ });
+ args.unshift(fn);
+ MODEL.mongo.apply(MODEL, args);
});
return MODEL;
@@ -219,10 +239,25 @@ module.exports = function (db, collection_name) {
options = options || {};
}
- var namespace = options.namespace ? ':' + options.namespace : '';
+ options = _.defaults(options, {upsert: false, multi: false});
+
+ var fn = {method: 'update', namespacing: false, mapping: false}
+ , args = [query, update, options];
+
+ if (options.namespace) {
+ fn.namespace = options.namespace;
+ }
delete options.namespace;
- options = _.defaults(options, {upsert: false, multi: false});
+ if (MODEL.namespaces && MODEL.namespaces[options.namespace]) {
+ // has side effects, alters args
+ MODEL.collection_proxy.namespacer.filter(MODEL.namespaces, options.namespace, 'update', args);
+ }
+
+ if (MODEL.maps) {
+ // has side effects, alters args
+ MODEL.collection_proxy.mapper.map(MODEL.maps, 'update', args);
+ }
MODEL.mongo('findOne', query, function (error, document) {
if (error) return callback(error);
@@ -231,11 +266,14 @@ module.exports = function (db, collection_name) {
if (error) return callback(error);
if (validator.hasErrors()) {
- callback(null, validator);
+ return callback(null, validator);
} else {
- MODEL.mongo('update' + namespace, {_id: document._id}, update, options, function (error, _) {
+ args.push(function cb(error, _) {
callback(error, validator);
});
+ args.unshift(fn);
+ args[1] = {_id: document._id};
+ MODEL.mongo.apply(MODEL, args);
}
});
});
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "mongolia",
"description": "Layer on top of the mongodb driver to implement data logic.",
- "version": "1.4.2",
+ "version": "1.4.3",
"author": "Pau Ramon <masylum@gmail.com>",
"contributors": [
{"name": "Pau Ramon", "email": "masylum@gmail.com"},
View
8 test/integration/integration_test.js
@@ -78,13 +78,7 @@ db.open(function (error) {
var User = Model(db, 'users'),
Country = Model(db, 'countries');
- Country.maps = {
- name: function capitalize(val) {
- return val.charAt(0).toUpperCase() + val.slice(1);
- }
- };
-
- Country.mongo('findOne', {name: 'andorra'}, function (error, doc) {
+ Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
User.updateEmbeddedDocument({_id: doc._id}, 'country', {name: 'France'}, {}, function (error) {
User.mongo('findOne', {name: 'zemba'}, done(function (error, doc) {
assert.equal(doc.country.name, 'France');
View
21 test/model_test.js
@@ -150,16 +150,23 @@ testosterone
})
.add('`validateAndInsert` when the model is valid inserts it afterwards', function () {
- var document = {},
+ var document = {foo: 'bar'},
validator = _mock_validator(false),
callback;
+ User.maps = {
+ foo: function (el) {
+ return el.toUpperCase();
+ }
+ };
+
gently.expect(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
});
gently.expect(User, 'mongo', function (_action, _document, _callback) {
- assert.equal(_action, 'insert');
+ assert.deepEqual(_action, {method: 'insert', namespacing: false, mapping: false});
+ assert.deepEqual(_document.foo, 'BAR');
_callback(null, _document);
});
@@ -227,6 +234,12 @@ testosterone
, options = {}
, callback;
+ User.maps = {
+ fleiba: function (el) {
+ return el.toLowerCase();
+ }
+ };
+
gently.expect(User, 'mongo', function (_method, _query, _callback) {
assert.equal(_method, 'findOne');
assert.deepEqual(_query, query);
@@ -242,9 +255,9 @@ testosterone
});
gently.expect(User, 'mongo', function (_action, _document, _update, _options, _callback) {
- assert.equal(_action, 'update');
+ assert.deepEqual(_action, {method: 'update', namespacing: false, mapping: false});
assert.deepEqual(_document._id, document._id);
- assert.deepEqual(_update, update);
+ assert.deepEqual(_update.$set.fleiba, 'john');
assert.deepEqual(_options, options);
_callback(null, _document);
});
Please sign in to comment.
Something went wrong with that request. Please try again.