Return updated docs tested

pull/2/head
Louis Chatriot 9 years ago
parent 5d190f17c5
commit 6ea34ee6ae
  1. 13
      lib/datastore.js
  2. 34
      test/db.test.js

@ -476,12 +476,12 @@ Datastore.prototype.findOne = function (query, projection, callback) {
/** /**
* Update all docs matching query * Update all docs matching query
* For now, very naive implementation (recalculating the whole database)
* @param {Object} query * @param {Object} query
* @param {Object} updateQuery * @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
* options.returnUpdatedDocs Defaults to false, if true return as third argument the array of updated matched documents (even if no change actually took place)
* @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)
* *
* @api private Use Datastore.update which has the same signature * @api private Use Datastore.update which has the same signature
@ -562,9 +562,16 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
} }
// Update the datafile // Update the datafile
self.persistence.persistNewState(_.pluck(modifications, 'newDoc'), function (err) { var updatedDocs = _.pluck(modifications, 'newDoc');
self.persistence.persistNewState(updatedDocs, function (err) {
if (err) { return callback(err); } if (err) { return callback(err); }
return callback(null, numReplaced); if (!options.returnUpdatedDocs) {
return callback(null, numReplaced);
} else {
var updatedDocsDC = [];
updatedDocs.forEach(function (doc) { updatedDocsDC.push(model.deepCopy(doc)); });
return callback(null, numReplaced, updatedDocsDC);
}
}); });
} }
]); ]);

@ -1477,6 +1477,40 @@ describe('Database', function () {
}); });
}); });
it("If options.returnUpdatedDocs is true, return all matched docs", function (done) {
d.insert([{ a: 4 }, { a: 5 }, { a: 6 }], function (err, docs) {
docs.length.should.equal(3);
d.update({ a: 7 }, { $set: { u: 1 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) {
num.should.equal(0);
updatedDocs.length.should.equal(0);
d.update({ a: 5 }, { $set: { u: 2 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) {
num.should.equal(1);
updatedDocs.length.should.equal(1);
updatedDocs[0].a.should.equal(5);
updatedDocs[0].u.should.equal(2);
d.update({ a: { $in: [4, 6] } }, { $set: { u: 3 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) {
num.should.equal(2);
updatedDocs.length.should.equal(2);
updatedDocs[0].u.should.equal(3);
updatedDocs[1].u.should.equal(3);
if (updatedDocs[0].a === 4) {
updatedDocs[0].a.should.equal(4);
updatedDocs[1].a.should.equal(6);
} else {
updatedDocs[0].a.should.equal(6);
updatedDocs[1].a.should.equal(4);
}
done();
});
});
});
});
});
}); // ==== End of 'Update' ==== // }); // ==== End of 'Update' ==== //

Loading…
Cancel
Save