From c9be041ded59a409755d010e5ee64d53945eb65d Mon Sep 17 00:00:00 2001 From: Agnes Lin Date: Fri, 27 Dec 2019 10:03:58 -0500 Subject: [PATCH] fix: returns undefined --- .../relations/acceptance/belongs-to.relation.acceptance.ts | 4 ++-- .../src/relations/belongs-to/belongs-to-accessor.ts | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts b/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts index ff61d1700bb5..585a9921fb6e 100644 --- a/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts +++ b/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts @@ -93,7 +93,7 @@ export function belongsToRelationAcceptance( expect(result).to.deepEqual(shipment); }); - it('returns an empty array if the source instance does not have the foreign key', async () => { + it('returns undefined if the source instance does not have the foreign key', async () => { await shipmentRepo.create({ name: 'Tuesday morning shipment', }); @@ -102,7 +102,7 @@ export function belongsToRelationAcceptance( description: 'Order that is shipped Tuesday morning', }); const result = await orderRepo.shipment(order.id); - expect(result).to.deepEqual([]); + expect(result).to.be.undefined(); }); it('throws EntityNotFound error when the related model does not exist', async () => { diff --git a/packages/repository/src/relations/belongs-to/belongs-to-accessor.ts b/packages/repository/src/relations/belongs-to/belongs-to-accessor.ts index 7066ebfd2b9f..362e1cf9629c 100644 --- a/packages/repository/src/relations/belongs-to/belongs-to-accessor.ts +++ b/packages/repository/src/relations/belongs-to/belongs-to-accessor.ts @@ -53,9 +53,10 @@ export function createBelongsToAccessor< const primaryKey = meta.keyTo; const sourceModel = await sourceRepository.findById(sourceId); const foreignKeyValue = sourceModel[foreignKey as keyof Source]; - // different dbs use different empty values - if (foreignKeyValue === undefined || foreignKeyValue === null) { - return ([] as unknown) as Target; + // workaround to check referential integrity. + // should be removed once the memory connector ref integrity is done + if (!foreignKeyValue) { + return (undefined as unknown) as Target; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const constraint: any = {[primaryKey]: foreignKeyValue};