Sequelize 从模型创建数据库模式

Sequelize create database schema from model

我可以使用 sequelize 从可用模型创建数据库模式吗?我从事的项目缺少许多迁移且没有测试。为了 运行 测试,我需要创建一个新的数据库 (sqlite3),但我无法使用迁移来初始化它的模式(因为它们丢失了)。是否可以使用模型来创建架构?

是的。如果您只是在应用程序中同步 sequelize,模型将为您创建数据库表

Is it possible to use the models to create the schema? ... Because I want to do this in Mocha config so whenever any test runs, I'm sure that it runs on an up to date test db. But because sync is async I have no idea how to do this

假设使用了 Sequelize CLI,这里有一种方法可以确保您在任何 mocha 测试中都有更新的数据库:

  // Force sync without migrations
  // This can be run at the top of every individual test suite
  before(function () {
    return require('../../models').sequelize.sync({ force: true })
  })

路径 '../../models' 指向 Sequelize CLI 为您创建的 models/index.js