Skip to content

Instantly share code, notes, and snippets.

@andrew-barnard
Last active July 31, 2024 00:01
Show Gist options
  • Save andrew-barnard/b0d24fd740dfbe47f9f57f3d591c9ec6 to your computer and use it in GitHub Desktop.
Save andrew-barnard/b0d24fd740dfbe47f9f57f3d591c9ec6 to your computer and use it in GitHub Desktop.
Custom Handlers
const cds = require('@sap/cds')
class northbreeze {
async selectProduct(req) {
const { communityid } = req.data
...
const { ProductName } = await SELECT.one.from(Products).columns(....)
return `${ProductName}` }
}
module.exports = northbreeze
// Style 1 - Function no extending
module.exports = function Sue(){
this.on('sum', ({data:{x,y}}) => x+y)
this.on('add', ({data:{x,to}}) => stocks[to] += x)
this.on('stock', ({data:{id}}) => stocks[id])
this.on('getStock','Foo', ({params:[id]}) => stocks[id])
this.on('order','Foo', ({params:[id],data:{x}}) => stocks[id] -= x)
}
// Style 2 - Extending cds.Service ( and sometimes I see extending cds.ApplicationService )
module.exports = class Sue extends cds.Service {
sum(x,y) { return x+y }
add(x,to) { return stocks[to] += x }
stock(id) { return stocks[id] }
getStock(Foo,id) { return stocks[id] }
order(Foo,id,x) { return stocks[id] -= x }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment