Able to use sync or async API with cursor

pull/2/head
Louis Chatriot 11 years ago
parent b8f641e0fe
commit b04a1d23fe
  1. 9
      lib/cursor.js
  2. 22
      lib/datastore.js
  3. 2
      test/db.test.js

@ -50,7 +50,7 @@ Cursor.prototype.sort = function(sortQuery) {
* *
* @param {Function} callback - Signature: err, results * @param {Function} callback - Signature: err, results
*/ */
Cursor.prototype.exec = function(callback) { Cursor.prototype._exec = function(callback) {
var candidates = this.db.getCandidates(this.query) var candidates = this.db.getCandidates(this.query)
, res = [], added = 0, skipped = 0, self = this , res = [], added = 0, skipped = 0, self = this
, i , i
@ -95,10 +95,9 @@ Cursor.prototype.exec = function(callback) {
return callback(null, res); return callback(null, res);
}; };
// Cursor.prototype.exec = function () { Cursor.prototype.exec = function () {
// console.log("=============IN EXEC"); this.db.executor.push({ this: this, fn: this._exec, arguments: arguments });
// this.db.executor.push({ this: this, fn: this._exec, arguments: arguments }); };
// };

@ -390,7 +390,7 @@ Datastore.prototype.count = function() {
* *
* @api private Use find * @api private Use find
*/ */
Datastore.prototype._find = function (query, callback) { Datastore.prototype.find = function (query, callback) {
var cursor = new Cursor(this, query); var cursor = new Cursor(this, query);
if (typeof callback === 'function') { if (typeof callback === 'function') {
@ -400,9 +400,9 @@ Datastore.prototype._find = function (query, callback) {
} }
}; };
Datastore.prototype.find = function () { // Datastore.prototype.find = function () {
this.executor.push({ this: this, fn: this._find, arguments: arguments }); // this.executor.push({ this: this, fn: this._find, arguments: arguments });
}; // };
/** /**
@ -411,7 +411,7 @@ Datastore.prototype.find = function () {
* *
* @api private Use findOne * @api private Use findOne
*/ */
Datastore.prototype._findOne = function (query, callback) { Datastore.prototype.findOne = function (query, callback) {
var cursor = new Cursor(this, query); var cursor = new Cursor(this, query);
cursor.limit(1); cursor.limit(1);
cursor.exec(function (err, docs) { cursor.exec(function (err, docs) {
@ -423,9 +423,9 @@ Datastore.prototype._findOne = function (query, callback) {
}); });
}; };
Datastore.prototype.findOne = function () { // Datastore.prototype.findOne = function () {
this.executor.push({ this: this, fn: this._findOne, arguments: arguments }); // this.executor.push({ this: this, fn: this._findOne, arguments: arguments });
}; // };
/** /**
@ -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 function (cb) { // If upsert option is set, check whether we need to insert the doc
if (!upsert) { return cb(); } 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 (err) { return callback(err); }
if (doc) { if (docs.length === 1) {
return cb(); return cb();
} else { } else {
return self._insert(model.modify(query, updateQuery), function (err) { return self._insert(model.modify(query, updateQuery), function (err) {

@ -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: 2, hello: 'world' }, function () {
d.insert({ a: 24, hello: 'earth' }, function () { d.insert({ a: 24, hello: 'earth' }, function () {
d.insert({ a: 13, hello: 'blueplanet' }, function () { d.insert({ a: 13, hello: 'blueplanet' }, function () {

Loading…
Cancel
Save