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

feat(repository): implement inclusionResolver for hasMany relation #3595

Merged
merged 1 commit into from
Sep 20, 2019

Conversation

agnes512
Copy link
Contributor

@agnes512 agnes512 commented Aug 23, 2019

resolves #3447

This PR implements the inclusionResolver for hasMany relation:

  • Add a helper flattenTargetsOfOneToManyRelation and its unit tests for hasMany
  • Add a new function createHasManyInclusionResolver to hasMany factory.
  • Add tests for hasManyInclusionResolver in repository-tests.
  • Add docs to explain the idea of hasManyInclusionResolver, and the basic setup/usages.

Suggested way to review:
-> start with the document file and hasmany-inclusion-resolver.acceptance.ts to get the idea of the inclusion resolver
-> review has-many.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 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 👈

@agnes512 agnes512 force-pushed the hasmany-resolver branch 3 times, most recently from 6bf98c6 to c74d6b6 Compare August 29, 2019 14:22
@agnes512 agnes512 force-pushed the hasmany-resolver branch 5 times, most recently from e2ba1c4 to fe46284 Compare September 6, 2019 02:34
@agnes512 agnes512 force-pushed the hasmany-resolver branch 6 times, most recently from c545033 to 6866dd7 Compare September 10, 2019 23:49
@agnes512 agnes512 marked this pull request as ready for review September 10, 2019 23:50
docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
]
```

- You can delete a relation from `inclusionResolvers` to disable the inclusion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this doing exactly? It is not clear to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to show the way to disable a certain inclusion resolver. Because not all models are traversable.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look reasonable at very high level, let's improve implementation details now.

docs/site/HasMany-relation.md Outdated Show resolved Hide resolved
`Invalid "filter.include" entries: {"relation":"orders"}`,
);
// reset
customerRepo.inclusionResolvers.set(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. If await expect above throws, then this line is never called.

I believe it should be safe to simply remove this line, because orders resolver is always initialized from the beforeEach hook (see lines 74-77 above).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. If await expect above throws, then this line is never called.

I believe it should be safe to simply remove this line, because orders resolver is always initialized from the beforeEach hook (see lines 74-77 above).

☝️

This comment haven't been addressed yet.

@agnes512
Copy link
Contributor Author

agnes512 commented Sep 13, 2019

Helpers for the inclusion resolver #3727

keyFrom is implemented in PR #3726

@agnes512 agnes512 force-pushed the hasmany-resolver branch 3 times, most recently from ff03493 to aeda4b0 Compare September 17, 2019 21:55
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good, let's improve the tests please.

`Invalid "filter.include" entries: {"relation":"orders"}`,
);
// reset
customerRepo.inclusionResolvers.set(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. If await expect above throws, then this line is never called.

I believe it should be safe to simply remove this line, because orders resolver is always initialized from the beforeEach hook (see lines 74-77 above).

☝️

This comment haven't been addressed yet.

expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('returns related instances to target models via findById() method', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please introduce a new describe block to group findById tests and apply the comments I posted for find tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed find* from all titles as my intention was to show different use cases instead of testing find* here.

];
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing the following test case for find method:

  • Create two customers
  • Create one order for each customer
  • Run find with a filter limiting the results to only one of the customers
  • Verify that data from the other customer (customer, order) did not leak into the results

Copy link
Contributor Author

@agnes512 agnes512 Sep 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I test this case in that findById() test case. Checks the result has correct instance and its related models.
Since I don't want the test cases in this file look like testing find* methods, I will reword the titles to show the intention of each test case.

@agnes512 agnes512 force-pushed the hasmany-resolver branch 3 times, most recently from 061af6b to d22962d Compare September 20, 2019 02:31
// get the repository class and create a new instance of it
const customerRepoClass = createCustomerRepo(repositoryClass);
const customerRepo: CustomerRepository = new customerRepoClass(
db,
async () => orderRepo,
async () => addressRepo,
);
// register the inclusionResolvers here for customerRepo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the newline before the comment. I feel the comment belongs to registration of inclusion resolvers below, not to new customerRepoClass code above.

  const customerRepo: CustomerRepository = new customerRepoClass(
    db,
    async () => orderRepo,
    async () => addressRepo,
  );

  // register the inclusionResolvers here for customerRepo
  customerRepo.inclusionResolvers.set(
    'orders',
    customerRepo.orders.inclusionResolver,
  );

@agnes512 agnes512 force-pushed the hasmany-resolver branch 6 times, most recently from ecca712 to 2b208b7 Compare September 20, 2019 21:11
);

// add this line to register inclusion resolver
this.registerInclusion('orders', this.orders.inclusionResolver);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can make it even simpler as we can use orders to get the inclusionResolver (this['orders'].inclusionResolver).

this.registerInclusion('orders');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I think it's doable.
Let me land this PR and open a new one for it.

@raymondfeng
Copy link
Contributor

@agnes512 Please fix CI failures.

@agnes512 agnes512 merged commit 4cf9a70 into master Sep 20, 2019
@agnes512 agnes512 deleted the hasmany-resolver branch September 20, 2019 23:37
@samarpanB
Copy link
Contributor

@agnes512 when can we expect this to be released ? I have taken a latest update but the inclusion resolver is not available there.

@agnes512
Copy link
Contributor Author

@samarpanB it will probably be released later today, worst case in 3-4 days 😄

@samarpanB
Copy link
Contributor

Is it possible to release today? Actually I need to use it in one of my ongoing project. I don’t want to use the poor mans inclusion resolver 😊

@agnes512
Copy link
Contributor Author

@samarpanB we've just released! thanks for waiting~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement InclusionResolver for hasMany relation
5 participants