1

I have a sequelize connection.

Then I create every table I need if they don't already exist. However, before I do that, I want to create the schema itself if it doesn`t exist, so I can run this script anywhere that has mysql installed and not worry. Is there a way to do so with sequelize? Or with any other tool if not.

Icaro Mota
  • 912
  • 1
  • 12
  • 20
  • 1
    try a combination of [this](https://stackoverflow.com/a/52915491/62282) and [sync](https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-method-sync) – Samuel Goldenbaum Aug 18 '20 at 20:17

1 Answers1

6

Following this answer as proposed by Samuel G:

const mysql = require('mysql2/promise');

await mysql.createConnection({
    user     : user,
    password : pwd
}).then((connection: Sequelize) => {
    connection.query(`CREATE DATABASE IF NOT EXISTS ${name};`);
});
Icaro Mota
  • 912
  • 1
  • 12
  • 20