Skip to content

Commit

Permalink
Code changes to support new Query#exec and Query#run for running reme…
Browse files Browse the repository at this point in the history
…mbered query operations.
  • Loading branch information
bnoguchi committed Mar 2, 2011
1 parent f5a8716 commit d85371f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
24 changes: 12 additions & 12 deletions lib/mongoose/model.js
Expand Up @@ -312,7 +312,7 @@ Model.remove = function (conditions, callback) {
callback = conditions;
conditions = {};
}
var query = new Query(conditions).bind(this);
var query = new Query(conditions).bind(this, 'remove');
if ('undefined' === typeof callback)
return query;
var cQuery;
Expand All @@ -322,7 +322,7 @@ Model.remove = function (conditions, callback) {
merge(query.options, cQuery.options);
delete this._cumulativeQuery;
}
if (!query.model) query.bind(this);
if (!query.model) query.bind(this, 'remove');
return query.remove(callback);
};

Expand Down Expand Up @@ -357,7 +357,7 @@ Model.find = function (conditions, fields, options, callback) {
options = null;
}

var query = new Query(conditions, options).select(fields).bind(this);
var query = new Query(conditions, options).select(fields).bind(this, 'find');

if ('undefined' === typeof callback)
return query;
Expand All @@ -372,7 +372,7 @@ Model.find = function (conditions, fields, options, callback) {
delete this._cumulativeQuery;
}
if (!query.model)
query.bind(this);
query.bind(this, 'find');
return query.find(callback);
};

Expand Down Expand Up @@ -420,7 +420,7 @@ Model.findOne = function (conditions, fields, options, callback) {
options = null;
}

var query = new Query(conditions, options).select(fields).bind(this);
var query = new Query(conditions, options).select(fields).bind(this, 'findOne');
if ('undefined' == typeof callback)
return query;
var cQuery;
Expand All @@ -432,7 +432,7 @@ Model.findOne = function (conditions, fields, options, callback) {
merge(query.options, cQuery.options);
delete this._cumulativeQuery;
}
if (!query.model) query.bind(this);
if (!query.model) query.bind(this, 'findOne');
return query.findOne(callback);
};

Expand All @@ -445,7 +445,7 @@ Model.findOne = function (conditions, fields, options, callback) {
*/

Model.count = function (conditions, callback) {
var query = new Query(conditions).bind(this);
var query = new Query(conditions).bind(this, 'count');
if ('undefined' == typeof callback)
return query;
var cQuery;
Expand All @@ -455,7 +455,7 @@ Model.count = function (conditions, callback) {
merge(query.options, cQuery.options);
delete this._cumulativeQuery;
}
if (!query.model) query.bind(this);
if (!query.model) query.bind(this, 'count');
return query.count(callback);
};

Expand All @@ -477,7 +477,7 @@ Model.count = function (conditions, callback) {
*/

Model.where = function (path, val) {
var q = new Query().bind(this);
var q = new Query().bind(this, 'find');
return q.where.apply(q, arguments);
};

Expand All @@ -493,7 +493,7 @@ Model.where = function (path, val) {
*/

Model.$where = function () {
var q = new Query().bind(this);
var q = new Query().bind(this, 'find');
return q.$where.apply(q, arguments);
};

Expand Down Expand Up @@ -564,7 +564,7 @@ Model.update = function (conditions, doc, options, callback) {
conditions = {};
}
}
var query = new Query(conditions, options).bind(this);
var query = new Query(conditions, options).bind(this, 'update', doc);
if ('undefined' == typeof callback)
return query;
var cQuery;
Expand All @@ -574,7 +574,7 @@ Model.update = function (conditions, doc, options, callback) {
merge(query.options, cQuery.options);
delete this._cumulativeQuery;
}
if (!query.model) query.bind(this);
if (!query.model) query.bind(this, 'update');
return query.update(doc, callback);
};

Expand Down
27 changes: 26 additions & 1 deletion lib/mongoose/query.js
Expand Up @@ -22,13 +22,33 @@ function Query (criteria, options) {
* @api public
*/

Query.prototype.bind = function (model) {
Query.prototype.bind = function (model, op) {
var ret = Object.create(this);
ret.model = model;
ret.op = op;
return ret;
};

Query.prototype.run =
Query.prototype.exec = function (op, callback) {
var args = arguments;
if ('function' === typeof op) {
callback = op;
if (this.op === 'update') {
return this[this.op](this._updateArg, callback);
}
return this[this.op](callback);
}
args = Array.prototype.slice(arguments, 1);
this.op = op;
if (op === 'update' && args.length === 1) {
args.unshift(this._updateArg);
}
this[this.op = op].apply(this, args);
};

Query.prototype.find = function (criteria, callback) {
this.op = 'find';
if ('function' === typeof criteria) {
callback = criteria;
criteria = {};
Expand Down Expand Up @@ -546,6 +566,7 @@ Query.prototype.execFind = function (callback) {
*/

Query.prototype.findOne = function (callback) {
this.op = 'findOne';
var model = this.model;
var options = this._optionsForExec(model);
options.fields = this._fields;
Expand Down Expand Up @@ -577,6 +598,7 @@ Query.prototype.findOne = function (callback) {
*/

Query.prototype.count = function (callback) {
this.op = 'count';
var model = this.model;
this.cast(model);
var castQuery = this._conditions;
Expand All @@ -594,6 +616,8 @@ Query.prototype.count = function (callback) {
*/

Query.prototype.update = function (doc, callback) {
this.op = 'update';
this._updateArg = doc;
var model = this.model
, options = this._optionsForExec(model);

Expand All @@ -619,6 +643,7 @@ Query.prototype.update = function (doc, callback) {
*/

Query.prototype.remove = function (callback) {
this.op = 'remove';
var model = this.model;
this.cast(model);
var castQuery = this._conditions;
Expand Down

0 comments on commit d85371f

Please sign in to comment.