Sequelize Migration Server连接两个数据库

时间:2018-08-07 04:07:41

标签: mysql node.js sequelize.js sequelize-cli

我已经创建了一个sequalize迁移服务器,并通过引用this doc.使用Sequelize CLI与mysql数据库连接,现在我想知道,是否可以使用同一迁移服务器在另一个mysql数据库上运行迁移?需要创建另一个迁移服务器以在另一个数据库上运行迁移。谢谢。

config.js

{
  "development": {
    "username": "root",
    "password": null,
    "database": "xxxx",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "yyy",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "zzz",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

models / index.js

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(__filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/..\config\config.json')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

migrations / create_user.js

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      firstName: {
        type: Sequelize.STRING
      },
      lastName: {
        type: Sequelize.STRING
      },
      email: {
        type: Sequelize.STRING
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  }
};

1 个答案:

答案 0 :(得分:0)

以此更改您的 config.js 文件。

const DB_DETAILS = {
    "development" : {
        "pg_db_host" : "127.0.0.1",
        "pg_db_name" : "zzz",
        "pg_db_username" : "root",
        "pg_db_passoword" : "", 
        "pg_db_dialect" : "mysql",
        "db_host" : "127.0.0.1",
        "db_name" : "xxx",
        "db_username" : "root",
        "db_passoword" : "", 
        "db_dialect" : "mysql",
        "multipleStatements": true                      
    },
    "local" : {
        "pg_db_host" : "localhost",
        "pg_db_name" : "collaboration",
        "pg_db_username" : "postgres",
        "pg_db_passoword" : "postgres", 
        "pg_db_dialect" : "postgres",
        "db_host" : "localhost",
        "db_name" : "colloboration",
        "db_username" : "colloboration",
        "db_passoword" : "s6K2q2W509gx",
        "db_dialect" : "postgres",
        "multipleStatements": true                      
    },
    "production" : {
        "pg_db_host" : "localhost",
        "pg_db_name" : "collaboration",
        "pg_db_username" : "postgres",
        "pg_db_passoword" : "postgres", 
        "pg_db_dialect" : "postgres",
        "db_host" : "localhost",
        "db_name" : "colloboration",
        "db_username" : "colloboration",
        "db_passoword" : "s6K2q2W509gx",
        "db_dialect" : "postgres",
        "multipleStatements": true
    }
}

const env_db = DB_DETAILS[process.env.NODE_ENV];

const sequelize = new Sequelize(env_db.pg_db_name, env_db.pg_db_username, env_db.pg_db_passoword, {
      host: env_db.pg_db_host,
      dialect: env_db.pg_db_dialect,
      pool: {
        max: 5,
        min: 0,
        idle: 10000
      }
});

const sequelize1 = new Sequelize(env_db.db_name, env_db.db_username, env_db.db_passoword, {
    host: env_db.db_host,
    dialect: env_db.db_dialect,
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    }
});

sequelize.authenticate().then(() => {
    console.log('Connection has been established successfully.');
})
.catch(err => {
    console.error('Unable to connect to the database:', err);
});


sequelize1.authenticate().then(() => {
    console.log('Connection has been established successfully.');
})
.catch(err => {
    console.error('Unable to connect to the database:', err);
});

module.exports = { Sequelize , sequelize };

module.exports = { Sequelize1 , sequelize1 };

还将 var db = {} 替换为 global.db = require('./ config.js'); // // index.js中的配置文件路径文件。

否,您可以相应地使用db.sequelize和db.sequelize1