Skip to content

Type Safety ​

Pass the generated type, or the generated handle of an operation, and the generics of the call do the rest: field names, values, rows, parameters and results are checked against the service's $metadata. Nothing is generated per entity set, and nothing is typed by hand.

ts
const orders = odata.client(roastery.Order); // ODataClient<typeof roastery.Order>

await orders.query({ filter: { status_cod: "X" } });
// 'status_cod' does not exist in type 'FilterExpr<{ ID: number; … }>'
await odata.call(ops.bestsellers, { params: { weeks: 12 } });
// Property 'count' is missing in type '{ weeks: number; }' but required in type 'bestsellersParams'

The examples on this page are the demo apps' code, over the Roastery service: roastery is the generated module roastery.as, and ops the operation handles of roastery.model.gen.ts (see Generating Models).

What you pass ​

  • A type of the .as module, roastery.Order, is a TypeScript interface and a value at once. Pass the value to get a client, a table or a URL; the type comes with it.
  • A handle of ops, ops.bestsellers or ops.Orders.confirm, carries the operation's parameters, its result, and the row it is called on.
  • A string is untyped, on purpose: a URL, an operation by name (callByName). Use one only where there is no type: an operation only the live service has, or an Atscript UI component, which takes a URL.

In the browser ​

ts
import * as roastery from "./generated/roastery.as";
import { ops } from "./generated/roastery.model.gen";

const stages = await odata.client(roastery.Order).aggregate({
  controls: {
    $groupBy: ["status_code", "statusName"],
    $select: ["status_code", "statusName", { $fn: "count", $field: "*", $as: "orders" }],
  },
}); // rows: status_code, statusName and orders, a number

const best = await odata.call(ops.bestsellers, { params: { weeks: 12, count: 5 } });
best.value; // roastery.Coffee[]

const url = odata.urlOf(roastery.Coffee); // the URL Atscript UI components take

odata.client(type) and odata.urlOf(type) are described in Typed clients; odata.call(handle, input) and its untyped twin odata.callByName(name, input) in Calling an operation.

On a Moost server ​

ts
const coffees = await space.getTable(roastery.Coffee).findMany({
  filter: { active: true },
  controls: { $select: ["ID", "name"], $sort: { name: 1 } },
});

const answer = await space.callOperation({
  operation: ops.bestsellers,
  params: { weeks: 12, count: 5 },
});
answer?.value; // roastery.Coffee[]

The tables of space.getTable(type) are Atscript DB tables, typed by the type. space.callOperation() takes a handle, params and rows as the operation needs them; space.callOperationByName() a name, untyped. See From scripts and jobs.

On a Moost page ​

The page talks REST to your server, through @atscript/db-client, whose Client<T> is typed the same way. The Moost demo gets its clients from Atscript UI's factory, typed by the type it is given, and calls the operations routes with operationsClient:

ts
// src/ui/api.ts, shortened
import { operationsClient } from "@atscript/moost-odata/client";
import type { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
import { getDefaultClientFactory } from "@atscript/ui";

/** An entity set's URL, from its type's @db.table: its controller on the server. */
export const urlOf = (type: TAtscriptAnnotatedType) => `/api/roastery/${type.metadata.get("db.table")}`;
export const client = <T extends TAtscriptAnnotatedType>(type: T) => getDefaultClientFactory()<T>(urlOf(type));
export const roasteryOps = operationsClient("/api/roastery/ops");

// a screen
await client(roastery.Order).aggregate({ … }); // checked as in the browser
await roasteryOps.call(ops.bestsellers, { weeks: 12, count: 5 }); // the parameters, without `params`

urlOf holds only while each controller is mounted at /api/roastery/<entity set>, as the demo's are. Without Atscript UI, new Client<typeof roastery.Order>("/api/roastery/Orders") is typed the same. See From a Moost page.

What is checked ​

MistakeBrowser client, Moost pageMoost server (space.getTable)
A field name in filter or $selectcheckedchecked
A value of the wrong type; an operator the field's type lackscheckedchecked
A field name in $sort, a relation name in $withnot checkedchecked
aggregate(): $groupBy, $field, $fn, the rowschecked (its filter is not)$fn only; the rows untyped
A field of the rows read; a field or value of a write; a keycheckedchecked
A field of a related type (inside $some, $with)not checkednot checked
An operation: its handle, parameters, row, resultchecked: odata.call, operationsClient.callchecked: callOperation
A misspelled type or handlecheckedchecked

The editor completes what is checked: field names in a filter, the operators that fit a field's type, the handles of ops.

decimal fields

The generated types write an Edm.Decimal as decimal, a type Atscript does not declare yet, so TypeScript reads those fields as any and any value compiles. Declare it as a string in an ambient .d.ts of your app (a file without imports or exports, such as src/env.d.ts), as the demos do:

ts
// src/env.d.ts
declare type decimal = string;

Typed screen configs ​

Atscript UI's tables and forms take a URL and read /meta at runtime, so a component cannot be typed by the set it shows. Type your app's configuration instead. The demos configure each list screen with a ListConfig<R>, whose field names are checked against R, the type of its rows:

ts
// src/routes.ts
export interface ListConfig<R = Record<string, unknown>> {
  path: string;
  /** The entity set's type (a type whose rows are R), which gives its URL. */
  type: TAtscriptAnnotatedType & (new () => R);
  title: string;
  name: keyof R;
  columns: (keyof R)[];
  filters: (keyof R)[];
}

const coffees: ListConfig<roastery.Coffee> = {
  path: "/coffees",
  type: roastery.Coffee,
  title: "Coffees",
  name: "name",
  columns: ["ID", "origin_code", "process_code", "roastLevel", "score", "price", "stockKg"],
  filters: ["origin_code", "process_code", "active"],
};

// ListScreen.vue
const url = odata.urlOf(props.screen.type); // <AsTableRoot :url="url" …>

A misspelled column is a compile error, and type takes roastery.Coffee only. A column your server adds joins the rows' type: the Moost demo's Coffees are a ListConfig<roastery.Coffee & CoffeeSales>, with its orderedKg column.

See it live

Both demo apps are typed this way: pnpm check type-checks them. Code: the browser app's OverviewScreen.vue and src/routes.ts; the Moost server app's src/ui/api.ts and src/server/controllers.ts.

Limitations ​

These compile, by design:

What compilesWhyInstead
A field left out of $select, read from the rowa row's type has every field of the set, whatever $select namesread what you select
sum or avg over a field that is not a numbera decimal is a string in TypeScript, so a string field cannot be told from itthe call is refused: "<field>" is not a number; "sum" needs a numeric property
$regex on a date fielddates are strings in Atscript, so a date field takes what a string field takes (and refuses a Date)pass dates as ISO strings
A misspelled $ control ($sortt), a key of $havingthe controls take any $ key: adapters have their own ($search)—
An operation by name: callByName, callOperationByNamefor operations only the live service hascall by the handle of ops
A field or set only the live service hasthe types are the saved $metadata'spnpm generate (atscript-odata fetch), then build
Atscript UI tables, forms and action bars; client.action(name)they take a URL and read /meta and the actions at runtimetype your config (above); get URLs with urlOf(type)

Also: sum or avg over a decimal field is typed number, but its value is a decimal string. Convert it (Number(row.revenue)), as the demos' Overview does.

DOs and DON'Ts ​

  • Get clients and URLs from the generated type (odata.client(roastery.Order), space.getTable(roastery.Order)) — don't spell a URL and cast the client.
  • Call an operation of the build by its handle (odata.call(ops.bestsellers, …)) — keep callByName for operations only the live service has.
  • Declare decimal once, in an ambient .d.ts — don't leave decimal fields any.
  • Type the configuration your screens read (ListConfig<R>) — the components take URLs, the config can take types.
  • Regenerate when the service changed — the types know the saved $metadata only.

See also ​

SAP, SAP S/4HANA, SAP Fiori, SAPUI5, SAP BTP and other SAP products and services mentioned herein, as well as their respective logos, are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Atscript OData is an independent open-source project and is not affiliated with, sponsored or endorsed by SAP SE.
Released under the MIT License.