0

How do I connect to multiple mysql databases using fastify-mysql plugin? I have a file mysql.db.js in the plugins folder. But can't seem to figure out how to make multiple connections to 2 dbs residing in different locations.

'use strict'

const fp = require('fastify-plugin')
const mysql = require('fastify-mysql')

module.exports = fp(async (fastify, opts) => {
  const mysqlOpts = Object.assign({}, {
    host: process.env.MYSQL_HOST || '',
    port: process.env.MYSQL_PORT || '',
    database: process.env.MYSQL_DATABASE || '',
    user: process.env.MYSQL_USER || '',
    password: process.env.MYSQL_PASSWORD || ''
  }, opts.mysql)

  fastify.register(mysql, mysqlOpts)
})
jps
  • 20,041
  • 15
  • 75
  • 79
oprogfrogo
  • 2,005
  • 5
  • 31
  • 42

1 Answers1

0

Might be a little late for the poster but for future readers, I have solved this by creating a regular plugin which require this module

(which is also used underneath by the fastify-mysql plugin) then created two connections/pool (one for each of the two DB I needed) then used fastify.decorate to name each db connections. It works as expected.

Robertino Vasilescu
  • 1,038
  • 1
  • 7
  • 13