From 03f9c24642d1d3ffa8cde55e46d10437ad2eccd4 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Fri, 19 Oct 2012 01:27:02 +0300 Subject: [PATCH 1/9] limit + skip Added filter limit + filter skip for query .all --- lib/adapters/cradle.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index d624652c..6dd13285 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -261,7 +261,8 @@ CradleAdapter.prototype.count = function(model, callback, where) { CradleAdapter.prototype.models = function(model, filter, callback, func) { this.client.all( - {include_docs: true}, + // maybe there is a better way to do this? + {include_docs:true, limit: filter.limit == null ? 9999999999 : filter.limit, skip: filter.skip == null ? 0 : filter.skip}, errorHandler(callback, function(res, cb) { var docs = res.map(function(doc) { return idealize(doc); From 0203d527287cdef99601ddaeaf9014ed39797b70 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Fri, 19 Oct 2012 23:49:01 +0300 Subject: [PATCH 2/9] Update lib/adapters/cradle.js fixed an issue where if no filters were passed in, the .all query would throw an exception --- lib/adapters/cradle.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index 6dd13285..aa462db0 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -260,9 +260,15 @@ 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 skip = 0; + if (filter != null) { + limit = filter.limit || limit; + skip = filter.skip ||skip; + } + this.client.all( - // maybe there is a better way to do this? - {include_docs:true, limit: filter.limit == null ? 9999999999 : filter.limit, skip: filter.skip == null ? 0 : filter.skip}, + {include_docs:true, limit: limit, skip: skip}, errorHandler(callback, function(res, cb) { var docs = res.map(function(doc) { return idealize(doc); From 2cc1896864dd05a081799af6ba7bab62605ce0c9 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Wed, 31 Oct 2012 01:30:55 +0200 Subject: [PATCH 3/9] using views speeds up the query by only fetching the documents for the model being queried. --- lib/adapters/cradle.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index aa462db0..9121ce38 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -260,24 +260,43 @@ 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( + // {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) { From 6f973f9fa5ea546efd07e9883f2dd07d06d8d79b Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Wed, 31 Oct 2012 01:34:39 +0200 Subject: [PATCH 4/9] 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) { From a3f30a112a95b0353385582f890cae3cb9852603 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Sun, 4 Nov 2012 21:30:08 +0200 Subject: [PATCH 5/9] - fixed typo in updateOrCreate method. - added views to do .all queries, it speeds up the query. --- lib/adapters/cradle.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index 9121ce38..8ee4f5b2 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -221,7 +221,7 @@ CradleAdapter.prototype.updateOrCreate = function(model, data, callback) { if(err) { this.create(model, data, callback); } else { - this.updateAttributes(mode, data.id, data, callback); + this.updateAttributes(model, data.id, data, callback); } }.bind(this) ) @@ -290,7 +290,7 @@ CradleAdapter.prototype.models = function(model, filter, callback, func) { // {include_docs:true, limit: limit, skip: skip}, // errorHandler(callback, function(res, cb) { // var docs = res.map(function(doc) { - // return idealize(doc); + // return idealize(doc); // }); // var filtered = filtering(docs, model, filter, this._models) From d248de7f9d76fd24eb5105953121ff6e38a319f3 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Sun, 4 Nov 2012 22:22:00 +0200 Subject: [PATCH 6/9] - added views to do .all queries, it speeds up the query. --- lib/adapters/cradle.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index aa462db0..f1d05519 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -260,24 +260,31 @@ 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; + 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))); + }); }; CradleAdapter.prototype.all = function(model, filter, callback) { From 75c96bdaa96542d62e9cb5040bc71325d6a5b315 Mon Sep 17 00:00:00 2001 From: Muneeb Samuels Date: Sun, 4 Nov 2012 22:24:15 +0200 Subject: [PATCH 7/9] - fixed typo in updateOrCreate method. --- lib/adapters/cradle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index f1d05519..cd1b74a7 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -221,7 +221,7 @@ CradleAdapter.prototype.updateOrCreate = function(model, data, callback) { if(err) { this.create(model, data, callback); } else { - this.updateAttributes(mode, data.id, data, callback); + this.updateAttributes(model, data.id, data, callback); } }.bind(this) ) From 885e9188f874ea6d5c16d820b7867646f1468f33 Mon Sep 17 00:00:00 2001 From: taiyoh Date: Mon, 5 Nov 2012 22:22:47 +0900 Subject: [PATCH 8/9] fixed type check strictly --- lib/adapters/redis2.js | 103 ++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/lib/adapters/redis2.js b/lib/adapters/redis2.js index 2dd4893d..7c7fb4b0 100644 --- a/lib/adapters/redis2.js +++ b/lib/adapters/redis2.js @@ -172,37 +172,98 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { cb(null, Number); }; +// BridgeToRedis.prototype.forDb = function (model, data) { +// var p = this._models[model].properties; +// for (var i in data) { +// if (p[i] && p[i].type.name === 'Date') { +// data[i] = data[i] && data[i].getTime ? data[i].getTime() : 0; +// } else if (p[i] && [ +// 'String', 'Text', 'Number', 'Boolean', 'Date' +// ].indexOf(p[i].type.name) === -1) { +// data[i] = JSON.stringify(data[i]); +// } +// } +// return data; +// }; + BridgeToRedis.prototype.forDb = function (model, data) { var p = this._models[model].properties; for (var i in data) { - if (p[i] && p[i].type.name === 'Date') { - data[i] = data[i] && data[i].getTime ? data[i].getTime() : 0; - } else if (p[i] && [ - 'String', 'Text', 'Number', 'Boolean', 'Date' - ].indexOf(p[i].type.name) === -1) { - data[i] = JSON.stringify(data[i]); + if (!p[i]) continue; + if (!data[i]) { + data[i] = ""; + continue; + } + switch (p[i].type.name) { + case "Date": + data[i] = data[i].getTime ? data[i].getTime().toString() : "0"; + break; + case "Number": + data[i] = data[i].toString(); + break; + case "Boolean": + data[i] = !!data[i] ? "1" : "0"; + break; + case "String": + case "Text": + break; + default: + data[i] = JSON.stringify(data[i]); } } return data; }; +// BridgeToRedis.prototype.fromDb = function (model, data) { +// var p = this._models[model].properties; +// for (var i in data) { +// if (p[i] && p[i].type.name === 'Date') { +// if (isNaN(parseInt(data[i]))) { +// data[i] = new Date(data[i]); +// } else { +// var ms = data[i]; +// data[i] = new Date(); +// data[i].setTime(ms); +// } +// } else if (p[i] && [ +// 'String', 'Text', 'Number', 'Boolean', 'Date' +// ].indexOf(p[i].type.name) === -1) { +// try { +// data[i] = JSON.parse(data[i]); +// } catch (e) {} +// } +// } +// return data; +// }; + BridgeToRedis.prototype.fromDb = function (model, data) { - var p = this._models[model].properties; + var p = this._models[model].properties, d; for (var i in data) { - if (p[i] && p[i].type.name === 'Date') { - if (isNaN(parseInt(data[i]))) { - data[i] = new Date(data[i]); - } else { - var ms = data[i]; - data[i] = new Date(); - data[i].setTime(ms); - } - } else if (p[i] && [ - 'String', 'Text', 'Number', 'Boolean', 'Date' - ].indexOf(p[i].type.name) === -1) { - try { - data[i] = JSON.parse(data[i]); - } catch (e) {} + if (!p[i]) continue; + if (!data[i]) { + data[i] = ""; + continue; + } + switch (p[i].type.name) { + case "Date": + d = new Date(data[i]); + d.setTime(data[i]); + data[i] = d; + break; + case "Number": + data[i] = Number(data[i]); + break; + case "Boolean": + data[i] = data[i] === "1"; + break; + default: + d = data[i]; + try { + data[i] = JSON.parse(data[i]); + } + catch(e) { + data[i] = d; + } } } return data; From 30e9983abff4374623fe7844439118d0712fd79c Mon Sep 17 00:00:00 2001 From: taiyoh Date: Mon, 5 Nov 2012 22:30:57 +0900 Subject: [PATCH 9/9] remove commentouted source --- lib/adapters/redis2.js | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/lib/adapters/redis2.js b/lib/adapters/redis2.js index 7c7fb4b0..53215b0f 100644 --- a/lib/adapters/redis2.js +++ b/lib/adapters/redis2.js @@ -172,20 +172,6 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { cb(null, Number); }; -// BridgeToRedis.prototype.forDb = function (model, data) { -// var p = this._models[model].properties; -// for (var i in data) { -// if (p[i] && p[i].type.name === 'Date') { -// data[i] = data[i] && data[i].getTime ? data[i].getTime() : 0; -// } else if (p[i] && [ -// 'String', 'Text', 'Number', 'Boolean', 'Date' -// ].indexOf(p[i].type.name) === -1) { -// data[i] = JSON.stringify(data[i]); -// } -// } -// return data; -// }; - BridgeToRedis.prototype.forDb = function (model, data) { var p = this._models[model].properties; for (var i in data) { @@ -214,28 +200,6 @@ BridgeToRedis.prototype.forDb = function (model, data) { return data; }; -// BridgeToRedis.prototype.fromDb = function (model, data) { -// var p = this._models[model].properties; -// for (var i in data) { -// if (p[i] && p[i].type.name === 'Date') { -// if (isNaN(parseInt(data[i]))) { -// data[i] = new Date(data[i]); -// } else { -// var ms = data[i]; -// data[i] = new Date(); -// data[i].setTime(ms); -// } -// } else if (p[i] && [ -// 'String', 'Text', 'Number', 'Boolean', 'Date' -// ].indexOf(p[i].type.name) === -1) { -// try { -// data[i] = JSON.parse(data[i]); -// } catch (e) {} -// } -// } -// return data; -// }; - BridgeToRedis.prototype.fromDb = function (model, data) { var p = this._models[model].properties, d; for (var i in data) {