搜索

此内容没有您所选择的语言版本。

Chapter 6. Connecting with node.js

download PDF

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.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.