From 639820bf29b5d433d4063dc21cabfc176025bf26 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Tue, 6 Mar 2012 16:46:18 +0400 Subject: [PATCH] Model.findOne, mongoose reverse sort, closes #38 --- lib/abstract-class.js | 12 +++++++ lib/adapters/mongoose.js | 13 +++++++- package.json | 2 +- test/common_test.js | 68 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index a32b1430..402de391 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -225,6 +225,18 @@ AbstractClass.all = function all(params, cb) { }); }; +AbstractClass.findOne = function findOne(params, cb) { + if (typeof params === 'function') { + cb = params; + params = {}; + } + params.limit = 1; + this.all(params, function (err, collection) { + if (err || !collection || !collection.length > 0) return cb(err); + cb(err, collection[0]); + }); +}; + function substractDirtyAttributes(object, data) { Object.keys(object.toObject()).forEach(function (attr) { if (data.hasOwnProperty(attr) && object.propertyChanged(attr)) { diff --git a/lib/adapters/mongoose.js b/lib/adapters/mongoose.js index 0f4f69d9..d817c39f 100644 --- a/lib/adapters/mongoose.js +++ b/lib/adapters/mongoose.js @@ -138,7 +138,18 @@ MongooseAdapter.prototype.all = function all(model, filter, callback) { }); } if (filter.order) { - query.asc(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; + } + if (reverse) { + query.desc(key); + } else { + query.asc(key); + } } if (filter.limit) { query.limit(filter.limit); diff --git a/package.json b/package.json index e0c1101b..2637d879 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jugglingdb", "author": "Anatoliy Chakkaev", "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite", - "version": "0.1.1", + "version": "0.1.2", "repository": { "url": "https://github.com/1602/jugglingdb" }, diff --git a/test/common_test.js b/test/common_test.js index b0dd34e0..c88ea0ff 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -53,6 +53,7 @@ function testOrm(schema) { User = schema.define('User', { name: String, + email: String, bio: Text, approved: Boolean, joinedAt: Date, @@ -618,6 +619,58 @@ function testOrm(schema) { } }); + it('should handle order clause with direction', function (test) { + var wait = 0; + var emails = [ + 'john@hcompany.com', + 'tom@hcompany.com', + 'admin@hcompany.com', + 'tin@hcompany.com', + 'mike@hcompany.com', + 'susan@hcompany.com', + 'test@hcompany.com' + ]; + User.destroyAll(function () { + emails.forEach(function (email) { + wait += 1; + User.create({email: email, name: 'Nick'}, done); + }); + }); + var tests = 2; + function done() { + process.nextTick(function () { + if (--wait === 0) { + doSortTest(); + doReverseSortTest(); + } + }); + } + + function doSortTest() { + User.all({order: 'email ASC', where: {name: 'Nick'}}, function (err, users) { + var _emails = emails.sort(); + users.forEach(function (user, i) { + test.equal(_emails[i], user.email, 'ASC sorting'); + }); + testDone(); + }); + } + + function doReverseSortTest() { + User.all({order: 'email DESC', where: {name: 'Nick'}}, function (err, users) { + var _emails = emails.sort().reverse(); + users.forEach(function (user, i) { + test.equal(_emails[i], user.email, 'DESC sorting'); + }); + testDone(); + }); + } + + function testDone() { + if (--tests === 0) test.done(); + } + }); + it('should return id in find result even after updateAttributes', function (test) { Post.create(function (err, post) { var id = post.id; @@ -642,6 +695,21 @@ function testOrm(schema) { test.done(); }); + it('should query one record', function (test) { + Post.findOne(function (err, post) { + test.ok(post.id); + Post.findOne({ where: { title: 'hey' } }, function (err, post) { + if (err) throw err; + test.equal(post.constructor.modelName, 'Post'); + test.equal(post.title, 'hey'); + Post.findOne({ where: { title: 'not exists' } }, function (err, post) { + test.ok(typeof post === 'undefined'); + test.done(); + }); + }); + }); + }); + it('all tests done', function (test) { test.done(); process.nextTick(allTestsDone);