Comparing version 0.0.22 to 0.0.23
@@ -169,2 +169,3 @@ 'use strict'; | ||
}; | ||
/** | ||
@@ -194,2 +195,25 @@ * Remove this object instance from the backend mongodb instance. | ||
/** | ||
* Update this object instance from the backend mongodb instance. | ||
* | ||
* @memberOf Model | ||
* @param {Object} query Search query of objects to remove | ||
* @param {Object|Function} [options] options for the query | ||
* @param {Function} callback Callback function (error) with the result of the operation | ||
*/ | ||
Model.update = function (query, document, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
} | ||
odm.update(mongoCollection, query, document, options, function (err) { | ||
// document deleted, delete from cache since it is not valid anymore | ||
if (cache !== undefined) { | ||
cache.reset(); | ||
} | ||
callback(err); | ||
}); | ||
}; | ||
/** | ||
* Ensure indexes are present | ||
@@ -196,0 +220,0 @@ * |
@@ -208,2 +208,12 @@ 'use strict'; | ||
update: function (collection_name, criteria, document, options, callback) { | ||
this.collection(collection_name, options, function (err, collection) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
collection.update(criteria, document, options, callback); | ||
}); | ||
}, | ||
save: function (collection_name, document, options, callback) { | ||
@@ -210,0 +220,0 @@ this.collection(collection_name, options, function (err, collection) { |
191
lib/model.js
@@ -286,2 +286,80 @@ 'use strict'; | ||
/** | ||
* Verify if the object matches the query | ||
* @param query | ||
* @param value | ||
*/ | ||
function matchesSimple(query, value) { | ||
var search = query; | ||
var i; | ||
if (search === undefined || search === null) { | ||
return -1; | ||
} | ||
var negate = false; | ||
if (query.hasOwnProperty('$ne')) { | ||
negate = true; | ||
search = query.$ne; | ||
if (search === undefined || search === null) { | ||
return -1; | ||
} | ||
} | ||
var multi = false; | ||
if (search.hasOwnProperty('$in')) { | ||
multi = true; | ||
if (!Array.isArray(search.$in)) { | ||
return -2; | ||
} | ||
search = search.$in; | ||
if (search === undefined || search === null) { | ||
return -1; | ||
} | ||
} | ||
if (search.hasOwnProperty('$nin')) { | ||
multi = true; | ||
negate = true; | ||
if (!Array.isArray(search.$nin)) { | ||
return -2; | ||
} | ||
search = search.$nin; | ||
if (search === undefined || search === null) { | ||
return -1; | ||
} | ||
} | ||
if (multi) { | ||
var matched = false; | ||
for (i = 0; i < search.length; i++) { | ||
if (matchValue(search[i], value, false)) { | ||
matched = true; | ||
break; | ||
} | ||
} | ||
if (negate) { | ||
if (matched) { | ||
return 0; | ||
} | ||
} else { | ||
if (!matched) { | ||
return 0; | ||
} | ||
} | ||
} else { | ||
if (!matchValue(search, value, negate)) { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
/** | ||
* @private | ||
@@ -550,14 +628,13 @@ * @param schema document schema | ||
var hasEquals = val.equals !== undefined && val.equals !== null && typeof val.equals === 'function'; | ||
for (i = 0; i < value.length; i++) { | ||
if (hasEquals) { | ||
if (val.equals(value[i])) { | ||
return callback(null, value[i]); | ||
} | ||
} else { | ||
if (val === value[i]) { | ||
return callback(null, value[i]); | ||
} | ||
var match = matchesSimple(val, value[i]); | ||
if (match === -1) { | ||
return callback('Bad query'); | ||
} | ||
if (match === -2) { | ||
return callback('$in/$nin expect an array'); | ||
} | ||
if (match === 1) { | ||
return callback(null, value[i]); | ||
} | ||
} | ||
@@ -582,14 +659,13 @@ callback(null, null); | ||
var hasEquals = val.equals !== undefined && val.equals !== null && typeof val.equals === 'function'; | ||
for (i = 0; i < value.length; i++) { | ||
if (hasEquals) { | ||
if (val.equals(value[i])) { | ||
result.push(value[i]); | ||
} | ||
} else { | ||
if (val === value[i]) { | ||
result.push(value[i]); | ||
} | ||
var match = matchesSimple(val, value[i]); | ||
if (match === -1) { | ||
return callback('Bad query'); | ||
} | ||
if (match === -2) { | ||
return callback('$in/$nin expect an array'); | ||
} | ||
if (match === 1) { | ||
result.push(value[i]); | ||
} | ||
} | ||
@@ -613,18 +689,15 @@ callback(null, result); | ||
var hasEquals = val.equals !== undefined && val.equals !== null && typeof val.equals === 'function'; | ||
for (i = 0; i < value.length; i++) { | ||
if (hasEquals) { | ||
if (val.equals(value[i])) { | ||
value.splice(i, 1); | ||
i--; | ||
removed++; | ||
} | ||
} else { | ||
if (val === value[i]) { | ||
value.splice(i, 1); | ||
i--; | ||
removed++; | ||
} | ||
var match = matchesSimple(val, value[i]); | ||
if (match === -1) { | ||
return callback('Bad query'); | ||
} | ||
if (match === -2) { | ||
return callback('$in/$nin expect an array'); | ||
} | ||
if (match === 1) { | ||
value.splice(i, 1); | ||
i--; | ||
removed++; | ||
} | ||
} | ||
@@ -1693,2 +1766,31 @@ callback(null, removed); | ||
/** | ||
* Update this object instance to the backend mongodb instance. | ||
* | ||
* @memberOf Model | ||
* @param {Object|Function} [options] options for the query | ||
* @param {Function} callback Callback function (error, documentId) with the result of the operation | ||
*/ | ||
Model.prototype.update = function (document, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (!mongoCollection) { | ||
return callback('Cannot save on embedded model'); | ||
} | ||
var self = this; | ||
odm.update(mongoCollection, {_id: self._internalDocument._id}, document, options, function (err, modifiedCount) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// document updated delete from cache since it is not valid anymore | ||
purgeCache(cache, Model.IndexKeys, self); | ||
callback(null, modifiedCount); | ||
}); | ||
}; | ||
/** | ||
* Remove this object instance from the backend mongodb instance. | ||
@@ -1780,2 +1882,23 @@ * | ||
/** | ||
* Remove this object instance from the backend mongodb instance. | ||
* | ||
* @memberOf Model | ||
* @param {Object} query Search query of objects to remove | ||
* @param {Object|Function} [options] options for the query | ||
* @param {Function} callback Callback function (error) with the result of the operation | ||
*/ | ||
Model.update = function (query, document, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (!mongoCollection) { | ||
return callback('Cannot remove on embedded model'); | ||
} | ||
odm.update(mongoCollection, query, document, options, callback); | ||
}; | ||
/** | ||
* schema for embedded objects | ||
@@ -1782,0 +1905,0 @@ * |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"engines": { | ||
@@ -11,0 +11,0 @@ "node": ">=0.4.12" |
Sorry, the diff of this file is not supported yet
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
108214
2288