节点访问多个数据库

时间:2016-02-25 15:29:43

标签: javascript angularjs node.js oracle

我想连接到服务器端的不同数据库,这样我就可以使用node执行包含这两个数据库的查询。

我有config.js这样:

module.exports = {
    database: {
        user: 'brunojs',
        password: 'bdpf5',
        connectString: 'localhost:1521/orcl'
    },
    jwtSecretKey: "jmvhDdDBMvqb=M@6h&QVA7x"
};

这会保存我访问第一个数据库的信息。

然后我有一个list.js文件执行查询:

var oracledb = require('oracledb');
var jwt = require('jsonwebtoken');
var config = require(__dirname + '../../config.js');

function get(req, res, next) {
    oracledb.getConnection(
        config.database,
        function(err, connection){
            if (err) {
                return next(err);
            }

            connection.execute(
                'select num_sequencial, notes, des_especialidade, dt_diag ' +
                'from organite_repository ',
                {},//no binds
                {
                    outFormat: oracledb.OBJECT
                },
                function(err, results){
                    if (err) {
                        connection.release(function(err) {
                            if (err) {
                                console.error(err.message);
                            }
                        });

                        return next(err);
                    }

                    res.status(200).json(results.rows);

                    connection.release(function(err) {
                        if (err) {
                            console.error(err.message);
                        }
                    });
                }
            );
        }
    );
}

module.exports.get = get;

一切正常。

问题是,现在,我想使用另一个数据库执行查询。我怎么能这样做?

4 个答案:

答案 0 :(得分:1)

首先,在config.js

中添加第二个凭据对象
module.exports = {
    database: {
        user: 'brunojs',
        password: 'bdpf5',
        connectString: 'localhost:1521/orcl'
    },
    database2: {
        user: 'user2',
        password: 'password',
        connectString: 'someotherhost:1521/orcl'
    },
    jwtSecretKey: "jmvhDdDBMvqb=M@6h&QVA7x"
};

然后在这里使用一个或其他:

oracledb.getConnection(
    config.database, // you could change this line to config.database2
    function(err, connection){
         if (err) { ...

如果要查询一个数据库,然后查询另一个数据库,则需要保留对connection个对象的引用(为简洁起见,省略错误检查):

oracledb.GetConnection(
    config.database,
    function(err, connection1) {
        oracledb.GetConnection(
            config.database2,
            function(err, connection2) {
                // in this function you can use either connection object
                connection1.execute(...);
                connection2.execute(...);
            }
    });

答案 1 :(得分:1)

这稍微超出了您的问题范围,但您也可以查看Waterline。它支持设置多个数据库,然后将模型绑定到它们,以便了解存储某些数据模型的位置。

答案 2 :(得分:1)

您始终可以使用数据库端的链接,因此您的Java代码不必连接到其他数据库,例如:

select num_sequencial, notes, des_especialidade, dt_diag 
from organite_repository@linkA
UNION
select num_sequencial, notes, des_especialidade, dt_diag 
from organite_repository@linkB
/* ... */

答案 3 :(得分:1)

正确的解决方案是使用池https://github.com/oracle/node-oracledb/blob/master/doc/api.md#createpool 创建大量池:

module.exports = {
    database: [{user: 'brunojs',
                password: 'bdpf5',
                connectString: 'localhost:1521/orcl',
                poolAlias:'database1'
               },
               {user: 'brunojs',
                password: 'bdpf5',
                connectString: 'localhost2:1521/orcl',
                poolAlias:'database2'
               }],
    jwtSecretKey: "jmvhDdDBMvqb=M@6h&QVA7x"
};

在初始化Web服务器期间,初始化池

const dbConfig = require('../config/database.js');
async function initialize() {
  dbConfig.database.forEach(async function(item) {
    const pool = await oracledb.createPool(item);
  });
}

然后,您可以在调用连接过程时使用创建的池:

conn = await oracledb.getConnection('database1');
 const execresult = await conn.execute(context.execsql, execbinds, context.opts);
相关问题