From d85371f6c21016a45d863b1f9ef33e95871ca303 Mon Sep 17 00:00:00 2001 From: bnoguchi Date: Wed, 2 Mar 2011 15:58:43 -0800 Subject: [PATCH] Code changes to support new Query#exec and Query#run for running remembered query operations. --- lib/mongoose/model.js | 24 ++++++++++++------------ lib/mongoose/query.js | 27 ++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/mongoose/model.js b/lib/mongoose/model.js index 3acc6ec512c..a775ce0a03b 100644 --- a/lib/mongoose/model.js +++ b/lib/mongoose/model.js @@ -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; @@ -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); }; @@ -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; @@ -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); }; @@ -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; @@ -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); }; @@ -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; @@ -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); }; @@ -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); }; @@ -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); }; @@ -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; @@ -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); }; diff --git a/lib/mongoose/query.js b/lib/mongoose/query.js index 652bd75e34d..f9d719b6482 100644 --- a/lib/mongoose/query.js +++ b/lib/mongoose/query.js @@ -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 = {}; @@ -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; @@ -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; @@ -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); @@ -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;