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
953 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
View Entire Topic
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"
  },
  ...
}​