Koa系列-Services:koa-orm
2019-01-30 字数统计:3k 字 阅读时长 ≈ 3 分钟

主要通过 sequelizesquel 来操作数据库。

参数 configs 可以是object或者是array,也就是可以有多个database实例。

configs 有一个属性是 modelPath ,用来导入 model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module.exports = (sequelize, modelPath) => {
const models = {};

// Bootstrap models
fs.readdirSync(modelPath) // 得是个文件夹,应该需要判断
.forEach(function(file) {
if (/\.js$/.test(file)) {
const model = sequelize.import(join(modelPath, file));
models[model.name] = model;
}
});

Object.keys(models).forEach(function(modelName) {
if (models[modelName].associate) {
models[modelName].associate(models);
}
});

return models;
};

最后导出 database ,挂载到 ctx.orm 上。