Lab 01 — Extend the Domain Model
Goal
Add a new entity Location to the domain model, link it to existing entities, and verify your changes compile and test cleanly.
Background
This lab teaches you how CAP's domain model (db/schema.cds) is structured and how new entities relate to the rest of the schema. You'll learn:
- How to declare a CDS entity with typed elements
- How
cuidandmanagedmixins add standard fields automatically - How
Associationlinks two entities
Steps
Step 1 — Add the Location entity
Open db/schema.cds and add the following entity at the end of the star.wars namespace block (after the last entity or view):
/**
* A named location in the Star Wars universe (not a full Planet, just a place).
*/
entity Location : cuid, managed {
name : String @mandatory;
description : String(500);
planet : Association to Planet;
}Step 2 — Verify the model compiles
From cap/:
cds compile db --to json > /dev/null && echo "Model OK"You should see Model OK. If you get an error, check your syntax carefully.
Step 3 — Add a basic test
Open test/model.test.js and add a test inside the describe('Star Wars CDS Model Tests', ...) block:
describe('Location entity', () => {
let db
before(async () => { db = await cds.connect.to('db') })
after(async () => {
await db.run(DELETE.from('star.wars.Location').where({ name: { like: '%-lab01' } }))
})
it('inserts a Location with an auto-generated ID', async () => {
await db.run(INSERT.into('star.wars.Location').entries({
name: 'Mos Eisley-lab01',
description: 'A wretched hive of scum and villainy',
}))
const [loc] = await db.run(
SELECT.from('star.wars.Location').where({ name: 'Mos Eisley-lab01' })
)
assert.ok(loc?.ID, 'Location.ID should be auto-generated')
assert.equal(loc.name, 'Mos Eisley-lab01')
})
})Step 4 — Run the tests
npm run testAll existing tests should still pass, and your new test should pass too.
Expected Outcome
cds compile dbcompletes without errorsnpm run testshows the new Location test passing- The entity has a UUID primary key,
createdAt,createdBy,modifiedAt,modifiedBy(fromcuid+managed)
Hints
- The
star.warsnamespace applies to everything inschema.cdsbecause of thenamespace star.wars;declaration at the top. cuidis from@sap/cds/common— it addskey ID : UUID.managedis from@sap/cds/common— it adds the four audit fields.- You don't need to reference
cap/db/sqlite/index.cds— it automatically includesschema.cds.
Stretch Exercise
Add a Composition of many Film2Locations so Films can reference Locations (like filming sites in the lore). Follow the pattern of Film2People in schema.cds.