2010-10-25 13:14:48 +08:00
|
|
|
#node-postgres
|
|
|
|
|
2010-10-29 11:27:50 +08:00
|
|
|
Non-blocking (async) JavaScript PostgreSQL client for node.js written
|
|
|
|
fully TDD and with lots of love.
|
2010-10-25 13:14:48 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
## alpha version
|
2010-10-16 07:21:24 +08:00
|
|
|
|
2010-11-03 14:57:41 +08:00
|
|
|
### Installation
|
|
|
|
|
|
|
|
npm install pg
|
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
### Whirlwind tour
|
2010-10-23 07:26:18 +08:00
|
|
|
|
2010-11-04 03:02:55 +08:00
|
|
|
var Client = require('pg').Client;
|
2010-10-29 08:46:50 +08:00
|
|
|
var client = new Client({
|
|
|
|
user: 'brianc',
|
|
|
|
database: 'test',
|
|
|
|
password: 'boom' //plaintext or md5 supported
|
2010-10-25 13:14:48 +08:00
|
|
|
});
|
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
client.connect();
|
2010-11-04 13:24:51 +08:00
|
|
|
client.on('drain', client.end.bind(client));
|
2010-11-04 13:41:44 +08:00
|
|
|
|
|
|
|
//queries are queued on a per-client basis and executed one at a time
|
|
|
|
client.query("create temp table user(heart varchar(10), birthday timestamptz);");
|
|
|
|
|
|
|
|
//parameters are always parsed & prepared inside of PostgreSQL server
|
|
|
|
//using unnamed prepared statement (no sql injection! yay!)
|
|
|
|
client.query({
|
|
|
|
text: 'INSERT INTO user(heart, birthday) VALUES ($1, $2)',
|
|
|
|
values: ['big', new Date(2031, 03, 03)]
|
|
|
|
});
|
|
|
|
client.query({
|
|
|
|
text: 'INSERT INTO user(heart, birthday) VALUES ($1, $2)',
|
|
|
|
values: ['filled with kisses', new Date(2010, 01, 01)]
|
|
|
|
});
|
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
var simpleQuery = client.query("select * from user where heart = 'big'");
|
2010-11-04 13:41:44 +08:00
|
|
|
simpleQuery.on('row', function(row){
|
|
|
|
console.log(row.heart); //big
|
|
|
|
console.log(row.birthday.getYear()); //2031
|
|
|
|
});
|
2010-10-25 13:14:48 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
var preparedStatement = client.query({
|
|
|
|
name: 'user by heart type',
|
|
|
|
text: 'select * from user where heart = $1',
|
|
|
|
values: ['big']
|
2010-10-25 13:14:48 +08:00
|
|
|
});
|
2010-11-04 13:41:44 +08:00
|
|
|
|
|
|
|
preparedStatement.on('row', function(row){
|
|
|
|
console.log(row.heart); //big
|
|
|
|
console.log(row.birthday.getYear()); //2031
|
|
|
|
});
|
2010-10-25 13:14:48 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
var cachedPreparedStatement = client.query({
|
|
|
|
name: 'user by heart type',
|
2010-11-04 13:41:44 +08:00
|
|
|
//you can omit the text the 2nd time if using a named query
|
2010-10-29 08:46:50 +08:00
|
|
|
values: ['filled with kisses']
|
2010-10-25 13:14:48 +08:00
|
|
|
});
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-11-04 13:41:44 +08:00
|
|
|
cachedPreparedStatement.on('row', function(row){
|
|
|
|
console.log(row.heart); //filled with kisses
|
|
|
|
console.log(row.birthday.getYear()); //2010
|
|
|
|
});
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
### Philosophy
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
* well tested
|
|
|
|
* no monkey patching
|
|
|
|
* no dependencies (well...besides PostgreSQL)
|
2010-11-05 12:21:05 +08:00
|
|
|
* [extreme documentation](http://github.com/brianc/node-postgres/wiki)
|
2010-11-05 12:20:16 +08:00
|
|
|
|
|
|
|
### features
|
|
|
|
|
|
|
|
- prepared statement support
|
|
|
|
- parameters
|
|
|
|
- query caching
|
|
|
|
- type coercion
|
|
|
|
- date <-> timestamptz
|
|
|
|
- integer <-> integer, smallint, bigint
|
|
|
|
- float <-> double, numeric
|
|
|
|
- boolean <-> boolean
|
|
|
|
- notification message support
|
|
|
|
- tested like a Toyota
|
|
|
|
~1000 assertions executed on
|
|
|
|
- ubuntu
|
|
|
|
- node v0.2.2, v0.2.3, v0.2.4, v0.3.0
|
|
|
|
- postgres 8.4.4
|
|
|
|
- osx
|
|
|
|
- node v0.2.2, v0.2.3, v0.2.4, v0.3.0
|
|
|
|
- postgres v8.4.4, v9.0.1
|
|
|
|
|
|
|
|
### party time
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-11-03 14:57:41 +08:00
|
|
|
clone the repo:
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
git clone git://github.com/brianc/node-postgres
|
|
|
|
cd node-postgres
|
|
|
|
node test/run.js
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
And just like magic, you're ready to contribute! <3
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
## More info please
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 11:27:50 +08:00
|
|
|
### Documentalicious
|
|
|
|
|
2010-11-01 07:02:12 +08:00
|
|
|
__PLEASE__ check out the [WIKI](node-postgres/wiki). __MUCH__ more information there.
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
p.s. want your own offline version of the wiki?
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 08:46:50 +08:00
|
|
|
git clone git://github.com/brianc/node-postgres.wiki.git
|
2010-10-26 14:58:42 +08:00
|
|
|
|
2010-10-29 11:27:50 +08:00
|
|
|
_github is magic_
|
2010-10-26 08:41:39 +08:00
|
|
|
|
2010-11-05 10:54:11 +08:00
|
|
|
### Working?
|
|
|
|
|
2010-11-05 10:55:03 +08:00
|
|
|
[this page](http://www.explodemy.com) is running the worlds worst (but fully functional) PostgreSQL backed, Node.js powered website.
|
2010-11-05 10:54:11 +08:00
|
|
|
|
2010-11-01 07:02:12 +08:00
|
|
|
### Why did you write this?
|
2010-10-26 08:41:39 +08:00
|
|
|
|
|
|
|
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
|
2010-10-29 08:46:50 +08:00
|
|
|
poked around for a while. I was excited. I still am!
|
|
|
|
|
|
|
|
Let's say for arguments sake you have to run a query from node.js on PostgreSQL before the
|
2010-10-26 08:41:39 +08:00
|
|
|
last petal falls off the rose and you are stuck as a beast forever?
|
2010-10-29 08:46:50 +08:00
|
|
|
You can't use NoSQL because your boss said he'd pour a cup of
|
|
|
|
Hoegarten into your laptop fan vent and you _hate_ that beer?
|
2010-10-26 08:41:39 +08:00
|
|
|
What if your entire production site depends on it? Well, fret no
|
|
|
|
more. And let [GastonDB](http://www.snipetts.com/ashley/mymusicals/disney/beauty-an-beast/images/gaston.gif) be vanquished.
|
|
|
|
|
|
|
|
I drew major inspiration from
|
|
|
|
[postgres-js](http://github.com/creationix/postgres-js). I didn't
|
|
|
|
just fork and contribute because it has
|
2010-10-29 08:46:50 +08:00
|
|
|
_0_ tests included with it, adds a bunch of methods to the Buffer()
|
|
|
|
object, and doesn't seem to be maintained. Still...was a lovely way
|
|
|
|
to learn & excellent reference material.
|
2010-10-26 08:41:39 +08:00
|
|
|
|
|
|
|
I also drew some major inspirrado from
|
|
|
|
[node-mysql](http://github.com/felixge/node-mysql) and liked what I
|
|
|
|
saw there. I'm thinking I might be stealing some of the ideas there
|
|
|
|
for the __Client__ api.
|
|
|
|
|
|
|
|
So...__boom__. I set out to write my own. I'm not working on anything
|
|
|
|
else in my spare time other than this. It's a labor of love. I'd
|
|
|
|
love for you to love it as well. Contribute. Fork, patch, and send
|
|
|
|
me a pull request. All I ask is everything you add you have complete
|
|
|
|
and possibly obsessive test coverage to back up.
|
|
|
|
|
2010-10-26 15:03:41 +08:00
|
|
|
## License
|
|
|
|
|
2010-10-29 11:29:47 +08:00
|
|
|
node-postgres is licensed under the [MIT license](node-postgres/blob/master/License).
|