domain-driven-hexagon icon indicating copy to clipboard operation
domain-driven-hexagon copied to clipboard

Order by query

Open wiatrM opened this issue 2 years ago • 2 comments

Hello, thanks for great repository.

I'm wondering how ordering shall be implemented with this interface?

export interface OrderBy {
  [key: number]: -1 | 1;
}

How can I pass DTO to sort some column ASC/DESC in controller?

wiatrM avatar Jul 22 '21 18:07 wiatrM

Hey

This interface is incorrect, I left it as number because I had some type overload issues and I forgot to fix it later.

TypeOrm interface looks like this:

    order?: {
        [P in keyof Entity]?: "ASC" | "DESC" | 1 | -1;
    };

Though interface is incorrect, it still works and you can order things. For example:

{ 
  params:{}, // some params here
  orderBy: { createdAt: -1 } 
}

^ This will order by createdAt DESC

Sairyss avatar Jul 23 '21 09:07 Sairyss

Unfortunetly your solution still gives me an error:

[backend] ERROR in src/modules/tkd/use-cases/crud/tkd.crud.services.ts:89:24
[backend] TS2322: Type '{ id: number; }' is not assignable to type 'OrderBy'.
[backend]   Object literal may only specify known properties, and 'id' does not exist in type 'OrderBy'.
[backend]     87 |                 skip: paginateDto?.offset
[backend]     88 |             },
[backend]   > 89 |             orderBy: { id : 1 }
[backend]        |                        ^^^^^^
[backend]     90 |         });
[backend]     91 |     }

Im not very good at TypeScript generic, could you write me how to change the interface to support ordering?

Thanks in advance

wiatrM avatar Aug 26 '21 20:08 wiatrM