Skip to content

Commit

Permalink
fix(repository): belongsto accessor should return undefined if foreig…
Browse files Browse the repository at this point in the history
…n key is not included
  • Loading branch information
Agnes Lin committed Dec 30, 2019
1 parent 730807e commit f45ce03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export function belongsToRelationAcceptance(
expect(result).to.deepEqual(shipment);
});

it('returns undefined if the source instance does not have the foreign key', async () => {
await shipmentRepo.create({
name: 'Tuesday morning shipment',
});
const order = await orderRepo.create({
// doesn't have the foreign key
description: 'Order that is shipped Tuesday morning',
});
const result = await orderRepo.shipment(order.id);
expect(result).to.be.undefined();
});

it('throws EntityNotFound error when the related model does not exist', async () => {
const deletedCustomer = await customerRepo.create({
name: 'Order McForder',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export function createBelongsToAccessor<
const primaryKey = meta.keyTo;
const sourceModel = await sourceRepository.findById(sourceId);
const foreignKeyValue = sourceModel[foreignKey as keyof Source];
// workaround to check referential integrity.
// should be removed once the memory connector ref integrity is done
// GH issue: https://github.com/strongloop/loopback-next/issues/2333
if (!foreignKeyValue) {
return (undefined as unknown) as Target;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const constraint: any = {[primaryKey]: foreignKeyValue};
const constrainedRepo = new DefaultBelongsToRepository(
Expand Down

0 comments on commit f45ce03

Please sign in to comment.