Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[CAP] Parses a string in CQL expression and add it as WHERE condition

https://cap.cloud.sap/docs/node.js/cds-compile#cds-parse-xpr

Some examples:

// equal
cds.parse.xpr (`lastName = 'Naidoo'`)

// or
cds.parse.xpr (`lastName = 'Naidoo' or firstName = 'Verónica'`)

// in
cds.parse.xpr (`lastName IN ('Naidoo','González Esteban')`) 

How to add an expression as where condition:

    srv.before('READ', EntityName, async req => { 

        const cxn  = cds.parse.xpr (`lastName = 'Naidoo'`) 

        req.query.SELECT.where ??= []
        if (req.query.SELECT.where.length > 0) {
            req.query.SELECT.where.push("and")
        }
        req.query.SELECT.where.push(...cxn)

    })

[CAP] Invoke custom handlers when querying local entity

const cds = require('@sap/cds');

module.exports = async srv => {

     const { Objects } = srv.entities // entities from myService.cds
   
     srv.on("myAction", async req => {
        const query = SELECT.one.from(Objects).where({ id: req.data.myId })
        const srv = await cds.connect.to('myService')
        const data = await srv.run(query)
        console.log(data) 
        return data
    })

    srv.on("READ", Objects, async req => {
        console.log("Objects called")
        // Select data from db or forward query to external system
        // ...
        // return data
    })

}