From 3494cecb9d55725027e02969e48e6d115f857c28 Mon Sep 17 00:00:00 2001 From: robin <1032740078@qq.com> Date: Tue, 31 May 2022 08:34:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/message/io/test.js | 4 +- extend/mysql/config.js | 46 +++++++++++++++++++++ extend/mysql/index.js | 84 +++++++++++++++++++++++++++++++++++++++ extend/redis/config.js | 7 ++++ extend/redis/index.js | 14 +++++++ extend/socket.io/index.js | 6 +-- lib/bamboo/index.js | 15 ++++++- middleware/time.js | 1 - package.json | 3 +- 9 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 extend/mysql/config.js create mode 100644 extend/mysql/index.js create mode 100644 extend/redis/config.js create mode 100644 extend/redis/index.js diff --git a/app/message/io/test.js b/app/message/io/test.js index 09c1740..98fccbd 100644 --- a/app/message/io/test.js +++ b/app/message/io/test.js @@ -1,3 +1,3 @@ -module.exports = (eventName, callback)=>{ - console.log('123321',eventName, callback); +module.exports = (msg, callback)=>{ + console.log('接收到的消息:', msg); } diff --git a/extend/mysql/config.js b/extend/mysql/config.js new file mode 100644 index 0000000..eee7bfd --- /dev/null +++ b/extend/mysql/config.js @@ -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(); + }, + }, + }, +} diff --git a/extend/mysql/index.js b/extend/mysql/index.js new file mode 100644 index 0000000..b04dfb9 --- /dev/null +++ b/extend/mysql/index.js @@ -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 +} diff --git a/extend/redis/config.js b/extend/redis/config.js new file mode 100644 index 0000000..aa220bf --- /dev/null +++ b/extend/redis/config.js @@ -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 +} diff --git a/extend/redis/index.js b/extend/redis/index.js new file mode 100644 index 0000000..3ee10d6 --- /dev/null +++ b/extend/redis/index.js @@ -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 + +} diff --git a/extend/socket.io/index.js b/extend/socket.io/index.js index a16ce21..de8e3f6 100644 --- a/extend/socket.io/index.js +++ b/extend/socket.io/index.js @@ -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) { //空间名称 diff --git a/lib/bamboo/index.js b/lib/bamboo/index.js index 5725639..538b542 100644 --- a/lib/bamboo/index.js +++ b/lib/bamboo/index.js @@ -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.startServer() + 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) diff --git a/middleware/time.js b/middleware/time.js index 4741f8c..97c7bac 100644 --- a/middleware/time.js +++ b/middleware/time.js @@ -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) } diff --git a/package.json b/package.json index 74799ac..6df1de9 100644 --- a/package.json +++ b/package.json @@ -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",