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: add depth limiter optional parameter when loading nested trees #7926

Conversation

tiagojsag
Copy link
Contributor

Description of change

feat: add depth limiter optional parameter when loading nested trees when using TreeRepository's findTrees() and findDescendantsTree()

Modified TreeRepository's findTrees() and findDescendantsTree() public methods to now accept an optional object with config options.
For now, said object contains a depth parameter that, if set, will limit how far down the tree those methods will crawl and retrieve nested entities.

BREAKING CHANGE: TreeRepository's protected method buildChildrenEntityTree() now requires a 4th argument. Anyone affected by this break should also review and update their implementation, otherwise this feature will not work.

Closes: #3909

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

Unrelated tests are failing for me both on the branch and on master. I ran the individual tests I added to cover my changes, and hopefully CI will take care of the rest.

/**
* When loading a tree from a TreeRepository, limits the depth of the descendents loaded
*/
depth: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

make depth optional.

when depth is undefined then treat that as the unlimited case.

I think that would be a clearer API & would match other cases where we have optional options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I agree that it does indeed make more sense.

I have implemented the suggested change, and extended the test to include examples of using empty objects for the new arguments. Let me know if there's anything else you think can be improved here.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@imnotjames I just rebased my branch to include the latest changes done to master. Assuming the tests (currently running) pass, is there anything else I can to do help move this PR forward?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@imnotjames I've once again rebased my branch, and the tests are passing. Let me know if this PR is relevant to TypeORM, or if not, I can close it. thanks

@tiagojsag tiagojsag force-pushed the add-optinal-depth-limit-to-tree-repository-methods branch 2 times, most recently from a737dd3 to a2f8ef0 Compare July 17, 2021 17:32
@tiagojsag tiagojsag force-pushed the add-optinal-depth-limit-to-tree-repository-methods branch from a2f8ef0 to 2fd5f93 Compare August 24, 2021 11:41
@tiagojsag tiagojsag force-pushed the add-optinal-depth-limit-to-tree-repository-methods branch from 2fd5f93 to 85ea143 Compare November 3, 2021 14:13
@pleerock
Copy link
Member

@tiagojsag please rebase it once again and I'll merge it. Thank you!

…using TreeRepository's findTrees() and findDescendantsTree()

Modified TreeRepository's findTrees() and findDescendantsTree() public methods to now accept an optional object with config options.
For now, said object contains a depth parameter that, if set, will limit how far down the tree those methods will crawl and retrieve nested entities.

BREAKING CHANGE: TreeRepository's protected method buildChildrenEntityTree() now requires a 4th argument. Anyone affected by this break should also review and update their implementation, otherwise this feature will not work.

Closes: typeorm#3909
@tiagojsag tiagojsag force-pushed the add-optinal-depth-limit-to-tree-repository-methods branch from 85ea143 to 4b7a520 Compare November 11, 2021 14:44
@tiagojsag
Copy link
Contributor Author

👍 done

@pleerock pleerock merged commit 0c44629 into typeorm:master Nov 11, 2021
@pleerock
Copy link
Member

Thank you for contribution! 🎉

@tiagojsag
Copy link
Contributor Author

Thanks for maintaining!

@tiagojsag tiagojsag deleted the add-optinal-depth-limit-to-tree-repository-methods branch November 11, 2021 16:14
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this pull request Nov 29, 2021
…using TreeRepository's findTrees() and findDescendantsTree() (typeorm#7926)

Modified TreeRepository's findTrees() and findDescendantsTree() public methods to now accept an optional object with config options.
For now, said object contains a depth parameter that, if set, will limit how far down the tree those methods will crawl and retrieve nested entities.

BREAKING CHANGE: TreeRepository's protected method buildChildrenEntityTree() now requires a 4th argument. Anyone affected by this break should also review and update their implementation, otherwise this feature will not work.

Closes: typeorm#3909
@@ -261,14 +266,18 @@ export class TreeRepository<Entity> extends Repository<Entity> {
});
}

protected buildChildrenEntityTree(entity: any, entities: any[], relationMaps: { id: any, parentId: any }[]): void {
protected buildChildrenEntityTree(entity: any, entities: any[], relationMaps: { id: any, parentId: any }[], options: (FindTreesOptions & { depth: number })): void {

Choose a reason for hiding this comment

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

@tiagojsag @pleerock this definition requires me to add depth property to the options argument.
Since FindTreesOptions only has depth as optional property, is there a reason why options can't be optional and just use FindTreesOptions as it's type and not cause a breaking change? As it is right now, it looks like I'll have to update buildChildrenEntityTree with { depth: -1 } everywhere I don't want changing functionality.

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 think you may be correct, and this can probably be modified to be optional.

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.

Define Tree Depth
4 participants