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

CDS7 -> CDS8 Programmatic adjustments to the served $metadata files

carlonnheim
Participant
0 Likes
565

Hi,

We are upgrading a CAP project from CDS7 to CDS8 where we have programmatic adjustments to the services metadata. Like this

// In custom server.js
const cds = require('@sap/cds');
module.exports = (o) => {
    // ...
    cds.on('loaded', csn => {
        Object.entries(csn.definitions).forEach(([rootName, def]) => {
            // Code to enhance annotations goes here ...
        });
    });
    // ...
    // delegate to default server
    return cds.server(o);
}

These stopped working with CDS8 which seems to be due to the metadata being generated at compile time. In the gen/srv folder we get ready-made "MyService.xml" files in a "odata/v4" subfolder, and it seems the service uses these, making our startup adjustments not apply.

For now we have avoided the issue by disabling those build items, like that in package.json

{
    ...
    "cds": {
        "build": {
            "tasks": [
                {
                    "for": "hana"
                },
                {
                    "for": "nodejs",
                    "options": {
                        "contentEdmx": false
                    }
                }
            ]
        },
    ...
}

We would like to use this new feature (it reduces application load time etc.), but we are then looking for a way to plug in such model adjustments during build. Is there such a way?

Thanks!

//Carl

Accepted Solutions (0)

Answers (1)

Answers (1)

Dinu
Active Contributor

I saw this method of making changes to the model in loaded event working in a plugin even with cds v8 with change-tracking plugin (main branch/v1.0.7[yet to be released]). The changes are present already in the saved model (csn.json) after build. So, you have to ensure that the change is not done again if the model is loaded from a built csn.json with these changes. 

Perhaps you could move your code for modifications to a plugin so that it is activated during build as well. 

carlonnheim
Participant
0 Likes
Thanks Dinu, that sounds like a great approach, I will try that!