在同一命令中运行 Sequelize 迁移和节点服务器不会启动服务器 [英] Running Sequelize Migration and Node Server in Same Command Won't Start Server Up

查看:13
本文介绍了在同一命令中运行 Sequelize 迁移和节点服务器不会启动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试运行我的 sequelize 迁移,然后在同一命令中运行我的 Node 服务器,我会遇到我的服务器永远无法启动的问题.如果迁移之前已经运行过,sequelize db:migrate 命令不会超过没有执行迁移,数据库架构已经是最新的".消息,我的第二个命令永远无法运行.如果之前没有运行过迁移,则一切都按顺序正常运行.

If I try to run my sequelize migrations and then run my Node server in the same command, I run into the issue of my server never starting up. If the migrations have already been run before, the sequelize db:migrate command doesn't go past the "No migrations were executed, database schema was already up to date." message, and my second command is never able to run. If the migration has not run before, everything runs properly in sequence.

这是我的 npm start 命令:sequelize db:migrate &&节点索引.js

我假设在显示此日志消息的情况下,内部 sequelize db:migrate 没有解决任何问题,所以有没有办法可以在一段时间后终止"此命令并继续我的节点命令?

I assume that internally sequelize db:migrate is not resolving anything in the case where this log message is shown, so is there a way I can "terminate" this command after some time and proceed to my node command?

推荐答案

对于遇到此问题的其他人,这就是我最终解决的方法.

For anyone else running into this issue, this is how I ended up solving it.

1) 创建一个将在 npm 脚本中运行的新文件.

1) Create a new file that you will run in your npm script.

2) 我最终将进程调用包装在 child_process exec 中,然后在收到上述 console.log 消息时终止进程,因为库本身确实如此目前无法解决任何问题.

2) I ended up wrapping the process call in a child_process exec, and then terminated the process when I received the above console.log message since the library itself does not resolve anything at this point.

// 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 中得到正确解决.

Obviously the above code is not ideal, but hopefully this is just temporary until the internals of this edge case are resolved properly in a promise.

3) 使用以下内容更新您的 npm 脚本:

3) Update your npm script with the following:

"start": "node myRuntimeFile.js && node index.js"

或者如果你在 Windows 机器上运行并且不能使用 &&,你可以使用 npm-run-all 库.

Or if you are running on a Windows machine and cannot use &&, you can use the npm-run-all library.

这篇关于在同一命令中运行 Sequelize 迁移和节点服务器不会启动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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