Connecting multiple MySQL databases

Hey @Arda_Yurdakul,

Actually, the mentioned documentation is not up-to-date with our latest development. Since you are using v7, you should hopefully have everything set up to easily add a new database of the same dialect to your project.

You should have a config/databases.js file in your project, that should contains something like:

module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models'),
  connection: {
    url: process.env.DATABASE_URL,
    options: { ...databaseOptions },
  },
}];

You should be able to add a new connection here, to your other database, like so:

module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models/your_database1_models'),
  connection: {
    url: process.env.DATABASE_URL,
    options: { ...databaseOptions },
  },
}, {
  name: 'another_connection',
  modelsDir: path.resolve(__dirname, '../models/your_database2_models'),
  connection: {
    url: process.env.ANOTHER_DATABASE_URL,
    options: { ...databaseOptions },
  },
}];

If everything goes well from here, running forest schema:update should create your missing folders & models for the new database. You will still have to remove previous files located in the models/ folder (except, of course, models/index.js).

Concerning the connection string, if you are using a SQL client to connect to your databases, it should be able to provide you the connection strings. If not, these are composed of your_dialect://your_db_username:your_db_passwd@your_db_ip:your_db_port/your_db_name

I’m not sure if that’s clear enough, but in case it doesn’t, just let me know.

1 Like