namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
stock : Integer;
author : Association to Authors;
}
entity Authors {
key ID : Integer;
name : String;
books : Association to many Books on books.author = $self;
}using my.bookshop as my from '../db/schema';
service CatalogService {
entity Books as projection on my.Books;
entity Authors as select from my.Authors {
*,
null as numberOfBooks: Integer
};
}cds compile srv/service.cds --to sqlCREATE VIEW CatalogService_Authors AS SELECT
Authors_0.ID,
Authors_0.name,
NULL AS numberOfBooks
FROM my_bookshop_Authors AS Authors_0;
CREATE VIEW CatalogService_Books AS SELECT
Books_0.ID,
Books_0.title,
Books_0.stock,
Books_0.author_ID
FROM my_bookshop_Books AS Books_0;cds compile srv/service.cds --to edmx<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
<edmx:Include Alias="Core" Namespace="Org.OData.Core.V1"/>
</edmx:Reference>
<edmx:DataServices>
<Schema Namespace="CatalogService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="EntityContainer">
<EntitySet Name="Authors" EntityType="CatalogService.Authors">
<NavigationPropertyBinding Path="books" Target="Books"/>
</EntitySet>
<EntitySet Name="Books" EntityType="CatalogService.Books">
<NavigationPropertyBinding Path="author" Target="Authors"/>
</EntitySet>
</EntityContainer>
<EntityType Name="Authors">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="name" Type="Edm.String"/>
<NavigationProperty Name="books" Type="Collection(CatalogService.Books)" Partner="author"/>
<Property Name="numberOfBooks" Type="Edm.Int32"/>
</EntityType>
<EntityType Name="Books">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="title" Type="Edm.String"/>
<Property Name="stock" Type="Edm.Int32"/>
<NavigationProperty Name="author" Type="CatalogService.Authors" Partner="books">
<ReferentialConstraint Property="author_ID" ReferencedProperty="ID"/>
</NavigationProperty>
<Property Name="author_ID" Type="Edm.Int32"/>
</EntityType>
<Annotations Target="CatalogService.Authors/numberOfBooks">
<Annotation Term="Core.Computed" Bool="true"/>
</Annotations>
</Schema>
</edmx:DataServices>
</edmx:Edmx><Annotations Target="CatalogService.Authors/numberOfBooks">
<Annotation Term="Core.Computed" Bool="true"/>
</Annotations>module.exports = srv => {
const { Books } = srv.entities
srv.after('READ', 'Authors', (authors, req) => {
return authors.map(async author => {
const publications = await cds.transaction(req).run(
SELECT .from(Books) .where({ author_ID: author.ID })
)
author.numberOfBooks = publications.length
})
})
}=> cds deploy && cds run
> filling my.bookshop.Authors from db/csv/my.bookshop-Authors.csv
> filling my.bookshop.Books from db/csv/my.bookshop-Books.csv
/> successfully deployed database to ./bookshop.db
[cds] - connect to datasource - sqlite:bookshop.db
[cds] - serving CatalogService at /catalog - impl: service.js
[cds] - service definitions loaded from:
srv/service.cds
db/schema.cds
[cds] - server listens at http://localhost:4004 ... (terminate with ^C)
[cds] - launched in: 1126.872ms=> curl -s http://localhost:4004/catalog/Authors | jq
{
"@odata.context": "$metadata#Authors",
"@odata.metadataEtag": "W/\"8q5jjLD6vJ0ARrjnkajTONXIn38vpa1wxoXucua4kzU=\"",
"value": [
{
"ID": 42,
"name": "Douglas Adams",
"numberOfBooks": 3
},
{
"ID": 101,
"name": "Emily Brontë",
"numberOfBooks": 1
},
{
"ID": 107,
"name": "Charlote Brontë",
"numberOfBooks": 1
},
{
"ID": 150,
"name": "Edgar Allen Poe",
"numberOfBooks": 2
},
{
"ID": 170,
"name": "Richard Carpenter",
"numberOfBooks": 1
}
]
}You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 36 | |
| 33 | |
| 29 | |
| 27 | |
| 26 | |
| 26 | |
| 25 | |
| 23 | |
| 23 | |
| 23 |