Skip to content

Commit

Permalink
Fixes #140 Return empty list when no join-instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jan 2, 2014
1 parent 7b18e2d commit a5cff8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/adapters/sql/sqlite.js
Expand Up @@ -67,6 +67,9 @@ utils.mixin(Adapter.prototype, new (function () {
// like Postgres/MySQL, where it takes a single final callback,
// and emits an event per-row
this.all = function (query, callback) {
if (model.log) {
model.log(query);
}
var rows = []
, emitter = new EventEmitter();
this.client.each(query, function (err, row) {
Expand Down
11 changes: 9 additions & 2 deletions lib/association/index.js
Expand Up @@ -121,6 +121,7 @@ association = new (function () {
// -----------
// FIXME: This is pretty terrible -- should really do these
// async queries in some sort of composable Promisey API
// TODO: Optimize SQL adapters by using eager-fetch w. join
// -----------
// Through's -- get the join-model instances, and re-fetch
// actual assns
Expand All @@ -147,8 +148,14 @@ association = new (function () {
else {
idParam = item[idColName];
}
query.id = idParam;
model[modelName][queryName](query, opts, callback);
// No join-instances, no associated items
if (!idParam.length) {
callback(null, []);
}
else {
query.id = idParam;
model[modelName][queryName](query, opts, callback);
}
});
}
// Normal assns, just do the damn query
Expand Down
11 changes: 11 additions & 0 deletions test/integration/adapters/shared.js
Expand Up @@ -835,6 +835,17 @@ tests = {
});
}

, 'test named hasMany/through association, no associated objects': function (next) {
model.Event.all(function (err, data) {
if (err) { throw err; }
data[0].getParticipants(function (err, data) {
if (err) { throw err; }
assert.equal(0, data.length);
next();
});
});
}

, 'test named hasMany/through with same model (reflexive association)': function (next) {
model.Person.all(function (err, data) {
if (err) { throw err; }
Expand Down

0 comments on commit a5cff8f

Please sign in to comment.