Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js fibers with pg/postgres

I've been trying to figure out how to use node-fibers to make my database code less messy in node.js, but I can't get it to work. I boiled the code down to this as a minimum test case:

var Future = require('fibers/future');
var pg=require('pg');

var connstr = "pg://not_the_real_user:or_password@localhost/db";
var pconnect = Future.wrap(pg.connect);

Fiber(function() {
    var client = pconnect(connstr).wait();
    console.log("called function");
}).run();

If I leave it as is, I get the following error:

pgfuture.js:10
}).run();
   ^
TypeError: undefined is not a function
    at Object.PG.connect.pools.(anonymous function).genericPool.Pool.create (/home/erik/code/treehouse-node/node_modules/pg/lib/index.js:49:20)
    at dispense (/home/erik/code/treehouse-node/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:223:17)
    at Object.exports.Pool.me.acquire (/home/erik/code/treehouse-node/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:267:5)
    at PG.connect (/home/erik/code/treehouse-node/node_modules/pg/lib/index.js:75:15)
    at Future.wrap (/home/erik/code/treehouse-node/node_modules/fibers/future.js:30:6)
    at /home/erik/code/treehouse-node/pgfuture.js:8:18

However, if I comment out the line that calls pconnect, I get the "called function" message on the console and no errors. The example on the github page has an almost identical structure, and it does work correctly on my system, but I'm stumped as to what I'm doing wrong here.

Edit: Additional details

I've managed to get the code to run after a fashion in two different ways that seem unrelated, but both have the same behavior. After the function finishes, node just hangs and I have to kill it with ctrl-c. Here are the two things I've done to get that result:

1) Wrap pg.connect in an anonymous function, and then wrap THAT with Future:

pconnect = Future.wrap(function(err,cb){pg.connect(err,cb);});

2) This one is a real mystery, but it appears to have the same result. Inside the fiber, I just directly call pg.connect before the call to pconnect, and everything seems to work out.

// add this line before call to pconnect
pg.connect(connstr, function(e,c){console.log("connected.");});
// and now the original call to pconnect
var client = pconnect(connstr).wait();

I can imagine a circumstance in which (1) would make sense if, for example, the pg.connect function has other optional arguments that are somehow interfering with the expected layout of the Future.wrap call. Another possibility is that an object is going out of scope and the "this" reference is undefined when the actual call to pconnect is made. I'm at a loss to understand why (2) has any effect though.

Edit: partial answer

Okay, so I answered at least part of the question. The thought I had about object scope turned out to be correct, and by using the bind() function I was able to eliminate the extra layer of callback wrapping:

var pconnect = Future.wrap(pg.connect.bind(pg));

It still hangs at the end of the execution for unknown reasons though.

like image 324
Erik Avatar asked Aug 16 '12 03:08

Erik


People also ask

Can I use node js with PostgreSQL?

Use node-postgres Module The node-postgres module is an npm package that allows you to connect to and interact with a PostgreSQL database. There are two options you can use to connect Node with PostgreSQL using the node-postgres module: a single client or a connection pool.

How do I use pg In node JS?

The node-postgres with async/await const pg = require('pg'); const R = require('ramda'); const cs = 'postgres://postgres:s$cret@localhost:5432/ydb'; async function fetchNow() { const client = new pg. Client(cs); try { await client. connect(); let result = await client. query('SELECT now()'); return R.

What is PG module?

pg module defines only a few methods that allow to connect to a database and to define "default variables" that override the environment variables used by PostgreSQL. These "default variables" were designed to allow you to handle general connection parameters without heavy code in your programs.


1 Answers

Are you disconnecting from database at the end of execution?

If not, it prevents node.js program from exiting.

like image 138
Almad Avatar answered Sep 21 '22 12:09

Almad