Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,44 @@ export function belongsToInclusionResolverAcceptance(
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('queries entities with null foreign key', async () => {
const customer = await customerRepo.create({
name: 'Thor',
});

// order with customer relation
const order1 = await orderRepo.create({
customerId: customer.id,
description: 'Take Out',
});

// order without customer relation
const order2 = await orderRepo.create({
description: 'Dine in',
});

const expected = [
{
...order1,
isShipped: features.emptyValue,
shipmentInfo: features.emptyValue,
customer: {
...customer,
parentId: features.emptyValue,
},
},
{
...order2,
customerId: features.emptyValue,
isShipped: features.emptyValue,
shipmentInfo: features.emptyValue,
},
];

const result = await orderRepo.find({include: [{relation: 'customer'}]});
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('throws error if the target repository does not have the registered resolver', async () => {
const customer = await customerRepo.create({name: 'customer'});
await orderRepo.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ export function createBelongsToInclusionResolver<
const sourceKey = relationMeta.keyFrom;
const sourceIds = entities.map(e => (e as AnyObject)[sourceKey]);
const targetKey = relationMeta.keyTo as StringKeyOf<Target>;
const dedupedSourceIds = deduplicate(sourceIds);

const targetRepo = await getTargetRepo();
const targetsFound = await findByForeignKeys(
targetRepo,
targetKey,
deduplicate(sourceIds),
dedupedSourceIds.filter(e => e),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a valid id value that's not truthy? For example, I can imagine 0 being a valid primary key. Your condition will filter such value out.

inclusion.scope as Filter<Target>,
options,
);
Expand Down