diff --git a/lib/dao.js b/lib/dao.js index f2e55a26..6595a576 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -341,41 +341,10 @@ DataAccessObject.findByIds = function(ids, cond, cb) { filter.where[pk] = { inq: ids }; mergeQuery(filter, cond || {}); this.find(filter, function(err, results) { - cb(err, err ? results : this.sortByIds(ids, results)); + cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results)); }.bind(this)); }; -DataAccessObject.sortByIds = function(ids, results) { - var pk = this.dataSource.idName(this.modelName) || 'id'; - ids = ids.map(function(id) { - return (typeof id === 'object') ? id.toString() : id; - }); - - var indexOf = function(x) { - var id = (typeof x[pk] === 'object') ? x[pk].toString() : x[pk]; - return ids.indexOf(id); - }; - - var heading = []; - var tailing = []; - - results.forEach(function(x) { - var idx = indexOf(x); - idx === -1 ? tailing.push(x) : heading.push(x); - }); - - heading.sort(function(x, y) { - var a = indexOf(x); - var b = indexOf(y); - if (a === -1 || b === -1) return 1; // last - if (a === b) return 0; - if (a > b) return 1; - if (a < b) return -1; - }); - - return heading.concat(tailing); -}; - function convertNullToNotFoundError(ctx, cb) { if (ctx.result !== null) return cb(); diff --git a/lib/utils.js b/lib/utils.js index 520cf462..087cd615 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,6 +6,7 @@ exports.parseSettings = parseSettings; exports.mergeSettings = mergeSettings; exports.isPlainObject = isPlainObject; exports.defineCachedRelations = defineCachedRelations; +exports.sortObjectsByIds = sortObjectsByIds; var traverse = require('traverse'); @@ -203,4 +204,39 @@ function defineCachedRelations(obj) { function isPlainObject(obj) { return (typeof obj === 'object') && (obj !== null) && (obj.constructor === Object); -} \ No newline at end of file +} + + + +function sortObjectsByIds(idName, ids, objects) { + ids = ids.map(function(id) { + return (typeof id === 'object') ? id.toString() : id; + }); + + var indexOf = function(x) { + var isObj = (typeof x[idName] === 'object'); // ObjectID + var id = isObj ? x[idName].toString() : x[idName]; + return ids.indexOf(id); + }; + + var heading = []; + var tailing = []; + + objects.forEach(function(x) { + if (typeof x === 'object') { + var idx = indexOf(x); + idx === -1 ? tailing.push(x) : heading.push(x); + } + }); + + heading.sort(function(x, y) { + var a = indexOf(x); + var b = indexOf(y); + if (a === -1 || b === -1) return 1; // last + if (a === b) return 0; + if (a > b) return 1; + if (a < b) return -1; + }); + + return heading.concat(tailing); +}; diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 184335e1..dd26928a 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -87,24 +87,6 @@ describe('basic-querying', function () { done(); }); }); - - it('should sortByIds', function(done) { - User.find(function(err, users) { - sorted = User.sortByIds([6, 5, 4, 3, 2, 1], users); - var names = sorted.map(function(u) { return u.name; }); - names.should.eql(['f', 'e', 'd', 'c', 'b', 'a']); - done(); - }); - }); - - it('should sortByIds - partial ids', function(done) { - User.find(function(err, users) { - sorted = User.sortByIds([5, 3, 2], users); - var names = sorted.map(function(u) { return u.name; }); - names.should.eql(['e', 'c', 'b', 'a', 'd', 'f']); - done(); - }); - }); }); diff --git a/test/util.test.js b/test/util.test.js index 81de4a75..37da6fe5 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -3,6 +3,7 @@ var utils = require('../lib/utils'); var fieldsToArray = utils.fieldsToArray; var removeUndefined = utils.removeUndefined; var mergeSettings = utils.mergeSettings; +var sortObjectsByIds = utils.sortObjectsByIds; describe('util.fieldsToArray', function () { it('Turn objects and strings into an array of fields to include when finding models', function () { @@ -185,4 +186,29 @@ describe('mergeSettings', function () { should.deepEqual(dst.acls, expected.acls, 'Merged settings should match the expectation'); }); +}); + +describe('sortObjectsByIds', function () { + + var items = [ + { id: 1, name: 'a' }, + { id: 2, name: 'b' }, + { id: 3, name: 'c' }, + { id: 4, name: 'd' }, + { id: 5, name: 'e' }, + { id: 6, name: 'f' } + ]; + + it('should sort', function() { + var sorted = sortObjectsByIds('id', [6, 5, 4, 3, 2, 1], items); + var names = sorted.map(function(u) { return u.name; }); + should.deepEqual(names, ['f', 'e', 'd', 'c', 'b', 'a']); + }); + + it('should sort - partial ids', function() { + var sorted =sortObjectsByIds('id', [5, 3, 2], items); + var names = sorted.map(function(u) { return u.name; }); + should.deepEqual(names, ['e', 'c', 'b', 'a', 'd', 'f']); + }); + }); \ No newline at end of file