数据库插件
This commit is contained in:
+53
-61
@@ -2,83 +2,75 @@
|
||||
* mysql/sequelize
|
||||
*
|
||||
* 参考文档:
|
||||
* https://sequelize.org/
|
||||
* https://sequelize.org/api/v6/identifiers
|
||||
* https://www.sequelize.com.cn/
|
||||
*/
|
||||
const config = require("./config")
|
||||
const Sequelize = require("sequelize");
|
||||
const operatorsAliases = require("./operatorsAliases");
|
||||
const Api = require("./api");
|
||||
module.exports = async (app) => {
|
||||
const {
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
options,
|
||||
} = config
|
||||
|
||||
const {database, username, password, options,} = config
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
...options,
|
||||
operatorsAliases,
|
||||
});
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('数据库连接成功');
|
||||
} catch (error) {
|
||||
console.error('数据库连接失败', error);
|
||||
}
|
||||
|
||||
app.sql = sequelize
|
||||
// 加载model文件
|
||||
let list = await app.load(config.path)
|
||||
list.forEach(item => {
|
||||
const {res, parse} = item
|
||||
const model = {}
|
||||
for (let key of Object.keys(res.model)) {
|
||||
res.model[key].type = Sequelize[res.model[key].type]
|
||||
model[key] = res.model[key]
|
||||
}
|
||||
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
|
||||
// console.log(api);
|
||||
console.log(await app.db.table("User").find());
|
||||
|
||||
//等待所有插件载入完成后,自动更新数据库字段
|
||||
//把所有模型的md5值存入到redis里面去,哪个文件的md5有变动就更新哪个表的字段.
|
||||
//todo
|
||||
app.willReadyList.push(()=>{
|
||||
|
||||
app.willReadyList.push(async () => {
|
||||
list.forEach(async item => {
|
||||
const {res, parse} = item
|
||||
const md5 = await app.redis.get("md5:" + parse.name)
|
||||
const newMd5 = getFileMd5(app.root + '/' + parse.dir + "/" + parse.base)
|
||||
if (!md5 || newMd5 !== md5) {
|
||||
//自动更新表字段
|
||||
await app.models[parse.name].sync({alter: true});
|
||||
console.log("自动更新表:", parse.name);
|
||||
//创建新的md5
|
||||
await app.redis.mset("md5:" + parse.name, newMd5)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
//定义别名
|
||||
function operatorsAliases() {
|
||||
const Op = Sequelize.Op;
|
||||
//操作符别名
|
||||
const operatorsAliases = {
|
||||
$eq: Op.eq,
|
||||
$ne: Op.ne,
|
||||
$gte: Op.gte,
|
||||
$gt: Op.gt,
|
||||
$lte: Op.lte,
|
||||
$lt: Op.lt,
|
||||
$not: Op.not,
|
||||
$in: Op.in,
|
||||
$notIn: Op.notIn,
|
||||
$is: Op.is,
|
||||
$like: Op.like,
|
||||
$notLike: Op.notLike,
|
||||
$iLike: Op.iLike,
|
||||
$notILike: Op.notILike,
|
||||
$regexp: Op.regexp,
|
||||
$notRegexp: Op.notRegexp,
|
||||
$iRegexp: Op.iRegexp,
|
||||
$notIRegexp: Op.notIRegexp,
|
||||
$between: Op.between,
|
||||
$notBetween: Op.notBetween,
|
||||
$overlap: Op.overlap,
|
||||
$contains: Op.contains,
|
||||
$contained: Op.contained,
|
||||
$adjacent: Op.adjacent,
|
||||
$strictLeft: Op.strictLeft,
|
||||
$strictRight: Op.strictRight,
|
||||
$noExtendRight: Op.noExtendRight,
|
||||
$noExtendLeft: Op.noExtendLeft,
|
||||
$substring: Op.substring,
|
||||
$startsWith: Op.startsWith,
|
||||
$endsWith: Op.endsWith,
|
||||
$and: Op.and,
|
||||
$or: Op.or,
|
||||
$any: Op.any,
|
||||
$all: Op.all,
|
||||
$values: Op.values,
|
||||
$col: Op.col
|
||||
};
|
||||
return operatorsAliases
|
||||
//文件md5值
|
||||
function getFileMd5(url) {
|
||||
const buffer = require('fs').readFileSync(url);
|
||||
const hash = require('crypto').createHash('md5');
|
||||
hash.update(buffer, 'utf8');
|
||||
const md5 = hash.digest('hex');
|
||||
return md5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user