Code examples · SAP BTP · Beginner
SAP BTP CAP: an order with items (composition) and a deep insert
The problem
An order must be created together with its items in one call.
How we solve it
- 1Data modelcomposition
- 2Serviceprojection
- 3Checkbefore CREATE
- 4Try itdeep insert
The result
One POST creates an order and its items.
1. The requirement
An order must be created together with its items in one call. Items belong to their order, so deleting an order must remove its items too. An item with a quantity of zero or less must be refused.
2. How we design it
In the data model, the order has a composition of items. A composition tells CAP the items are part of the order, so CAP exposes them, accepts them inside one POST (a deep insert), reads them with $expand and deletes them with the order. A small handler adds the business rule that CAP does not know.
Why we decided it this way
- 1Composition instead of a plain associationItems are owned by the order: they are created, read and deleted with it.
- 2Let CAP expose the itemsA composition target is exposed automatically, so the service stays small.
- 3Check items in a before handlerThe rule applies to every item in the request, in one place.
Before you start
- Node.js 20+ and @sap/cds-dk installed.
- An empty project: cds init orders-demo, then open it in your editor.
3. The code, step by step
Step 1 of 4: Model orders and items
Two entities, one composition.
// Orders with a composition of items
using { cuid, managed } from '@sap/cds/common';
namespace demo.orders;
entity Orders : cuid, managed {
customer : String(80);
items : Composition of many OrderItems on items.parent = $self;
}
entity OrderItems : cuid {
parent : Association to Orders;
product : String(60);
quantity : Integer;
}
Part by part (plain text)
- Orders with a composition of items. cuid adds a generated UUID key and managed adds created and changed fields. 'Composition of many OrderItems on items.parent = $self' says the items belong to the order and are linked through their parent field. The item has the matching 'parent' association.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
One POST creates an order and its items. A GET with $expand returns both. A bad quantity is refused with an error.
What you should see
POST with two items: 201 and the new order with its items. GET /orders/Orders?$expand=items returns the order and both items. POST with an item quantity of 0: 400 with the message 'Quantity must be positive'. DELETE of the order removes its items.
Try it and check the result
- Run cds watch. CAP deploys the model to an in-memory SQLite database and prints the service URL.
- Send the three requests in order. The first returns 201, the second shows both items inside the order, the third returns 400.
- Send DELETE http://localhost:4004/orders/Orders(<id>) with the id from the first answer, then read the items again: they are gone with the order.
Common mistakes
- A plain Association to the items does not give you deep insert or cascading delete. Use Composition for parts that belong to the parent.
- Check items one by one. A request can hold many items and one bad item must fail the whole request.
- With the default in-memory database the data is gone after a restart. Add a persistent database before real use.