2. Move the Services
In the second step an SAP CAP service's entities move to Atscript DB: an .as model, a database you choose, and Moost controllers that serve it. Move one service at a time (or one entity at a time); the others keep running on SAP CAP, and the screens of step 1 keep working throughout.
before: screens ──► /api/catalog/Books ──► ODataTableController ──► SAP CAP ──► database
after: screens ──► /api/catalog/Books ──► AsDbController ────────────────────► PostgreSQLWrite the model
Model each entity as an .as interface with @db.table, following the concept map. Two shortcuts:
- Start from the generated file of step 1.
src/odata/catalog.asalready holds each entity with its keys, labels, lengths, precision and value helps. Copy an interface into a file of your own, then remove the@odata.*lines, the__etagversion token (use an integer@db.column.versionfield for optimistic concurrency), and@db.default.store; turn@db.rel.storenavigations into foreign-key relations; add the defaults and validation your.cdsmodel had. - Keep the field names of the elements of your
.cdsmodel. The screens, saved table presets and filters refer to fields by name, so the same names keep them working.
The worked example shows an entity of a .cds model and its Atscript model side by side.
Choose the database
Configure the adapter in the Atscript config and create the tables with schema sync:
// atscript.config.ts
export default defineConfig({
rootDir: "src",
plugins: [
ts(),
dbPlugin(),
uiPlugin(),
odataPlugin({
services: [
/* the services still on SAP CAP */
],
}),
],
db: {
adapter: "@atscript/db-postgres",
connection: process.env.DATABASE_URL!,
exclude: ["src/odata/**"], // the generated OData models are no tables of this database
},
});npx asc db sync --dry-run # show the plan
npx asc db sync # apply itPostgreSQL, MySQL, SQLite and MongoDB are supported; see Atscript DB setup and schema sync. While some services still run on SAP CAP, keep their generated files out of sync with exclude, as above.
The data
- A new database. Sync creates the tables; move the rows with your database's tools, or with a small script that reads each SAP CAP entity set through the OData service (Atscript OData's table API reads it like any table) and inserts into the new table.
- The database SAP CAP already uses (PostgreSQL or SQLite). Name the tables and columns as SAP CAP created them, with
@db.tableand@db.column, and check withasc db sync --dry-runthat the plan changes nothing you do not want changed;--safeskips destructive steps. SAP CAP's default names are the qualified entity name with dots as underscores (my_bookshop_Books), and<association>_<key>for the foreign key of a managed association (author_ID); compare with what your database holds.
Serve it with Moost
Replace the OData controller of step 1 with an Atscript DB one at the same URL:
import { DbSpace } from "@atscript/db";
import { PgDriver, PostgresAdapter } from "@atscript/db-postgres";
import { AsDbController, TableController } from "@atscript/moost-db";
import { Book } from "./models/bookshop.as";
const db = new DbSpace(
() => new PostgresAdapter(new PgDriver({ connectionString: process.env.DATABASE_URL })),
);
@TableController(db.getTable(Book), "api/catalog/Books")
export class BooksController extends AsDbController<typeof Book> {}The screens that read /api/catalog/Books do not change: they ask the same URL, and /meta now describes the new table. Screens that used the browser client get the new URL instead of /odata/catalog/Books; the client factory's fallback already gives every non-OData URL a plain Atscript DB client.
One Moost application can serve both kinds side by side: an OData space with OData controllers for the services still on SAP CAP, and an Atscript DB space for the moved ones.
Move the logic
Handlers become overrides on the controller (or on a base class of your own, marked @Inherit()):
| SAP CAP | Moost DB |
|---|---|
before('CREATE' | 'UPDATE', …) | onWrite(action, data): change the body, or return an HttpError to refuse |
before('READ', …) restricting rows | transformFilter(filter) |
| checks inside the transaction | guardWrite(ctx) |
@assert.*, @mandatory | annotations in the model: @expect.*, @meta.required |
| actions and functions | controller actions or custom endpoints |
@requires and @restrict become Aooth roles with @ArbacResource and @ArbacAction on the controllers. See Moost DB customization for every hook.
Drop the SAP CAP runtime
When the last service has moved:
- [ ] Every screen reads an Atscript DB endpoint; the OData plugin config and the generated files of retired services are gone.
- [ ] No other consumer needs the OData API. Atscript DB serves its own REST protocol, not OData: an integration with SAP S/4HANA® or an SAP Fiori® elements app that reads the service needs an OData service of its own.
- [ ] Authentication has a new home: your identity provider, with Aooth or the Moost authentication of your choice, in place of the SAP CAP setup.
- [ ] The data lives in the database Atscript DB syncs, and the deployment pipeline runs
asc db syncinstead ofcds deploy. - [ ]
@sap/cdsand its plugins, the.cdsfiles and the SAP CAP build steps are removed.
The application is then an Atscript and Moost application on Node.js, with open-source dependencies, on the host and database you choose.
See also
- Worked Example: one entity through all steps.
- Atscript DB: tables, relations, views, sync.
- Moost DB controllers.