cancel
Showing results for 
Search instead for 
Did you mean: 

How to work with CAP CDS > 7 ? .How do i access and modify req.query in custom handlers?

michaelschmid03
Explorer
0 Kudos

When developing CAP CDS for Node.js I regularly had Custom Handlers in which I needed to read some part of the req.query object to make some other backend calls, sometimes I even had to modify the pre-generated queries so that they would properly work on a backend.

Let's take a look on what changed with CDS(here version 7.4.0)

As you can see, req.query.SELECT only contains a from section. Some other params like filter, limit and orderBY are not in this query object, but in Symbol(Original). -Why?



This makes developing with CAP a bit more confusing and more difficult for me.

For Example last week I had accessed a filter with req.req.query.$filter and tried to parse those filter back to CQL, so I could modify and then reattach it to the query parameters. But this doesn't seem like a good Practice

Are there new Best Practices when working with req.query in Custom Handlers using the newer CDS versions? How can I Access req.query.Symbol(Original) in my custom handlers (something like req.query.Symbol(Original) or req.query.['Symbol(Original'] doesnt work)?

Any Guidance or reference to documentation would be massively helpful.

View Entire Topic
Dinu
Contributor

The view in the debugger is misleading you. You can access all the information you need as properties. Try the following and you will see that information is available.

let {from,columns,where,orderBy,limit} = req.query.SELECT
EkanshCapgemini
Active Contributor
0 Kudos

Ohh.. yes. You are right. Somehow the debugger doesn't show these. I am surprised.

michaelschmid03
Explorer
0 Kudos

you're totally right. i've never noticed this. Why would the debugger even hide the fields even though they are still there

i've actually wrote this abomination of code to solve it temporarily but this will now go away of my production code hopefully forever

  /**
   * Because CDS annoyed me with accessing filters in a normal way
   * @Param{String} sFilterString
   * @returns {Object} aParams,all filters indexed by Property
   */
  parseFilterString(sFilterString) {
    if (!sFilterString) return;
    const sCleanedString = sFilterString.replace(/['()]+|\b(?:and|or)\b/g, "");
    let aStringElements = sCleanedString.split(/\s+/);
    let aParams = {};
    while (aStringElements.length > 0) {
      const [paramName, operator, value, ...rest] = aStringElements;
      aParams[paramName] = aParams[paramName] || {};
      aParams[paramName][operator] = value;
      aStringElements = rest;
    }
    return aParams;
  }
michaelschmid03
Explorer
0 Kudos

-

michaelschmid03
Explorer
0 Kudos

-