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

matching doesn't set _populated to true #5559

Open
5 tasks done
SDAdham opened this issue May 11, 2024 · 8 comments
Open
5 tasks done

matching doesn't set _populated to true #5559

SDAdham opened this issue May 11, 2024 · 8 comments

Comments

@SDAdham
Copy link
Contributor

SDAdham commented May 11, 2024

Describe the bug

_populated is not set to true when using matching with populate and populateWhere which leads .toJSON to strip off the populated entities

Reproduction

https://github.com/SDAdham/reproduction/blob/master/src/example.test.ts

What driver are you using?

@mikro-orm/mysql

MikroORM version

6.2.5

Node.js version

20.9.0

Operating system

No response

Validations

@SDAdham
Copy link
Contributor Author

SDAdham commented May 12, 2024

@B4nan
Copy link
Member

B4nan commented May 14, 2024

A workaround for your repro would be using em.populate, which will adjust the user entity populate hint internally and that is the main source when serializing now. The _populated flag is only meant for manual overrides (via the public populated() method, no need to access it directly).

await orm.em.populate(user, ['groups.permissions'], {
  where: {
    groups: {
      permissions: {
        write: true,
      },
    },
  },
  orderBy: {
    groups: {
      permissions: {
        id: 'ASC',
      },
    },
  },
});

Note that you need to call this on the root entity (user) which you will be serializing, it wouldn't help if called on user.groups separately.

@SDAdham
Copy link
Contributor Author

SDAdham commented May 16, 2024

Will it affect the loaded groups? I have a where condition on those as well?

@B4nan
Copy link
Member

B4nan commented May 16, 2024

Why don't you check the query yourself? It should be equivalent (it's actually a better approach in general).

@SDAdham
Copy link
Contributor Author

SDAdham commented May 25, 2024

@B4nan, how can I limit populate from populating an entire collection? I want to just take 1 for example.

@SDAdham
Copy link
Contributor Author

SDAdham commented May 25, 2024

So, here is a conversion of your suggestion:

await this.repo.populate(
            user,
            ['pictures', 'profiles.links', 'profiles.themes', 'profiles.groups', 'settings'], // here, I want to get the last applied settings by the user
            {
              orderBy: {
                profiles: {
                  links: {
                    urlOrder: 'ASC'
                  }
                },
                pictures: {
                  sizes: 'ASC'
                }
              },
              where: {
                profiles: {
                  links: {
                    publish: true
                  },
                  themes: {
                    default: true
                  }
                }
              }
            }
          );

@SDAdham
Copy link
Contributor Author

SDAdham commented May 29, 2024

Hello @B4nan , have you had a chance to see my comment above? Is there any chance I limit the populate for settings? I used to be able to do this easy with matching.

@B4nan
Copy link
Member

B4nan commented May 29, 2024

you can't do that with em.populate, that only loads complete collections.

i think i have a workaround for you, you could use setSerializationContext private method of EntityLoader, that is what makes it work with em.populate:

await users[0].groups.matching({ ... });

// @ts-ignore accessing private variables and methods
orm.em.entityLoader.setSerializationContext(users, [{ field: 'groups', children: [{ field: 'permissions' }] }], {});

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

No branches or pull requests

2 participants