feat(repository): add findByForeignKeys function#3473
Conversation
642d5dc to
0d77fb9
Compare
0d77fb9 to
d37714a
Compare
| >( | ||
| targetRepository: EntityCrudRepository<Target, TargetID, TargetRelations>, | ||
| fkName: StringKeyOf<Target>, | ||
| fkValues: ForeignKey[], |
There was a problem hiding this comment.
Can we accept a single value too? For example:
fkValues: ForeignKey[] | ForeignKey,There was a problem hiding this comment.
Is it possible to have a type param such as FK extends StringKeyOf<Target> and use it to constrain fk values, such as Target[FK]?
There was a problem hiding this comment.
Let me reword it to make sure that I understand the type constrains in tsc correctly:
if we do foreignKey extends StringKeyOf<Target>, it would constrain the passed in foreignKey to be a string-like <Target> instance?
There was a problem hiding this comment.
I'm proposing the following:
export async function findByForeignKeys<
Target extends Entity,
FK extends StringKeyOf<Target>,
TargetRelations extends object,
>(
targetRepository: EntityCrudRepository<Target, unknown, TargetRelations>,
fkName: FK,
fkValues: Target[FK][],
scope?: Filter<Target>,
options?: Options,
): Promise<(Target & TargetRelations)[]> {
}f8645cc to
af293c3
Compare
| } | ||
|
|
||
| const value = Array.isArray(fkValues) | ||
| ? fkValues.length === 1 |
There was a problem hiding this comment.
Let's add a shortcut for empty array to return [] immediately. Include a test to cover this corner case too.
There was a problem hiding this comment.
If we pass in an empty array, it complains that Type 'never[]' is not assignable to type 'number'. (or whatever the type of the foreign key value is)
There was a problem hiding this comment.
You can do the following:
const fkIds: number[] = []; | return targetRepository.find(targetFilter, options); | ||
| } | ||
|
|
||
| export type StringKeyOf<T> = Extract<keyof T, string>; |
There was a problem hiding this comment.
Maybe it's not necessary to export this type?
af293c3 to
d096fbd
Compare
Implemented initial version of the new helper function findByForeignKeys that finds model instances that contain any of the provided foreign key values. Co-authored-by: Agnes Lin <agneslin.lin@ibm.com> Co-authored-by: Miroslav Bajtoš <mbajtoss@gmail.com>
a3a7796 to
149f1a3
Compare
Implemented initial version of the new helper function
findByForeignKeys.Resolves #3443.
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈