cap-js / cds-types

Type definitions for CDS
Apache License 2.0
9 stars 8 forks source link

[FEATURE] fully-typed orderBy #227

Open ThePlenkov opened 2 weeks ago

ThePlenkov commented 2 weeks ago

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 commented 2 weeks ago

@daogrady Do you many know? thanks!

daogrady commented 1 week ago

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 commented 1 week ago

@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:

What do you think of this proposal?

daogrady commented 1 week ago

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 commented 1 week ago

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