【问题标题】:Node access multiple databases节点访问多个数据库
【发布时间】:2023-03-20 21:37:01
【问题描述】:

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

我有一个这样的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;

一切正常。

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

【问题讨论】:

    标签: javascript angularjs node.js oracle


    【解决方案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"
    };
    

    在初始化网络服务器期间,初始化池

    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);
    

    【讨论】:

      【解决方案2】:

      首先,在您的 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(...);
                  }
          });
      

      【讨论】:

      • 非常感谢我的保罗。如果我想使用这两个数据库执行查询怎么办?
      【解决方案3】:

      这超出了您的问题的范围,但您也可以查看Waterline。它支持建立多个数据库,然后将模型绑定到它们,这样就可以抽象出知道某些数据模型的存储位置。

      【讨论】:

        【解决方案4】:

        您始终可以在 DB 端使用链接,因此您的 java 代码不必连接到另一个 DB,例如:

        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
        /* ... */
        

        【讨论】:

          猜你喜欢
          • 2020-12-06
          • 1970-01-01
          • 1970-01-01
          • 2017-12-29
          • 2023-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-11
          相关资源
          最近更新 更多