什么是使用node.js postgresql模块的正确方法? [英] What is the proper way to use the node.js postgresql module?

查看:111
本文介绍了什么是使用node.js postgresql模块的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Heroku上编写node.js应用程序,并使用 pg模块。我无法弄清楚为需要查询数据库的每个请求获取客户端对象的正确方式。



文档使用如下代码:

  pg.connect(conString,function(err,client){
//使用客户端在这里执行
});

但是你确实不需要调用 pg.connect 在每个使用数据库权限的函数中?我已经看到了其他代码

  var conString = process.env.DATABASE_URL || TCP:// postgres的:1234 @本地/ postgres的; 
var client = new pg.Client(conString);
client.connect();
//客户端是全局的,所以你可以在任何地方使用它

我倾向于第二种选择,因为我相信Heroku的免费数据库实例无论如何只限于一个连接,但是这样做有什么缺点吗?我是否需要检查我的客户端对象是否每次都在我使用之前连接?

解决方案

我是 node-postgres 。首先,我很抱歉,文件没有明确说明正确的选项:这是我的错。我会尽力改进它。我刚刚写了 Gist 来解释这个,因为


使用 pg.connect

code>是要走的路



PostgreSQL服务器每个连接一次只能处理1个查询。
这意味着如果你有1个全局新的pg.Client()连接到你的
后端,你的整个应用程序就会根据postgres
可以回应查询。它从字面上将一切排列起来,排队
每个查询。是的,这是异步的,所以这是没问题的...但是,你不是
而是将你的吞吐量乘以10倍?使用 pg.connect
pg.defaults.poolSize 设置为理智的(我们做25-100 ,不确定
的正确数字)。



新的pg.Client 用于当您知道你在做什么。当你需要
一个长期居住的客户出于某种原因或需要非常小心
控制生命周期。一个很好的例子是使用
LISTEN / NOTIFY 。监听客户端需要在
之间连接并且不共享,因此它可以正确处理 NOTIFY 消息。
另一个例子是打开一个1-off的客户端来杀死一些
挂起的东西或者在命令行脚本中。



一个非常有用的做法是将应用程序中对数据库的所有访问集中到一个文件中。不要乱丢 pg.connect 呼叫或整个新客户端。有一个像 db.js 这样的文件:

  module.exports = {
query:function(text,values,cb){
pg.connect(function(err,client,done){
client.query(text,values,function (err,result){
done();
cb(err,result);
})
});


$ / code $ / pre

这样你就可以将你的实现从 pg.connect 到一个自定义客户端池或任何其他地方,只需要在一个地方改变它。



看一看在 node-pg-query模块执行此操作。


I am writing a node.js app on Heroku and using the pg module. I can't figure out the "right" way to get a client object for each request that I need to query the database.

The documentation uses code like this:

pg.connect(conString, function(err, client) {
  // Use the client to do things here
});

But surely you don't need to call pg.connect inside every function that uses the database right? I've seen other code that does this:

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

I am leaning toward the second option since I believe the free database instance for Heroku is limited to one connection anyway, but are there any drawbacks to doing it this way? Do I need to check if my client object is still connected every time before I use it?

解决方案

I'm the author of node-postgres. First, I apologize the documentation has failed to make the right option clear: that's my fault. I'll try to improve it. I wrote a Gist just now to explain this because the conversation grew too long for Twitter.

Using pg.connect is the way to go in a web environment.

PostgreSQL server can only handle 1 query at a time per connection. That means if you have 1 global new pg.Client() connected to your backend your entire app is bottleknecked based on how fast postgres can respond to queries. It literally will line everything up, queuing each query. Yeah, it's async and so that's alright...but wouldn't you rather multiply your throughput by 10x? Use pg.connect set the pg.defaults.poolSize to something sane (we do 25-100, not sure the right number yet).

new pg.Client is for when you know what you're doing. When you need a single long lived client for some reason or need to very carefully control the life-cycle. A good example of this is when using LISTEN/NOTIFY. The listening client needs to be around and connected and not shared so it can properly handle NOTIFY messages. Other example would be when opening up a 1-off client to kill some hung stuff or in command line scripts.

One very helpful thing is to centralize all access to your database in your app to one file. Don't litter pg.connect calls or new clients throughout. Have a file like db.js that looks something like this:

module.exports = {
   query: function(text, values, cb) {
      pg.connect(function(err, client, done) {
        client.query(text, values, function(err, result) {
          done();
          cb(err, result);
        })
      });
   }
}

This way you can change out your implementation from pg.connect to a custom pool of clients or whatever and only have to change things in one place.

Have a look at the node-pg-query module that does just this.

这篇关于什么是使用node.js postgresql模块的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆