如何更改sequelize数据库名称的动态方式?从路由参数获取数据库名称,然后我想连接我的数据库

时间:2019-04-13 05:39:44

标签: node.js express sequelize.js

dbconfig.js

const Sequelize = require('sequelize');
    const sequelize = new Sequelize('mysql://root:password'+MysqlInfo.DatabaseURL+'mydb');
    sequelize.authenticate()
      .then(() => {
        console.log('Connection has been established successfully.');
      })
      .catch(err => {
        console.error('Unable to connect to the database:', err);
      });


    exports.config={
       sequelize:sequelize
    }

app.js

const express = require('express')
const path = require('path')
require('./dbconfig.js')

modle.js

const sequelize = require('../dbconfig.js')


const mediums = sequelize.define('mediums', {
    medium_name: Sequelize.STRING,
    langauge: Sequelize.STRING
})


module.exports = {
    mediums ,
}

注意

我已经创建了静态数据库连接字符串。但是我需要动态的数据库连接字符串。我从每个请求的网址或标头中获取数据库名称

0 个答案:

没有答案