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] Expose 'keys' & 'gens' as static props per each compiled CDS entity #294

Closed
siarhei-murkou opened this issue Aug 13, 2024 · 3 comments · Fixed by #308
Closed
Labels
feature request New feature or request

Comments

@siarhei-murkou
Copy link
Contributor

siarhei-murkou commented Aug 13, 2024

Description

I need to know which CDS property names are marked as CDS keys & which are auto-generated if to not provide them explicitly.

Suggested Solution

CDS entity with primary key & auto-generated props:

entity TestObjects: cuid {
  name: String not null;
  description: String;
  type: String not null default 'test';
  updatedAt: Timestamp not null default $now;
}

It has to be compiled into:

class TestObjects {
  ...
  ID: string;
  name: string;
  description: string | null;
  type: string;
  updatedAt: String;
  ...
  /** primary keys of CDS entity */
  static readonly keys: ("ID")[];
  /** auto-generated properties of CDS entity */
  static readonly gens: ("ID" | "type" | "updatedAt")[];
}

Alternatives

No response

Additional Context

No response

@siarhei-murkou siarhei-murkou added feature request New feature or request new labels Aug 13, 2024
@daogrady
Copy link
Contributor

daogrady commented Aug 13, 2024

Hi Siarhei,

I think providing a static keys property is perfectly fine, as that is present in CSN during runtime.
Screenshot 2024-08-13 at 18 50 43

One thing that has bitten me in the past for similar scenarios is inheritance. Explicitly listing all keys would probably require LinkedCSN, which further cements the fact that we currently need to carry two flavours of CSN. I have played a bit around with deriving a utility type that can automatically include all key elements.
But all in all, I don't see any reason why there shouldn't be a static keys property.

The other part of your suggestion I am less sure about. I don't seem to see a static gen property in CSN, so that would be something that would purely exist in the world cds-typer is fabricating on type level. I am trying to avoid that as much as possible. Could you maybe point out where you found gen, if that can be present in the resulting CSN?

Best,
Daniel

@daogrady
Copy link
Contributor

daogrady commented Sep 4, 2024

Hi Siarhei,

I have put up a PR that introduces a static .keys property that contains all the keys in your entities. Feel free to check if that was what you were looking for.

Best,
Daniel

@daogrady
Copy link
Contributor

daogrady commented Sep 5, 2024

Note to self: the static .gen property OP asked for is a list of properties that have a default value and would therefore automatically generate a correponding value on write operations.
As this can not be found in CSN during runtime, this particular part of the request is a wontfix for now. But we could at some point in the future expand the type of each property to contain more information that is actually contained in the CSN:

class Book {
  createdAt: string & { default?: string }  // could be some utility type, like Key<...>
}

daogrady added a commit that referenced this issue Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants