如何以编程方式运行 sequelize 迁移 [英] How to programmatically run sequelize migrations

查看:48
本文介绍了如何以编程方式运行 sequelize 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sequelize 的文档似乎已经过时,因为它们不再支持从 sequelize 本身运行迁移,而是依赖于 sequelize-cli.是否有关于如何以编程方式使用 sequeliz-cli 运行最新迁移的示例?所有文档似乎都集中在如何在 shell 中使用客户端.

The documentation for sequelize seems out of date as they no longer support running migrations from sequelize itself, but instead relies on sequelize-cli. Is there an example of how to use sequeliz-cli programmatically to run the latest migrations? All the documentation seems to be focused on using the client in a shell.

db.js 似乎有函数 db:migrate ,也许我可以包括.

db.js seems to have the function db:migrate that perhaps I can include.

https://github.com/sequelize/cli/blob/master/lib/tasks/db.js

推荐答案

我深入研究了 sequelize db:migrate 命令,并且有足够的事情发生,恕我直言,最简单/最好的方法是在孩子中运行该命令过程.这是我用于此的代码(作为等待的承诺):

I dug into the code for the sequelize db:migrate command, and there's enough going on there that, IMHO, the simplest/best approach is to just run the command in a child process. Here's the code I used for this (as an await'ed Promise):

const {exec} = require('child_process');

await new Promise((resolve, reject) => {
  const migrate = exec(
    'sequelize db:migrate',
    {env: process.env},
    err => (err ? reject(err): resolve())
  );

  // Forward stdout+stderr to this process
  migrate.stdout.pipe(process.stdout);
  migrate.stderr.pipe(process.stderr);
});

这篇关于如何以编程方式运行 sequelize 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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