Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
-5
rated 0 times [  1] [ 6]  / answers: 1 / hits: 43416  / 8 Years ago, fri, may 6, 2016, 12:00:00

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.



So in MySQL:



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


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', { /* ... */ });


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.


More From » mysql

 Answers
12

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(/* ... */);

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.


From their docs:



Observe that, in the examples above, Sequelize refers to the library itself while sequelize refers to an instance of Sequelize, which represents a connection to one database. This is the recommended convention and it will be followed throughout the documentation.



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'
}
}
}

Your app


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

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


[#62275] Wednesday, May 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;