javascriptmikro-orm

How to order the query through the greatest updatedAt on a nested one to many entity


I have a series of nested entities that works like that:

Entity1 -> Entity2 -> Entity3 ( With arrows representing a one to many relationship)

Im using MikroORM.

How can i write a findAndCount query that order the return of Entity1 based on the Entity3.updatedAt?

Im expecting to return a list of entity1 ordered by entity3.updatedAt


Solution

  • Should be as simple as this:

    const res = await em.findAndCount(Entity1, {}, {
      orderBy: {
        entity2: {
          entity3: { updatedAt: 'desc' },
        },
      },
    });