cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CAP: How can I add custom query options in my custom handler?

robbewuyts
Explorer
0 Likes
1,393

I need to retrieve all entries from my HANA DB entity with temporal aspect, so also the 'invalid' ones. I was hoping a CQN query like this should work, but only the 'valid' entries are retrieved from the database:

    public async getTemporalFactors(id:Array<string>){
        const service = await cds.connect.to('CO2SC3Service');
        const cqn = {
            SELECT: {
                from: { ref: ['TransportFactors'] },
                'sap-valid-from': [{ val: '1970-01-01' }]
            }
        };
        return await service.run(cqn);
    }

I tried this because I noticed you can add these parameters for external oData services: August 2022 | cap≽ire (cloud.sap)

Ideally I want to change the req.query parameter in my on Read handler, to add the sap-valmid-from parameter.

Accepted Solutions (0)

Answers (1)

Answers (1)

Willem_Pardaens
Product and Topic Expert
Product and Topic Expert
0 Likes

Temporal entities rely on specific columns to calculate the 'valid-at' and 'valid-from' entries (see: https://cap.cloud.sap/docs/guides/temporal-data). This would imply you can create your own standard query to select the desired entries like this:

 

SELECT.from(TransportFactors).where({ validFrom: { '>=': '1970-01-01' } })

 

assuming the 'valid-from' temporal column is called validFrom in your entity.

robbewuyts
Explorer
0 Likes

This would only fetch 'valid' entries (so where validFrom <= now and validTo >= now), and filter those where validFrom >= 1/1/1970.

To me it seems that the the annotations on this temporal aspect make sure on DB level that only valid entries are fetched, unless you use this url parameter. 

I hope I'm wrong here 🙂