使用Node.js服务器中的Typescript连接到Postgresdb

时间:2018-12-13 10:48:00

标签: javascript node.js database typescript middleware

我是Typescript和Node.js的新手。我想将节点上运行的服务器连接到容器化环境中的postgresdb。有一些没有typecirpt的示例(仅Java脚本)。 任何示例或链接都将有所帮助。

谢谢

编辑: 这个JS示例

const { Client } = require('pg')
const client = new Client()
await client.connect()
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()

这是取自https://node-postgres.com/的示例,我们可以将其转换为typescript,使用typescirpt是设计决策,因此我不能使用javascript,想要在放弃之前尝试尝试一下。

1 个答案:

答案 0 :(得分:0)

我最终使用了typeorm,并且可以与TS一起使用。 https://github.com/typeorm/typeorm

Github Repo中的安装和示例。 https://github.com/typeorm/typeorm#Installation

这可能不适用于每个人,但似乎最适合我,并且库看起来很稳定。