运行 在同一命令中进行 Sequelize 迁移和节点服务器无法启动服务器

Running Sequelize Migration and Node Server in Same Command Won't Start Server Up

如果我尝试 运行 我的续集迁移然后 运行 我的节点服务器在同一命令中,我 运行 进入我的服务器从未启动的问题。如果迁移已经 运行 之前,sequelize db:migrate 命令不会通过 "No migrations were executed, database schema was already up to date." 消息,我的第二个命令永远无法 运行。如果迁移之前没有 运行,则所有内容 运行 都按顺序正确。

这是我的 npm start 命令:sequelize db:migrate && node index.js

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

对于其他 运行 解决这个问题的人,这就是我最终解决它的方式。

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();
      }
    });
  });
})();

显然上面的代码并不理想,但希望这只是暂时的,直到这个边缘案例的内部结构在承诺中得到妥善解决。

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

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

或者,如果您 运行 在 Windows 机器上使用并且无法使用 &&,您可以使用 npm-run-all 库。