-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Collection populate with references #1158
Comments
It's worth noting that there is actually only one query that loads both the pivot table and joins the target table (also in the select in strategy). There will always be entities - references are still entities. The only things that we can skip by doing this is the join and hydration of target entities. Of course for large collections this can be worth it. I will also add that in v4 it is possible to add to an uninitialized collection. Here is a proposed API for this: await book.tags.init({ reference: true });
await em.populate(book, ['tags:ref']);
await em.find(Book, { ... }, ['tags:ref']);
// might use something else than `LoadStrategy`, not sure now
await em.find(Book, { ... }, { populate: { tags: LoadStrategy.REFERENCE } }); |
@B4nan This is exactly what we were looking for. We'd love to be able to just init the join/pivot table primary key(s). If you need any help bringing this feature up or testing it, lmk. 😃 |
I guess the biggest issue right now might be the proposed API, as populate hints are now (v5) string arrays only and they are strictly typed, so we would need to adjust that part to support things like |
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Implemented in v6 via #4776 |
Awesome! Looking forward to our v6 upgrade. |
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references: ```ts const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] }); console.log(book1.tags.isInitialized()); // true console.log(wrap(book1.tags[0]).isInitialized()); // false // or alternatively use `init({ ref: true })` const book2 = await em.findOne(Book, 1); await book2.tags.init({ ref: true }); console.log(book2.tags.isInitialized()); // true console.log(wrap(book2.tags[0]).isInitialized()); // false ``` Closes #1158
Is your feature request related to a problem? Please describe.
When working with a
ManyToMany
relationship, I only need the primary-key of the corresponding entity. All methods of populating theCollection
yields the corresponding entities fully populated.Describe the solution you'd like
A method that only accesses the relationship table to make uninitialized references in the
Collection
.Describe alternatives you've considered
Fully populating the entities works, but it's much more data than the problem requires.
Additional context
Joining the relationship-table can produce a lot of data in quadratic big-O, and double-select increases the request latency.
The text was updated successfully, but these errors were encountered: