on ‎2018 Nov 12 11:37 AM
Hello folks,
I'm building an app on Cloud Platform (CF) using Node.js/Express. This thing has a /db module containing a entity definition (books-model.cds) and /srv that runs a node.js app on server.js that should read data declared on db module.
According to this documentation the package @sap/cds should help me do exactly that, using the statements below:
cds.importEntities([
{ $entity: "xsds.test.cds::ds_test.e1" },
{ $entity: "xsds.test.cds::ds_test.e2",
$fields: {
a: { $association: "xsds.test.cds::ds_test.e2",
$viaBacklink: "b" }
}
}
], callback);
function callback(error, entities) {
var E1 = entities["xsds.test.cds::ds_test.e1"];
var E2 = entities["xsds.test.cds::ds_test.e2"];
// ...
}
Thing is, @sap/cds module v2.10 doesn't seem to think that it is responsible for doing that. This version only handles the build and deployment of .cds files (to hdbcds et etc).
In a old version of this module, before going to npm repository, it was called "sap-cds" and it did provided this .importEntity method.
Does someone know what would be the equivalent of sap-cds on npm.sap.com, or even if it exists?
So far, I'm able to interact with the database using @sap/xsenv and @sap/hdbext.
import xsenv = require('@sap/xsenv') import hdbext = require('@sap/hdbext')
const services = xsenv.getServices({ hana:'plcockpit-hdi' })
express.use('/', hdbext.middleware(services.hana))
express.get('/', (req, res) => {
req.db.exec('SELECT * FROM \"com.acme::MyLibrary.Books\"', function (err, rows) {
res.json({
result: JSON.stringify(rows[0])
})
})
})
(Code is a simplified version of the original)
Although it works, I would like to know if is there a more "high-level" api to interact with the CDS layer.
Thanks
Request clarification before answering.
This might help someone.
I managed to get an express.js version of the cap to work by simple copying from node_modules @sap/cds/server.js. From there you can adapt to your needs. For instance, I added static additional content.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ulysses,
I'm doing similar things in CF environment like you.
And I'm referencing below document.
SAP Cloud Application Programming Model - Javascript API
I want to connect to the HANA DB at CF in my node.js module but I can't.
const cds = require('@sap/cds');
// examples in the document
const srv = await cds.connect.to('db');
const { Books, Authors } = srv.entities('my.booskhop'); // undefined
const model = srv.model; // undefinedDo you have any idea?
Thank you,
JaeHyeon Lee.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi All and stefanvoicila,
I am also getting same error as Stefan.
Could anyone please help me to resolve the issue.
Regards,
Deep
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Onu,
I am working on the same thing as you. When you work with HANA, you will probably want to use XSA with HDI concept. This means when you deploy your database part, an HDI container will be created. You then have to make sure, that your node.js module requires the database container. The cloud foundry environment will then inject the connection setting of the hdi container into your node.js module environment variables. This is standard cloud foundry development with XSA. You can then get the generated technical user und password for the container and use them in your above example. But...this does not solve the problem when deploying the application to the cloud platform.
How are we going to tell the cds to use the environment variables for the connection instead of the fixed values in the package.json? I am just trying to figure out how to fill the options parameter of the cds.connect function. If not parameters are given, is seems it will get them from the package.json.
Hope you could follow.
Cheers,
Tobi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Tobias,
| How are we going to tell the cds to use the environment variables for the connection instead of the fixed values in the package.json?
The access to HDI container will be done via xsenv, like below (see my original post)
const xsenv = require("@sap/xsenv")
// Read container reference
const services = xsenv.getServices({ hana:'mycontainer-hdi' })
// Use hana service middleware
express.use('/', hdbext.middleware(services.hana)) This should provide your application the access to the right HDI container.
Hope this help you out
HI Ulysses,
Don't know if this helps.
I have followed the tutorial https://developers.sap.com/tutorials/cp-apm-nodejs-create-service.html
After exposing and consuming OData from SQLite i have implemented the same scenario in hdb as well.
Everything that I did it was to change the package.json and then to run
cds deploy.
Because I used the SYSTEM user of the tenant, all tables were created under system schema ( please see below ).
I am still investigating/learning node & @sap/cds modules so any piece of advice is very appreciated.
Thank you
package.json
{ "name": "test1-hdb", "description": "Generated by cds init", "repository": "<Add your repository here>", "license": "ISC", "version": "1.0.0", "dependencies": { "@sap/cds": "^3.5.0", "express": "^4.16.4" }, "engines": { "node": ">=8" }, "cds": { "requires": { "db": { "kind": "hana", "model": "srv", "credentials": { "host": "xx.xxx.xxx.xxx", "port": 39041, "user": "SYSTEM", "password": "*****" } } } } }

You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Onu,
I'm not sure on what the question was, but it seems that you're troubled for tables on "SYSTEM" schema.
I think the idea would be to configure hdi-container on your mta.yaml (checkout https://developers.sap.com/tutorials/xsa-hdi-module.html Step 1: Create HDB Module). Once deployed it will generate a specific schema for your app.
Hope this helps.
PS.: Your question albeit interesting is not directly related to the original. I would suggest create a new topic next time, so you gain more visibility and other people with more expertise can also shine some light 😃
Good luck in your journey
Hi Ulysses,
have you solved the problem? I'm facing the same problem and basically i'm not able to use CDS api following the documenation.
Tks.
Daniele.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Daniele,
No, the problem is still open. The workaround is to use @sap/xsenv and @sap/hdbext to read from DB directly.
I can't find an official tutorial on it, but you can find a sample code from here.
Hope this helps
| User | Count |
|---|---|
| 9 | |
| 7 | |
| 6 | |
| 4 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.