koa或express中用sequelize一般都要把一个个model放到单文件,官方文档给了一个奇怪的做法,它自己提供了个import的方法,然后让model单文件导出一个函数
// in your server file - e.g. app.js
const Project = sequelize.import(__dirname + "/path/to/models/project")
// The model definition is done in /path/to/models/project.js
// As you might notice, the DataTypes are the very same as explained above
module.exports = (sequelize, DataTypes) => {
return sequelize.define("project", {
name: DataTypes.STRING,
description: DataTypes.TEXT
})
}
https://github.com/sequelize/...
这个官方样例更详细
但是我觉得我这样写,模块化会更舒服一点
// sequelize.js
import Sequelize from 'sequelize'
const sequelize = new Sequelize({
...
})
export default sequelize
// model/modelA.js
import Sequelize from 'sequelize'
import sequelize from './sequelize'
export default sequelize.define("project", {
name: Sequelize.STRING,
description: Sequelize.TEXT
})
// in route
import modelA from 'path/to/model/modelA'
// or by using model/index.js
import { modelA } from 'path/to/model'
不知道我的想法会有什么问题
慕的地10843
相关分类