本文介绍了Sequelize v6 从目录下的文件加载模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Sequelize v5 或更早版本中,有一种方法可以使用 sequelize.import
函数从文件中加载模型
In Sequelize v5 or earlier, there is a way to load models from files using sequelize.import
function
import Sequelize from 'sequelize';
let sequelize = new Sequelize(...);
let db = {};
arrayOfFiles.forEach(function (file) {
let model = sequelize['import'](file);
db[model.name] = model;
});
sequelize.import
方法已从 v6 中移除.那么如何在 Sequelize v6 中做同样的事情.
The sequelize.import
method is removed from v6. So how can I do the same thing in Sequelize v6.
推荐答案
这种情况下可以直接传sequelize和DataTypes:
you can pass sequelize and DataTypes directly in this case:
const model = require(`./${file}`)(sequelize, Sequelize.DataTypes);
这篇关于Sequelize v6 从目录下的文件加载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!