Show Table of Contents
Chapter 6. Connecting with node.js
6.1. Access Red Hat JBoss Data Virtualization from node.js
If you are writing a "node.js" application and would like to access Red Hat JBoss Data Virtualization with it, you can do so by using an NPM package called "pg". Red Hat JBoss Data Virtualization supports the ODBC transport, so you can use this PostgreSQL client for "node.js" to access it.
For example, if you have a virtual database called "northwind" deployed on your Red Hat JBoss Data Virtualization server, and it has a table called "customers", your default configuration will look like this:
user = 'user' password = 'user' host = 127.0.0.1 port = 35432
You can then write a short access program like this:
var pg = require('pg');
var connectionString = "pg://user:user@localhost:35432/northwind"
pg.connect(connectionString, function(err, client) {
client.query('SELECT CustomerID, ContactName, ContactTitle FROM Customers', function(err, result) {
console.log(result.rows)
});
});
If you want to access one row at a time, you can also use the event mechanism by writing a piece of code like this:
var pg = require('pg');
var connectionString = "pg://user:user@localhost:35432/northwind"
pg.connect(connectionString, function(err, client) {
var query = client.query('SELECT CustomerID, ContactName, ContactTitle FROM Customers');
query.on('row', function(row) {
console.log(row);
});
query.on('error', function(error) {
console.log("failed to query");
});
query.on('end', function(error) {
console.log("closing client");
client.end();
});
});
For more information, please refer to the node-postgres package documentation. You can also find the source code on GitHub.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.