Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixup findByForeignKey #3516

Merged
merged 1 commit into from
Aug 7, 2019
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 @@ -19,27 +19,46 @@ describe('findByForeignKeys', () => {
await productRepo.deleteAll();
});

it('returns an empty array when no foreign keys are passed in', async () => {
const fkIds: number[] = [];
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', fkIds);
expect(products).to.be.empty();
});

it('returns an empty array when no instances have the foreign key value', async () => {
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', [2]);
const products = await findByForeignKeys(productRepo, 'categoryId', 2);
expect(products).to.be.empty();
});

it('returns an empty array when no instances have the foreign key values', async () => {
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', [2, 3]);
expect(products).to.be.empty();
});
it('returns all instances that have the foreign key value', async () => {
const pens = await productRepo.create({name: 'pens', categoryId: 1});
const pencils = await productRepo.create({name: 'pencils', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', [1]);
const products = await findByForeignKeys(productRepo, 'categoryId', 1);
expect(products).to.deepEqual([pens, pencils]);
});

it('does not include instances with different foreign key values', async () => {
const pens = await productRepo.create({name: 'pens', categoryId: 1});
const pencils = await productRepo.create({name: 'pencils', categoryId: 2});
const products = await findByForeignKeys(productRepo, 'categoryId', [1]);
const products = await findByForeignKeys(productRepo, 'categoryId', 1);
expect(products).to.deepEqual([pens]);
expect(products).to.not.containDeep(pencils);
});

it('includes instances when there is one value in the array of foreign key values', async () => {
const pens = await productRepo.create({name: 'pens', categoryId: 1});
const pencils = await productRepo.create({name: 'pencils', categoryId: 2});
const products = await findByForeignKeys(productRepo, 'categoryId', [2]);
expect(products).to.deepEqual([pencils]);
expect(products).to.not.containDeep(pens);
});
it('returns all instances that have any of multiple foreign key values', async () => {
const pens = await productRepo.create({name: 'pens', categoryId: 1});
const pencils = await productRepo.create({name: 'pencils', categoryId: 2});
Expand All @@ -61,6 +80,18 @@ describe('findByForeignKeys', () => {
expect(errorMessage).to.eql('scope is not supported');
});

it('does not throw an error if scope is passed in and is undefined or empty', async () => {
let products = await findByForeignKeys(
productRepo,
'categoryId',
[1],
undefined,
{},
);
expect(products).to.be.empty();
products = await findByForeignKeys(productRepo, 'categoryId', 1, {}, {});
expect(products).to.be.empty();
});
/******************* HELPERS *******************/

@model()
Expand Down
26 changes: 16 additions & 10 deletions packages/repository/src/relations/relation.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import {Entity, EntityCrudRepository, Filter, Options, Where} from '..';
*
* @param targetRepository - The target repository where the model instances are found
* @param fkName - Name of the foreign key
* @param fkValues - Array of the values of the foreign keys to be included
* @param fkValues - One value or array of values of the foreign key to be included
* @param scope - Additional scope constraints (not currently supported)
* @param options - Options for the operations
*/
export async function findByForeignKeys<
Target extends Entity,
TargetID,
TargetRelations extends object,
ForeignKey
ForeignKey extends StringKeyOf<Target>
>(
targetRepository: EntityCrudRepository<Target, TargetID, TargetRelations>,
fkName: StringKeyOf<Target>,
fkValues: ForeignKey[],
targetRepository: EntityCrudRepository<Target, unknown, TargetRelations>,
fkName: ForeignKey,
fkValues: Target[ForeignKey][] | Target[ForeignKey],
scope?: Filter<Target>,
options?: Options,
): Promise<(Target & TargetRelations)[]> {
Expand All @@ -33,12 +32,19 @@ export async function findByForeignKeys<
throw new Error('scope is not supported');
}

const where = ({
[fkName]: fkValues.length === 1 ? fkValues[0] : {inq: fkValues},
} as unknown) as Where<Target>;
let value;

if (Array.isArray(fkValues)) {
if (fkValues.length === 0) return [];
value = fkValues.length === 1 ? fkValues[0] : {inq: fkValues};
} else {
value = fkValues;
}

const where = ({[fkName]: value} as unknown) as Where<Target>;
const targetFilter = {where};

return targetRepository.find(targetFilter, options);
}

export type StringKeyOf<T> = Extract<keyof T, string>;
type StringKeyOf<T> = Extract<keyof T, string>;