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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repository): add findByForeignKeys function #3473

Merged
merged 1 commit into from Aug 6, 2019

Conversation

@nabdelgadir
Copy link
Contributor

commented Jul 30, 2019

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 machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

馃憠 Check out how to submit a PR 馃憟

@nabdelgadir nabdelgadir force-pushed the find-by-foreign-keys branch 6 times, most recently from 642d5dc to 0d77fb9 Jul 30, 2019

@nabdelgadir nabdelgadir changed the title Find by foreign keys feat(repository): add findByForeignKeys function Jul 31, 2019

@nabdelgadir nabdelgadir force-pushed the find-by-foreign-keys branch from 0d77fb9 to d37714a Jul 31, 2019

@nabdelgadir nabdelgadir marked this pull request as ready for review Jul 31, 2019

@nabdelgadir nabdelgadir requested review from bajtos and raymondfeng as code owners Jul 31, 2019

>(
targetRepository: EntityCrudRepository<Target, TargetID, TargetRelations>,
fkName: StringKeyOf<Target>,
fkValues: ForeignKey[],

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Jul 31, 2019

Member

Can we accept a single value too? For example:

fkValues: ForeignKey[] | ForeignKey,

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Jul 31, 2019

Member

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]?

This comment has been minimized.

Copy link
@agnes512

agnes512 Aug 1, 2019

Contributor

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?

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 1, 2019

Member

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)[]> {
}

@nabdelgadir nabdelgadir force-pushed the find-by-foreign-keys branch 3 times, most recently from f8645cc to af293c3 Aug 1, 2019

}

const value = Array.isArray(fkValues)
? fkValues.length === 1

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 2, 2019

Member

Let's add a shortcut for empty array to return [] immediately. Include a test to cover this corner case too.

This comment has been minimized.

Copy link
@nabdelgadir

nabdelgadir Aug 2, 2019

Author Contributor

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)

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 2, 2019

Member

You can do the following:

const fkIds: number[] = []; 
return targetRepository.find(targetFilter, options);
}

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

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 2, 2019

Member

Maybe it's not necessary to export this type?

@nabdelgadir nabdelgadir force-pushed the find-by-foreign-keys branch from af293c3 to d096fbd Aug 2, 2019

feat(repository): add function findByForeignKeys
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>

@agnes512 agnes512 force-pushed the find-by-foreign-keys branch from a3a7796 to 149f1a3 Aug 6, 2019

@agnes512 agnes512 merged commit f5eaf1d into master Aug 6, 2019

3 checks passed

clahub All contributors have signed the Contributor License Agreement.
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@delete-merged-branch delete-merged-branch bot deleted the find-by-foreign-keys branch Aug 6, 2019

@agnes512 agnes512 referenced this pull request Aug 7, 2019
1 of 7 tasks complete
@bajtos bajtos referenced this pull request Sep 2, 2019
7 of 7 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can鈥檛 perform that action at this time.