diff --git a/docs/pages/guides/upgrading.md b/docs/pages/guides/upgrading.md index fe435d7cd..6a09d2ec1 100644 --- a/docs/pages/guides/upgrading.md +++ b/docs/pages/guides/upgrading.md @@ -50,7 +50,7 @@ pg.end() // new way, available since 6.0.0: // create a pool -var pool = new pg.Pool() +const pool = new pg.Pool() // connection using created pool pool.connect(function (err, client, done) { diff --git a/packages/pg-connection-string/README.md b/packages/pg-connection-string/README.md index 26d52bff7..d7ae10a40 100644 --- a/packages/pg-connection-string/README.md +++ b/packages/pg-connection-string/README.md @@ -15,9 +15,9 @@ MIT License ## Usage ```js -var parse = require('pg-connection-string').parse; +const parse = require('pg-connection-string').parse; -var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') +const config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') ``` The resulting config contains a subset of the following properties: diff --git a/packages/pg-native/README.md b/packages/pg-native/README.md index 307237f34..160ddfae6 100644 --- a/packages/pg-native/README.md +++ b/packages/pg-native/README.md @@ -30,40 +30,40 @@ $ npm i pg-native ### async ```js -var Client = require('pg-native') +const Client = require('pg-native') -var client = new Client(); +const client = new Client(); client.connect(function(err) { if(err) throw err - //text queries + // text queries client.query('SELECT NOW() AS the_date', function(err, rows) { if(err) throw err - console.log(rows[0].the_date) //Tue Sep 16 2014 23:42:39 GMT-0400 (EDT) + console.log(rows[0].the_date) // Tue Sep 16 2014 23:42:39 GMT-0400 (EDT) - //parameterized statements + // parameterized statements client.query('SELECT $1::text as twitter_handle', ['@briancarlson'], function(err, rows) { if(err) throw err console.log(rows[0].twitter_handle) //@briancarlson }) - //prepared statements + // prepared statements client.prepare('get_twitter', 'SELECT $1::text as twitter_handle', 1, function(err) { if(err) throw err - //execute the prepared, named statement + // execute the prepared, named statement client.execute('get_twitter', ['@briancarlson'], function(err, rows) { if(err) throw err console.log(rows[0].twitter_handle) //@briancarlson - //execute the prepared, named statement again + // execute the prepared, named statement again client.execute('get_twitter', ['@realcarrotfacts'], function(err, rows) { if(err) throw err - console.log(rows[0].twitter_handle) //@realcarrotfacts + console.log(rows[0].twitter_handle) // @realcarrotfacts client.end(function() { console.log('ended') @@ -81,27 +81,27 @@ client.connect(function(err) { Because `pg-native` is bound to [libpq](https://github.com/brianc/node-libpq) it is able to provide _sync_ operations for both connecting and queries. This is a bad idea in _non-blocking systems_ like web servers, but is exteremly convienent in scripts and bootstrapping applications - much the same way `fs.readFileSync` comes in handy. ```js -var Client = require('pg-native') +const Client = require('pg-native') -var client = new Client() +const client = new Client() client.connectSync() -//text queries -var rows = client.querySync('SELECT NOW() AS the_date') -console.log(rows[0].the_date) //Tue Sep 16 2014 23:42:39 GMT-0400 (EDT) +// text queries +const rows = client.querySync('SELECT NOW() AS the_date') +console.log(rows[0].the_date) // Tue Sep 16 2014 23:42:39 GMT-0400 (EDT) -//parameterized queries -var rows = client.querySync('SELECT $1::text as twitter_handle', ['@briancarlson']) -console.log(rows[0].twitter_handle) //@briancarlson +// parameterized queries +const rows = client.querySync('SELECT $1::text as twitter_handle', ['@briancarlson']) +console.log(rows[0].twitter_handle) // @briancarlson -//prepared statements +// prepared statements client.prepareSync('get_twitter', 'SELECT $1::text as twitter_handle', 1) -var rows = client.executeSync('get_twitter', ['@briancarlson']) -console.log(rows[0].twitter_handle) //@briancarlson +const rows = client.executeSync('get_twitter', ['@briancarlson']) +console.log(rows[0].twitter_handle) // @briancarlson -var rows = client.executeSync('get_twitter', ['@realcarrotfacts']) -console.log(rows[0].twitter_handle) //@realcarrotfacts +const rows = client.executeSync('get_twitter', ['@realcarrotfacts']) +console.log(rows[0].twitter_handle) // @realcarrotfacts ``` ## api @@ -125,14 +125,14 @@ Returns an `Error` to the `callback` if the connection was unsuccessful. `callb ##### example ```js -var client = new Client() +const client = new Client() client.connect(function(err) { if(err) throw err console.log('connected!') }) -var client2 = new Client() +const client2 = new Client() client2.connect('postgresql://user:password@host:5432/database?param=value', function(err) { if(err) throw err @@ -147,7 +147,7 @@ Execute a query with the text of `queryText` and _optional_ parameters specified ##### example ```js -var client = new Client() +const client = new Client() client.connect(function(err) { if (err) throw err @@ -175,7 +175,7 @@ Prepares a _named statement_ for later execution. You _must_ supply the name of ##### example ```js -var client = new Client() +const client = new Client() client.connect(function(err) { if(err) throw err @@ -197,7 +197,7 @@ Executes a previously prepared statement on this client with the name of `statem ```js -var client = new Client() +const client = new Client() client.connect(function(err) { if(err) throw err @@ -221,7 +221,7 @@ Ends the connection. Calls the _optional_ callback when the connection is termin ##### example ```js -var client = new Client() +const client = new Client() client.connect(function(err) { if(err) throw err client.end(function() { @@ -236,9 +236,9 @@ Cancels the active query on the client. Callback receives an error if there was ##### example ```js -var client = new Client() +const client = new Client() client.connectSync() -//sleep for 100 seconds +// sleep for 100 seconds client.query('select pg_sleep(100)', function(err) { console.log(err) // [Error: ERROR: canceling statement due to user request] }) diff --git a/packages/pg-pool/README.md b/packages/pg-pool/README.md index f456402ca..3ff657fe0 100644 --- a/packages/pg-pool/README.md +++ b/packages/pg-pool/README.md @@ -15,17 +15,17 @@ npm i pg-pool pg to use pg-pool you must first create an instance of a pool ```js -var Pool = require('pg-pool') +const Pool = require('pg-pool') // by default the pool uses the same // configuration as whatever `pg` version you have installed -var pool = new Pool() +const pool = new Pool() // you can pass properties to the pool // these properties are passed unchanged to both the node-postgres Client constructor // and the node-pool (https://github.com/coopernurse/node-pool) constructor // allowing you to fully configure the behavior of both -var pool2 = new Pool({ +const pool2 = new Pool({ database: 'postgres', user: 'brianc', password: 'secret!', @@ -37,14 +37,14 @@ var pool2 = new Pool({ maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion) }) -//you can supply a custom client constructor -//if you want to use the native postgres client -var NativeClient = require('pg').native.Client -var nativePool = new Pool({ Client: NativeClient }) +// you can supply a custom client constructor +// if you want to use the native postgres client +const NativeClient = require('pg').native.Client +const nativePool = new Pool({ Client: NativeClient }) -//you can even pool pg-native clients directly -var PgNativeClient = require('pg-native') -var pgNativePool = new Pool({ Client: PgNativeClient }) +// you can even pool pg-native clients directly +const PgNativeClient = require('pg-native') +const pgNativePool = new Pool({ Client: PgNativeClient }) ``` ##### Note: @@ -86,7 +86,7 @@ const pool = new Pool(config); pg-pool supports a fully promise-based api for acquiring clients ```js -var pool = new Pool() +const pool = new Pool() pool.connect().then(client => { client.query('select $1::text as name', ['pg-pool']).then(res => { client.release() @@ -106,10 +106,10 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o ```js // with async/await (async () => { - var pool = new Pool() - var client = await pool.connect() + const pool = new Pool() + const client = await pool.connect() try { - var result = await client.query('select $1::text as name', ['brianc']) + const result = await client.query('select $1::text as name', ['brianc']) console.log('hello from', result.rows[0]) } finally { client.release() @@ -118,9 +118,9 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o // with co co(function * () { - var client = yield pool.connect() + const client = yield pool.connect() try { - var result = yield client.query('select $1::text as name', ['brianc']) + const result = yield client.query('select $1::text as name', ['brianc']) console.log('hello from', result.rows[0]) } finally { client.release() @@ -133,16 +133,16 @@ co(function * () { because its so common to just run a query and return the client to the pool afterward pg-pool has this built-in: ```js -var pool = new Pool() -var time = await pool.query('SELECT NOW()') -var name = await pool.query('select $1::text as name', ['brianc']) +const pool = new Pool() +const time = await pool.query('SELECT NOW()') +const name = await pool.query('select $1::text as name', ['brianc']) console.log(name.rows[0].name, 'says hello at', time.rows[0].now) ``` you can also use a callback here if you'd like: ```js -var pool = new Pool() +const pool = new Pool() pool.query('SELECT $1::text as name', ['brianc'], function (err, res) { console.log(res.rows[0].name) // brianc }) @@ -158,7 +158,7 @@ clients back to the pool after the query is done. pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years: ```js -var pool = new Pool() +const pool = new Pool() pool.connect((err, client, done) => { if (err) return done(err) @@ -178,8 +178,8 @@ When you are finished with the pool if all the clients are idle the pool will cl will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows: ```js -var pool = new Pool() -var client = await pool.connect() +const pool = new Pool() +const client = await pool.connect() console.log(await client.query('select now()')) client.release() await pool.end() @@ -194,7 +194,7 @@ The pool should be a __long-lived object__ in your application. Generally you'l // correct usage: create the pool and let it live // 'globally' here, controlling access to it through exported methods -var pool = new pg.Pool() +const pool = new pg.Pool() // this is the right way to export the query method module.exports.query = (text, values) => { @@ -208,7 +208,7 @@ module.exports.connect = () => { // every time we called 'connect' to get a new client? // that's a bad thing & results in creating an unbounded // number of pools & therefore connections - var aPool = new pg.Pool() + const aPool = new pg.Pool() return aPool.connect() } ``` @@ -245,7 +245,7 @@ Example: const Pool = require('pg-pool') const pool = new Pool() -var count = 0 +const count = 0 pool.on('connect', client => { client.count = count++ @@ -272,20 +272,20 @@ Example: This allows you to count the number of clients which have ever been acquired from the pool. ```js -var Pool = require('pg-pool') -var pool = new Pool() +const Pool = require('pg-pool') +const pool = new Pool() -var acquireCount = 0 +const acquireCount = 0 pool.on('acquire', function (client) { acquireCount++ }) -var connectCount = 0 +const connectCount = 0 pool.on('connect', function () { connectCount++ }) -for (var i = 0; i < 200; i++) { +for (let i = 0; i < 200; i++) { pool.query('SELECT NOW()') } @@ -324,7 +324,7 @@ if (typeof Promise == 'undefined') { You can use any other promise implementation you'd like. The pool also allows you to configure the promise implementation on a per-pool level: ```js -var bluebirdPool = new Pool({ +const bluebirdPool = new Pool({ Promise: require('bluebird') }) ``` diff --git a/packages/pg-query-stream/README.md b/packages/pg-query-stream/README.md index b2e860528..f32a27788 100644 --- a/packages/pg-query-stream/README.md +++ b/packages/pg-query-stream/README.md @@ -15,16 +15,16 @@ _requires pg>=2.8.1_ ```js const pg = require('pg') -var pool = new pg.Pool() +const pool = new pg.Pool() const QueryStream = require('pg-query-stream') const JSONStream = require('JSONStream') -//pipe 1,000,000 rows to stdout without blowing up your memory usage +// pipe 1,000,000 rows to stdout without blowing up your memory usage pool.connect((err, client, done) => { if (err) throw err const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [1000000]) const stream = client.query(query) - //release the client when the stream is finished + // release the client when the stream is finished stream.on('end', done) stream.pipe(JSONStream.stringify()).pipe(process.stdout) })