如何从IBM Cloud Functions连接PostgreSQL?

时间:2018-01-04 12:06:39

标签: node.js postgresql ibm-cloud compose ibm-cloud-functions

我正在使用IBM Cloud Functions将音频文件转换为文本,我正在使用IBM Watson语音来为其提供文本服务。在这里,我想将成绩单存储到PostgreSQL数据库。 IBM Cloud Functions和Compose for PostgreSQL服务之间是否存在任何关联,以便我可以将脚本存储到数据库。

我在云端功能中使用Node Runtime。

1 个答案:

答案 0 :(得分:0)

使用Cloud Functions运行时中包含的Node.js模块 pg 效果很好。以下是对我有用的函数存根(taken from this GitHub repo):

function myactualfunc(connection, some params) {
  const client=new Client({
    connectionString: connection['postgres']['composed'][0],
    ssl: true
  });

  return client.connect()
     .then(() =>
          client.query(
            "select ....",
            query-parameters))
     .then(res => perform some processing here)
     .then(() => client.end())
     .then(() => {return {"result": my-result} })
     .catch(e => {return {"error": e}})
}

function main({some params, __bx_creds: {'databases-for-postgresql': {connection}}}) {
    return myactualfunc(connection,some params);
}