860 likes | 1k Views
A COMMAND PROMPT TALK. Node.js and postgres. An Introductory Glance With Live Code, Later. 2010 AURYNN SHAW, COMMAND PROMPT INC. Hi!. What is Node.js. Fast Pure event-driven server-side javascript programming environment. Ew, javascript. Built on Google’s V8
E N D
Node.js and postgres • An Introductory Glance • With Live Code, Later 2010 AURYNN SHAW, COMMAND PROMPT INC.
What is Node.js • Fast • Pure event-driven • server-side • javascript programming environment
Ew, javascript • Built on Google’s V8 • Runs on modern unix-likes (Linux, OSX, Freebsd, Cygwin) • Speaks network natively, and easily (Hello world is in http!) • Ends up Similar to Python, Perl, Ruby, ETc.
Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block
Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block • Keeps the slow tasks from blocking • Keeps the quick tasks quick
blocking? # Standard synchronous processing fh = open(“somefile”,”r”) contents = fh.read() # what if somefile is huge, 1GB+ ? print contents
Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block • Keeps the slow tasks from blocking • Keeps the quick tasks quick
Non-blocking! fs.open(“somefile”, function (fd) { // When the file descriptor gets returned. var buffer = null; fs.read(fd, buffer, length, position, function (e, bytes) { // bytes read are here. // buffer will be not-null. }); });
Some caveats • node is entirely single-threaded • Event-driven programming is *really* different
A maze of callbacks fs.open(“/tmp/hello”, function (fd) { var buffer = null; fs.read(fd, buffer, length, position, function (e, bytes) { // bytes read are here. // buffer will be not-null. }); });
Some caveats • node is entirely single-threaded • Event-driven programming is *really* different • Javascript’s prototype model is also really different • The syntax has warts
Build a bridge out of her? /* This allows you to kind of inherit properly. */ var o = function () {}; var p = function () { /* Because ‘this’ changes */ var self = this; o.call(this); }; p.prototype = o; p.prototype.constructor = p;
Why is it important? • Javascript owns the frontend
Why is it important? • Javascript owns the frontend • More jquery and javascript programmers every day
Why is it important? • Javascript owns the frontend • More jquery and javascript programmers every day • Javascript is fast
When I got here... • postgres.js worked.
When I got here... • postgres.js worked. • Used the simple query protocol
When I got here... • postgres.js worked. • Used the simple query protocol • Used SQL escape to prevent injection attacks.
whyyyy SOURCE: http://www.flickr.com/photos/striatic/2192192956/
to prepare a query • postgres.js Didn’t use libpq.
to prepare a query • postgres.js Didn’t use libpq. • Speaks pure protocol
to prepare a query • postgres.js Didn’t use libpq. • Speaks pure protocol • ...So I had to learn the protocol.
Wax on, wax off MONTAGE!
Complexity Abounds • Simple protocol sends one message to pg • Easy to queue, internally.
REALLY SIMPLE MESSAGE QUEUE QUERY QUERY QUERY QUERY QUERY QUERY RESULTS
Really, though • Simple protocol sends one message to PG • Easy to queue, internally. • Prepared statements require at least 5 • Parse, describe, execute, bind, flush
THIS TYPE OF QUEUE DOESN’T WORK SYNC EXECUTE BIND DESCRIBE PARSE QUERY QUERY
And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response • But it does give responses...
And while all this is happening • You cannot block the main thread
And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response
And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response • But it does give responses...
KIND OF LIKE THIS SIMPLE PREPARED GROUP SIMPLE SIMPLE PREPARED GROUP SIMPLE SIMPLE