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

Add findByForeignKeys helper (initial version) #3443

Closed
bajtos opened this issue Jul 26, 2019 · 0 comments · Fixed by #3473
Closed

Add findByForeignKeys helper (initial version) #3443

bajtos opened this issue Jul 26, 2019 · 0 comments · Fixed by #3473
Assignees
Labels
feature Relations Model relations (has many, etc.) Repository Issues related to @loopback/repository package

Comments

@bajtos
Copy link
Member

bajtos commented Jul 26, 2019

Implement a new helper function findByForeignKeys, it will become a part of a public API of @loopback/repository.

Signature:

function findByForeignKeys<
  Target extends Entity,
  TargetID,
  TargetRelations extends object,
  ForeignKey
>(
  targetRepository: EntityCrudRepository<Target, TargetID, TargetRelations>,
  fkName: StringKeyOf<Target>,
  fkValues: ForeignKey[],
  scope?: Filter<Target>,
  options?: Options,
): Promise<(Target & TargetRelations)[]>;

The initial version should be simple, "inq splitting" and additional "scope" constraints are out of scope of this task.

Example implementation - I think it pretty much covers the full scope of this task, but obviously we need to add test coverage as explained in the acceptance criteria. It would be also great to throw an error if scope argument was provided, because we don't support scopes yet.

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

return targetRepository.find(targetFilter, options));

See #3387 for more details & a prototype implementation.

Acceptance criteria

Beside the implementation, it's important to create testing infrastructure that will make it easy to add new tests in the future. There should be two kinds of tests:

  • Unit-level tests inside packages/repository, these tests should mock targetRepository. By using a mock repository, we can assert on how many queries are called, inject errors to verify how are they handled, etc.
  • Integration-level tests inside packages/repository-tests, these tests will call real repository class & database, we will invoke them against different connectors (MySQL, MongoDB, and so on).

Out of scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Relations Model relations (has many, etc.) Repository Issues related to @loopback/repository package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants