SAP-samples / hana-hdbext-promisfied-example

Example of how to use @sap/hdbext (standard node.js SAP HANA interface) via a promisfied wrapper.
Apache License 2.0
13 stars 8 forks source link

Can I use this with yo Saphanaacademy Saas generated boiler plate code? #5

Closed prakritidev closed 3 years ago

prakritidev commented 3 years ago

Hi,

I am using Saphanaacademy Saas Yo generator for multitenant application boilerplate code. I tried to send the HDI credentials of a tenant stored in the service manager, but that did not work. Any workaround for this ?

Middleware injection to the express server.

` if (environment == 'production') { app.use(function (req, res, next) {

  lib.getSMInstance(services.sm, services.registry.appName + '-' + 'req.authInfo.identityZone').then(
    function (serviceBinding) {
      if (!serviceBinding.hasOwnProperty('error')) {
          let hanaOptions = serviceBinding.credentials;
          console.log(hanaOptions)
          hdbext.middleware(hanaOptions)
        next()
      } else {
        res.status(500).send(serviceBinding);
      }
    })

}) }`

Accessing the db from req in my api

  app.get("/srv/database", async(req, res) => {
    try {
        const dbClass = require("sap-hdbext-promisfied")
        let db = new dbClass(req.db);
        console.log(req);
        console.log(db)
        const statement = await db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA FROM "DUMMY"`)
        const results = await db.statementExecPromisified(statement, [])
        let result = JSON.stringify({
            Objects: results
        })
        return res.type("application/json").status(200).send(result)
    } catch (e) {
        return res.type("text/plain").status(500).send(`ERROR: ${e.toString()}`)
    }

  });

I don't get any error or warning or db object in my req.

jung-thomas commented 3 years ago

So you are saying that req.db isn't a valid object? If so that isn't this module that creates or maintains that object. Its the standard @sap/hdbext. If that is the case you need to report this problem to @sap/hdbext. I can't help you here in this promise wrapper module with functionality that isn't part of this module.

Is the above code complete? I see you are creating the hdbext.middleware but you aren't returning it or setting it into express. I don't really see from the snipped you have above how req.db would get filled.