Red Hat Training

A Red Hat training course is available for Red Hat JBoss Data Virtualization

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.