gpt4 book ai didi

node.js - 在同一命令中运行 Sequelize Migration 和 Node Server 不会启动服务器

转载 作者:行者123 更新时间:2023-12-03 22:26:31 25 4
gpt4 key购买 nike

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

这是我的 npm start 命令:sequelize db:migrate && node index.js
我假设在显示此日志消息的情况下,内部 sequelize db:migrate 没有解决任何问题,那么有没有办法在一段时间后“终止”此命令并继续执行我的 Node 命令?

最佳答案

对于遇到此问题的其他任何人,这就是我最终解决它的方式。

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 库。

关于node.js - 在同一命令中运行 Sequelize Migration 和 Node Server 不会启动服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com