初始化

This commit is contained in:
robin
2022-08-02 19:21:03 +08:00
parent 73c1343060
commit b7e5ee5fec
27 changed files with 564 additions and 156 deletions
+20 -10
View File
@@ -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