Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    bc_uk
    Can node-oracledb be used in Mobile Hub Custom APIs?
    Topic posted June 10, 2019 by bc_uk, last edited June 10, 2019, tagged API, Mobile 
    56 Views, 4 Comments
    Title:
    Can node-oracledb be used in Mobile Hub Custom APIs?
    Content:

    From within my Mobile Hub Custom APIs I need to be able to directly access an Oracle DB instance using Basic authentication. As per the Oracle Mobile Platform videos on youtube, I added the oracledb package as a dependency:

    {

      "name": "patients",

      "version": "1.0.0",

      "description": "API to handle getting, adding, and updating of patient records.",

      "main": "patients.js",

      "dependencies": {

        "oracledb": "3.1.2"

      },

      "oracleMobile": {

        "dependencies": {

          "apis": {},

          "connectors": {}

        }

      }

    }

     

    I then executed "npm install" within the package folder, and it completed without error, and generated a package-lock.json file:

     

    {

      "name": "patients",

      "version": "1.0.0",

      "lockfileVersion": 1,

      "requires": true,

      "dependencies": {

        "oracledb": {

          "version": "3.1.2",

          "resolved": "https://registry.npmjs.org/oracledb/-/oracledb-3.1.2.tgz",

          "integrity": "sha512-DOBKpUlfvGAX6bcpuPGtPVVfOOUqFue8eLjbnnmkDyBTU+YuH+gXHRy4ftlFzkHBXYkrSGenFJrXy8dDfEVXCg=="

        }

      }

    }

     

    However, when I uploaded the Custom API package and tested it, I got the following error:

     

    MOBILE-43560: Custom Code VM: Failed to start container due to an error in the custom code: /u01/app/oracle/mobile/mobile_ccc/custom_code_modules/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_6/patients/patients.js:29
    connection = await oracledb.getConnection( {
                       ^^^^^^^^
    SyntaxError: Unexpected identifier.

     

    Here is my full node.js code for the Custom API:

     

    module.exports = function(service) {

        service.get('/mobile/custom/patients/patients', function(req,res) {

            var oracledb = require('oracledb');

            let connection;

            try {

                connection = await oracledb.getConnection( {

                    user          : "system",

                    password      : "xxxxxxxxxx",

                    connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = ***.xx.xx.***)(PORT = 1521))(CONNECT_DATA =(SID= db1)))"

                });

                let result = await connection.execute('SELECT * FROM patients');

                console.info(result.rows);           

            } catch (err) {

                console.error(err);

            } finally {

                if (connection) {

                  try {

                    await connection.close();

                  } catch (err) {

                    console.error(err);

                  }

                }

            }

       

            var result = {};

            var statusCode = 200;

            res.status(statusCode).send(result);

        });

     

        // router: get patient by id

        service.get('/mobile/custom/patients/patients/:id', function(req,res) {

            var result = {};

            var statusCode = 200;

            if (statusCode == 200){

                var acceptType = req.accepts(['application/json']);

                if (acceptType == 'application/json'){

                    result = {

                          "items":

                            [

                              {

                                "id": 1,

                                "first_name": "Ted",

                                "surname": "Smith",

                                "gender": "M",

                                "dob": "12-Jun-1987"

                              }

                            ]

                        };

                }

            }

            res.status(statusCode).send(result);

        });

    };

     

    Please advise.

    Comment

     

    • Rohit Dhamija

      Hi Bill,

      As per my understanding  oracle node driver requires a native library to run.  The native libraries support is not available currently in MCS Custom API.

      Regards,

      Rohit

      • bc_uk

        Thank you for the info. How can I access an Oracle database from within a Mobile Hub Custom API then without using Mobile Hub Connectors (which would mean having to write another web service on the server that hosts the Oracle db)?

    • Laban Ndwaru

      Hi Bill,

      You may try to explore the OIC ATP adapter, create a connector on your custom component to OIC.

      Check this blog > https://blogs.oracle.com/integration/oic-integration-with-oracleatp