|
|
@ -187,13 +187,13 @@ Datastore.prototype.persistWholeDatabase = function (data, cb) { |
|
|
|
* Update all docs matching query |
|
|
|
* Update all docs matching query |
|
|
|
* For now, very naive implementation (recalculating the whole database) |
|
|
|
* For now, very naive implementation (recalculating the whole database) |
|
|
|
* @param {Object} query |
|
|
|
* @param {Object} query |
|
|
|
* @param {Object} newDoc Will replace the former docs |
|
|
|
* @param {Object} updateQuery |
|
|
|
* @param {Object} options Optional options |
|
|
|
* @param {Object} options Optional options |
|
|
|
* options.multi If true, can update multiple documents (defaults to false) |
|
|
|
* options.multi If true, can update multiple documents (defaults to false) |
|
|
|
* options.upsert If true, document is inserted if the query doesn't match anything |
|
|
|
* options.upsert If true, document is inserted if the query doesn't match anything |
|
|
|
* @param {Function} cb Optional callback, signature: err, numReplaced, upsert (set to true if the update was in fact an upsert) |
|
|
|
* @param {Function} cb Optional callback, signature: err, numReplaced, upsert (set to true if the update was in fact an upsert) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype.update = function (query, newDoc, options, cb) { |
|
|
|
Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
var callback |
|
|
|
var callback |
|
|
|
, self = this |
|
|
|
, self = this |
|
|
|
, numReplaced = 0 |
|
|
|
, numReplaced = 0 |
|
|
@ -214,7 +214,9 @@ Datastore.prototype.update = function (query, newDoc, options, cb) { |
|
|
|
if (doc) { |
|
|
|
if (doc) { |
|
|
|
return cb(); |
|
|
|
return cb(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return self.insert(model.modify({}, newDoc), function (err) { |
|
|
|
// The upserted document is the query (since for now queries have the same structure as
|
|
|
|
|
|
|
|
// documents), modified by the updateQuery
|
|
|
|
|
|
|
|
return self.insert(model.modify(query, updateQuery), function (err) { |
|
|
|
if (err) { return callback(err); } |
|
|
|
if (err) { return callback(err); } |
|
|
|
return callback(null, 1, true); |
|
|
|
return callback(null, 1, true); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -226,7 +228,7 @@ Datastore.prototype.update = function (query, newDoc, options, cb) { |
|
|
|
self.data.forEach(function (d) { |
|
|
|
self.data.forEach(function (d) { |
|
|
|
if (Datastore.match(d, query) && (multi || numReplaced === 0)) { |
|
|
|
if (Datastore.match(d, query) && (multi || numReplaced === 0)) { |
|
|
|
numReplaced += 1; |
|
|
|
numReplaced += 1; |
|
|
|
newData.push(model.modify(d, newDoc)); |
|
|
|
newData.push(model.modify(d, updateQuery)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
newData.push(d); |
|
|
|
newData.push(d); |
|
|
|
} |
|
|
|
} |
|
|
|