javascript - Manage node-postgres query -
i've read through node-postgres’s api documentation.
it recommended use pg
object create pooled clients. , in pg.connect
api documentation said:
the connect method retrieves client client pool, or if pooled clients busy , pool not full, connect method create new client passing first argument directly client constructor.
so according recommendation, using pg.connect
mean "using pg
object create pooled clients"? if it's not, mean?
and in implementation example, made several queries in route:
app.get('/post', function(req, res) { pg.connect(dbconfig, function(err, client, done) { client.query('select * post', function(err, result) { res.render('post/list', { posts: result.rows }); }); }); }); app.get('/post/new', function(req, res) { res.render('post/new'); }); app.post('/api/v1/post', function(req, res) { var b = req.body; pg.connect(dbconfig, function(err, client, done) { client.query('insert post (title, content) values ($1, $2)', [b.title, b.content], function(err, result) { done(); res.redirect('/post'); }); }); });
is right way call pg.connect
each time want make query? if not, better idea?
it look, according documentation pg.connect() handle pooled connections. suggest 1 thing better (assuming have 1 set of credentials app using).
if looking @ doing this, work on saving duplication of effort/keystrokes/opportunities error bit , @ wrapping pg.connect() in sort of function use return client. enable more like:
app.get('/post', function(req, res) { db.run( function(client) { client.query('select * post', function(err, result) { res.render('post/list', { posts: result.rows }); }); }); });
however, given way have things done, not convinced have lot gain such approach, don't see wrong approach.
Comments
Post a Comment