每次查詢數據庫時,是否需要使用pg.connect()?看在githhub頁面和wiki後,示例顯示了pg.connect回調像這裏面的查詢(註釋是從GitHub的例子,我沒有寫他們)使用node-postgres查詢postgres db
//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 20 (also configurable)
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
});
});
的意見混亂,因爲它聽起來像pg.connect()正在爲每個調用創建一個新的客戶端池,這顯然是效率低下的。我在創建客戶端的文檔中看到了其他示例,但我想使用客戶端池。
如果您想避免連接複雜性,請使用[pg-promise](https://github.com/vitaly-t/pg-promise) –