create database dynamically sql server using sequalize

Solutions on MaxInterview for create database dynamically sql server using sequalize by the best coders in the world

showing results for - "create database dynamically sql server using sequalize"
Lisa
11 Aug 2019
1const config = require('config.json');
2const mysql = require('mysql2/promise');
3const { Sequelize } = require('sequelize');
4
5module.exports = db = {};
6
7initialize();
8
9async function initialize() {
10    // create db if it doesn't already exist
11    const { host, port, user, password, database } = config.database;
12    const connection = await mysql.createConnection({ host, port, user, password });
13    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
14
15    // connect to db
16    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
17
18    // init models and add them to the exported db object
19    db.User = require('../users/user.model')(sequelize);
20
21    // sync all models with database
22    await sequelize.sync();
23}
similar questions