初始化
This commit is contained in:
+20
-10
@@ -16,7 +16,7 @@ module.exports = async (app) => {
|
||||
const {database, username, password, options,} = config
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
...options,
|
||||
operatorsAliases,
|
||||
operatorsAliases:operatorsAliases,
|
||||
});
|
||||
|
||||
// 加载model文件
|
||||
@@ -24,22 +24,32 @@ module.exports = async (app) => {
|
||||
list.forEach(item => {
|
||||
const {res, parse} = item
|
||||
const model = {}
|
||||
let primaryKey = false
|
||||
for (let key of Object.keys(res.model)) {
|
||||
res.model[key].type = Sequelize[res.model[key].type]
|
||||
model[key] = res.model[key]
|
||||
//如果表设置了主键,那么就不设置默认主键为id了
|
||||
if (res.model[key].primaryKey) {
|
||||
primaryKey = true
|
||||
res.model[key].allowNull = false
|
||||
res.model[key].unique = key
|
||||
res.model[key].autoIncrement = true
|
||||
}
|
||||
}
|
||||
model['id'] = {
|
||||
type : Sequelize.INTEGER,
|
||||
comment : '表自增id',
|
||||
allowNull : false,
|
||||
unique : 'id',
|
||||
primaryKey : true,
|
||||
autoIncrement: true,
|
||||
//如果没有默认主键
|
||||
if (!primaryKey) {
|
||||
model['id'] = {
|
||||
type : Sequelize.INTEGER,
|
||||
comment : '表自增id',
|
||||
allowNull : false,
|
||||
unique : 'id',
|
||||
primaryKey : true,
|
||||
autoIncrement: true,
|
||||
}
|
||||
}
|
||||
|
||||
sequelize.define(parse.name, model)
|
||||
})
|
||||
|
||||
|
||||
app.models = sequelize.models
|
||||
const api = new Api(sequelize, sequelize.models)
|
||||
app.db = api
|
||||
|
||||
Reference in New Issue
Block a user