-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we accept a single value too? For example:
fkValues: ForeignKey[] | ForeignKey,
There was a problem hiding this comment.
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]> Co-authored-by: Miroslav Bajtoš <[email protected]>
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 test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈