Skip to content

Commit

Permalink
Return updated docs tested
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Jan 9, 2016
1 parent 5d190f1 commit 6ea34ee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ Datastore.prototype.findOne = function (query, projection, callback) {

/**
* Update all docs matching query
* For now, very naive implementation (recalculating the whole database)
* @param {Object} query
* @param {Object} updateQuery
* @param {Object} options Optional options
* 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.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)
*
* @api private Use Datastore.update which has the same signature
Expand Down Expand Up @@ -562,9 +562,16 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
}

// 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); }
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);
}
});
}
]);
Expand Down
2 changes: 1 addition & 1 deletion lib/indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function projectForUnique (elt) {
if (typeof elt === 'boolean') { return '$boolean' + elt; }
if (typeof elt === 'number') { return '$number' + elt; }
if (util.isArray(elt)) { return '$date' + elt.getTime(); }

return elt; // Arrays and objects, will check for pointer equality
}

Expand Down
34 changes: 34 additions & 0 deletions test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ==== //


Expand Down

0 comments on commit 6ea34ee

Please sign in to comment.