Implemented findByIds

This commit is contained in:
Fabien Franzen 2014-07-29 15:01:47 +02:00
parent 296bb0d73e
commit b18384459a
4 changed files with 109 additions and 2 deletions

View File

@ -14,6 +14,7 @@ var Relation = require('./relations.js');
var Inclusion = require('./include.js'); var Inclusion = require('./include.js');
var List = require('./list.js'); var List = require('./list.js');
var geo = require('./geo'); var geo = require('./geo');
var mergeQuery = require('./scope.js').mergeQuery;
var Memory = require('./connectors/memory').Memory; var Memory = require('./connectors/memory').Memory;
var utils = require('./utils'); var utils = require('./utils');
var fieldsToArray = utils.fieldsToArray; var fieldsToArray = utils.fieldsToArray;
@ -324,6 +325,47 @@ DataAccessObject.findById = function find(id, cb) {
}.bind(this)); }.bind(this));
}; };
DataAccessObject.findByIds = function(ids, cond, cb) {
if (typeof cond === 'function') {
cb = cond;
cond = {};
}
var pk = this.dataSource.idName(this.modelName) || 'id';
if (ids.length === 0) {
process.nextTick(function() { cb(null, []); });
return;
}
var filter = { where: {} };
filter.where[pk] = { inq: ids };
mergeQuery(filter, cond || {});
this.find(filter, function(err, results) {
cb(err, err ? results : this.sortByIds(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;
});
results.sort(function(x, y) {
var idA = (typeof x[pk] === 'object') ? x[pk].toString() : x[pk];
var idB = (typeof y[pk] === 'object') ? y[pk].toString() : y[pk];
var a = ids.indexOf(idA);
var b = ids.indexOf(idB);
if (a === -1 || b === -1) return 1; // last
if (a !== b) {
if (a > b) return 1;
if (a < b) return -1;
}
});
return results;
};
function convertNullToNotFoundError(ctx, cb) { function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb(); if (ctx.result !== null) return cb();

View File

@ -1425,7 +1425,7 @@ HasOne.prototype.related = function (refresh, params) {
} }
}; };
RelationDefinition.embedsMany = function hasMany(modelFrom, modelTo, params) { RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params) {
var thisClassName = modelFrom.modelName; var thisClassName = modelFrom.modelName;
params = params || {}; params = params || {};
if (typeof modelTo === 'string') { if (typeof modelTo === 'string') {
@ -1764,3 +1764,8 @@ EmbedsMany.prototype.build = HasOne.prototype.build = function(targetModelData)
return inst; return inst;
}; };
RelationDefinition.referencesMany = function referencesMany(modelFrom, modelTo, params) {
};

View File

@ -51,6 +51,45 @@ describe('basic-querying', function () {
}); });
describe('findById', function () {
before(function(done) {
var people = [
{ id: 1, name: 'a', vip: true },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd', vip: true },
{ id: 5, name: 'e' },
{ id: 6, name: 'f' }
];
User.destroyAll(function() {
User.create(people, done);
});
});
it('should query by ids', function (done) {
User.findByIds([3, 2, 1], function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['c', 'b', 'a']);
done();
});
});
it('should query by ids and condition', function (done) {
User.findByIds([4, 3, 2, 1],
{ where: { vip: true } }, function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['d', 'a']);
done();
});
});
});
describe('find', function () { describe('find', function () {
before(seed); before(seed);

View File

@ -1681,7 +1681,7 @@ describe('relations', function () {
scope: { include: 'linked' } scope: { include: 'linked' }
}); });
Link.belongsTo('linked', { Link.belongsTo('linked', {
polymorphic: true, polymorphic: true, // needs unique auto-id
properties: { name: 'name' } // denormalized properties: { name: 'name' } // denormalized
}); });
db.automigrate(done); db.automigrate(done);
@ -1787,4 +1787,25 @@ describe('relations', function () {
}); });
describe('referencesMany', function () {
before(function (done) {
db = getSchema();
Category = db.define('Category', {name: String});
Product = db.define('Product', {name: String});
db.automigrate(function () {
Category.destroyAll(function() {
Product.destroyAll(done);
});
});
});
it('can be declared', function (done) {
Category.referencesMany(Product);
db.automigrate(done);
});
});
}); });