Skip to content

Commit 1d76cce

Browse files
committed
feat(@142vip/egg-mysql): mysql实例自动加载
1 parent a49c812 commit 1d76cce

File tree

3 files changed

+24
-67
lines changed

3 files changed

+24
-67
lines changed

packages/egg-mysql/agent.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
const { registerPlugin } = require('@142vip/egg')
2-
const { RegisterEggPluginName } = require('@142vip/egg')
3-
const { createMysqlInstance } = require('./core/mysql')
1+
const { RegisterEggPluginName, EggPluginBoot } = require('@142vip/egg')
2+
const { createEggMysqlInstance } = require('./core/mysql')
43

54
/**
65
* agent启动器
76
*/
8-
class EggMysqlAgentBoot {
7+
class EggMysqlAgentBoot extends EggPluginBoot {
98
constructor(agent) {
10-
this.agent = agent
11-
}
12-
13-
configWillLoad() {
14-
console.log(this.agent)
15-
}
16-
17-
configDidLoad() {
18-
}
19-
20-
async didLoad() {
21-
// 所有文件已加载,此时可以启动插件。
22-
if (this.agent.config[RegisterEggPluginName.EGG_MYSQL]) {
23-
registerPlugin(RegisterEggPluginName.EGG_MYSQL, this.agent, createMysqlInstance)
24-
}
9+
super({
10+
pluginName: RegisterEggPluginName.EGG_MYSQL,
11+
appOrAgent: agent,
12+
createEggPluginInstance: createEggMysqlInstance,
13+
})
2514
}
2615
}
2716

packages/egg-mysql/app.js

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
1-
const { registerPlugin } = require('@142vip/egg')
2-
const { RegisterEggPluginName } = require('@142vip/egg')
3-
const { createMysqlInstance } = require('./core/mysql')
1+
const { RegisterEggPluginName, EggPluginBoot } = require('@142vip/egg')
2+
const { createEggMysqlInstance } = require('./core/mysql')
43

5-
class EggMysqlAppBoot {
4+
class EggMysqlAppBoot extends EggPluginBoot {
65
constructor(app) {
7-
this.app = app
8-
}
9-
10-
configWillLoad() {
11-
// 预备调用 configDidLoad,
12-
// Config 和 plugin 文件已被引用,
13-
// 这是修改配置的最后机会。
14-
console.log(this.app)
15-
}
16-
17-
configDidLoad() {
18-
// Config 和 plugin 文件已加载。
19-
}
20-
21-
async didLoad() {
22-
// 所有文件已加载,此时可以启动插件。
23-
if (this.app.config[RegisterEggPluginName.EGG_MYSQL]) {
24-
console.log(111, this.app.config.mysql)
25-
registerPlugin(RegisterEggPluginName.EGG_MYSQL, this.app, createMysqlInstance)
26-
}
27-
}
28-
29-
async willReady() {
30-
// 所有插件已启动,这里可以执行一些在应用准备好之前的操作。
31-
}
32-
33-
async didReady() {
34-
// Worker 已准备好,可以执行一些不会阻塞应用启动的操作。
35-
}
36-
37-
async serverDidReady() {
38-
// 服务器已监听。
39-
}
40-
41-
async beforeClose() {
42-
// 应用关闭前执行的操作。
6+
super({
7+
pluginName: RegisterEggPluginName.EGG_MYSQL,
8+
appOrAgent: app,
9+
createEggPluginInstance: createEggMysqlInstance,
10+
})
4311
}
4412
}
4513

packages/egg-mysql/core/mysql.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { VipEggPluginLogger } = require('@142vip/egg')
22
const mysql = require('mysql2/promise')
3-
const { name: pkgName } = require('../package.json')
43
const { createDataBase } = require('./database')
54

65
async function creatPool(config) {
@@ -27,19 +26,20 @@ async function creatPool(config) {
2726
/**
2827
* 创建MySQL实例
2928
*/
30-
function createMysqlInstance(config, app) {
31-
const pluginLogger = VipEggPluginLogger.getInstance(pkgName, app)
32-
29+
async function createEggMysqlInstance(pluginConfig, app) {
30+
const pluginLogger = VipEggPluginLogger.getInstance(pluginConfig, app)
3331
try {
34-
const client = creatPool(config)
35-
pluginLogger.info(`DataBase create success , the name is ${config.database}`)
32+
const client = await creatPool(pluginConfig)
33+
pluginLogger.info(`dataBase create success , the name is ${pluginConfig.database}`)
3634
return client
3735
}
36+
3837
catch (e) {
39-
pluginLogger.error(`DataBase create failed ,check egg-sequelize-plus config carefully ${e.message}`)
38+
pluginLogger.error(`dataBase create failed,check config carefully!!!!`)
39+
pluginLogger.error(e.message)
4040
}
4141
}
4242

4343
module.exports = {
44-
createMysqlInstance,
44+
createEggMysqlInstance,
4545
}

0 commit comments

Comments
 (0)