Able to choose whether to update multiple docs or just one

pull/2/head
Louis Chatriot 12 years ago
parent c6ef338c8d
commit bf9bddb408
  1. 15
      lib/datastore.js

@ -186,16 +186,23 @@ Datastore.prototype.persistWholeDatabase = function (data, cb) {
* For now, very naive implementation (recalculating the whole database)
* @param {Object} query
* @param {Object} newDoc Will replace the former docs
* @param {Object} options Optional options
* options.multi If true, can update multiple documents (defaults to false)
* @param {Function} cb Optional callback, signature: err, numReplaced
*/
Datastore.prototype.update = function (query, newDoc, cb) {
var callback = cb || function () {}
Datastore.prototype.update = function (query, newDoc, options, cb) {
var callback
, self = this
, numReplaced = 0
, multi
, newData = [];
if (typeof options === 'function') { cb = options; options = {}; }
callback = cb || function () {};
multi = options.multi !== undefined ? options.multi : false;
self.data.forEach(function (d) {
if (Datastore.match(d, query)) {
if (Datastore.match(d, query) && (multi || numReplaced === 0)) {
numReplaced += 1;
newData.push(newDoc);
} else {
@ -217,7 +224,7 @@ var d = new Datastore('workspace/test.db');
d.loadDatabase(function (err) {
console.log(d.data);
d.update({ yahoo: 'dailymotion' }, {yahoo: 'great' }, function (err, n) {
d.update({ yahoo: 'great' }, {yahoo: 'great2' }, { multi: false }, function (err, n) {
console.log("--------------");
console.log(err);
console.log(n);

Loading…
Cancel
Save