From 6f973f9fa5ea546efd07e9883f2dd07d06d8d79b Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Wed, 31 Oct 2012 01:34:39 +0200 Subject: [PATCH] cradle adapter update using views speeds up the query by only fetching the documents for the model being queried. --- lib/adapters/cradle.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index aa462db0..e660a630 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -260,24 +260,44 @@ CradleAdapter.prototype.count = function(model, callback, where) { }; CradleAdapter.prototype.models = function(model, filter, callback, func) { - var limit = 99999999; // maybe there is a better way to do this? + var limit = 200; var skip = 0; if (filter != null) { limit = filter.limit || limit; skip = filter.skip ||skip; } - this.client.all( - {include_docs:true, limit: limit, skip: skip}, - errorHandler(callback, function(res, cb) { - var docs = res.map(function(doc) { - return idealize(doc); - }); - var filtered = filtering(docs, model, filter, this._models) + var self = this; - func ? func(filtered, cb) : cb(filtered); - }.bind(this)) - ); + self.client.save('_design/'+model, { + views : { + all : { + map : 'function(doc) { if (doc.nature == "'+model+'") { emit(doc._id, doc); } }' + } + } + }, function() { + self.client.view(model+'/all', {include_docs:true, limit:limit, skip:skip}, errorHandler(callback, function(res, cb) { + var docs = res.map(function(doc) { + return idealize(doc); + }); + var filtered = filtering(docs, model, filter, this._models) + + func ? func(filtered, cb) : cb(filtered); + }.bind(self))); + }); + +// this.client.all( +// // maybe there is a better way to do this? +// {include_docs:true, limit: limit, skip: skip}, +// errorHandler(callback, function(res, cb) { +// var docs = res.map(function(doc) { +// return idealize(doc); +// }); +// var filtered = filtering(docs, model, filter, this._models) +// +// func ? func(filtered, cb) : cb(filtered); +// }.bind(this)) +// ); }; CradleAdapter.prototype.all = function(model, filter, callback) {