Sequelize:根据请求在运行时连接到数据库 [英] Sequelize: connect to database on run time based on the request

查看:76
本文介绍了Sequelize:根据请求在运行时连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 node.js 应用程序,我需要连接到多个数据库.数据库之一是中央数据库,其中包含所有公共信息.然后是国家级数据库,根据国家/地区存储数据.

I am working on a node.js app where I need to connect to more than one databases. One of the database is central database which contains information common to all. And then there are country level databases where data is stored according to the countries.

我在应用中使用 sequelize ORM.
数据库是 postgresql.
框架很明确.

I am using sequelize ORM in the app.
Database is postgresql.
Framework is express.

问题是我想根据使用哪个数据库和模型应该自动连接到适当数据库的请求来决定运行时.我见过这个问题,但是没有发现它有帮助.

The problem is I want to decide on runtime based on the request which database to use and models should automatically connect to the appropriate database. I have seen this question but didn't found it helpful.

我也查看了其他论坛,但没有找到任何内容.

I have also checked in another forums but didn't find anything.

推荐答案

您需要创建与您的每个数据库相对应的对象,并且在每个此对象上您需要实例化 Sequelize.此外,对于每个 sequelize 实例,您都需要导入模型(假设所有这些数据库具有完全相同的表和模型表示).

You need to create objects corresponding to each of your database, and at each this object you need to instantiate the Sequelize. Further, for each sequelize instance you need to import the models (assumming that all those databases have exactly the same tables and model representations).

import Sequelize from 'sequelize';

let connectionsArray = [
    'postgres://user:pass@example.com:5432/country1',
    'postgres://user:pass@example.com:5432/country2',
    'postgres://user:pass@example.com:5432/country3',
];

let country1DB, country2DB, country3DB;
country1DB = country2DB = country3DB = {};
country1DB.Sequelize = country2DB.Sequelize = country3DB.Sequelize = Sequelize;

country1DB.sequelize = new Sequelize(connectionsArray[0]);
country2DB.sequelize = new Sequelize(connectionsArray[1]);
country3DB.sequelize = new Sequelize(connectionsArray[2]);

// here you need to access the models path, maybe with fs module
// iterate over every model and import it into every country sequelize instance
// let's assume that models' paths are in simple array
models.forEach(modelFile => {
    let model1DB = country1DB.sequelize.import(modelFile);
    let model2DB = country2DB.sequelize.import(modelFile);
    let model3DB = country3DB.sequelize.import(modelFile);

    country1DB[model1DB.name] = model1DB;
    country2DB[model2DB.name] = model2DB;
    country3DB[model3DB.name] = model3DB;
});

// now every country?DB object has it's own sequelize instance and all model definitions inside
export {
    country1DB,
    country2DB,
    country3DB
};

这只是一些示例代码,需要重构才能有用(引入一些循环等).它应该只是向您展示如何在单个应用程序中使用多个数据库的想法.如果您想使用例如country1 数据库某处,你只需要做

This is just some example code, it would need refactor to be useful (introduce some loops etc.). It should just show you the idea of how to use multiple databases within single application. If you would like to use e.g. country1 database somewhere, you would simply do

import { country1DB } from './databases';

country1DB.User.findAll({...});

以上代码将在先前指定的country1 数据库中执行SELECT * FROM users.示例 express 路线可能如下所示:

Above code would perform SELECT * FROM users in previously specified country1 database. Example express route could look like below:

import * as databases from './databases';

app.get('/:dbIndex/users', (req, res) => {
    databases['country' + req.params.dbIndex + 'DB'].User.find().then(user => {
        res.json(user.toJSON());
    });
});

或者,更好的是,您可以编写一些 middleware 函数,该函数将在每个请求之前运行,并负责为进一步操作选择合适的数据库.

Or, even better, you could write some middleware function that would be run before every request and which would be responsible for choosing proper database for further operations.

这篇关于Sequelize:根据请求在运行时连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆