forked from brianc/node-postgres
- Notifications
You must be signed in to change notification settings - Fork 0
tnbiscuits edited this page Jul 1, 2016
·
61 revisions
The root pg object returned by require('pg') exports a few internal constructors which might be helpful.
It has a reference to the other components of node-postgres:
There are two common ways to interact with your PostgreSQL server via node-postgres.
First, and what you'll generally want to do is interact with your server via a pool of clients
varPool=require('pg').Pool;varpool=newPool({user: 'foo',password: 'bar',host: 'localhost',database: 'my_db',max: 10,// max number of clients in poolidleTimeoutMillis: 1000,// close & remove clients which have been idle > 1 second});pool.on('error',function(e,client){// if a client is idle in the pool// and receives an error - for example when your PostgreSQL server restarts// the pool will catch the error & let you handle it here});// you can run queries directly against the poolpool.query('SELECT $1::text as name',['foo'],function(err,result){console.log(result.rows[0].name);// output: foo});// the query object implements the promise APIpool.query('SELECT $1::text as name',['foo']).then(res=>console.log(res.rows[0].name));// output: foo// the pool also supports checking out a client for// multiple operations, such as a transactionpool.connect(function(err,client,release){// TODO - you'll want to handle the error in real codeclient.query('SELECT $1::text as name',['foo'],function(err,result){// you MUST return your client back to the pool when you're done!release();console.log(result.rows[0].name);// output: foo});});// again this api supports promises// if you use something like [co](https://github.com/tj/co) // you can end up with much cleaner looking codeco(function*(){varclient=yieldpool.connect()try{yieldclient.query('BEGIN')varresult=yieldclient.query('SELECT $1::text as name',['foo'])yieldclient.query('INSERT INTO something(name) VALUES($1)',[result.rows[0].name])yieldclient.query('COMMIT')client.release()}catch(e){// pass truthy value to release to destroy the client// instead of returning it to the pool// the pool will create a new client next time// this will also roll back the transaction within postgresclient.release(true)}})If you want to interact with postgres without using the pool, you can instantiate a client directly & use it as follows:
varClient=require('pg').Client;varclient=newClient();client.connect();client.query('SELECT $1::text as name',['foo'],function(err,res){console.log(res.rows[0].name);client.end();});