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(core): add support for nested partial loading #1306

Merged
merged 1 commit into from
Jan 15, 2021
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Jan 15, 2021

Adds support for nested partial loading:

const author = await orm.em.findOne(Author, '...', { fields: ['name', 'books.title', 'books.author', 'books.price'] });

Or with alternative object syntax:

const author = await orm.em.findOne(Author, '...', { fields: ['name', { books: ['title', 'author', 'price'] }] });

It is also possible to use multiple levels:

const author = await orm.em.findOne(Author, '...', { fields: ['name', { books: ['title', 'price', 'author', { author: ['email'] }] }] });

Primary keys are always selected even if you omit them. On the other hand, you are responsible
for selecting the FKs - if you omit such property, the relation might not be loaded properly.
In the following example the books would not be linked the author, because we did not specify
the books.author field to be loaded.

// this will load both author and book entities, but they won't be connected due to the missing FK in select
const author = await orm.em.findOne(Author, '...', { fields: ['name', { books: ['title', 'price'] });

Same problem can occur in mongo with M:N collections - those are stored as array property
on the owning entity, so you need to make sure to mark such properties too.

Available for testing since 4.3.5-dev.77.

Closes #221

@B4nan B4nan merged commit 3878e6b into master Jan 15, 2021
@B4nan B4nan deleted the nested-partials branch January 15, 2021 10:55
@astahmer
Copy link

That's a great addition ! Thank you for this.

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.

The fields property doesn't work with nested fields
2 participants