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

[FEATURE] fully-typed orderBy #227

Open
ThePlenkov opened this issue Sep 6, 2024 · 5 comments
Open

[FEATURE] fully-typed orderBy #227

ThePlenkov opened this issue Sep 6, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@ThePlenkov
Copy link

Question

Hi!

I want to implement the order when selecting from the generated entity. It works well for building the projection and filtering, however I'm struggling to udnerstand what should be the sorting pattern. So far I'm only able to provide fields as a string, which is OK but would be better to use types somehow. Thanks!

@ThePlenkov ThePlenkov added the question Further information is requested label Sep 6, 2024
@ThePlenkov
Copy link
Author

@daogrady Do you many know? thanks!

@daogrady
Copy link
Contributor

Hi Petr,

can you please show the code of what you have tried so far to illustrate the issue? Thanks!

Best,
Daniel

P.S.: no need to ping us individually, all relevant maintainers receive a notification when a new issue is opened.

@ThePlenkov
Copy link
Author

@daogrady indeed

So from what I see in the types - it is only possible to provide string array to orderby:

image

However what I would like to do is something like:

const { OBJECTS } = await import("../@cds-models/index");

    await SELECT.from(OBJECTS, (o) => {
      o.OBJECT_TYPE, o.OBJECT_OID, o.SCHEMA_NAME, o.OBJECT_NAME;
    }).orderBy( OBJECTS.OBJECT_NAME);

or may be something like :

const { OBJECTS } = await import("../@cds-models/index");
    await SELECT.from(OBJECTS, (o) => {
      o.OBJECT_TYPE, o.OBJECT_OID, o.SCHEMA_NAME, o.OBJECT_NAME;
    }).orderBy(  (o) => ([o.OBJECT_NAME, {desc: o.TIMESTAMP}]) );

So if we'll be able to operate with types instead of the strings - that would help us to prevent possible errors during the runtime when non existing fields are used for ordering for example.

So here is the proposal for the sorter function:

type OrderBy<T, C = keyof T > = (Entity: T) => Array< string |  C | Record<"asc"|"desc", C > >

So we reach following goals:

  • abilty to sort by Entity fields,
  • ability to use as and desc in a strictly typed way
  • keeping string type for the rest

What do you think of this proposal?

@daogrady daogrady transferred this issue from cap-js/cds-typer Sep 10, 2024
@daogrady
Copy link
Contributor

daogrady commented Sep 10, 2024

Hi Petr,

that sounds like a reasonable addition. I will see if this fits in with a current rework of the QL types.

Also, I have transferred this issue over to cds-types too, as this is only related to how the cds runtime handles input and what kind of overloads are being offered and is unrelated to the generation of type information for the CDS model (cds-typer).

Best,
Daniel

@ThePlenkov
Copy link
Author

thanks for transferring. Can we also change labels, since is now more like a feature proposal?

@daogrady daogrady added enhancement New feature or request and removed question Further information is requested labels Sep 10, 2024
@ThePlenkov ThePlenkov changed the title [QUESTION] What should be the orderBy pattern when using types? [FEATURE] fully-typed orderBy Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants