Skip to content
This repository was archived by the owner on Apr 17, 2020. It is now read-only.

Commit 29b97e8

Browse files
author
Joel Mukuthu
committed
fix: include referenced model as null when no rows are matched
to stay consistent with `Query.prototype.fetch` et al
1 parent 7e55ad5 commit 29b97e8

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lib/KnormRelations.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,25 @@ class KnormRelations {
249249

250250
if (this.options.joins) {
251251
this.options.joins.forEach(join => {
252+
const as = join.options.as;
253+
254+
if (join.options.first && parsedRow[as] !== undefined) {
255+
return;
256+
}
257+
252258
const data = join.parseRow(
253259
row,
254260
Object.assign({ isNested: true }, options)
255261
);
256-
if (data) {
257-
const as = join.options.as;
258-
if (join.options.first) {
259-
if (!parsedRow[as]) {
260-
parsedRow[as] = data;
261-
}
262-
} else {
262+
263+
if (join.options.first) {
264+
parsedRow[as] = data;
265+
} else {
266+
if (data) {
263267
parsedRow[as] = parsedRow[as] || [];
264268
parsedRow[as].push(data);
269+
} else if (!parsedRow[as]) {
270+
parsedRow[as] = data;
265271
}
266272
}
267273
});
@@ -279,6 +285,7 @@ class KnormRelations {
279285

280286
if (this.options.joins) {
281287
const parsedRows = Object.values(this.parsedRows);
288+
// this is needed for `distinct` without an `id` column
282289
if (parsedRows.length) {
283290
rows = parsedRows;
284291
}

test/KnormRelations.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe('KnormRelations', () => {
257257
name: 'User 1',
258258
image: [new Image({ id: 1, userId: 1, categoryId: 1 })]
259259
}),
260-
new User({ name: 'User 2' })
260+
new User({ name: 'User 2', image: null })
261261
]
262262
)
263263
);
@@ -306,7 +306,7 @@ describe('KnormRelations', () => {
306306
name: 'User 1',
307307
image: [new Image({ id: 1 })]
308308
}),
309-
new User({ id: 2, name: 'User 2' })
309+
new User({ id: 2, name: 'User 2', image: null })
310310
]
311311
);
312312
});
@@ -326,20 +326,21 @@ describe('KnormRelations', () => {
326326
new User({
327327
id: 2,
328328
name: 'User 2',
329-
confirmed: true
329+
confirmed: true,
330+
image: null
330331
})
331332
]
332333
);
333334
});
334335

335-
it('does not include the joined model if no rows were matched', async () => {
336+
it('includes the joined model as `null` if no rows were matched', async () => {
336337
const query = new Query(User)
337338
.leftJoin(new Query(Image))
338339
.where({ id: 2 });
339340
await expect(
340341
query.fetch(),
341342
'to be fulfilled with sorted rows satisfying',
342-
[new User({ id: 2, name: 'User 2' })]
343+
[new User({ id: 2, name: 'User 2', image: null })]
343344
);
344345
});
345346

@@ -461,7 +462,7 @@ describe('KnormRelations', () => {
461462
await expect(
462463
query.fetch(),
463464
'to be fulfilled with sorted rows satisfying',
464-
[{ messages: undefined }, { messages: undefined }]
465+
[{ messages: null }, { messages: null }]
465466
);
466467
});
467468

@@ -772,9 +773,8 @@ describe('KnormRelations', () => {
772773

773774
await expect(
774775
query.fetch(),
775-
// TODO: hmmmmmmm
776776
'to be fulfilled with sorted rows exhaustively satisfying',
777-
[{ image: undefined }]
777+
[{ image: null }]
778778
);
779779
});
780780

0 commit comments

Comments
 (0)