Skip to content

Only return unique values in relationship object response #36

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

Merged
merged 3 commits into from
Mar 20, 2023

Conversation

too-soon
Copy link

The issue

When including related entities on a GET endpoint, some relationships will be duplicated:

image

Solution

Reduce this array to only hold unique objects.

Please let me know if there's any way to improve my solution.

Tests

New test failing before fix
image

All tests passing after fix
image

type: camelToKebab(typeName),
id: i[this.relationPrimaryField.get(field)].toString(),
}));
builtData.data = propsData
Copy link
Owner

@klerick klerick Mar 20, 2023

Choose a reason for hiding this comment

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

I think will be better like this:

const tmp = propsData.reduce((acum, item) => {
  const key = `${camelToKebab(typeName)}:${i[
    this.relationPrimaryField.get(field)
  ].toString()}`;

  if (!acum.has(key)) {
    acum.set(key, {
      type: camelToKebab(typeName),
      id: i[this.relationPrimaryField.get(field)].toString(),
    });
  }

  return acum;
}, new Map());

builtData.data = [...tmp.values()];

because we will use only one iterate otherwise we will have 2 iterate(map and reduce) and inner iterate(find) for every call reduce callback

Copy link
Author

Choose a reason for hiding this comment

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

Agreed. Fixed as per instructions.

@too-soon too-soon requested a review from klerick March 20, 2023 13:34
@klerick klerick merged commit 98f459c into klerick:master Mar 20, 2023
@github-actions
Copy link

🎉 This pull request is included in version [email protected] 🎉

The release is available on GitHub release

@too-soon too-soon deleted the unique-relations branch March 20, 2023 14:40
@github-actions
Copy link

github-actions bot commented Jun 1, 2023

🎉 This pull request is included in version [email protected] 🎉

The release is available on GitHub release

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

Successfully merging this pull request may close these issues.

2 participants