find and findOne take care of deep copying the returned docs

pull/2/head
Louis Chatriot 11 years ago
parent 905a520e0a
commit 1625e79ec9
  1. 5
      lib/cursor.js
  2. 10
      lib/datastore.js
  3. 2
      test/db.test.js

@ -48,6 +48,7 @@ Cursor.prototype.sort = function(sortQuery) {
/**
* Get all matching elements
* Will return pointers to matched elements (shallow copies), returning full copies is the role of find or findOne
* This is an internal function, use exec which uses the executor
*
* @param {Function} callback - Signature: err, results
@ -66,12 +67,12 @@ Cursor.prototype._exec = function(callback) {
if (this._skip && this._skip > skipped) {
skipped += 1;
} else {
res.push(model.deepCopy(candidates[i]));
res.push(candidates[i]);
added += 1;
if (this._limit && this._limit <= added) { break; }
}
} else {
res.push(model.deepCopy(candidates[i]));
res.push(candidates[i]);
}
}
}

@ -390,8 +390,14 @@ Datastore.prototype.count = function() {
*/
Datastore.prototype.find = function (query, callback) {
var cursor = new Cursor(this, query, function(err, docs, callback) {
var res = [], i;
if (err) { return callback(err); }
return callback(null, docs);
for (i = 0; i < docs.length; i += 1) {
res.push(model.deepCopy(docs[i]));
}
return callback(null, res);
});
if (typeof callback === 'function') {
@ -410,7 +416,7 @@ Datastore.prototype.findOne = function (query, callback) {
var cursor = new Cursor(this, query, function(err, docs, callback) {
if (err) { return callback(err); }
if (docs.length === 1) {
return callback(null, docs[0]);
return callback(null, model.deepCopy(docs[0]));
} else {
return callback(null, null);
}

@ -645,7 +645,7 @@ describe('Database', function () {
d.findOne({ a: { $gt: 14 } }).sort({ a: 1 }).skip(2).exec(function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
done();
});
});

Loading…
Cancel
Save