1

I have tried for more hours than I care to admit to get an openWhisk function to call a postgre sql datbase on Compose.io. Here is my code:

My latest incarnation is this:

function myAction(params) {
 return new Promise(function(resolve, reject) {
    console.log('Connecting to Compose database');
 // console.log('Params ---> ', params);
    var mysql = require('promise-mysql');
 var fs = require('fs');
 var pg = require('pg');
 var request = require('request')
 var Promise = require('promise/lib/es6-extensions');
 var connString = "postgres:xxxx";
 
 
 
  pg.connect(connString, function(err, client, done) {
   console.log("connectiong..", err, client, done);
   
          if (err) {

            console.log('[connectToCompose] failed to fetch client from pool', err);
            reject(err);

          } else {
            params.client = client; params.done = done;
            console.log('[connectToCompose] obtained a Compose client');
            return(params);

          }
       
  })
  
 // params.client.done();
 // console.log("closing connectiong");
 })

}

exports.main = myAction;

I have a similar example where I connect to a different SQL database (not Compose) and use sql promise not postgre and it works. What am I doing wrong?

  • Just to clarify, I get this as the result:{ "error": {} } and this for the log message 2017-08-14T02:39:07.29005115Z stdout: Connecting to Compose database – Chris Miyachi Aug 14 '17 at 02:39
  • Another piece: The compose database was created on Compose.io and not on bluemix - that should be OK but maybe not. – Chris Miyachi Aug 14 '17 at 14:00

1 Answers1

2

To work with OpenWhisk and any database offering, you need to use promisified JavaScript code; the event loop as it's used in ordinary nodejs isn't available. I have an example that uses pg-promise (taken more or less exactly from the project docs) and it works fine for me. Try something like this:

const promise = require('bluebird');

const initOptions = {
    promiseLib: promise // overriding the default (ES6 Promise);
};

const pgp = require('pg-promise')(options);
const conn_info = {...connection info...};

const db = pgp(conn_info);

module.exports.main = function main(args) {
    db.any('SELECT * FROM items')
        .then(data => {
            console.log('DATA:', data);
            // return whatever data you wanted
            resolve({message: 'success'});
        })
        .catch(error => {
            console.log('ERROR:', error);
        });
}

Not all of the dependencies here are available on OpenWhisk by default, so when you deploy the action, include both your *.js file and the whole of node_modules/ in a zip file, and upload that. It's definitely possible to use the Compose Postgres with OpenWhisk, if that helps to encourage you :)

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
Lorna Mitchell
  • 1,819
  • 13
  • 22
  • Thanks so much for your response. I do include all my node modules. I will try your code and report back. – Chris Miyachi Aug 14 '17 at 20:34
  • I've modified a bit of code here: 1) Must not re-initialize everything inside the function, rather do it only once 2) No point creating another useless Promise on top 3) Must not call `pgp.end()` without understanding what it does and when to use it. @ChrisMiyachi – vitaly-t Aug 16 '17 at 06:43
  • Thanks for the edits, the new version is much tidier and clearer! I wasn't sure about the extra promise either, this makes sense. – Lorna Mitchell Aug 17 '17 at 10:17