Skip to content

Commit

Permalink
Able to use sync or async API with cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Jan 26, 2014
1 parent b8f641e commit b04a1d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
9 changes: 4 additions & 5 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Cursor.prototype.sort = function(sortQuery) {
*
* @param {Function} callback - Signature: err, results
*/
Cursor.prototype.exec = function(callback) {
Cursor.prototype._exec = function(callback) {
var candidates = this.db.getCandidates(this.query)
, res = [], added = 0, skipped = 0, self = this
, i
Expand Down Expand Up @@ -95,10 +95,9 @@ Cursor.prototype.exec = function(callback) {
return callback(null, res);
};

// Cursor.prototype.exec = function () {
// console.log("=============IN EXEC");
// this.db.executor.push({ this: this, fn: this._exec, arguments: arguments });
// };
Cursor.prototype.exec = function () {
this.db.executor.push({ this: this, fn: this._exec, arguments: arguments });
};



Expand Down
22 changes: 12 additions & 10 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Datastore.prototype.count = function() {
*
* @api private Use find
*/
Datastore.prototype._find = function (query, callback) {
Datastore.prototype.find = function (query, callback) {
var cursor = new Cursor(this, query);

if (typeof callback === 'function') {
Expand All @@ -400,9 +400,9 @@ Datastore.prototype._find = function (query, callback) {
}
};

Datastore.prototype.find = function () {
this.executor.push({ this: this, fn: this._find, arguments: arguments });
};
// Datastore.prototype.find = function () {
// this.executor.push({ this: this, fn: this._find, arguments: arguments });
// };


/**
Expand All @@ -411,7 +411,7 @@ Datastore.prototype.find = function () {
*
* @api private Use findOne
*/
Datastore.prototype._findOne = function (query, callback) {
Datastore.prototype.findOne = function (query, callback) {
var cursor = new Cursor(this, query);
cursor.limit(1);
cursor.exec(function (err, docs) {
Expand All @@ -423,9 +423,9 @@ Datastore.prototype._findOne = function (query, callback) {
});
};

Datastore.prototype.findOne = function () {
this.executor.push({ this: this, fn: this._findOne, arguments: arguments });
};
// Datastore.prototype.findOne = function () {
// this.executor.push({ this: this, fn: this._findOne, arguments: arguments });
// };


/**
Expand Down Expand Up @@ -457,9 +457,11 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
function (cb) { // If upsert option is set, check whether we need to insert the doc
if (!upsert) { return cb(); }

self._findOne(query, function (err, doc) {
// Need to use an internal function not tied to the executor to avoid deadlock
var cursor = new Cursor(self, query);
cursor.limit(1)._exec(function (err, docs) {
if (err) { return callback(err); }
if (doc) {
if (docs.length === 1) {
return cb();
} else {
return self._insert(model.modify(query, updateQuery), function (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ describe('Database', function () {
});
});

it.only('Can use sort, skip and limit if the callback is not passed to find but to exec', function (done) {
it('Can use sort, skip and limit if the callback is not passed to find but to exec', function (done) {
d.insert({ a: 2, hello: 'world' }, function () {
d.insert({ a: 24, hello: 'earth' }, function () {
d.insert({ a: 13, hello: 'blueplanet' }, function () {
Expand Down

0 comments on commit b04a1d2

Please sign in to comment.