Code Monkey home page Code Monkey logo

Comments (8)

SDAdham avatar SDAdham commented on September 28, 2024

Discord Thread: https://discord.com/channels/1214904142443839538/1238875455436881971

from mikro-orm.

B4nan avatar B4nan commented on September 28, 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.

from mikro-orm.

SDAdham avatar SDAdham commented on September 28, 2024

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

from mikro-orm.

B4nan avatar B4nan commented on September 28, 2024

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

from mikro-orm.

SDAdham avatar SDAdham commented on September 28, 2024

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

from mikro-orm.

SDAdham avatar SDAdham commented on September 28, 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
                  }
                }
              }
            }
          );

from mikro-orm.

SDAdham avatar SDAdham commented on September 28, 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.

from mikro-orm.

B4nan avatar B4nan commented on September 28, 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' }] }], {});

from mikro-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.