Database config file

Hi. In my application we have 3 databases (1 database per each language). Changing connection from controller based on language parametr not working properly. It someties connects to wrong database. I want to create separate config/database_lang.js file per each database. How I can achieve this?. Whatever other solution would you recommend?

I advise you to put everything in the configuration file config/database.js. Like :

...
mysql_en: {
    ...
  },
mysql_fr: {
    ...
  },
...

Thanks CrBast for your reply. I have already done this.But when i change connection it sometimes(rarely) connects to wrong database.I change connection as below

let lang = request.params.lang
Config.set(‘database.connection’, ‘db_’+lang)

1 Like

Hi @sanslan!

It sometimes connects to wrong database because AdonisJS uses Knex under the hood. Knex has it’s own connection pool. When you change DB config some connections in connection pool might be still using some other configuration.

I haven’t user multi DB connections myself too much so I can’t give you working solutions, but here’s some ideas that I have:

  1. Clear all connection pools - disconnect from DB before changing config and then connect back with new connection. This might result in all kinds of errors tho.
  2. Define connection in models and use different models.
  3. Use Database / Knex directly when doing stuff between multiple databases
2 Likes