From 3179c05b958b1754acac98188c545440d094de12 Mon Sep 17 00:00:00 2001 From: Jonathan Spies Date: Sun, 13 May 2012 23:28:37 -0500 Subject: [PATCH 1/2] added multiple sort for mongoose --- coverage.html | 61 +++++++++++++++++++++++++++++++++++++ lib/adapters/mongoose.js | 25 +++++++++------- test/common_test.js | 65 ++++++++++++++++++++++++++++++++++------ 3 files changed, 132 insertions(+), 19 deletions(-) create mode 100644 coverage.html diff --git a/coverage.html b/coverage.html new file mode 100644 index 00000000..945471e0 --- /dev/null +++ b/coverage.html @@ -0,0 +1,61 @@ + + + + + + + + + +
+
78% [310/938]
+
53% [116/530]
+ +
75% [81/314]
+
88% [25/52]
+
100% [5/10]
+
50% [4/12]
+
+ + diff --git a/lib/adapters/mongoose.js b/lib/adapters/mongoose.js index 4c0ab333..01a0a5e5 100644 --- a/lib/adapters/mongoose.js +++ b/lib/adapters/mongoose.js @@ -45,6 +45,7 @@ MongooseAdapter.prototype.define = function (descr) { Object.keys(descr.properties).forEach(function (key) { props[key] = descr.properties[key].type; if (props[key].name === 'Text') props[key] = String; + if (props[key].name === 'Object') props[key] = mongoose.Schema.Types.Mixed; }); var schema = new mongoose.Schema(props); this._models[descr.model.modelName] = mongoose.model(descr.model.modelName, schema); @@ -151,17 +152,21 @@ MongooseAdapter.prototype.all = function all(model, filter, callback) { }); } if (filter.order) { - var m = filter.order.match(/\s+(A|DE)SC$/); - var key = filter.order; - var reverse = false; - if (m) { - key = key.replace(/\s+(A|DE)SC$/, ''); - if (m[1] === 'DE') reverse = true; + var keys = filter.order; // can be Array or String + if (typeof(keys) == "string") { + keys = keys.split(','); } - if (reverse) { - query.desc(key); - } else { - query.asc(key); + var args = []; + + for(index in keys) { + var m = keys[index].match(/\s+(A|DE)SC$/); + + keys[index] = keys[index].replace(/\s+(A|DE)SC$/, ''); + if (m && m[1] === 'DE') { + query.sort(keys[index].trim(), -1); + } else { + query.sort(keys[index].trim(), 1); + } } } if (filter.limit) { diff --git a/test/common_test.js b/test/common_test.js index 98520adc..a1e8b695 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -59,11 +59,13 @@ function testOrm(schema) { approved: Boolean, joinedAt: Date, age: Number, - passwd: String + passwd: String, + extra: Object }); Post = schema.define('Post', { title: { type: String, length: 255, index: true }, + subject: { type: String }, content: { type: Text }, date: { type: Date, default: Date.now, index: true }, published: { type: Boolean, default: false } @@ -143,7 +145,7 @@ function testOrm(schema) { it('should be expoted to JSON', function (test) { test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json', date: 1})), - '{"id":1,"title":"hello, json","content":null,"date":1,"published":false,"userId":null}'); + '{"id":1,"title":"hello, json","subject":null,"content":null,"date":1,"published":false,"userId":null}'); test.done(); }); @@ -473,17 +475,23 @@ function testOrm(schema) { }); it('should handle ORDER clause', function (test) { - var titles = [ 'Title A', 'Title Z', 'Title M', 'Title B', 'Title C' ]; + var titles = [ { title: 'Title A', subject: "B" }, + { title: 'Title Z', subject: "A" }, + { title: 'Title M', subject: "C" }, + { title: 'Title A', subject: "A" }, + { title: 'Title B', subject: "A" }, + { title: 'Title C', subject: "D" }]; var isRedis = Post.schema.name === 'redis' || Post.schema.name === 'memory'; - var dates = isRedis ? [ 5, 9, 0, 17, 9 ] : [ + var dates = isRedis ? [ 5, 9, 0, 17, 10, 9 ] : [ new Date(1000 * 5 ), new Date(1000 * 9), new Date(1000 * 0), new Date(1000 * 17), + new Date(1000 * 10), new Date(1000 * 9) ]; titles.forEach(function (t, i) { - Post.create({title: t, date: dates[i]}, done); + Post.create({title: t.title, subject: t.subject, date: dates[i]}, done); }); var i = 0, tests = 0; @@ -493,18 +501,29 @@ function testOrm(schema) { doFilterAndSortReverseTest(); doStringTest(); doNumberTest(); + + if (schema.name == 'mongoose') { + doMultipleSortTest(); + doMultipleReverseSortTest(); + } } } + function compare(a, b) { + if (a.title < b.title) return -1; + if (a.title > b.title) return 1; + return 0; + } + // Post.schema.log = console.log; function doStringTest() { tests += 1; Post.all({order: 'title'}, function (err, posts) { if (err) console.log(err); - test.equal(posts.length, 5); - titles.sort().forEach(function (t, i) { - if (posts[i]) test.equal(posts[i].title, t); + test.equal(posts.length, 6); + titles.sort(compare).forEach(function (t, i) { + if (posts[i]) test.equal(posts[i].title, t.title); }); finished(); }); @@ -514,7 +533,7 @@ function testOrm(schema) { tests += 1; Post.all({order: 'date'}, function (err, posts) { if (err) console.log(err); - test.equal(posts.length, 5); + test.equal(posts.length, 6); dates.sort(numerically).forEach(function (d, i) { // fix inappropriated tz convert if (posts[i]) @@ -552,6 +571,34 @@ function testOrm(schema) { }); } + function doMultipleSortTest() { + tests += 1; + Post.all({order: "title ASC, subject ASC"}, function(err, posts) { + if (err) console.log(err); + test.equal(posts.length, 6); + test.equal(posts[0].title, "Title A"); + test.equal(posts[0].subject, "A"); + test.equal(posts[1].title, "Title A"); + test.equal(posts[1].subject, "B"); + test.equal(posts[5].title, "Title Z"); + finished(); + }); + } + + function doMultipleReverseSortTest() { + tests += 1; + Post.all({order: "title ASC, subject DESC"}, function(err, posts) { + if (err) console.log(err); + test.equal(posts.length, 6); + test.equal(posts[0].title, "Title A"); + test.equal(posts[0].subject, "B"); + test.equal(posts[1].title,"Title A"); + test.equal(posts[1].subject, "A"); + test.equal(posts[5].title, "Title Z"); + finished(); + }); + } + var fin = 0; function finished() { if (++fin === tests) { From 75f9373ae57c09af4c344d98f038a11519f2e01a Mon Sep 17 00:00:00 2001 From: Jonathan Spies Date: Thu, 17 May 2012 22:13:29 -0500 Subject: [PATCH 2/2] added custom collection name to mongoose --- lib/adapters/mongoose.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/adapters/mongoose.js b/lib/adapters/mongoose.js index 01a0a5e5..4758ffae 100644 --- a/lib/adapters/mongoose.js +++ b/lib/adapters/mongoose.js @@ -48,7 +48,7 @@ MongooseAdapter.prototype.define = function (descr) { if (props[key].name === 'Object') props[key] = mongoose.Schema.Types.Mixed; }); var schema = new mongoose.Schema(props); - this._models[descr.model.modelName] = mongoose.model(descr.model.modelName, schema); + this._models[descr.model.modelName] = mongoose.model(descr.model.modelName, schema, descr.settings.table || null); this.cache[descr.model.modelName] = {}; };