Preview release (0.0.x) — APIs may still change between releases.
Installation
pnpm add @atscript/odata @atscript/core @atscript/typescript @atscript/db @atscript/ui @uniqu/core
pnpm add -D unplugin-atscript
Needs Atscript 0.2.28 or newer (@atscript/core, @atscript/typescript, unplugin-atscript),
@atscript/db 0.1.204 or newer and @atscript/ui 0.1.202 or newer.
The generated .as files use annotations of @atscript/db and @atscript/ui, so the build needs
their plugins next to this one; both hosts need them at runtime too. The packages of each host are
listed in the installation guide. Every export is documented in the
API reference (plugin and CLI).
Generating models from $metadata
Save a service's $metadata document next to your models and name it in the Atscript config. The
plugin turns it into Atscript types and a service model your app can import.
atscript-odata fetch saves it for you, with the value-help documents it references (see below):
ODATA_USER=me ODATA_PASSWORD=… npx atscript-odata fetch \
'https://host/sap/opu/odata4/sap/zsales/srvd/sap/zsales/0001/$metadata' \
--out src/odata/sales.xml --query sap-client=100 --language EN
It takes basic authentication from ODATA_USER / ODATA_PASSWORD (or --user / --password;
an Authorization given with --header wins),
extra headers with --header "Name: value" and query options for every call with --query (or in
the URL). --language EN reads every document in that language (as sap-language and
Accept-Language) and writes it to the manifest beside the file, where the plugin takes it as the
service's language; without it the command says so. Run it again to refresh: unchanged files
stay as they are. The same is fetchODataMetadata({ url, out, headers, query, language }) of
@atscript/odata/plugin.
A $metadata saved from a browser where you are signed in works as well: then set the service's
language in the plugin to the language the browser showed it in.
// atscript.config.ts
import { defineConfig } from "@atscript/core";
import ts from "@atscript/typescript";
import dbPlugin from "@atscript/db/plugin";
import uiPlugin from "@atscript/ui/plugin";
import odataPlugin from "@atscript/odata/plugin";
export default defineConfig({
rootDir: "src",
plugins: [
ts(),
dbPlugin(),
uiPlugin(),
odataPlugin({
services: [{ name: "sales", metadata: "odata/sales.xml", language: "EN" }],
}),
],
});
A build then writes two files beside src/odata/sales.xml:
| File |
Holds |
src/odata/sales.as |
One interface per entity set, with @db.*, @ui.* and @odata.* annotations. Import it like any .as file. |
src/odata/sales.model.gen.ts |
export const model: ServiceModel (also the default export): what the query and service layers need at runtime, so the app ships neither the XML nor a parser. model.build records what it was built from; the hosts compare the live documents with it. |
Both are generated: do not edit them. To add labels, validation or UI hints, write annotate
blocks in your own .as files. The end of the .as file lists what the service declares and the
types cannot express yet. The model module holds no raw vocabulary annotations: the facts the
layers use are read out of them.
The generated types use annotations of @atscript/db and @atscript/ui, so keep dbPlugin() and
uiPlugin() in the config.
A navigation between two entity sets becomes a relation. When the service's referential constraint
is a foreign key atscript-db accepts (it covers the target's whole key), the properties get
@db.rel.FK and the navigation @db.rel.to / @db.rel.from, which foreign-key value helps and
filters use. Every other navigation, the ones without a referential constraint included, gets
@db.rel.store: a relation without foreign keys that the service resolves when it expands the
navigation. Both kinds load with $with ($expand) in @atscript/db-odata and
@atscript/odata-client; a store relation is never written. A navigation to a type that no single
entity set serves (a contained one, for example) is listed at the end of the .as file instead.
Value helps
A property with a value list in the $metadata (Common.ValueList, with the v2 sap:value-list
attribute and the v2 Annotations blocks; the unqualified list, else the first) gets the
atscript-ui value help of that list, when the list's entity set is part of the generated model:
| In the value list |
In the generated type |
CollectionPath, the Out / InOut of the property |
@ui.valueHelp <EntitySet type>, '<ValueListProperty>' |
In / InOut of another property |
a condition <ValueListProperty> = <Host>.<LocalDataProperty> of its filter: the form's value narrows the picker |
Constant |
a condition <ValueListProperty> = <literal> of its filter |
Out / InOut of another property |
@ui.valueHelp.fill '<LocalDataProperty>', '<ValueListProperty>' |
the text of the picked field (its own Common.Text), named |
@ui.valueHelp.fill '<the property's text>', '<that text>', unless the list returns the property's text itself |
the properties the list names (not Constant) |
@ui.valueHelp.column '<ValueListProperty>', in order; none when it names only the bound one |
ValueListWithFixedValues, sap:value-list="fixed-values" |
@ui.type 'select': a dropdown in forms, a checklist in the filter bar |
Common.Text, sap:text, with UI.TextArrangement |
@ui.valueLabel '<text property>' (TextLast: 'keyText', TextOnly: 'text') |
<Host> is the interface that declares the property; atscript-ui reads the host field by its name
in every form the binding reaches, and only in forms (a table's filter bar lists every value).
A pick so fills the text next to a code with the picked row's text, also a text the service
computes (a read-only field), and the code shows it (@ui.valueLabel). A property the service
computes carries no length or format constraint (@expect.*): its value is the service's, so a
picked text longer than its declared MaxLength does not block the save. A list over a composite
key shows the other key members it names as columns, next to each row in the combobox.
Left out, and listed at the end of the .as file:
- a value list whose entity set is in another service (
CollectionRoot) or in a v2 annotation
document outside the $metadata, and the value lists after the first (for v4
Common.ValueListReferences, see below);
- a list that does not name the property in an Out or InOut parameter, or that would list a value
more than once: the property is not the whole key of the list, and another key member is
neither pinned (by a constant, or by a host field the pick writes back) nor named by the list
(a column, which tells the rows apart);
- a parameter whose local property is not a single primitive property next to the bound one (a
path such as
to_Header/Country), or of another type than the list property, and a constant
that does not read as a value of the list property's type;
@ui.valueLabel with a text that is not a string property next to the code (a path through a
navigation), and on a code with a value help or a foreign key whose pick does not fill the
text: the text would go stale after a pick.
- a dropdown where the rest of the key is pinned only by a host field or told apart only by
columns (a dropdown commits a value, not a row, and shows no columns): the field keeps the
search picker.
TextSeparate asks for no text next to the code, so the code gets no @ui.valueLabel.
The text of an entity type's single key property, when it is a string property of the same type,
gets @ui.dict.label whatever its arrangement: a value-help picker over the entity set lists its
rows by that text, not by the first string property.
Value helps in value-help services (OData v4, SAP RAP)
On SAP RAP v4 a property names its value list by reference: Common.ValueListReferences holds
the URL of the $metadata of a separate value-help service, relative to the service's
$metadata, such as
../../../../srvd_f4/sap/country_vh/0001;ps='srvd-sales-0001';va='…country'/$metadata.
atscript-odata fetch … --out src/odata/sales.xml reads the document of each reference (resolved
against the $metadata URL, with the same credentials, headers and query options) and saves it
in src/odata/sales.value-helps/ (country_vh.country.xml: the value-help service and the
property), with the manifest src/odata/sales.value-helps.json, which maps each reference to its
file. The plugin reads the manifest beside a service's metadata file by itself: nothing to
configure. A document the command cannot read is reported (exit code 1); the one an earlier
fetch saved for it stays. The others are saved.
Saved some other way, name each document by its reference, exactly as the $metadata writes it,
in the valueHelps option (an entry wins over the manifest's):
odataPlugin({
services: [
{
name: "sales",
metadata: "odata/sales.xml",
valueHelps: {
"../../../../srvd_f4/sap/country_vh/0001;ps='srvd-sales-0001';va='com.sap.gateway.srvd.sales.v0001.et-sales_order.country'/$metadata":
"sap/vh/country.xml",
},
},
],
});
Until a reference has a document, the end of the .as file lists it (with the reference to copy).
A property then gets the value help of the document of its first reference, by the same rules as
a value list in the service's own $metadata (the table above). The value-help entity sets the
bindings target become interfaces of the same .as file, with
@odata.valueHelpService '<service>' next to @db.table; only those a value help binds. Several
properties that reference one value-help service share its interface. The model module holds
the value-help services with their roots (valueHelpServices): the hosts read a value-help set
at its own service's root, through service.valueHelpService(name). At runtime the hosts read the
value-help documents the build recorded together with the live $metadata, with the same options.
Listed at the end of the .as file instead:
- a reference with no document, or whose document cannot be read, is no
$metadata or holds no
mapping for the property; generation does not fail over it;
- the references after the first of a property: they are not read;
- complex and enumeration properties, base types, navigations and value lists of a value-help
service's own types.
Service options
| Option |
Default |
Meaning |
name |
required |
Name of the service in your app. Written as @db.space; the hosts take it from the model module. |
metadata |
required |
Path of the saved $metadata file. |
output |
metadata with the extension .as |
Path of the generated .as file. Keep it inside rootDir, or Atscript does not compile it. |
model |
output with .model.gen.ts |
Path of the model module, or false to write none. |
entitySets |
all |
Generate only these entity sets and what their interfaces reference. Types and model are cut alike. |
valueHelps |
the manifest's |
Saved value-help documents: { [reference]: path } for v4 Common.ValueListReferences (above). |
language |
the manifest's |
The language the documents were saved in (EN, or a tag such as en). Every metadata read asks for it. Set it. |
operations |
every operation |
{ exclude: [...] }: operations left out of the types, the ops handles and the model. |
controllers |
off |
true or { output?, writable? }: also the controllers module of a Moost server (see @atscript/moost-odata). |
Relative paths are taken from the Atscript rootDir. Set the plugin option root (an absolute
directory, for example import.meta.dirname) to take them from somewhere else, such as a metadata
folder outside rootDir.
When the files are generated
asc and bundler builds generate. Every asc run, every bundler build, watch rebuild and dev
server start regenerates before reading any .as file. A file is rewritten only when its content
changes. A missing or unreadable metadata file, an unreadable value-help manifest, an unknown
name in entitySets, operations.exclude or controllers.writable, or controllers without the
model module, fails the build; a missing value-help document does not.
- The editor does not generate. It reads the files the last build left on disk. After you replace
a
$metadata file, run asc once (or restart the dev server); the editor picks the new files up.
- Generate on install. Add
"prepare": "asc -f dts" to your package.json scripts, so a fresh
checkout has the .as files and model modules before the editor or the type checker needs them.
If you commit the generated files instead, commit both.
- One root for every host. A relative
rootDir resolves against the folder of
atscript.config.*, and a missing one is that folder, in asc, the bundler and the editor
alike. Set the root option only for metadata kept outside rootDir.
At runtime: the live $metadata
The saved $metadata gives the .as models and the TypeScript types an app is built against. At
runtime both hosts (@atscript/odata-client
in the browser, @atscript/db-odata on a
server) read the live $metadata and the value-help documents the build recorded, and run on
them. The generated module's own classes become the live declarations, so every import is live.