Sequelize:使用多个数据库 [英] Sequelize: Using Multiple Databases

查看:709
本文介绍了Sequelize:使用多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用两个数据库,是否需要创建Sequelize的多个实例?也就是说,同一台机器上的两个数据库。

Do I need to create multiple instances of Sequelize if I want to use two databases? That is, two databases on the same machine.

如果没有,这样做的正确方法是什么?对我来说,连接两次以使用两个数据库似乎有些过分。

If not, what's the proper way to do this? It seems like overkill to have to connect twice to use two databases, to me.

例如,我有不同的数据库用于不同的功能,例如,让我们说我将客户数据存储在一个数据库中,将统计数据存储在另一个

So for example, I have different databases for different functions, for example, let's say I have customer data in one database, and statistical data in another.

所以在MySQL中:

MySQL [customers]> show databases;
+--------------------+
| Database           |
+--------------------+
| customers          |
| stats              |
+--------------------+

我有这个连接sequelize

And I have this to connect with sequelize

// Create a connection....
var Sequelize = require('sequelize');
var sequelize = new Sequelize('customers', 'my_user', 'some_password', {
    host: 'localhost',
    dialect: 'mysql',

    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
    logging: function(output) {
        if (opts.log_queries) {
            log.it("sequelize_log",{log: output});
        }
    }

});

// Authenticate it.
sequelize.authenticate().nodeify(function(err) {

    // Do stuff....

});

我试图通过使用点符号定义模型来欺骗它

I tried to "trick" it by in a definition of a model using dot notation

var InterestingStatistics = sequelize.define('stats.interesting_statistics', { /* ... */ });

但这会创建表 customers.stats.interesting_statistics 。我需要使用统计数据库中的现有表。

But that creates the table customers.stats.interesting_statistics. I need to use an existing table in the stats database.

实现这一目标的正确方法是什么?谢谢。

What's the proper way to achieve this? Thanks.

推荐答案

您需要为要创建的每个数据库连接创建不同的sequelize实例:

You need to create different instances of sequelize for each DB connection you want to create:

const Sequelize = require('Sequelize');
const userDb = new Sequelize(/* ... */);
const contentDb = new Sequelize(/* ... */);

从sequelize创建的每个实例都有自己的数据库信息(db host,url,user,传递,等等...),并且这些值不应该被更改,因此没有正确的方法来创建与一个sequelize实例的多个连接。

Each instance created from sequelize has its own DB info (db host, url, user, pass, etc...), and these values are not meant to be changed, so there is no "correct" way to create multiple connections with one instance of sequelize.

来自他们的文档


Sequelize会在初始化时设置连接池,所以你应该
理想情况下每个数据库只创建一个实例。

Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database.

每个数据库一个实例


One instance per database

执行此操作的常见方法是将数据库放在 config.json 文件中并循环遍历它以创建连接,如下所示也许:

A "common" approach to do this, is having your databases in a config.json file and loop over it to create connections dinamically, something like this maybe:

config.json

{
    /*...*/
    databases: {
        user: {
            path: 'xxxxxxxx'
        },
        content: {
            path: 'xxxxxxxx'
        }
    }
}

您的应用

const Sequelize = require('sequelize');
const config = require('./config.json');

// Loop through
const db = {};
const databases = Object.keys(config.databases);
for(let i = 0; i < databases.length; ++i) {
    let database = databases[i];
    let dbPath = config.databases[database];
    db[database] = new Sequelize( dbPath );
}

// Or a one liner
const db = Object.entries(asd).reduce((r, db) => (r[db[0]] = db[1].path) && r, {});

// Sequelize instances:
// db.user
// db.content

你需要做更多的编码来启动和运行,但这是一个大概。

You will need to do a little bit more coding to get it up and running but its a general idea.

这篇关于Sequelize:使用多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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