数据库插件

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
+2 -2
View File
@@ -1,3 +1,3 @@
module.exports = (eventName, callback)=>{
console.log('123321',eventName, callback);
module.exports = (msg, callback)=>{
console.log('接收到的消息:', msg);
}
+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
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = {
port: 6379, // Redis port
host: "192.168.1.26", // Redis host
// username: "", // needs Redis >= 6
password: "9715115286",
db: 0, // Defaults to 0
}
+14
View File
@@ -0,0 +1,14 @@
/**
* redis
*
* 参考文档:
* https://github.com/luin/ioredis
* https://luin.github.io/ioredis/classes/Redis.html
*/
const config = require("./config")
const Redis = require("ioredis");
module.exports = async (app) => {
const redis = new Redis(config);
app.redis = redis
}
+3 -3
View File
@@ -14,11 +14,11 @@ module.exports = async (app) => {
app.io = io
//默认命名空间
io.on('connection', socket => {
socket.emit('open', "连接成功!");//通知客户端已连接
console.log('通知客户端已连接');
socket.emit('open', "id:"+socket.id+"连接成功!");//通知客户端已连接
console.log('连接=>',"id:"+socket.id);
//监听disconnect事件
socket.on('disconnect', (eventName, callback) => {
console.log('断开')
console.log('断开=X',"id:"+socket.id)
})
for (let el of list) {
//空间名称
+12 -1
View File
@@ -14,6 +14,7 @@ module.exports = class Bamboo extends Koa {
this.xe = xe
this.root = this.isPath() || root;
this.extend_directory = extend_directory || `extend/*/index.js`
this.willReadyList = []
console.log(`当前根目录${this.root}`);
console.log(`当前插件目录${this.extend_directory}`);
this.loadEvent()
@@ -25,7 +26,18 @@ module.exports = class Bamboo extends Koa {
for (let listElement of list) {
await listElement.res(this)
}
this.willReadyList.push(()=>{
this.startServer()
})
await this.willReady()
}
/*扩展已经加载完成触发*/
async willReady() {
for (let WRL of this.willReadyList) {
WRL()
}
}
/*当前根目录*/
@@ -58,7 +70,6 @@ module.exports = class Bamboo extends Koa {
}
/*启动服务*/
startServer(prod) {
this.listen(prod || 3000)
-1
View File
@@ -7,7 +7,6 @@ module.exports = {
// console.log(ctx, next, options);
// const timetaken = `${ctx.request.method}${ctx.request.url} 响应时间`
// console.time(timetaken)
// app.logger.debug("123")
await next()
// console.timeEnd(timetaken)
}
+2 -1
View File
@@ -49,6 +49,7 @@
"clean-webpack-plugin": "^4.0.0",
"cross-env": "^7.0.3",
"glob": "^8.0.1",
"ioredis": "^5.0.5",
"koa-bodyparser": "^4.3.0",
"koa-json-error": "^3.1.2",
"koa-logger": "^2.0.1",
@@ -61,7 +62,7 @@
"pm2": "^5.1.2",
"require-all": "^3.0.0",
"require-directory": "^2.1.1",
"sequelize": "^6.18.0",
"sequelize": "^6.20.1",
"shelljs": "^0.8.5",
"socket.io": "^4.5.1",
"sqlite3": "^5.0.2",