-
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): implement inclusionResolver for belongsTo relation #3721
Conversation
packages/repository-tests/src/crud/relations/acceptance/unit.test.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
ee9b621
to
a2817b0
Compare
packages/repository/src/__tests__/unit/repositories/relations-helpers/uniq.helpers.unit.ts
Outdated
Show resolved
Hide resolved
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.
Most of my comments posted for hasMany relations apply to this pull request too, please read them here: #3595 (review)
packages/repository-tests/src/crud/relations/acceptance/unit.test.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
Helpers for the inclusion resolver #3727 |
9e4f4e5
to
4225c24
Compare
docs/site/BelongsTo-relation.md
Outdated
through the following code: | ||
|
||
```ts | ||
addressRepo.find({include: [{relation: 'customer'}]}); |
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 be consistent here, either use order belongsTo customer
or address belongsTo customer
. Personally I prefer order belongsTo customer
.
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.
Yeah I think that would be more consistent with other tests.
packages/repository/src/relations/belongs-to/belongs-to.inclusion-resolver.ts
Show resolved
Hide resolved
...ory-tests/src/crud/relations/acceptance/belongs-to-inclusion-resolver.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
...ory-tests/src/crud/relations/acceptance/belongs-to-inclusion-resolver.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
...ory-tests/src/crud/relations/acceptance/belongs-to-inclusion-resolver.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
...s/repository/src/__tests__/unit/repositories/relations-helpers/relations-helpers-fixtures.ts
Outdated
Show resolved
Hide resolved
...s/repository/src/__tests__/unit/repositories/relations-helpers/relations-helpers-fixtures.ts
Outdated
Show resolved
Hide resolved
c407cbf
to
e09dfd9
Compare
packages/repository/src/relations/belongs-to/belongs-to.inclusion-resolver.ts
Outdated
Show resolved
Hide resolved
61895ff
to
bb6974a
Compare
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.
Most of the comments posted in #3595 (review) apply to this pull request too.
7471e50
to
f424c2a
Compare
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 quickly skimmed through the changes and don't see any obvious problems. Please get at least one more approval before landing.
f424c2a
to
f71c96f
Compare
...ory-tests/src/crud/relations/acceptance/belongs-to.inclusion-resolver.relation.acceptance.ts
Outdated
Show resolved
Hide resolved
} | ||
} | ||
interface ManufacturerRelations { | ||
products?: Product; |
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.
should be product?: ProductWithRelations
...s/repository/src/__tests__/unit/repositories/relations-helpers/relations-helpers-fixtures.ts
Outdated
Show resolved
Hide resolved
packages/repository/src/relations/belongs-to/belongs-to.inclusion-resolver.ts
Outdated
Show resolved
Hide resolved
02d5f7f
to
200f54a
Compare
description: "Odin's Coffee Maker", | ||
customerId: odin.id, | ||
}); | ||
|
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 add a test case to cover scope
? Such as:
{include: {relation: 'customer', scope: {fields: ['name']}}}
@@ -100,3 +161,7 @@ export function createCategory(properties: Partial<Category>) { | |||
export function createProduct(properties: Partial<Product>) { | |||
return new Product(properties as Product); | |||
} | |||
|
|||
export function createManufacturer(properties: Partial<Manufacturer>) { | |||
return new Manufacturer(properties as Manufacturer); |
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.
The constructor of Manufacturer
class takes in a partial manufacturer on line, any reason why specify the type as Manufacturer
again?
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.
Before we were passing in a single value. Will remove
}); | ||
expect( | ||
orderRepo.find({include: [{relation: 'customer'}], limit: 1}), | ||
).to.be.rejectedWith(`Invalid "scope is not supported`); |
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.
Oh, so the initial version does not support scope
?
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.
BTW, I was referring to the scope inside include
.
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.
No. See #3443
// the order of sourceIds matters | ||
const result = flattenTargetsOfOneToOneRelation( | ||
[erasers.categoryId, pencil.categoryId, pen.categoryId], | ||
[book, stationery, stationery], |
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.
Hmm, a question for flattenTargetsOfOneToOneRelation
, does it mean if I provide the second parameter in a random order, like [stationery, book, stationery]
, it still returns an array of the results according to the order of [erasers.categoryId, pencil.categoryId, pen.categoryId]
?
I am very confused about what this function does, could you elaborate more about it?
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.
Sure. This API doc of this function:
Returns an array of instances. The order of arrays is based on
the order of sourceIds
The second parameter is just a group of entities that we found by the foreign key (all related models), the order doesn't matter. So this function basically just matches each the passed in sourceId with its related instance.
In this case, if you pass in [pencil.categoryId, erasers.categoryId, pen.categoryId]
as the first param, the result will be [stationery, book, stationery]
.
And you will also find a function called flattenTargetsOfOneToManyRelation
in has many relation, it does the same thing except that one sourceId might have more than one instances.
ea98efe
to
8e860a5
Compare
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.
👏 Great effort @agnes512 and @nabdelgadir on implementing the inclusion story! Mostly LGTM just a few nitpick and question.
8e860a5
to
2a39041
Compare
Co-authored-by: Miroslav <[email protected]>
2a39041
to
aa13a8b
Compare
resolves #3448
This PR implements the inclusionResolver for
belongsTo
relation:flattenTargetsOfOneToOneRelation
and its unit tests forbelongsTo
andhasOne
relation ( since they both use this helper to implement inclusion resolver)createBelongsToInclusionResolver
into the belongsTo factorycreateBelongsToAccessorFor
createBelongsToInclusionResolver
inrepository-tests
.supportsInclusionResolvers
.belongsToInclusionResolver
, and the basic setup/usages.Suggested way to review:
-> start with the document file and
belongs-to-inclusion-resolver.acceptance.ts
to get the idea of theinclusion resolver
.-> review
belongs-to.inclusion-resolver.ts
to see the implementation details.-> review the rest of files.
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 👈