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

Order by query #22

Closed
wiatrM opened this issue Jul 22, 2021 · 2 comments
Closed

Order by query #22

wiatrM opened this issue Jul 22, 2021 · 2 comments

Comments

@wiatrM
Copy link

wiatrM commented Jul 22, 2021

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?

@Sairyss
Copy link
Owner

Sairyss commented Jul 23, 2021

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

@wiatrM
Copy link
Author

wiatrM commented Aug 26, 2021

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

@Sairyss Sairyss closed this as completed Oct 10, 2022
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