【问题标题】:Running Sequelize Migration and Node Server in Same Command Won't Start Server Up在同一命令中运行 Sequelize 迁移和节点服务器不会启动服务器
【发布时间】:2020-02-05 07:27:19
【问题描述】:
如果我尝试运行我的 sequelize 迁移,然后在同一命令中运行我的 Node 服务器,我会遇到我的服务器永远无法启动的问题。如果之前已经运行过迁移,sequelize db:migrate 命令不会超过“没有执行迁移,数据库架构已经是最新的”。消息,我的第二个命令永远无法运行。如果之前没有运行过迁移,则一切都按顺序正常运行。
这是我的npm start 命令:sequelize db:migrate && node index.js
我假设在显示此日志消息的情况下,内部 sequelize db:migrate 没有解决任何问题,那么有没有办法可以在一段时间后“终止”此命令并继续执行我的节点命令?
【问题讨论】:
标签:
node.js
sequelize.js
sequelize-cli
【解决方案1】:
对于遇到此问题的其他人,这就是我最终解决的方法。
1) 创建一个将在 npm 脚本中运行的新文件。
2) 我最终将进程调用包装在 child_process exec 中,然后在收到上述 console.log 消息时终止进程,因为此时库本身没有解决任何问题。
// myRuntimeFile.js --> Make sure this file is in the same directory where your .sequelizerc file lives
(async()=> {
const { exec } = require('child_process');
await new Promise((resolve, reject) => {
const migrate = exec(
'sequelize db:migrate',
{ env: process.env },
(err, stdout, stderr) => {
resolve();
}
);
// Listen for the console.log message and kill the process to proceed to the next step in the npm script
migrate.stdout.on('data', (data) => {
console.log(data);
if (data.indexOf('No migrations were executed, database schema was already up to date.') !== -1) {
migrate.kill();
}
});
});
})();
显然,上面的代码并不理想,但希望这只是暂时的,直到这个边缘情况的内部在 Promise 中得到正确解决。
3) 使用以下内容更新您的 npm 脚本:
"start": "node myRuntimeFile.js && node index.js"
或者,如果您在 Windows 机器上运行并且无法使用 &&,则可以使用 npm-run-all 库。