数据库插件

This commit is contained in:
robin
2022-05-31 08:34:53 +08:00
parent c764e7097d
commit 3494cecb9d
9 changed files with 171 additions and 9 deletions
+46
View File
@@ -0,0 +1,46 @@
module.exports = {
database: "bamboo",
username: "bamboo",
password: "bamboo",
options: {
dialect: 'mysql',
host: "192.168.1.26",
port: 3306,
// 禁用日志记录或提供自定义日志记录功能;默认值:console.log
// logging: false,
// model的全局配置
define: {
// 添加create,update,delete时间戳
timestamps: true,
// 添加软删除
paranoid: false,
// 防止修改表名为复数
freezeTableName: true,
// 防止驼峰式字段被默认转为下划线
underscored: false,
},
// 由于orm用的UTC时间,这里必须加上东八区,否则取出来的时间相差8小时
timezone: '+08:00',
// 连接数 = ((核心数 * 2) + 有效磁盘数)
pool: {// 连接池
max: require('os').cpus().length * 2 + 1,
min: 0,
acquire: 60000,
idle: 100000,
},
dialectOptions: {
charset: "utf8mb4",
// collate: "utf8mb4_general_ci",
supportBigNumbers: true,
bigNumberStrings: true,
dateStrings: true,
typeCast(field, next) {// 让读取date类型数据时返回字符串而不是UTC时间
if (field.type === 'DATETIME') {
// console.log(field);
return field.string();
}
return next();
},
},
},
}
+84
View File
@@ -0,0 +1,84 @@
/**
* mysql/sequelize
*
* 参考文档:
* https://www.sequelize.com.cn/
*/
const config = require("./config")
const Sequelize = require("sequelize");
module.exports = async (app) => {
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
//等待所有插件载入完成后,自动更新数据库字段
//把所有模型的md5值存入到redis里面去,哪个文件的md5有变动就更新哪个表的字段.
//todo
app.willReadyList.push(()=>{
})
}
//定义别名
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
}