Skip to content

Commit 1ddbe52

Browse files
author
andythiv
committed
merge with upstream
2 parents 86448e5 + 55abbaa commit 1ddbe52

22 files changed

+280
-160
lines changed

.travis.yml

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,27 @@ addons:
1414

1515
matrix:
1616
include:
17-
- node_js: "0.10"
18-
addons:
19-
postgresql: "9.1"
20-
- node_js: "0.10"
21-
addons:
22-
postgresql: "9.2"
23-
- node_js: "0.10"
24-
addons:
25-
postgresql: "9.3"
2617
- node_js: "0.10"
2718
addons:
2819
postgresql: "9.4"
2920
- node_js: "0.12"
3021
addons:
31-
postgresql: "9.1"
32-
- node_js: "0.12"
33-
addons:
34-
postgresql: "9.2"
35-
- node_js: "0.12"
22+
postgresql: "9.4"
23+
- node_js: "4"
3624
addons:
37-
postgresql: "9.3"
38-
- node_js: "0.12"
25+
postgresql: "9.4"
26+
- node_js: "5"
3927
addons:
4028
postgresql: "9.4"
41-
- node_js: "4.1"
29+
- node_js: "6"
4230
addons:
4331
postgresql: "9.1"
44-
- node_js: "4.1"
32+
- node_js: "6"
4533
addons:
4634
postgresql: "9.2"
47-
- node_js: "4.1"
35+
- node_js: "6"
4836
addons:
4937
postgresql: "9.3"
50-
- node_js: "4.1"
38+
- node_js: "6"
5139
addons:
5240
postgresql: "9.4"

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ For richer information consult the commit log on github with referenced pull req
44

55
We do not include break-fix version release in this file.
66

7+
### v4.5.0
8+
- Add option to parse JS date objects in query parameters as [UTC](https://github.com/brianc/node-postgres/pull/943)
9+
710
### v4.4.0
811
- Warn to `stderr` if a named query exceeds 63 characters which is the max lenght supported by postgres.
912

README.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ var conString = "postgres://username:password@localhost/database";
2424

2525
//this initializes a connection pool
2626
//it will keep idle connections open for a (configurable) 30 seconds
27-
//and set a limit of 20 (also configurable)
27+
//and set a limit of 10 (also configurable)
2828
pg.connect(conString, function(err, client, done) {
2929
if(err) {
3030
return console.error('error fetching client from pool', err);
3131
}
3232
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
3333
//call `done()` to release the client back to the pool
3434
done();
35-
35+
3636
if(err) {
3737
return console.error('error running query', err);
3838
}
@@ -124,32 +124,14 @@ Usually I'll pop the code into the repo as a test. Hopefully the test fails. T
124124

125125
If you need help or run into _any_ issues getting node-postgres to work on your system please report a bug or contact me directly. I am usually available via google-talk at my github account public email address.
126126

127-
I usually tweet about any important status updates or changes to node-postgres on twitter.
127+
I usually tweet about any important status updates or changes to node-postgres on twitter.
128128
Follow me [@briancarlson](https://twitter.com/briancarlson) to keep up to date.
129129

130130

131131
## Extras
132132

133-
node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture:
134-
135-
- [brianc/node-pg-native](https://github.com/brianc/node-pg-native) - Simple interface abstraction on top of [libpq](https://github.com/brianc/node-libpq)
136-
- [brianc/node-pg-query-stream](https://github.com/brianc/node-pg-query-stream) - Query results from node-postgres as a readable (object) stream
137-
- [brianc/node-pg-cursor](https://github.com/brianc/node-pg-cursor) - Query cursor extension for node-postgres
138-
- [brianc/node-pg-copy-streams](https://github.com/brianc/node-pg-copy-streams) - COPY FROM / COPY TO for node-postgres. Stream from one database to another, and stuff.
139-
- [brianc/node-postgres-pure](https://github.com/brianc/node-postgres-pure) - node-postgres without any of the C/C++ stuff
140-
- [brianc/node-pg-types](https://github.com/brianc/node-pg-types) - Type parsing for node-postgres
141-
- [Suor/pg-bricks](https://github.com/Suor/pg-bricks) - A higher level wrapper around node-postgres to handle connection settings, sql generation, transactions and ease data access.
142-
- [grncdr/node-any-db](https://github.com/grncdr/node-any-db) - Thin and less-opinionated database abstraction layer for node.
143-
- [brianc/node-sql](https://github.com/brianc/node-sql) - SQL generation for node.js
144-
- [hiddentao/squel](https://hiddentao.github.io/squel/) - SQL query string builder for Javascript
145-
- [CSNW/sql-bricks](https://github.com/CSNW/sql-bricks) - Transparent, Schemaless SQL Generation
146-
- [datalanche/node-pg-format](https://github.com/datalanche/node-pg-format) - Safely and easily create dynamic SQL queries with this Node implementation of [PostgreSQL format()](http://www.postgresql.org/docs/9.3/static/functions-string.html#FUNCTIONS-STRING-FORMAT).
147-
- [iceddev/pg-transact](https://github.com/iceddev/pg-transact) - A nicer API on node-postgres transactions
148-
- [sehrope/node-pg-db](https://github.com/sehrope/node-pg-db) - Simpler interface, named parameter support, transaction management and event hooks.
149-
- [vitaly-t/pg-promise](https://github.com/vitaly-t/pg-promise) - Use node-postgres via [Promises/A+](https://promisesaplus.com/).
150-
- [pg-then](https://github.com/coderhaoxin/pg-then) A tiny wrapper of `pg` for promise api.
151-
- [acarl/pg-restify](https://github.com/acarl/pg-restify) - Creates a generic REST API for a postgres database using restify.
152-
- [XeCycle/pg-template-tag](https://github.com/XeCycle/pg-template-tag) - Write queries with ES6 tagged template literals, a "poor man's query builder".
133+
node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture.
134+
Entire list can be found on [wiki](https://github.com/brianc/node-postgres/wiki/Extras)
153135

154136
## License
155137

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Client.prototype.connect = function(callback) {
8585
//password request handling
8686
con.on('authenticationMD5Password', checkPgPass(function(msg) {
8787
var inner = Client.md5(self.password + self.user);
88-
var outer = Client.md5(inner + msg.salt.toString('binary'));
88+
var outer = Client.md5(Buffer.concat([new Buffer(inner), msg.salt]));
8989
var md5password = "md5" + outer;
9090
con.password(md5password);
9191
}));

lib/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Connection.prototype.cancel = function(processID, secretKey) {
159159
.addInt16(5678)
160160
.addInt32(processID)
161161
.addInt32(secretKey)
162-
.addCString('').flush();
162+
.flush();
163163

164164
var length = bodyBuffer.length + 4;
165165

@@ -423,7 +423,7 @@ Connection.prototype.parseR = function(buffer, length) {
423423
return msg;
424424
}
425425
}
426-
throw new Error("Unknown authenticatinOk message type" + util.inspect(msg));
426+
throw new Error("Unknown authenticationOk message type" + util.inspect(msg));
427427
};
428428

429429
Connection.prototype.parseS = function(buffer, length) {

lib/defaults.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var defaults = module.exports = {
2-
// database host defaults to localhost
2+
// database host. defaults to localhost
33
host: 'localhost',
44

55
//database user's name
@@ -30,7 +30,7 @@ var defaults = module.exports = {
3030
//from the pool and destroyed
3131
poolIdleTimeout: 30000,
3232

33-
//frequeny to check for idle clients within the client pool
33+
//frequency to check for idle clients within the client pool
3434
reapIntervalMillis: 1000,
3535

3636
//pool log function / boolean
@@ -40,8 +40,10 @@ var defaults = module.exports = {
4040

4141
ssl: false,
4242

43-
application_name : undefined,
44-
fallback_application_name: undefined
43+
application_name: undefined,
44+
fallback_application_name: undefined,
45+
46+
parseInputDatesAsUTC: false
4547
};
4648

4749
//parse int8 so you can get your count values as actual numbers

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ var Connection = require('./connection');
88
var PG = function(clientConstructor) {
99
EventEmitter.call(this);
1010
this.defaults = defaults;
11-
this.Client = pool.Client = clientConstructor;
11+
this.Client = clientConstructor;
1212
this.Query = this.Client.Query;
13-
this.pools = pool;
13+
this.pools = pool(clientConstructor);
1414
this.Connection = Connection;
1515
this.types = require('pg-types');
1616
};

lib/native/result.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ NativeResult.prototype.addCommandComplete = function(pq) {
1919
});
2020
}
2121
};
22+
23+
NativeResult.prototype.addRow = function(row) {
24+
// This is empty to ensure pg code doesn't break when switching to pg-native
25+
// pg-native loads all rows into the final result object by default.
26+
// This is because libpg loads all rows into memory before passing the result
27+
// to pg-native.
28+
};

lib/pool.js

Lines changed: 83 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,96 @@ var EventEmitter = require('events').EventEmitter;
33
var defaults = require('./defaults');
44
var genericPool = require('generic-pool');
55

6-
var pools = {
7-
//dictionary of all key:pool pairs
8-
all: {},
9-
//reference to the client constructor - can override in tests or for require('pg').native
10-
Client: require('./client'),
11-
getOrCreate: function(clientConfig) {
12-
clientConfig = clientConfig || {};
13-
var name = JSON.stringify(clientConfig);
14-
var pool = pools.all[name];
15-
if(pool) {
16-
return pool;
17-
}
18-
pool = genericPool.Pool({
19-
name: name,
20-
max: clientConfig.poolSize || defaults.poolSize,
21-
idleTimeoutMillis: clientConfig.poolIdleTimeout || defaults.poolIdleTimeout,
22-
reapIntervalMillis: clientConfig.reapIntervalMillis || defaults.reapIntervalMillis,
23-
log: clientConfig.poolLog || defaults.poolLog,
24-
create: function(cb) {
25-
var client = new pools.Client(clientConfig);
26-
// Ignore errors on pooled clients until they are connected.
27-
client.on('error', Function.prototype);
28-
client.connect(function(err) {
29-
if(err) return cb(err, null);
306

31-
// Remove the noop error handler after a connection has been established.
32-
client.removeListener('error', Function.prototype);
7+
module.exports = function(Client) {
8+
var pools = {
9+
Client: Client,
10+
//dictionary of all key:pool pairs
11+
all: {},
12+
//reference to the client constructor - can override in tests or for require('pg').native
13+
getOrCreate: function(clientConfig) {
14+
clientConfig = clientConfig || {};
15+
var name = JSON.stringify(clientConfig);
16+
var pool = pools.all[name];
17+
if(pool) {
18+
return pool;
19+
}
20+
pool = genericPool.Pool({
21+
name: name,
22+
max: clientConfig.poolSize || defaults.poolSize,
23+
idleTimeoutMillis: clientConfig.poolIdleTimeout || defaults.poolIdleTimeout,
24+
reapIntervalMillis: clientConfig.reapIntervalMillis || defaults.reapIntervalMillis,
25+
log: clientConfig.poolLog || defaults.poolLog,
26+
create: function(cb) {
27+
var client = new pools.Client(clientConfig);
28+
// Ignore errors on pooled clients until they are connected.
29+
client.on('error', Function.prototype);
30+
client.connect(function(err) {
31+
if(err) return cb(err, null);
3332

34-
//handle connected client background errors by emitting event
35-
//via the pg object and then removing errored client from the pool
36-
client.on('error', function(e) {
37-
pool.emit('error', e, client);
33+
// Remove the noop error handler after a connection has been established.
34+
client.removeListener('error', Function.prototype);
3835

39-
// If the client is already being destroyed, the error
40-
// occurred during stream ending. Do not attempt to destroy
41-
// the client again.
42-
if (!client._destroying) {
43-
pool.destroy(client);
44-
}
45-
});
36+
//handle connected client background errors by emitting event
37+
//via the pg object and then removing errored client from the pool
38+
client.on('error', function(e) {
39+
pool.emit('error', e, client);
4640

47-
// Remove connection from pool on disconnect
48-
client.on('end', function(e) {
49-
// Do not enter infinite loop between pool.destroy
50-
// and client 'end' event...
51-
if ( ! client._destroying ) {
41+
// If the client is already being destroyed, the error
42+
// occurred during stream ending. Do not attempt to destroy
43+
// the client again.
44+
if (!client._destroying) {
45+
pool.destroy(client);
46+
}
47+
});
48+
49+
// Remove connection from pool on disconnect
50+
client.on('end', function(e) {
51+
// Do not enter infinite loop between pool.destroy
52+
// and client 'end' event...
53+
if ( ! client._destroying ) {
54+
pool.destroy(client);
55+
}
56+
});
57+
client.poolCount = 0;
58+
return cb(null, client);
59+
});
60+
},
61+
destroy: function(client) {
62+
client._destroying = true;
63+
client.poolCount = undefined;
64+
client.end();
65+
}
66+
});
67+
pools.all[name] = pool;
68+
//mixin EventEmitter to pool
69+
EventEmitter.call(pool);
70+
for(var key in EventEmitter.prototype) {
71+
if(EventEmitter.prototype.hasOwnProperty(key)) {
72+
pool[key] = EventEmitter.prototype[key];
73+
}
74+
}
75+
//monkey-patch with connect method
76+
pool.connect = function(cb) {
77+
var domain = process.domain;
78+
pool.acquire(function(err, client) {
79+
if(domain) {
80+
cb = domain.bind(cb);
81+
}
82+
if(err) return cb(err, null, function() {/*NOOP*/});
83+
client.poolCount++;
84+
cb(null, client, function(err) {
85+
if(err) {
5286
pool.destroy(client);
87+
} else {
88+
pool.release(client);
5389
}
5490
});
55-
client.poolCount = 0;
56-
return cb(null, client);
5791
});
58-
},
59-
destroy: function(client) {
60-
client._destroying = true;
61-
client.poolCount = undefined;
62-
client.end();
63-
}
64-
});
65-
pools.all[name] = pool;
66-
//mixin EventEmitter to pool
67-
EventEmitter.call(pool);
68-
for(var key in EventEmitter.prototype) {
69-
if(EventEmitter.prototype.hasOwnProperty(key)) {
70-
pool[key] = EventEmitter.prototype[key];
71-
}
92+
};
93+
return pool;
7294
}
73-
//monkey-patch with connect method
74-
pool.connect = function(cb) {
75-
var domain = process.domain;
76-
pool.acquire(function(err, client) {
77-
if(domain) {
78-
cb = domain.bind(cb);
79-
}
80-
if(err) return cb(err, null, function() {/*NOOP*/});
81-
client.poolCount++;
82-
cb(null, client, function(err) {
83-
if(err) {
84-
pool.destroy(client);
85-
} else {
86-
pool.release(client);
87-
}
88-
});
89-
});
90-
};
91-
return pool;
92-
}
93-
};
95+
};
9496

95-
module.exports = pools;
97+
return pools;
98+
};

lib/query.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ Query.prototype.prepare = function(connection) {
146146
}, true);
147147
}
148148

149-
//TODO is there some better way to prepare values for the database?
150149
if(self.values) {
151-
for(var i = 0, len = self.values.length; i < len; i++) {
152-
self.values[i] = utils.prepareValue(self.values[i]);
153-
}
150+
self.values = self.values.map(utils.prepareValue);
154151
}
155152

156153
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

0 commit comments

Comments
 (0)