Mammoth
  • |
  • Contact
  • |
CMD | Command Prompt, Inc. - PostgreSQL Solutions, Support & Hosting
  • |
  • |
  • |
  • |
  • |
Recent Updates to postgres.js
Posted Friday Jan 14th, 2011 03:02pm
by Aurynn Shaw
| Permalink

Follow cmdpromptinc on Twitter


We've been a bit quiet on the postgres.js front lately, but there's a couple of exciting new announcements that we'd like to go over with Postgres.js Firstly, we've plugged in LISTEN/NOTIFY support. This is huge, as it allows you to register asynchronous callbacks based on events from Postgres, outside of any transactional context. Additionally, as of PG 9.0, NOTIFY messages from the Postgres server can contain an arbitrary text payload. Combined with JSON parsing in Node, you can easily pass formatted objects from Postgres to your application code, without an intermediate query stage. Here's an example from our test case on how to use it:
var pg = require("postgres");

var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
var db2 = new pg.connect("pgsql://test:12345@localhost:5432/template1");

// Request notification for "test" messages

db.notify("test", function (err, payload) {
    // I got a payload!
    console.log(payload);
});

// On connection, run the notification.

db2.on("connection", function () {
    db2.query("NOTIFY test 'myPayload'", function (err,rs) {}); // Null
    db2.close();
});
db.close();

Which, on 9.0, will have the callback called with "payload." Pre 9.0, you'd get an error on the NOTIFY command, and a blank string for the payload contents.

Empty resultsets

Next up, we fixed the bug whereby an empty resultset from a SELECT statement (or really any statement) would throw an error. Now, if you get an empty set, your callback will receive a zero-length array, just like you'd expect:
var pg = require("postgres");

var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
db.query("SELECT * FROM empty_table;", function (err, rs) {
    console.log(rs.length); // will be 0
});
db.close();

REPL support, and the "connection" event

Finally, the last big thing is we cleared up is the driver bug where, after the initial connection, new query events weren't getting executed, if the internal query buffer was ever drained. This would cause the driver to appear to hang, and was pretty much a major, show-stopping bug. Fortunately, this issue has now been fixed - Draining the event buffer will now have newly added items re-start the drain, and all queries buffered afterwards will be run on the wire, as expected. This should resolve any lingering effects or bugs with queries making it to PG. Additionally, to take advantage of and test this, you can listen for the "connection" event from the driver:
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
db.on("connection", function () {
    db.query("SELECT 1::int", function (err, rs, tx) {
        // err should be null
        console.log(eq(err, null));
        // Test if the returned value is what we think it is.
        console.log(eq(rs[0]['int4'], 1));
    });
    db.close();
});
With these latest fixes, we've inched forward to Release Candidate 2 - the driver should be just about stable enough for production use. As always, you can get it at: https://github.com/commandprompt/postgres-js and report bugs at https://public.commandprompt.com/projects/postgresjs

Categories: OpenSource, PostgreSQL

blog comments powered by Disqus

Copyright © 2000-2014 Command Prompt, Inc. All Rights Reserved. All trademarks property of their respective owners.