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

populate Record's relations returned from async methods #525

Open
crobinson42 opened this issue Oct 2, 2018 · 2 comments
Open

populate Record's relations returned from async methods #525

crobinson42 opened this issue Oct 2, 2018 · 2 comments
Labels
enhancement mapper relations Mapper relations are the ability to define a relationship one Mapper has with another Mapper

Comments

@crobinson42
Copy link
Member

Description

When using JSData server-side, it is common to immediately return the result from an async op, ie:

...
async getUsers(req, res) {
  res.body = await containerStore.findAll('users', { active: true }, { with: ['posts'] })
}
...

The problem with the above code is that the relation field, posts will not be populated due to the Mapper's toJSON method not being invoked on each record instance with the options { with: ['posts'] }.

This is a hassle when you are required to write server-side controller methods like this:

...
async getUsers(req, res) {
  res.body = await containerStore.findAll('users', { active: true }, { with: ['posts'] }).map(user => user.toJSON({ with: ['posts'] })
}
...

Proposal

Adding an option populateRelations to the Container class that defaults to true would make sense considering a Container class is typically used in a server-side environment. When the flag is true any async methods invoked with relations with: [...] option will be populated on the Record object automatically so that common methods will capture the relation fields, ie: JSON.stringify(records) or console.log(records).

Environment

  • js-data version: 3.0.x
@crobinson42
Copy link
Member Author

Upon further investigation this is related to Mappers that have a schema defined and possibly the Schema class.

@crobinson42
Copy link
Member Author

Consideration for recursive depth of .toJSON needs to be taken because of these computed props can become circular depending on the defined relations.

@crobinson42 crobinson42 added the mapper relations Mapper relations are the ability to define a relationship one Mapper has with another Mapper label Jan 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement mapper relations Mapper relations are the ability to define a relationship one Mapper has with another Mapper
Projects
None yet
Development

No branches or pull requests

1 participant