While working on this topic, there are currently two chapters in the CAP docs regarding this topic:
And some SCN posts: here, here and here
But all this didn’t help me to complete the task. Got the solution which finally worked for me from this SAP Sample Susaas (two snippets: here and here).
server.js
const cds = require("@sap/cds")
cds.on('served', async () => {
const { 'cds.xt.SaasProvisioningService': provisioning } = cds.services
// Add provisioning logic only if multitenancy is there..
if (provisioning) {
let tenantProvisioning = require('./provisioning')
provisioning.prepend(tenantProvisioning)
} else {
console.log(">>> There is no service, therefore does not serve multitenancy!")
}
})
module.exports = cds.server
provisioning.js
const xsenv = require('@sap/xsenv')
xsenv.loadEnv()
module.exports = (service) => {
service.on('dependencies', async (req, next) => {
let dependencies = await next()
const services = xsenv.getServices({
registry: { tag: 'SaaS' },
destination: { tag: 'destination' }
})
dependencies.push({ xsappname: services.destination.xsappname }) //adds the subscriber destination as dependency
console.log(">>> SaaS Dependencies:", JSON.stringify(dependencies))
return dependencies
})
}
It injects the destination dependency by manually reading it using xsenv package and returning it in a dependencies callback handler.