How to create mysql database with sequelize (nodejs)

后端 未结 2 740
挽巷
挽巷 2020-12-19 08:56

I am trying to automate the process of creating db and tables as much as possible. Is it possible to create database via sequelize? Can I make a connection string that conne

2条回答
  •  时光取名叫无心
    2020-12-19 09:23

    Short Answer: Sure you can!

    Here's how I got it done:

    //create the sequelize instance, omitting the database-name arg
    const sequelize = new Sequelize("", "", "", {
      dialect: ""
    });
    
    return sequelize.query("CREATE DATABASE ``;").then(data 
    => {
      // code to run after successful creation.
    });
    

    P.S. Where to place code would depend on your need. Inside an initial migration file worked for me.

提交回复
热议问题