Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anyone can post an RXJS example using node.js for querying postgresql db?

anyone can post an RXJS example using node.js for querying postgresql db?

google doesn't seem to have anything about this...

regards

Sean.

like image 229
born2net Avatar asked Jul 13 '26 21:07

born2net


1 Answers

voilà!

var Rx = require('rx');
var pg = require('pg');


var rowObservable = Rx.Observable.create(function(observer) {
    var pgClient = new pg.Client();
    pgClient.connect();

    var pgQuery = pgClient.query("SELECT * FROM information_schema.tables;");
    pgQuery.on('error', observer.onError.bind(observer));
    pgQuery.on('row', observer.onNext.bind(observer));
    pgQuery.on('end', observer.onCompleted.bind(observer));

    return pgClient.end.bind(pgClient);
});


var subscription = rowObservable.subscribe(function(row) {
    console.log(row);
}, function(err) {
    console.error(err);
}, function() {
    subscription.dispose();
});

In this example, the client connection pgClient is closed when the subscription is disposed. The rest is pretty self explanatory i think :-)

like image 114
Elmer Avatar answered Jul 15 '26 10:07

Elmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!