2. A Moost server in front of SAP CAP
The same SAP® CAP project and the same page as step 1. The page now talks only to a Moost server of yours, at /api/catalog/<EntitySet>. The server calls SAP CAP as one technical user. Your users and rules go there, and step 3 keeps its URLs.
browser ──REST /api/catalog/Books──► Moost server ──OData /odata/v4/catalog──► SAP CAP ──► databaseThe code: 2_atscript_moost.
Running the Moost server app
cd 2_atscript_moost
pnpm dev # SAP CAP on :4004, and Vite on :5173 running the page and the Moost server
pnpm build # the page into dist/client, the server into dist/server
pnpm start:cap # SAP CAP on :4004, in one terminal
pnpm start # the Moost server on :3000, in anotherIn development, @moostjs/vite runs the server inside Vite and passes it every /api request (vite.config.ts). CAP_URL says where SAP CAP is.
The SAP CAP service as an Atscript DB space
src/server/space.ts makes each entity set a table the controllers serve:
import "../models/catalog.ui.as"; // the app's annotate blocks, before the types are used: /meta serves them to the page
// …
export const space = createODataSpace({
baseUrl: process.env.CAP_URL ?? "http://localhost:4004/odata/v4/catalog",
model,
types: catalog,
// A service that asks for a login gets the technical user here, from the environment:
// init: basicAuth({ user: process.env.CAP_USER!, password: process.env.CAP_PASSWORD! }), // basicAuth from @atscript/moost-odata
});
provideDbSpace(space, "catalog"); // the generated types carry @db.space 'catalog'The generated types and your hints are step 1's, unchanged. See The space.
The controllers, with a generated base for Books
One line in atscript.config.ts makes the OData plugin write a controller base with the service's operations:
controllers: { writable: ["Books"] },catalog.controllers.gen.ts then has BooksBase, with restock as a db action that calls the service with each book's ETag. src/server/controllers.ts uses it:
@TableController(catalog.Books, "api/catalog/Books")
export class BooksController extends BooksBase {}
/** Authors: read-only, as in the service. */
@ReadableController(catalog.Authors, "api/catalog/Authors")
export class AuthorsController extends ODataReadableController<typeof catalog.Authors> {}
/** Genres: the genre picker's list. A value help appears only where its target has a URL. */
@ReadableController(catalog.Genres, "api/catalog/Genres")
export class GenresController extends ODataReadableController<typeof catalog.Genres> {}The page shows the same Restock button as in step 1, now read from the books' /meta. Its dialog still names the book by its title, then its key (Emma (b7e2d4a0-…)). The page reads the authors' and genres' names from the other two URLs, one query per dictionary and page. src/server/main.ts reads SAP CAP's $metadata (space.connect(), retried while SAP CAP starts), then registers the controllers and calls app.init(). See The controllers module and Connecting.
Calling SAP CAP as one technical user
The example's service asks for no login, so init is a comment in space.ts. With a login, set CAP_USER and CAP_PASSWORD and uncomment it; a token goes in bearerAuth(token). SAP CAP then sees only that user, so its @requires and @restrict apply to it, and who may do what moves to the server. See Credentials and Sessions.
Where your own users and rules go
- Users sign in at the server: an auth interceptor or guards, such as Aooth's. The place is marked in
src/server/main.ts. - Rules go on the controllers, as hooks (
onWrite,transformFilter, …) and guards (Moost DB customization). - The types are checked first. A missing price or a stock of -1 is refused before SAP CAP is called.
- SAP CAP's rules still run, and the form shows their messages at their fields, as in step 1.
What changes on the page: src/api.ts only
The screens, the form and the hints stay. src/api.ts loses its OData client, and Atscript UI makes an Atscript DB client for each URL by itself:
-import "./models/catalog.ui.as"; // the app's annotate blocks, before the types are used
-import { createODataClientFactory } from "@atscript/odata-client";
// …
-export const odata = createODataClientFactory({ services: [{ baseUrl: "/odata/v4/catalog", model, types: catalog }] });
-setDefaultClientFactory(odata);
-export const connect = () => odata.connect();
-export const urls = { books: odata.urlOf(catalog.Books), authors: odata.urlOf(catalog.Authors) };
+export const connect = async () => {};
+export const urls = { books: "/api/catalog/Books", authors: "/api/catalog/Authors" };Typed calls change too: odata.call(ops.Books.restock, …) becomes the controller's db action (Switching from a browser app to a Moost server).
The other files that change from step 1
- New:
src/server/space.ts,controllers.ts,main.ts, and the generatedcatalog.controllers.gen.ts. atscript.config.ts:controllers: { writable: ["Books"] }.vite.config.ts:@moostjs/viteruns the server; no/odataproxy, no build intoapp/.package.json: Moost,@atscript/moost-db,@atscript/moost-odataand@atscript/db-odatain;@atscript/odata-clientout.- Unchanged:
db/,srv/, the generated types, every screen and component.
Choose URLs you will keep
The page only knows /api/catalog/Books. In step 3 a different controller answers there, and the page doesn't change.