我正在写一个 node .在Heroku上安装js应用程序,并使用pg module.我无法找到"正确"的方法来为查询数据库所需的每个请求获取客户机对象.
该文档使用如下代码:
pg.connect(conString, function(err, client) {
// Use the client to do things here
});
但你肯定不需要在每个使用数据库的函数中调用pg.connect
,对吗?我见过other code个这样做的:
var conString = process.env.DATABASE_URL || "tcp://postgres:1234@localhost/postgres";
var client = new pg.Client(conString);
client.connect();
// client is a global so you can use it anywhere now
我倾向于第二种 Select ,因为我认为Heroku的免费数据库实例无论如何都只限于一个连接,但是这样做有什么缺点吗?在每次使用客户端对象之前,是否需要判断它是否仍处于连接状态?