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

31 Views Asked by At

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

1

There are 1 best solutions below

0
Martin Adámek On BEST ANSWER

Should be as simple as this:

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