Skip to content

[FEATURE] Strong type where condition predicates #455

@dimitar-nikolov-lazarov

Description

@dimitar-nikolov-lazarov

Description

Not sure whether this is the correct place or inside cds-types.
cds-typer provide us entity classes as known TS types, so this is the reason I rise the question here.

What is the problem:
CDS gives us a way to define where predicate and run queries, but we are missing strong types about what attributes or what values could be set at each filter.
Why this is a problem:
Model changes, typos or incorrect values could be provided to query. Only way to find out is via running the particular query/code ( via test or manually ).

What we want:

  1. Strong type for any filter predicate based on Entity(generic one).
  2. IDE auto-complete for possible attributes and their types.
  3. Build validation
// If we have entities:

entity  MachineType {
  key name: String;
  key platform: Platform;
  memoryMB: Integer;
  cpuCount: Integer;
}

define entity VM {
  key platform               :      Platform;
  key platformId             :      String;
      name                   :      String;
      status                 :      String(20);

      machineTypeName: String;
      machineType: Association to MachineType on machineType.name = $self.machineTypeName
                                    and machineType.platform = $self.platform;


 // So if we try to write query like 
const query = SELECT.from(VM).where({platformVal: "AZURE", "machineType.CPUCount" : { ">": 'asd' });
const result = await query;
}

we should get errors during cds build

Suggested Solution

We could use TS conditional types and define EntityFilterPredicate generic type.
It allow us to specify that filters should have attributes from TEntity or its navigation properties.
Also it specify what are possible operators for filter( such like =,!=, >, <, etc), and/or combination

Example of such types could be found here:
git repo: https://github.com/dimitar-nikolov-lazarov/capjs-demos/
branch: feature/tagged-template-builder
usage: https://github.com/dimitar-nikolov-lazarov/capjs-demos/blob/feature/tagged-template-builder/srv/CloudService.ts#L65

        const whereClause: EntityFilterPredicate<VM> = {
            "lob.name": {
                like: "%LOB%"
            },
            or: {
                platform: Platform.GCP,
                or: {
                    "machineType.cpuCount": {
                        ">=": 4
                    }
                }
            },
        }

Alternatives

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions