Merge tag 'v6.4.2' into cdb-6.1
Bump version
This commit is contained in:
commit
389c25f44d
14
CHANGELOG.md
14
CHANGELOG.md
@ -4,6 +4,20 @@ For richer information consult the commit log on github with referenced pull req
|
||||
|
||||
We do not include break-fix version release in this file.
|
||||
|
||||
### v6.4.0
|
||||
|
||||
- Add support for passing `client_encoding` as a connection parameter. Used when decoding strings in the JavaScript driver. The default is still `utf8`.
|
||||
|
||||
### v6.3.0
|
||||
|
||||
- Deprecate `pg.connect` `pg.end` and `pg.cancel` - favor using `new pg.Pool()` instead of pg singleton.
|
||||
- Deprecate undocumented but possibly used `query.promise()` method. Use the promise returned directly from `client.query` / `pool.query`.
|
||||
- Deprecate returning an automatically created query result from `client.query`. Instead return more idomatic responses for callback/promise methods.
|
||||
|
||||
### v6.2.0
|
||||
|
||||
- Add support for [parsing `replicationStart` messages](https://github.com/brianc/node-postgres/pull/1271/files).
|
||||
|
||||
### v6.1.0
|
||||
|
||||
- Add optional callback parameter to the pure JavaScript `client.end` method. The native client already supported this.
|
||||
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2010 - 2017 Brian Carlson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
197
README.md
197
README.md
@ -1,4 +1,4 @@
|
||||
#node-postgres
|
||||
# node-postgres
|
||||
|
||||
[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.svg?branch=master)](http://travis-ci.org/brianc/node-postgres)
|
||||
[![Dependency Status](https://david-dm.org/brianc/node-postgres.svg)](https://david-dm.org/brianc/node-postgres)
|
||||
@ -15,7 +15,124 @@ $ npm install pg
|
||||
|
||||
## Intro & Examples
|
||||
|
||||
### Simple example
|
||||
There are 3 ways of executing queries
|
||||
|
||||
1. Passing the query to a pool
|
||||
2. Borrowing a client from a pool and executing the query with it
|
||||
3. Obtaining an exclusive client and executing the query with it
|
||||
|
||||
It is recommended to pass the query to a pool as often as possible. If that isn't possible, because of long and complex transactions for example, borrow a client from a pool. Just remember to initialize the pool only once in your code so you maximize reusability of connections.
|
||||
|
||||
### Why pooling?
|
||||
|
||||
If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? For one thing, there is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Furthermore, PostgreSQL can support only a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a __very bad thing__. :tm: Additionally, PostgreSQL can only execute 1 query at a time per connected client, so pipelining all queries for all requests through a single, long-lived client will likely introduce a bottleneck into your application if you need high concurrency.
|
||||
|
||||
With that in mind we can imagine a situation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >100 simultaneous requests your web server will attempt to open 100 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, your database will become unresponsive, your app will seem to hang, and everything will break. Boooo!
|
||||
|
||||
__Good news__: node-postgres ships with built in client pooling. Client pooling allows your application to use a pool of already connected clients and reuse them for each request to your application. If your app needs to make more queries than there are available clients in the pool the queries will queue instead of overwhelming your database & causing a cascading failure. :thumbsup:
|
||||
|
||||
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convenience. If you want, you can `require('pg-pool')` and use it directly - it's the same as the constructor exported at `pg.Pool`.
|
||||
|
||||
It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
|
||||
|
||||
[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
|
||||
|
||||
For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)
|
||||
|
||||
### Pooling example
|
||||
|
||||
Let's create a pool in `./lib/db.js` which will be reused across the whole project
|
||||
|
||||
```javascript
|
||||
const pg = require('pg');
|
||||
|
||||
// create a config to configure both pooling behavior
|
||||
// and client options
|
||||
// note: all config is optional and the environment variables
|
||||
// will be read if the config is not present
|
||||
var config = {
|
||||
user: 'foo', //env var: PGUSER
|
||||
database: 'my_db', //env var: PGDATABASE
|
||||
password: 'secret', //env var: PGPASSWORD
|
||||
host: 'localhost', // Server hosting the postgres database
|
||||
port: 5432, //env var: PGPORT
|
||||
max: 10, // max number of clients in the pool
|
||||
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
|
||||
};
|
||||
|
||||
//this initializes a connection pool
|
||||
//it will keep idle connections open for 30 seconds
|
||||
//and set a limit of maximum 10 idle clients
|
||||
const pool = new pg.Pool(config);
|
||||
|
||||
pool.on('error', function (err, client) {
|
||||
// if an error is encountered by a client while it sits idle in the pool
|
||||
// the pool itself will emit an error event with both the error and
|
||||
// the client which emitted the original error
|
||||
// this is a rare occurrence but can happen if there is a network partition
|
||||
// between your application and the database, the database restarts, etc.
|
||||
// and so you might want to handle it and at least log it out
|
||||
console.error('idle client error', err.message, err.stack);
|
||||
});
|
||||
|
||||
//export the query method for passing queries to the pool
|
||||
module.exports.query = function (text, values, callback) {
|
||||
console.log('query:', text, values);
|
||||
return pool.query(text, values, callback);
|
||||
};
|
||||
|
||||
// the pool also supports checking out a client for
|
||||
// multiple operations, such as a transaction
|
||||
module.exports.connect = function (callback) {
|
||||
return pool.connect(callback);
|
||||
};
|
||||
```
|
||||
|
||||
Now if in `./foo.js` you want to pass a query to the pool
|
||||
|
||||
```js
|
||||
const pool = require('./lib/db');
|
||||
|
||||
//to run a query we just pass it to the pool
|
||||
//after we're done nothing has to be taken care of
|
||||
//we don't have to return any client to the pool or close a connection
|
||||
pool.query('SELECT $1::int AS number', ['2'], function(err, res) {
|
||||
if(err) {
|
||||
return console.error('error running query', err);
|
||||
}
|
||||
|
||||
console.log('number:', res.rows[0].number);
|
||||
});
|
||||
```
|
||||
|
||||
Or if in `./bar.js` you want borrow a client from the pool
|
||||
|
||||
```js
|
||||
const pool = require('./lib/db');
|
||||
|
||||
//ask for a client from the pool
|
||||
pool.connect(function(err, client, done) {
|
||||
if(err) {
|
||||
return console.error('error fetching client from pool', err);
|
||||
}
|
||||
|
||||
//use the client for executing the query
|
||||
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
|
||||
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
|
||||
done(err);
|
||||
|
||||
if(err) {
|
||||
return console.error('error running query', err);
|
||||
}
|
||||
console.log(result.rows[0].number);
|
||||
//output: 1
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
For more examples, including how to use a connection pool with promises and async/await see the [example](https://github.com/brianc/node-postgres/wiki/Example) page in the wiki.
|
||||
|
||||
### Obtaining an exclusive client, example
|
||||
|
||||
```js
|
||||
var pg = require('pg');
|
||||
@ -45,76 +162,6 @@ client.connect(function (err) {
|
||||
|
||||
```
|
||||
|
||||
### Client pooling
|
||||
|
||||
If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? For one thing, there is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Furthermore, PostgreSQL can support only a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a __very bad thing__. :tm: Additionally, PostgreSQL can only execute 1 query at a time per connected client, so pipelining all queries for all requests through a single, long-lived client will likely introduce a bottleneck into your application if you need high concurrency.
|
||||
|
||||
With that in mind we can imagine a situation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >100 simultaneous requests your web server will attempt to open 100 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, your database will become unresponsive, your app will seem to hang, and everything will break. Boooo!
|
||||
|
||||
__Good news__: node-postgres ships with built in client pooling. Client pooling allows your application to use a pool of already connected clients and reuse them for each request to your application. If your app needs to make more queries than there are available clients in the pool the queries will queue instead of overwhelming your database & causing a cascading failure. :thumbsup:
|
||||
|
||||
```javascript
|
||||
var pg = require('pg');
|
||||
|
||||
// create a config to configure both pooling behavior
|
||||
// and client options
|
||||
// note: all config is optional and the environment variables
|
||||
// will be read if the config is not present
|
||||
var config = {
|
||||
user: 'foo', //env var: PGUSER
|
||||
database: 'my_db', //env var: PGDATABASE
|
||||
password: 'secret', //env var: PGPASSWORD
|
||||
host: 'localhost', // Server hosting the postgres database
|
||||
port: 5432, //env var: PGPORT
|
||||
max: 10, // max number of clients in the pool
|
||||
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
|
||||
};
|
||||
|
||||
|
||||
//this initializes a connection pool
|
||||
//it will keep idle connections open for 30 seconds
|
||||
//and set a limit of maximum 10 idle clients
|
||||
var pool = new pg.Pool(config);
|
||||
|
||||
// to run a query we can acquire a client from the pool,
|
||||
// run a query on the client, and then return the client to the pool
|
||||
pool.connect(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(err)` to release the client back to the pool (or destroy it if there is an error)
|
||||
done(err);
|
||||
|
||||
if(err) {
|
||||
return console.error('error running query', err);
|
||||
}
|
||||
console.log(result.rows[0].number);
|
||||
//output: 1
|
||||
});
|
||||
});
|
||||
|
||||
pool.on('error', function (err, client) {
|
||||
// if an error is encountered by a client while it sits idle in the pool
|
||||
// the pool itself will emit an error event with both the error and
|
||||
// the client which emitted the original error
|
||||
// this is a rare occurrence but can happen if there is a network partition
|
||||
// between your application and the database, the database restarts, etc.
|
||||
// and so you might want to handle it and at least log it out
|
||||
console.error('idle client error', err.message, err.stack)
|
||||
})
|
||||
```
|
||||
|
||||
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convenience. If you want, you can `require('pg-pool')` and use it directly - it's the same as the constructor exported at `pg.Pool`.
|
||||
|
||||
It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
|
||||
|
||||
|
||||
[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
|
||||
|
||||
|
||||
For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)
|
||||
|
||||
## [More Documentation](https://github.com/brianc/node-postgres/wiki)
|
||||
|
||||
## Native Bindings
|
||||
@ -183,6 +230,10 @@ Information about the testing processes is in the [wiki](https://github.com/bria
|
||||
|
||||
Open source belongs to all of us, and we're all invited to participate!
|
||||
|
||||
## Troubleshooting and FAQ
|
||||
|
||||
The causes and solutions to common errors can be found among the [Frequently Asked Questions(FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)
|
||||
|
||||
## Support
|
||||
|
||||
If at all possible when you open an issue please provide
|
||||
@ -202,7 +253,7 @@ Follow me [@briancarlson](https://twitter.com/briancarlson) to keep up to date.
|
||||
|
||||
## License
|
||||
|
||||
Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -13,6 +13,7 @@ var pgPass = require('pgpass');
|
||||
var TypeOverrides = require('./type-overrides');
|
||||
|
||||
var ConnectionParameters = require('./connection-parameters');
|
||||
var utils = require('./utils');
|
||||
var Query = require('./query');
|
||||
var defaults = require('./defaults');
|
||||
var Connection = require('./connection');
|
||||
@ -26,19 +27,24 @@ var Client = function(config) {
|
||||
this.port = this.connectionParameters.port;
|
||||
this.host = this.connectionParameters.host;
|
||||
this.password = this.connectionParameters.password;
|
||||
this.replication = this.connectionParameters.replication;
|
||||
|
||||
var c = config || {};
|
||||
|
||||
this._types = new TypeOverrides(c.types);
|
||||
this._ending = false;
|
||||
this._connecting = false;
|
||||
this._connectionError = false;
|
||||
|
||||
this.connection = c.connection || new Connection({
|
||||
stream: c.stream,
|
||||
ssl: this.connectionParameters.ssl,
|
||||
keepAlive: c.keepAlive || false
|
||||
keepAlive: c.keepAlive || false,
|
||||
client_encoding: this.connectionParameters.client_encoding || 'utf8',
|
||||
});
|
||||
this.queryQueue = [];
|
||||
this.binary = c.binary || defaults.binary;
|
||||
this.encoding = 'utf8';
|
||||
this.encoding = this.connectionParameters.client_encoding || 'utf8';
|
||||
this.processID = null;
|
||||
this.secretKey = null;
|
||||
this.ssl = this.connectionParameters.ssl || false;
|
||||
@ -49,6 +55,7 @@ util.inherits(Client, EventEmitter);
|
||||
Client.prototype.connect = function(callback) {
|
||||
var self = this;
|
||||
var con = this.connection;
|
||||
this._connecting = true;
|
||||
|
||||
if(this.host && this.host.indexOf('/') === 0) {
|
||||
con.connect(this.host + '/.s.PGSQL.' + this.port);
|
||||
@ -106,6 +113,7 @@ Client.prototype.connect = function(callback) {
|
||||
//hook up query handling events to connection
|
||||
//after the connection initially becomes ready for queries
|
||||
con.once('readyForQuery', function() {
|
||||
self._connecting = false;
|
||||
|
||||
//delegate rowDescription to active query
|
||||
con.on('rowDescription', function(msg) {
|
||||
@ -178,34 +186,52 @@ Client.prototype.connect = function(callback) {
|
||||
});
|
||||
|
||||
con.on('error', function(error) {
|
||||
if(self.activeQuery) {
|
||||
if(this.activeQuery) {
|
||||
var activeQuery = self.activeQuery;
|
||||
self.activeQuery = null;
|
||||
this.activeQuery = null;
|
||||
return activeQuery.handleError(error, con);
|
||||
}
|
||||
if(!callback) {
|
||||
return self.emit('error', error);
|
||||
|
||||
if (this._connecting) {
|
||||
// set a flag indicating we've seen an error during connection
|
||||
// the backend will terminate the connection and we don't want
|
||||
// to throw a second error when the connection is terminated
|
||||
this._connectionError = true;
|
||||
}
|
||||
|
||||
if(!callback) {
|
||||
return this.emit('error', error);
|
||||
}
|
||||
|
||||
con.end(); // make sure ECONNRESET errors don't cause error events
|
||||
callback(error);
|
||||
callback = null;
|
||||
});
|
||||
}.bind(this));
|
||||
|
||||
con.once('end', function() {
|
||||
if ( callback ) {
|
||||
// haven't received a connection message yet !
|
||||
if (callback) {
|
||||
// haven't received a connection message yet!
|
||||
var err = new Error('Connection terminated');
|
||||
callback(err);
|
||||
callback = null;
|
||||
return;
|
||||
}
|
||||
if(self.activeQuery) {
|
||||
if(this.activeQuery) {
|
||||
var disconnectError = new Error('Connection terminated');
|
||||
self.activeQuery.handleError(disconnectError, con);
|
||||
self.activeQuery = null;
|
||||
this.activeQuery.handleError(disconnectError, con);
|
||||
this.activeQuery = null;
|
||||
}
|
||||
self.emit('end');
|
||||
});
|
||||
if (!this._ending) {
|
||||
// if the connection is ended without us calling .end()
|
||||
// on this client then we have an unexpected disconnection
|
||||
// treat this as an error unless we've already emitted an error
|
||||
// during connection.
|
||||
if (!this._connectionError) {
|
||||
this.emit('error', new Error('Connection terminated unexpectedly'));
|
||||
}
|
||||
}
|
||||
this.emit('end');
|
||||
}.bind(this));
|
||||
|
||||
|
||||
con.on('notice', function(msg) {
|
||||
@ -226,6 +252,9 @@ Client.prototype.getStartupConf = function() {
|
||||
if (appName) {
|
||||
data.application_name = appName;
|
||||
}
|
||||
if (params.replication) {
|
||||
data.replication = '' + params.replication;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
@ -325,10 +354,12 @@ Client.prototype.copyTo = function (text) {
|
||||
throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams");
|
||||
};
|
||||
|
||||
var DeprecatedEmitterQuery = utils.deprecateEventEmitter(Query);
|
||||
|
||||
Client.prototype.query = function(config, values, callback) {
|
||||
//can take in strings, config object or query object
|
||||
var query = (typeof config.submit == 'function') ? config :
|
||||
new Query(config, values, callback);
|
||||
new DeprecatedEmitterQuery(config, values, callback);
|
||||
if(this.binary && !query.binary) {
|
||||
query.binary = true;
|
||||
}
|
||||
@ -342,6 +373,7 @@ Client.prototype.query = function(config, values, callback) {
|
||||
};
|
||||
|
||||
Client.prototype.end = function(cb) {
|
||||
this._ending = true;
|
||||
this.connection.end();
|
||||
if (cb) {
|
||||
this.connection.once('end', cb);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -57,6 +57,7 @@ var ConnectionParameters = function(config) {
|
||||
this.binary = val('binary', config);
|
||||
this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl;
|
||||
this.client_encoding = val("client_encoding", config);
|
||||
this.replication = val("replication", config);
|
||||
//a domain socket begins with '/'
|
||||
this.isDomainSocket = (!(this.host||'').indexOf('/'));
|
||||
this.keepAlive = config.keepAlive || false;
|
||||
@ -65,10 +66,15 @@ var ConnectionParameters = function(config) {
|
||||
this.fallback_application_name = val('fallback_application_name', config, false);
|
||||
};
|
||||
|
||||
// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
|
||||
var quoteParamValue = function(value) {
|
||||
return "'" + ('' + value).replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "'";
|
||||
};
|
||||
|
||||
var add = function(params, config, paramName) {
|
||||
var value = config[paramName];
|
||||
if(value) {
|
||||
params.push(paramName+"='"+value+"'");
|
||||
params.push(paramName + "=" + quoteParamValue(value));
|
||||
}
|
||||
};
|
||||
|
||||
@ -80,21 +86,30 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
|
||||
add(params, this, 'application_name');
|
||||
add(params, this, 'fallback_application_name');
|
||||
|
||||
var ssl = typeof this.ssl === 'object' ? this.ssl : {sslmode: this.ssl};
|
||||
add(params, ssl, 'sslmode');
|
||||
add(params, ssl, 'sslca');
|
||||
add(params, ssl, 'sslkey');
|
||||
add(params, ssl, 'sslcert');
|
||||
|
||||
if(this.database) {
|
||||
params.push("dbname='" + this.database + "'");
|
||||
params.push("dbname=" + quoteParamValue(this.database));
|
||||
}
|
||||
if(this.replication) {
|
||||
params.push("replication=" + quoteParamValue(this.replication));
|
||||
}
|
||||
if(this.host) {
|
||||
params.push("host=" + this.host);
|
||||
params.push("host=" + quoteParamValue(this.host));
|
||||
}
|
||||
if(this.isDomainSocket) {
|
||||
return cb(null, params.join(' '));
|
||||
}
|
||||
if(this.client_encoding) {
|
||||
params.push("client_encoding='" + this.client_encoding + "'");
|
||||
params.push("client_encoding=" + quoteParamValue(this.client_encoding));
|
||||
}
|
||||
dns.lookup(this.host, function(err, address) {
|
||||
if(err) return cb(err, null);
|
||||
params.push("hostaddr=" + address);
|
||||
params.push("hostaddr=" + quoteParamValue(address));
|
||||
return cb(null, params.join(' '));
|
||||
});
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -41,7 +41,7 @@ var Connection = function(config) {
|
||||
this.lastOffset = 0;
|
||||
this.buffer = null;
|
||||
this.offset = null;
|
||||
this.encoding = 'utf8';
|
||||
this.encoding = config.client_encoding || 'utf8';
|
||||
this.parsedStatements = {};
|
||||
this.writer = new Writer();
|
||||
this.ssl = config.ssl || false;
|
||||
@ -89,9 +89,6 @@ Connection.prototype.connect = function(port, host) {
|
||||
});
|
||||
|
||||
this.stream.on('close', function() {
|
||||
// NOTE: node-0.10 emits both 'end' and 'close'
|
||||
// for streams closed by the peer, while
|
||||
// node-0.8 only emits 'close'
|
||||
self.emit('end');
|
||||
});
|
||||
|
||||
@ -145,8 +142,6 @@ Connection.prototype.attachListeners = function(stream) {
|
||||
};
|
||||
|
||||
Connection.prototype.requestSsl = function() {
|
||||
this.checkSslResponse = true;
|
||||
|
||||
var bodyBuffer = this.writer
|
||||
.addInt16(0x04D2)
|
||||
.addInt16(0x162F).flush();
|
||||
@ -427,6 +422,9 @@ Connection.prototype.parseMessage = function(buffer) {
|
||||
case 0x48: //H
|
||||
return this.parseH(buffer, length);
|
||||
|
||||
case 0x57: //W
|
||||
return new Message('replicationStart', length);
|
||||
|
||||
case 0x63: //c
|
||||
return new Message('copyDone', length);
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
|
16
lib/index.js
16
lib/index.js
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -27,7 +27,7 @@ var PG = function(clientConstructor) {
|
||||
|
||||
util.inherits(PG, EventEmitter);
|
||||
|
||||
PG.prototype.end = function() {
|
||||
PG.prototype.end = util.deprecate(function() {
|
||||
var self = this;
|
||||
var keys = Object.keys(this._pools);
|
||||
var count = keys.length;
|
||||
@ -47,14 +47,13 @@ PG.prototype.end = function() {
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}, 'PG.end is deprecated - please see the upgrade guide at https://node-postgres.com/guides/upgrading');
|
||||
|
||||
PG.prototype.connect = function(config, callback) {
|
||||
PG.prototype.connect = util.deprecate(function(config, callback) {
|
||||
if(typeof config == "function") {
|
||||
callback = config;
|
||||
config = null;
|
||||
}
|
||||
var poolName = JSON.stringify(config || {});
|
||||
if (typeof config == 'string') {
|
||||
config = new ConnectionParameters(config);
|
||||
}
|
||||
@ -66,6 +65,7 @@ PG.prototype.connect = function(config, callback) {
|
||||
config.idleTimeoutMillis = config.idleTimeoutMillis || config.poolIdleTimeout || defaults.poolIdleTimeout;
|
||||
config.log = config.log || config.poolLog || defaults.poolLog;
|
||||
|
||||
var poolName = JSON.stringify(config);
|
||||
this._pools[poolName] = this._pools[poolName] || new this.Pool(config);
|
||||
var pool = this._pools[poolName];
|
||||
if(!pool.listeners('error').length) {
|
||||
@ -75,10 +75,10 @@ PG.prototype.connect = function(config, callback) {
|
||||
}.bind(this));
|
||||
}
|
||||
return pool.connect(callback);
|
||||
};
|
||||
}, 'PG.connect is deprecated - please see the upgrade guide at https://node-postgres.com/guides/upgrading');
|
||||
|
||||
// cancel the query running on the given client
|
||||
PG.prototype.cancel = function(config, client, query) {
|
||||
PG.prototype.cancel = util.deprecate(function(config, client, query) {
|
||||
if(client.native) {
|
||||
return client.cancel(query);
|
||||
}
|
||||
@ -89,7 +89,7 @@ PG.prototype.cancel = function(config, client, query) {
|
||||
}
|
||||
var cancellingClient = new this.Client(c);
|
||||
cancellingClient.cancel(client, query);
|
||||
};
|
||||
}, 'PG.cancel is deprecated - use client.cancel instead');
|
||||
|
||||
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
|
||||
module.exports = new PG(require('./native'));
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -46,6 +46,8 @@ var Client = module.exports = function(config) {
|
||||
this.namedQueries = {};
|
||||
};
|
||||
|
||||
Client.Query = NativeQuery;
|
||||
|
||||
util.inherits(Client, EventEmitter);
|
||||
|
||||
//connect to the backend
|
||||
@ -139,6 +141,35 @@ Client.prototype.query = function(config, values, callback) {
|
||||
return query;
|
||||
};
|
||||
|
||||
var DeprecatedQuery = require('../utils').deprecateEventEmitter(NativeQuery);
|
||||
|
||||
//send a query to the server
|
||||
//this method is highly overloaded to take
|
||||
//1) string query, optional array of parameters, optional function callback
|
||||
//2) object query with {
|
||||
// string query
|
||||
// optional array values,
|
||||
// optional function callback instead of as a separate parameter
|
||||
// optional string name to name & cache the query plan
|
||||
// optional string rowMode = 'array' for an array of results
|
||||
// }
|
||||
Client.prototype.query = function(config, values, callback) {
|
||||
if (typeof config.submit == 'function') {
|
||||
// accept query(new Query(...), (err, res) => { }) style
|
||||
if (typeof values == 'function') {
|
||||
config.callback = values;
|
||||
}
|
||||
this._queryQueue.push(config);
|
||||
this._pulseQueryQueue();
|
||||
return config;
|
||||
}
|
||||
|
||||
var query = new DeprecatedQuery(config, values, callback);
|
||||
this._queryQueue.push(query);
|
||||
this._pulseQueryQueue();
|
||||
return query;
|
||||
};
|
||||
|
||||
//disconnect from the backend server
|
||||
Client.prototype.end = function(cb) {
|
||||
var self = this;
|
||||
@ -178,9 +209,11 @@ Client.prototype._pulseQueryQueue = function(initialConnection) {
|
||||
this._activeQuery = query;
|
||||
query.submit(this);
|
||||
var self = this;
|
||||
query.once('_done', function() {
|
||||
var pulseOnDone = function() {
|
||||
self._pulseQueryQueue();
|
||||
});
|
||||
query.removeListener('_done', pulseOnDone);
|
||||
};
|
||||
query._on('_done', pulseOnDone);
|
||||
};
|
||||
|
||||
//attempt to cancel an in-progress query
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -11,15 +11,15 @@ var util = require('util');
|
||||
var utils = require('../utils');
|
||||
var NativeResult = require('./result');
|
||||
|
||||
var NativeQuery = module.exports = function(native) {
|
||||
var NativeQuery = module.exports = function(config, values, callback) {
|
||||
EventEmitter.call(this);
|
||||
this.native = native;
|
||||
this.text = null;
|
||||
this.values = null;
|
||||
this.name = null;
|
||||
this.callback = null;
|
||||
config = utils.normalizeQueryConfig(config, values, callback);
|
||||
this.text = config.text;
|
||||
this.values = config.values;
|
||||
this.name = config.name;
|
||||
this.callback = config.callback;
|
||||
this.state = 'new';
|
||||
this._arrayMode = false;
|
||||
this._arrayMode = config.rowMode == 'array';
|
||||
|
||||
//if the 'row' event is listened for
|
||||
//then emit them as they come in
|
||||
@ -27,30 +27,51 @@ var NativeQuery = module.exports = function(native) {
|
||||
//this has almost no meaning because libpq
|
||||
//reads all rows into memory befor returning any
|
||||
this._emitRowEvents = false;
|
||||
this.on('newListener', function(event) {
|
||||
this._on('newListener', function(event) {
|
||||
if(event === 'row') this._emitRowEvents = true;
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
util.inherits(NativeQuery, EventEmitter);
|
||||
|
||||
// TODO - remove in 7.0
|
||||
// this maintains backwards compat so someone could instantiate a query
|
||||
// manually: `new Query().then()`...
|
||||
NativeQuery._on = NativeQuery.on;
|
||||
NativeQuery._once = NativeQuery.once;
|
||||
|
||||
|
||||
NativeQuery.prototype.then = function(onSuccess, onFailure) {
|
||||
return this.promise().then(onSuccess, onFailure);
|
||||
return this._getPromise().then(onSuccess, onFailure);
|
||||
};
|
||||
|
||||
NativeQuery.prototype.catch = function(callback) {
|
||||
return this.promise().catch(callback);
|
||||
return this._getPromise().catch(callback);
|
||||
};
|
||||
|
||||
NativeQuery.prototype.promise = function() {
|
||||
NativeQuery.prototype._getPromise = function() {
|
||||
if (this._promise) return this._promise;
|
||||
this._promise = new Promise(function(resolve, reject) {
|
||||
this.once('end', resolve);
|
||||
this.once('error', reject);
|
||||
var onEnd = function (result) {
|
||||
this.removeListener('error', onError);
|
||||
this.removeListener('end', onEnd);
|
||||
resolve(result);
|
||||
};
|
||||
var onError = function (err) {
|
||||
this.removeListener('error', onError);
|
||||
this.removeListener('end', onEnd);
|
||||
reject(err);
|
||||
};
|
||||
this._on('end', onEnd);
|
||||
this._on('error', onError);
|
||||
}.bind(this));
|
||||
return this._promise;
|
||||
};
|
||||
|
||||
NativeQuery.prototype.promise = util.deprecate(function() {
|
||||
return this._getPromise();
|
||||
}, 'Query.promise() is deprecated - see the upgrade guide at https://node-postgres.com/guides/upgrading');
|
||||
|
||||
NativeQuery.prototype.handleError = function(err) {
|
||||
var self = this;
|
||||
//copy pq error fields into the error object
|
||||
@ -71,6 +92,7 @@ NativeQuery.prototype.handleError = function(err) {
|
||||
NativeQuery.prototype.submit = function(client) {
|
||||
this.state = 'running';
|
||||
var self = this;
|
||||
self.native = client.native;
|
||||
client.native.arrayMode = this._arrayMode;
|
||||
|
||||
var after = function(err, rows) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
|
@ -3,7 +3,6 @@ var util = require('util');
|
||||
var Pool = require('pg-pool');
|
||||
|
||||
module.exports = function(Client) {
|
||||
|
||||
var BoundPool = function(options) {
|
||||
var config = { Client: Client };
|
||||
for (var key in options) {
|
||||
|
32
lib/query.js
32
lib/query.js
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
@ -40,23 +40,43 @@ var Query = function(config, values, callback) {
|
||||
|
||||
util.inherits(Query, EventEmitter);
|
||||
|
||||
// TODO - remove in 7.0
|
||||
// this maintains backwards compat so someone could instantiate a query
|
||||
// manually: `new Query().then()`...
|
||||
Query._on = Query.on;
|
||||
Query._once = Query.once;
|
||||
|
||||
Query.prototype.then = function(onSuccess, onFailure) {
|
||||
return this.promise().then(onSuccess, onFailure);
|
||||
return this._getPromise().then(onSuccess, onFailure);
|
||||
};
|
||||
|
||||
Query.prototype.catch = function(callback) {
|
||||
return this.promise().catch(callback);
|
||||
return this._getPromise().catch(callback);
|
||||
};
|
||||
|
||||
Query.prototype.promise = function() {
|
||||
Query.prototype._getPromise = function () {
|
||||
if (this._promise) return this._promise;
|
||||
this._promise = new Promise(function(resolve, reject) {
|
||||
this.once('end', resolve);
|
||||
this.once('error', reject);
|
||||
var onEnd = function (result) {
|
||||
this.removeListener('error', onError);
|
||||
this.removeListener('end', onEnd);
|
||||
resolve(result);
|
||||
};
|
||||
var onError = function (err) {
|
||||
this.removeListener('error', onError);
|
||||
this.removeListener('end', onEnd);
|
||||
reject(err);
|
||||
};
|
||||
this._on('end', onEnd);
|
||||
this._on('error', onError);
|
||||
}.bind(this));
|
||||
return this._promise;
|
||||
};
|
||||
|
||||
Query.prototype.promise = util.deprecate(function() {
|
||||
return this._getPromise();
|
||||
}, 'Query.promise() is deprecated - see the upgrade guide at https://node-postgres.com/guides/upgrading');
|
||||
|
||||
Query.prototype.requiresPreparation = function() {
|
||||
//named queries must always be prepared
|
||||
if(this.name) { return true; }
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
|
24
lib/utils.js
24
lib/utils.js
@ -1,11 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var defaults = require('./defaults');
|
||||
|
||||
function escapeElement(elementRepresentation) {
|
||||
@ -31,6 +33,9 @@ function arrayString(val) {
|
||||
else if(Array.isArray(val[i])) {
|
||||
result = result + arrayString(val[i]);
|
||||
}
|
||||
else if(val[i] instanceof Buffer) {
|
||||
result += '\\\\x' + val[i].toString('hex');
|
||||
}
|
||||
else
|
||||
{
|
||||
result += escapeElement(prepareValue(val[i]));
|
||||
@ -137,11 +142,26 @@ function normalizeQueryConfig (config, values, callback) {
|
||||
return config;
|
||||
}
|
||||
|
||||
var queryEventEmitterOverloadDeprecationMessage = 'Using the automatically created return value from client.query as an event emitter is deprecated and will be removed in pg@7.0. Please see the upgrade guide at https://node-postgres.com/guides/upgrading';
|
||||
|
||||
var deprecateEventEmitter = function(Emitter) {
|
||||
var Result = function () {
|
||||
Emitter.apply(this, arguments);
|
||||
};
|
||||
util.inherits(Result, Emitter);
|
||||
Result.prototype._on = Result.prototype.on;
|
||||
Result.prototype._once = Result.prototype.once;
|
||||
Result.prototype.on = util.deprecate(Result.prototype.on, queryEventEmitterOverloadDeprecationMessage);
|
||||
Result.prototype.once = util.deprecate(Result.prototype.once, queryEventEmitterOverloadDeprecationMessage);
|
||||
return Result;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
prepareValue: function prepareValueWrapper (value) {
|
||||
//this ensures that extra arguments do not get passed into prepareValue
|
||||
//by accident, eg: from calling values.map(utils.prepareValue)
|
||||
return prepareValue(value);
|
||||
},
|
||||
normalizeQueryConfig: normalizeQueryConfig
|
||||
normalizeQueryConfig: normalizeQueryConfig,
|
||||
deprecateEventEmitter: deprecateEventEmitter,
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pg",
|
||||
"version": "6.1.6",
|
||||
"version": "6.4.2",
|
||||
"description": "PostgreSQL client - pure javascript & libpq with the same API",
|
||||
"keywords": [
|
||||
"postgres",
|
||||
@ -20,11 +20,11 @@
|
||||
"dependencies": {
|
||||
"buffer-writer": "1.0.1",
|
||||
"js-string-escape": "1.0.1",
|
||||
"packet-reader": "0.2.0",
|
||||
"packet-reader": "0.3.1",
|
||||
"pg-connection-string": "0.1.3",
|
||||
"pg-pool": "1.*",
|
||||
"pg-types": "1.*",
|
||||
"pgpass": "1.x",
|
||||
"pgpass": "1.*",
|
||||
"semver": "4.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
23
test/integration/client/deprecation-tests.js
Normal file
23
test/integration/client/deprecation-tests.js
Normal file
@ -0,0 +1,23 @@
|
||||
var helper = require('./test-helper')
|
||||
|
||||
process.on('warning', function (warning) {
|
||||
console.log(warning)
|
||||
throw new Error('Should not emit deprecation warning')
|
||||
})
|
||||
|
||||
var client = new helper.pg.Client()
|
||||
|
||||
client.connect(function (err) {
|
||||
if (err) throw err
|
||||
client.query('SELECT NOW()')
|
||||
.then(function (res) {
|
||||
client.query('SELECT NOW()', function () {
|
||||
client.end(function () {
|
||||
})
|
||||
})
|
||||
}).catch(function (err) {
|
||||
setImmediate(function () {
|
||||
throw err
|
||||
})
|
||||
})
|
||||
})
|
@ -1,6 +1,24 @@
|
||||
var helper = require(__dirname + '/test-helper');
|
||||
var util = require('util');
|
||||
|
||||
|
||||
test('non-query error with callback', function () {
|
||||
var client = new Client({
|
||||
user:'asldkfjsadlfkj'
|
||||
});
|
||||
client.connect(assert.calls(function (err) {
|
||||
assert(err);
|
||||
}));
|
||||
});
|
||||
|
||||
test('non-query error', function() {
|
||||
var client = new Client({
|
||||
user:'asldkfjsadlfkj'
|
||||
});
|
||||
assert.emits(client, 'error');
|
||||
client.connect();
|
||||
});
|
||||
|
||||
var createErorrClient = function() {
|
||||
var client = helper.client();
|
||||
client.once('error', function(err) {
|
||||
@ -11,9 +29,8 @@ var createErorrClient = function() {
|
||||
return client;
|
||||
};
|
||||
|
||||
test('error handling', function(){
|
||||
test('error handling', function() {
|
||||
test('within a simple query', function() {
|
||||
|
||||
var client = createErorrClient();
|
||||
|
||||
var query = client.query("select omfg from yodas_dsflsd where pixistix = 'zoiks!!!'");
|
||||
@ -77,7 +94,6 @@ test('error handling', function(){
|
||||
});
|
||||
|
||||
test('non-query error', function() {
|
||||
|
||||
var client = new Client({
|
||||
user:'asldkfjsadlfkj'
|
||||
});
|
||||
|
90
test/integration/client/network-partition-tests.js
Normal file
90
test/integration/client/network-partition-tests.js
Normal file
@ -0,0 +1,90 @@
|
||||
var co = require('co')
|
||||
|
||||
var buffers = require('../../test-buffers')
|
||||
var helper = require('./test-helper')
|
||||
|
||||
var net = require('net')
|
||||
|
||||
var Server = function(response) {
|
||||
this.server = undefined
|
||||
this.socket = undefined
|
||||
this.response = response
|
||||
}
|
||||
|
||||
Server.prototype.start = function (cb) {
|
||||
// this is our fake postgres server
|
||||
// it responds with our specified response immediatley after receiving every buffer
|
||||
// this is sufficient into convincing the client its connectet to a valid backend
|
||||
// if we respond with a readyForQuery message
|
||||
this.server = net.createServer(function (socket) {
|
||||
this.socket = socket
|
||||
if (this.response) {
|
||||
this.socket.on('data', function (data) {
|
||||
// deny request for SSL
|
||||
if (data.length == 8) {
|
||||
this.socket.write(new Buffer('N', 'utf8'))
|
||||
// consider all authentication requests as good
|
||||
} else if (!data[0]) {
|
||||
this.socket.write(buffers.authenticationOk())
|
||||
// respond with our canned response
|
||||
} else {
|
||||
this.socket.write(this.response)
|
||||
}
|
||||
}.bind(this))
|
||||
}
|
||||
}.bind(this))
|
||||
|
||||
var port = 54321
|
||||
|
||||
var options = {
|
||||
host: 'localhost',
|
||||
port: port,
|
||||
}
|
||||
this.server.listen(options.port, options.host, function () {
|
||||
cb(options)
|
||||
})
|
||||
}
|
||||
|
||||
Server.prototype.drop = function () {
|
||||
this.socket.end()
|
||||
}
|
||||
|
||||
Server.prototype.close = function (cb) {
|
||||
this.server.close(cb)
|
||||
}
|
||||
|
||||
var testServer = function (server, cb) {
|
||||
// wait for our server to start
|
||||
server.start(function(options) {
|
||||
// connect a client to it
|
||||
var client = new helper.Client(options)
|
||||
client.connect()
|
||||
|
||||
// after 50 milliseconds, drop the client
|
||||
setTimeout(function() {
|
||||
server.drop()
|
||||
}, 50)
|
||||
|
||||
// blow up if we don't receive an error
|
||||
var timeoutId = setTimeout(function () {
|
||||
throw new Error('Client should have emitted an error but it did not.')
|
||||
}, 5000)
|
||||
|
||||
// return our wait token
|
||||
client.on('error', function () {
|
||||
clearTimeout(timeoutId)
|
||||
server.close(cb)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// test being disconnected after readyForQuery
|
||||
const respondingServer = new Server(buffers.readyForQuery())
|
||||
testServer(respondingServer, function () {
|
||||
process.stdout.write('.')
|
||||
// test being disconnected from a server that never responds
|
||||
const silentServer = new Server()
|
||||
testServer(silentServer, function () {
|
||||
process.stdout.write('.')
|
||||
})
|
||||
})
|
@ -29,12 +29,18 @@ test('query killed during query execution of prepared statement', function() {
|
||||
var client = new Client(helper.args);
|
||||
client.connect(assert.success(function() {
|
||||
var sleepQuery = 'select pg_sleep($1)';
|
||||
var query1 = client.query({
|
||||
|
||||
const queryConfig = {
|
||||
name: 'sleep query',
|
||||
text: sleepQuery,
|
||||
values: [5] },
|
||||
assert.calls(function(err, result) {
|
||||
assert.equal(err.message, 'terminating connection due to administrator command');
|
||||
values: [5],
|
||||
};
|
||||
|
||||
// client should emit an error because it is unexpectedly disconnected
|
||||
assert.emits(client, 'error')
|
||||
|
||||
var query1 = client.query(queryConfig, assert.calls(function(err, result) {
|
||||
assert.equal(err.message, 'terminating connection due to administrator command');
|
||||
}));
|
||||
|
||||
query1.on('error', function(err) {
|
||||
@ -53,7 +59,6 @@ test('query killed during query execution of prepared statement', function() {
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
test('client end during query execution of prepared statement', function() {
|
||||
var client = new Client(helper.args);
|
||||
client.connect(assert.success(function() {
|
||||
|
@ -1,2 +1,2 @@
|
||||
var helper = require(__dirname + "/test-helper")
|
||||
var helper = require("./test-helper")
|
||||
helper.testPoolSize(2);
|
||||
|
@ -1,4 +1,4 @@
|
||||
var helper = require(__dirname + '/test-helper')
|
||||
var helper = require('./test-helper')
|
||||
|
||||
var called = false;
|
||||
test('disconnects', function() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
var helper = require(__dirname + '/test-helper')
|
||||
var helper = require('./test-helper')
|
||||
|
||||
var called = false;
|
||||
|
||||
test('disconnects', function() {
|
||||
var sink = new helper.Sink(4, function() {
|
||||
called = true;
|
||||
|
@ -1,5 +1,5 @@
|
||||
var helper = require(__dirname + "/../test-helper");
|
||||
var pg = require(__dirname + "/../../../lib");
|
||||
var helper = require("../test-helper");
|
||||
var pg = require("../../../lib");
|
||||
|
||||
//first make pool hold 2 clients
|
||||
pg.defaults.poolSize = 2;
|
||||
|
@ -1,4 +1,4 @@
|
||||
var helper = require(__dirname + '/test-helper');
|
||||
var helper = require('./test-helper');
|
||||
var _ = require('lodash')
|
||||
|
||||
const config = _.extend({ }, helper.config, { idleTimeoutMillis: 50 })
|
||||
|
@ -1,2 +1,2 @@
|
||||
var helper = require(__dirname + "/test-helper")
|
||||
var helper = require("./test-helper")
|
||||
helper.testPoolSize(40);
|
||||
|
@ -1,4 +1,4 @@
|
||||
var helper = require(__dirname + "/../test-helper")
|
||||
var helper = require("./../test-helper")
|
||||
var pg = helper.pg
|
||||
var native = helper.args.native
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
var helper = require(__dirname + '/test-helper');
|
||||
var helper = require('./test-helper');
|
||||
|
||||
//setup defaults
|
||||
helper.pg.defaults.user = helper.args.user;
|
||||
|
@ -1,2 +1,2 @@
|
||||
var helper = require(__dirname + "/test-helper")
|
||||
var helper = require("./test-helper")
|
||||
helper.testPoolSize(1);
|
||||
|
@ -0,0 +1,13 @@
|
||||
var helper = require("../test-helper");
|
||||
var pg = require("../../../lib");
|
||||
|
||||
pg.connect(helper.config, assert.success(function(client, done) {
|
||||
assert.equal(Object.keys(pg._pools).length, 1);
|
||||
pg.connect(helper.config, assert.success(function(client2, done2) {
|
||||
assert.equal(Object.keys(pg._pools).length, 1);
|
||||
|
||||
done();
|
||||
done2();
|
||||
pg.end();
|
||||
}));
|
||||
}));
|
@ -1,2 +1,2 @@
|
||||
var helper = require(__dirname + "/test-helper")
|
||||
var helper = require("./test-helper")
|
||||
helper.testPoolSize(200);
|
||||
|
@ -59,6 +59,17 @@ test('client settings', function() {
|
||||
|
||||
test('initializing from a config string', function() {
|
||||
|
||||
test('uses connectionString property', function () {
|
||||
var client = new Client({
|
||||
connectionString: 'postgres://brian:pass@host1:333/databasename'
|
||||
})
|
||||
assert.equal(client.user, 'brian');
|
||||
assert.equal(client.password, "pass");
|
||||
assert.equal(client.host, "host1");
|
||||
assert.equal(client.port, 333);
|
||||
assert.equal(client.database, "databasename");
|
||||
})
|
||||
|
||||
test('uses the correct values from the config string', function() {
|
||||
var client = new Client("postgres://brian:pass@host1:333/databasename")
|
||||
assert.equal(client.user, 'brian');
|
||||
|
@ -1,27 +0,0 @@
|
||||
require(__dirname + '/test-helper');
|
||||
|
||||
/*
|
||||
* Perhaps duplicate of test named 'initializing from a config string' in
|
||||
* configuration-tests.js
|
||||
*/
|
||||
|
||||
test("using connection string in client constructor", function() {
|
||||
var client = new Client("postgres://brian:pw@boom:381/lala");
|
||||
|
||||
test("parses user", function() {
|
||||
assert.equal(client.user,'brian');
|
||||
});
|
||||
test("parses password", function() {
|
||||
assert.equal(client.password, 'pw');
|
||||
});
|
||||
test("parses host", function() {
|
||||
assert.equal(client.host, 'boom');
|
||||
});
|
||||
test('parses port', function() {
|
||||
assert.equal(client.port, 381)
|
||||
});
|
||||
test('parses database', function() {
|
||||
assert.equal(client.database, 'lala')
|
||||
});
|
||||
});
|
||||
|
@ -7,12 +7,14 @@ test('emits end when not in query', function() {
|
||||
stream.write = function() {
|
||||
//NOOP
|
||||
}
|
||||
|
||||
var client = new Client({connection: new Connection({stream: stream})});
|
||||
client.connect(assert.calls(function() {
|
||||
client.query('SELECT NOW()', assert.calls(function(err, result) {
|
||||
assert(err);
|
||||
}));
|
||||
}));
|
||||
assert.emits(client, 'error');
|
||||
assert.emits(client, 'end');
|
||||
client.connection.emit('connect');
|
||||
process.nextTick(function() {
|
||||
|
@ -126,7 +126,7 @@ test('libpq connection string building', function() {
|
||||
checkForPart(parts, "user='brian'");
|
||||
checkForPart(parts, "password='xyz'");
|
||||
checkForPart(parts, "port='888'");
|
||||
checkForPart(parts, "hostaddr=127.0.0.1");
|
||||
checkForPart(parts, "hostaddr='127.0.0.1'");
|
||||
checkForPart(parts, "dbname='bam'");
|
||||
}));
|
||||
});
|
||||
@ -143,7 +143,7 @@ test('libpq connection string building', function() {
|
||||
assert.isNull(err);
|
||||
var parts = constring.split(" ");
|
||||
checkForPart(parts, "user='brian'");
|
||||
checkForPart(parts, "hostaddr=127.0.0.1");
|
||||
checkForPart(parts, "hostaddr='127.0.0.1'");
|
||||
}));
|
||||
});
|
||||
|
||||
@ -173,7 +173,23 @@ test('libpq connection string building', function() {
|
||||
assert.isNull(err);
|
||||
var parts = constring.split(" ");
|
||||
checkForPart(parts, "user='brian'");
|
||||
checkForPart(parts, "host=/tmp/");
|
||||
checkForPart(parts, "host='/tmp/'");
|
||||
}));
|
||||
});
|
||||
|
||||
test('config contains quotes and backslashes', function() {
|
||||
var config = {
|
||||
user: 'not\\brian',
|
||||
password: 'bad\'chars',
|
||||
port: 5432,
|
||||
host: '/tmp/'
|
||||
};
|
||||
var subject = new ConnectionParameters(config);
|
||||
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
|
||||
assert.isNull(err);
|
||||
var parts = constring.split(" ");
|
||||
checkForPart(parts, "user='not\\\\brian'");
|
||||
checkForPart(parts, "password='bad\\'chars'");
|
||||
}));
|
||||
});
|
||||
|
||||
|
@ -347,6 +347,13 @@ test('Connection', function() {
|
||||
name: 'portalSuspended'
|
||||
});
|
||||
});
|
||||
|
||||
test('parses replication start message', function() {
|
||||
testForMessage(new Buffer([0x57, 0x00, 0x00, 0x00, 0x04]), {
|
||||
name: 'replicationStart',
|
||||
length: 4
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//since the data message on a stream can randomly divide the incomming
|
||||
@ -465,5 +472,4 @@ test('split buffer, multiple message parsing', function() {
|
||||
splitAndVerifyTwoMessages(1);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -126,6 +126,13 @@ test('prepareValue: date array prepared properly', function() {
|
||||
helper.resetTimezoneOffset();
|
||||
});
|
||||
|
||||
test('prepareValue: buffer array prepared properly', function() {
|
||||
var buffer1 = Buffer.from ? Buffer.from('dead', 'hex') : new Buffer('dead', 'hex');
|
||||
var buffer2 = Buffer.from ? Buffer.from('beef', 'hex') : new Buffer('beef', 'hex');
|
||||
var out = utils.prepareValue([buffer1, buffer2]);
|
||||
assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}');
|
||||
});
|
||||
|
||||
test('prepareValue: arbitrary objects prepared properly', function() {
|
||||
var out = utils.prepareValue({ x: 42 });
|
||||
assert.strictEqual(out, '{"x":42}');
|
||||
|
Loading…
Reference in New Issue
Block a user