【问题标题】:How to Connect Compose for PostgreSQL from IBM Cloud Functions?如何从 IBM Cloud Functions 连接 Compose for PostgreSQL?
【发布时间】:2018-06-14 03:57:33
【问题描述】:
我正在使用 IBM Cloud Functions 将音频文件转换为文本,并为此使用 IBM Watson 语音转文本服务。在这里,我想将成绩单存储到 PostgreSQL 数据库。 IBM Cloud Functions 和 Compose for PostgreSQL 服务之间是否有任何连接,以便我可以将脚本存储到数据库中。
我在云功能中使用 Node Runtime。
【问题讨论】:
标签:
node.js
postgresql
ibm-cloud
compose-db
ibm-cloud-functions
【解决方案1】:
使用包含在 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);
}