Skip to content

Commit 207b7db

Browse files
committed
Merge remote branch 'upstream/master'
Conflicts: lib/query.js test/unit/client/typed-query-results-tests.js
2 parents 727de59 + d33ebd8 commit 207b7db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2400
-610
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/.emacs-project
22
*.swp
33
*.log
4+
.lock-wscript
5+
build/
6+
/todo.org

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.lock-wscript
2+
build/

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ params := -u $(user) --password $(password) -p $(port) -d $(database) -h $(host)
1111

1212
node-command := xargs -n 1 -I file node file $(params)
1313

14-
.PHONY : test test-connection test-integration bench
14+
.PHONY : test test-connection test-integration bench test-native build/default/binding.node
1515
test: test-unit
1616

17-
test-all: test-unit test-integration
17+
test-all: test-unit test-integration test-native
1818

1919
bench:
2020
@find benchmark -name "*-bench.js" | $(node-command)
2121

22+
build/default/binding.node:
23+
@node-waf configure build
24+
2225
test-unit:
2326
@find test/unit -name "*-tests.js" | $(node-command)
2427

2528
test-connection:
2629
@node script/test-connection.js $(params)
2730

31+
test-native: build/default/binding.node
32+
@echo "***Testing native bindings***"
33+
@find test/native -name "*-tests.js" | $(node-command)
34+
@find test/integration -name "*-tests.js" | $(node-command) --native true
35+
2836
test-integration: test-connection
37+
@echo "***Testing Pure Javascript***"
2938
@find test/integration -name "*-tests.js" | $(node-command)

README.md

Lines changed: 104 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,102 @@
11
#node-postgres
22

3-
Non-blocking (async) pure JavaScript PostgreSQL client for node.js written
4-
with love and TDD.
3+
Non-blocking PostgreSQL client for node.js. Pure JavaScript and native libpq bindings.
54

65
## Installation
76

87
npm install pg
98

10-
## Example
11-
12-
var pg = require('pg');
13-
var connectionString = "pg://user:password@host:port/database";
14-
pg.connect(connectionString, function(err, client) {
15-
if(err) {
16-
//handle connection error
17-
}
18-
else {
19-
//queries are queued and executed in order
20-
client.query("CREATE TEMP TABLE user(name varchar(50), birthday timestamptz)");
21-
client.query("INSERT INTO user(name, birthday) VALUES('brianc', '1982-01-01T10:21:11')");
22-
23-
//parameterized queries with transparent type coercion
24-
client.query("INSERT INTO user(name, birthday) VALUES($1, $2)", ['santa', new Date()]);
25-
26-
//nested queries with callbacks
27-
client.query("SELECT * FROM user ORDER BY name", function(err, result) {
28-
if(err) {
29-
//handle query error
30-
}
31-
else {
32-
client.query("SELECT birthday FROM user WHERE name = $1", [result.rows[0].name], function(err, result) {
33-
//typed parameters and results
34-
assert.ok(result.rows[0].birthday.getYear() === 1982)
35-
})
36-
}
37-
})
38-
}
39-
}
40-
41-
## Philosophy
42-
43-
* well tested
44-
* no monkey patching
45-
* no dependencies (...besides PostgreSQL)
46-
* [in-depth documentation](http://github.com/brianc/node-postgres/wiki) (work in progress)
47-
48-
## features
49-
50-
- prepared statement support
51-
- parameters
52-
- query caching
53-
- type coercion
54-
- date <-> timestamptz
55-
- integer <-> integer, smallint, bigint
56-
- float <-> double, numeric
57-
- boolean <-> boolean
58-
- notification message support
59-
- connection pooling
60-
- mucho testing
61-
~250 tests executed on
62-
- ubuntu
63-
- node v0.2.2, v0.2.3, v0.2.4, v0.2.5, v0.2.6, v0.3.0, v0.3.1, v0.3.2, v0.3.3, v0.3.4, v0.3.5, v0.3.6, v0.3.7, v0.3.8
64-
- postgres 8.4.4
65-
- osx
66-
- node v0.2.2, v0.2.3, v0.2.4, v0.2.5, v0.2.6, v0.3.0, v0.3.1, v0.3.2, v0.3.3, v0.3.4, v0.3.5, v0.3.6, v0.3.7, v0.3.8
67-
- postgres v8.4.4, v9.0.1 installed both locally and on networked Windows 7
68-
69-
## Contributing
70-
71-
clone the repo:
72-
73-
git clone git://github.com/brianc/node-postgres
74-
cd node-postgres
75-
make test
76-
77-
And just like magic, you're ready to contribute! <3
9+
## Examples
10+
11+
All examples will work with the pure javascript bindings (currently default) or the libpq native (c/c++) bindings (currently in beta)
12+
13+
To use native libpq bindings replace `require('pg')` with `require('pg').native`.
14+
15+
The two share the same interface so __no other code changes should be required__. If you find yourself having to change code other than the require statement when switching from `pg` to `pg.native`, please report an issue.
16+
17+
node-postgres supports both an 'event emitter' style API and a 'callback' style. The callback style is more concise and generally preferred, but the evented API can come in handy. They can be mixed and matched. The only events which do __not__ fire when callbacks are supplied are the `error` events, as they are to be handled by the callback function.
18+
19+
### Simple, using built-in client pool
20+
21+
var pg = require('pg');
22+
//or native libpq bindings
23+
//var pg = require('pg').native
24+
25+
var conString = "tcp://postgres:1234@localhost/postgres";
26+
27+
//error handling omitted
28+
pg.connect(conString, function(err, client) {
29+
client.query("SELECT NOW() as when", function(err, result) {
30+
console.log("Row count: %d",result.rows.length); // 1
31+
console.log("Current year: %d", result.rows[0].when.getYear());
32+
});
33+
});
34+
35+
### Evented api
36+
37+
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
38+
var conString = "tcp://postgres:1234@localhost/postgres";
39+
40+
var client = new pg.Client(conString);
41+
client.connect();
42+
43+
//queries are queued and executed one after another once the connection becomes available
44+
client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");
45+
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);
46+
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);
47+
48+
//queries can be executed either via text/parameter values passed as individual arguments
49+
//or by passing an options object containing text, (optional) parameter values, and (optional) query name
50+
client.query({
51+
name: 'insert beatle',
52+
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
53+
values: ['George', 70, new Date(1946, 02, 14)]
54+
});
55+
56+
//subsequent queries with the same name will be executed without re-parsing the query plan by postgres
57+
client.query({
58+
name: 'insert beatle',
59+
values: ['Paul', 63, new Date(1945, 04, 03)]
60+
});
61+
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
62+
63+
//can stream row results back 1 at a time
64+
query.on('row', function(row) {
65+
console.log(row);
66+
console.log("Beatle name: %s", row.name); //Beatle name: John
67+
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
68+
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints
69+
});
70+
71+
//fired after last row is emitted
72+
query.on('end', function() {
73+
client.end();
74+
});
75+
76+
### Info
77+
78+
* a pure javascript client and native libpq bindings with _the same api_
79+
* _heavily_ tested
80+
* the same suite of 200+ integration tests passed by both javascript & libpq bindings
81+
* benchmark & long-running memory leak tests performed before releases
82+
* tested with with
83+
* postgres 8.x, 9.x
84+
* Linux, OS X
85+
* node 2.x & 4.x
86+
* row-by-row result streaming
87+
* optional, built-in connection pooling
88+
* responsive project maintainer
89+
* supported PostgreSQL features
90+
* parameterized queries
91+
* named statements with query plan caching
92+
* async notifications
93+
* extensible js<->postgresql data-type coercion
94+
* query queue
95+
* active development
96+
* fast
97+
* No dependencies (other than PostgreSQL)
98+
* No monkey patching
99+
* Tried to mirror the node-mysql api as much as possible for future multi-database-supported ORM implementation ease
78100

79101
### Contributors
80102

@@ -85,36 +107,26 @@ Many thanks to the following:
85107
* [pshc](https://github.com/pshc)
86108
* [pjornblomqvist](https://github.com/bjornblomqvist)
87109
* [JulianBirch](https://github.com/JulianBirch)
110+
* [ef4](https://github.com/ef4)
111+
* [napa3um](https://github.com/napa3um)
88112

89-
## More info please
113+
## Documentation
90114

91-
### [Documentation](node-postgres/wiki)
115+
Still a work in progress, I am trying to flesh out the wiki...
92116

93-
### __PLEASE__ check out the WIKI
94-
95-
## Help
96-
97-
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.
98-
99-
### Working?
100-
101-
[this page](http://www.explodemy.com) is running the worlds worst (but fully functional) PostgreSQL backed, Node.js powered website.
117+
### [Documentation](https://github.com/brianc/node-postgres/wiki)
102118

103-
### Why did you write this?
104-
105-
As soon as I saw node.js for the first time I knew I had found something lovely and simple and _just what I always wanted!_. So...I poked around for a while. I was excited. I still am!
106-
107-
I drew major inspiration from [postgres-js](http://github.com/creationix/postgres-js).
119+
### __PLEASE__ check out the WIKI
108120

109-
I also drew some major inspirrado from
110-
[node-mysql](http://github.com/felixge/node-mysql) and liked what I
111-
saw there.
121+
## Production Use
122+
* [bayt.com](http://bayt.com)
112123

113-
### Plans for the future?
124+
_if you use node-postgres in production and would like your site listed here, fork & add it_
114125

115-
- transparent prepared statement caching
116-
- more testings of error scenarios
126+
## Help
117127

128+
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.
129+
118130
## License
119131

120132
Copyright (c) 2010 Brian Carlson (brian.m.carlson@gmail.com)

benchmark/js-versus-native-bench.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var pg = require(__dirname + '/../lib')
2+
var pgNative = require(__dirname + '/../lib/native');
3+
var bencher = require('bencher');
4+
var helper = require(__dirname + '/../test/test-helper')
5+
var conString = helper.connectionString()
6+
7+
var round = function(num) {
8+
return Math.round((num*1000))/1000
9+
}
10+
11+
var doBenchmark = function() {
12+
var bench = bencher({
13+
name: 'js/native compare',
14+
repeat: 1000,
15+
actions: [{
16+
name: 'javascript client - simple query',
17+
run: function(next) {
18+
var query = client.query('SELECT name, age FROM person WHERE age > 10');
19+
query.on('end', function() {
20+
next();
21+
});
22+
}
23+
},{
24+
name: 'native client - simple query',
25+
run: function(next) {
26+
var query = nativeClient.query('SELECT name FROM person WHERE age > $1', [10]);
27+
query.on('end', function() {
28+
next();
29+
});
30+
}
31+
}, {
32+
name: 'javascript client - parameterized query',
33+
run: function(next) {
34+
var query = client.query('SELECT name, age FROM person WHERE age > $1', [10]);
35+
query.on('end', function() {
36+
next();
37+
});
38+
}
39+
},{
40+
name: 'native client - parameterized query',
41+
run: function(next) {
42+
var query = nativeClient.query('SELECT name, age FROM person WHERE age > $1', [10]);
43+
query.on('end', function() {
44+
next();
45+
});
46+
}
47+
}]
48+
});
49+
bench(function(result) {
50+
console.log();
51+
console.log("%s (%d repeats):", result.name, result.repeat)
52+
result.actions.forEach(function(action) {
53+
console.log(" %s: \n average: %d ms\n total: %d ms", action.name, round(action.meanTime), round(action.totalTime));
54+
})
55+
client.end();
56+
nativeClient.end();
57+
})
58+
}
59+
60+
var client = new pg.Client(conString);
61+
var nativeClient = new pgNative.Client(conString);
62+
client.connect();
63+
client.on('connect', function() {
64+
nativeClient.connect();
65+
nativeClient.on('connect', function() {
66+
doBenchmark();
67+
});
68+
});

0 commit comments

Comments
 (0)