cancel
Showing results for 
Search instead for 
Did you mean: 

index.cds not automatically honored in CAP node.js plugins

09-30-2024 6:45 PM
martinstenzig Contributor
954 views 3 comments
0 Likes
SAP Managed Tags
Subscribe

I have application (a) and 2 plugins (p1 and p2). In P2's db folder i created a file x.cds that contains an extension

 

extend entity domaind.Bla with {
...additional fields
}
 
also in P2 I reference 
a) p1 in the package.json
b) the file x.cds in an index.cds (that I thought would automatically be used)
 
The problem is that the extensions don't seem to be actively going into effect UNLESS, I specify a using somewhere in my app. 
I was under the impression that if I specify 'index.cds' it will be called and used to possibly define additional services, data models etc. I admit, I might be using plugins different than everybody else at the moment, but wanted to know what the general intent is. 
My work around right now is to simply add 
 
using from '[p2 package name] 
into one of my files in the db folder of the app and that seems to be doing the trick. 
 
So my Question is: Is there a standard .cds file that is honored when a plugin is used? At the moment the only entry point seems to be cds-plugin.js
 
0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

merbinjanselm
Product and Topic Expert
Product and Topic Expert

To expose CDS files from a plugin, it needs to be added to `cds.env.roots` for CDS to resolve `*.cds` file from non project path.

  1. Create a `index.cds` file in the root of the package or in a folder (say `db/`)
  2. Create a `cds-plugin.js` file with following:
const cds = require("@sap/cds");

// add this plugin's root to cds.env.roots
// cds find the `index.cds` file to be loaded into CDS model and available for any DB or tenancy approach
const root = __dirname; // or path.join(__dirname, "db") - if you have a custom folder with `index.cds`
if (!cds.env.roots.includes(root)) cds.env.roots.push(root);
​
  • Make sure, this new `cds-plugin.js` is discoverable by CDS on startup, by marking it as exported in the plugin's `package.json`
{
  ...
  "exports": {
    ...
    "./cds-plugin": "./cds-plugin.js"
  },
  ...
}​

 

geert-janklaps
SAP Mentor
SAP Mentor
0 Likes

Hi @martinstenzig,

Did you try adding a requires section to your plugin referencing the model?

Haven't tested it myself, but for example the change-tracking plugin uses an index.cds and adds a reference to the model in the requires section of package.json of the plugin. (Which I assume will automatically load the contents of the index.cds file when the plugin configuration is merged with the root project's configuration)

https://github.com/cap-js/change-tracking/blob/457a2a0cf9c95f3c4af75dc23dfcd68ce2aa30ee/package.json...

Worth a try I think 😊

Cheers,

Geert-Jan

martinstenzig
Contributor
0 Likes
Geert, good idea. I think the problem I have though is that I don't want to "overwrite" the original model defined in P1 in P2 but just extend it. So If I would add the required section in P2, the original service related required section implicitly defined in P1 would be plowed over.