cap-js/cds-types

Type Definitions for SELECT commands - is there an improvement possible?

Opened this issue · 3 comments

I have an entity definitions like follows:

entity Travels : managed, cuid {
  displayId          : Integer                       @readonly  default 0  @assert.unique;
  beginDate          : Date;
  endDate            : Date;
  bookingFee         : Decimal(16, 3);
  totalPrice         : Decimal(16, 3)                @readonly;
  currencyCode       : Currency;
  description        : String(40);
  travelStatus       : Association to TravelStatuses @readonly;
  agency             : Association to TravelAgencies @assert.integrity;
  customer           : Association to Passengers     @assert.integrity;
  bookings           : Composition of many Bookings
                         on bookings.travel = $self;
  bookingSupplements : Association to many BookingSupplements
                         on bookingSupplements.travel = $self;
};

Now I am writing a SELECT command:

await SELECT
  .from(Travels)
  .columns(
      'displayId',
      'invalidColumn'
  )
  .where({
    invalidColumn: 'ABC'
  });

I do not get a type error if I use an invalid column name when supplying columns or using where.

I would expect to get a type error if I use an invalid column name to make the development easier and faster.
Is this possible?
Through the .from(Travels) the valid column names could be inferred or not? I am no TypeScript expert and just enjoy using it. As I think this would be very beneficial I just want to ask if this is possible or understand why this is not possible.

Maybe try out cds-ts-repository if you need good type auto-completion:

image

You also can use the projection syntax which is provided out of the box and provides at least the typings for the columns method,

await SELECT
  .from(Travels)
  .columns((t) => [t.displayId, t.invalidColumn]) // compiler will complain that invalidColumn is not a property of t
  )
  .where({
    invalidColumn: 'ABC'
  });

Hi everyone,

I will attempt to add this in the next release of cds-types. As CQL is quite powerful and allows for several different syntaxes to express where and having, we'll have to circle in on the a full solution over time.

Best,
Daniel