Model.findOne, mongoose reverse sort, closes #38

This commit is contained in:
Anatoliy Chakkaev 2012-03-06 16:46:18 +04:00
parent 53f39a046a
commit 639820bf29
4 changed files with 93 additions and 2 deletions

View File

@ -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)) {

View File

@ -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);

View File

@ -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"
},

View File

@ -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);