Sequelize Create Database

前端 未结 4 1132
耶瑟儿~
耶瑟儿~ 2021-02-05 01:42

Is there a way to have sequelize create the database I\'m trying to connect to if it doesn\'t exist?

I have a raw MySQL instance and I get this error back:



        
4条回答
  •  粉色の甜心
    2021-02-05 02:21

    I ended up using the mysql2 package, here is what I did..

    const mysql = require('mysql2/promise');
    
    mysql.createConnection({
        user     : config.sequelize.username,
        password : config.sequelize.password
    }).then(() => {
        connection.query('CREATE DATABASE IF NOT EXISTS myRandomDb;').then(() => {
            // Safe to use sequelize now
        })
    })
    

    After that I can connect to that database using sequelize.

提交回复
热议问题