Skip to Content
1
Jan 15 at 09:16 AM

CAP: How to properly pass parameters to a View when consumed programmatically (Node.js)

292 Views

Hi experts,

I am using a view with parameters in my CAP project that is intended to be exposed to and used by other CAP services.

The documentation describes how to call these views via service URL like this: (which works just fine)

GET: /OrderWithParameter(foo=5)/Set or GET: /OrderWithParameter(5)/Set
GET: /ViewInService(p1=5, p2=true)/Set

I would like to know what the proper way would be to pass the parameters when these views are consumed programmatically from a different CAP project, e.g.:

// Consuming Service
const ext = await cds.connect.to('external_service')
const { ViewInService } = ext.entities
const results = await srv.run(SELECT.from(ViewInService)) // How to properly pass p1 and p2 to this query?

When I log the query of a View with parameter (e.g. when called via the browser) I can see that the passed parameters are part of the args object. Of course the consumer could just manually edit the query object, however I was hoping that there is better way, similar to how I would fluently pass where arguments to a query.

Thanks in advance,

Selim