如何使用Sequelize(Node.js)创建MySQL数据库 [英] How to create mysql database with sequelize (nodejs)

查看:982
本文介绍了如何使用Sequelize(Node.js)创建MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试尽可能自动地创建数据库和表的过程.是否可以通过续集创建数据库?我可以建立一个仅连接服务器而不直接连接数据库的连接字符串吗?

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 connects just to server, not to db directly?

推荐答案

简短回答:当然可以!

这是我的完成方式:

//create the sequelize instance, omitting the database-name arg
const sequelize = new Sequelize("", "<db_user>", "<db_password>", {
  dialect: "<dialect>"
});

return sequelize.query("CREATE DATABASE `<database_name>`;").then(data 
=> {
  // code to run after successful creation.
});

P.S.在哪里放置代码将取决于您的需要.在一个初始迁移文件中对我有用.

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

这篇关于如何使用Sequelize(Node.js)创建MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆