Merge pull request #125 from SAP-samples/simplified-reviewed

Simplified reviewed/server.js
This commit is contained in:
Daniel Hutzel
2020-08-10 23:24:51 +02:00
committed by GitHub
3 changed files with 19 additions and 29 deletions

View File

@@ -3,37 +3,30 @@
// This is an example of using a project-local server.js to intercept
// the default bootstrapping process.
//
const cds = require ('@sap/cds')
// Mashup services after all are served...
cds.once('served', async()=>{
// Connect CatalogService and ReviewsService when all are served...
cds.once('served', async ({CatalogService}) => {
// react on event messages from reviews service
// reflect entity definitions used below...
const { Books } = cds.entities('sap.capire.bookshop')
const { Reviews } = cds.entities('ReviewsService')
// prepend the following handler so it overrides the default handler
CatalogService.prepend (srv => srv.on ('READ', 'Books/reviews', (req) => {
console.debug ('> delegating request to ReviewsService')
const [id] = req.params, { columns, limit } = req.query.SELECT
return SELECT(columns).from(Reviews).limit(limit).where({subject:String(id)})
}))
// subscribe to events emitted by ReviewsService
const ReviewsService = await cds.connect.to ('ReviewsService')
const db = await cds.connect.to ('db')
// reflect entities required below...
const { Books } = db.entities('sap.capire.bookshop')
const { Reviews } = ReviewsService.entities
ReviewsService.on ('reviewed', (msg) => {
console.debug ('> received:', msg.event, msg.data)
const { subject, rating } = msg.data
const tx = db.tx (msg) // TODO: db.tx(msg) fully implemented?
return tx.update (Books,subject) .with ({rating})
return UPDATE(Books,subject).with({rating})
})
// delegate requests to read reviews to ReviewsService
const CatalogService = await cds.connect.to ('CatalogService')
CatalogService.impl (srv => srv.on ('READ', 'Books/reviews', (req) => {
console.debug ('> delegating to ReviewsService')
const [ id ] = req.params
const tx = ReviewsService.tx(req)
return tx.read (Reviews) .where ({ subject: String(id) })
.columns (req.query.SELECT.columns)
}))
})
// Other bootstrapping events you could hook in to...
@@ -42,14 +35,9 @@ cds.on('bootstrap',(app) => {/* ... */})
cds.on('loaded', (model) => {/* ... */})
cds.on('connect', (srv) => {/* ... */})
cds.on('serving', (srv) => {/* ... */})
cds.once('served', (all) => {/* ... */})
cds.once('listening', ({server,url}) => {/* ... */})
// Delegate bootstrapping to built-in server.js
module.exports = cds.server
// Monkey-patching older releases
if (cds.version < '3.33.4') cds.once('listening', ()=> cds.emit('served'))
// Launch server if started directly from command-line
if (!module.parent) cds.server()

View File

@@ -31,7 +31,8 @@ GET {{bookshop}}/browse/Books(201)/reviews?
### Alternative OData URL
GET {{bookshop}}/browse/Books/201/reviews?
&$select=rating,date,reviewer,title
&$select=rating,date,title
&$top=3
###
GET {{bookshop}}/browse/Books(201)?

View File

@@ -1,2 +1,3 @@
cds.requires.messaging.kind = file-based-messaging
cds.odata.skipValidation = true
PORT = 5005