-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09d618f
commit 3524eae
Showing
54 changed files
with
850 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,77 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const RDS = require('ali-rds'); | ||
|
||
module.exports = app => { | ||
app.addSingoneTon('rds', (config, app) => { | ||
config = Object.assign({}, config); | ||
/* todo | ||
if (config.cluster) { | ||
return rds(config); | ||
}*/ | ||
return RDS(config); | ||
const ClientManager = require('./client-manager'); | ||
|
||
module.exports = function(app) { | ||
// 兼容老的配置 | ||
const mysqlConfig = app.config.mysql; | ||
app.config.mysql = compact(mysqlConfig); | ||
|
||
if (app.config.mysql !== mysqlConfig) { | ||
app.deprecate('[egg-mysql] 配置格式已修改,请尽快查阅 egg-mysql 文档并更新到新版配置格式'); | ||
} | ||
// 创建 mysql | ||
const clientManager = new ClientManager(app); | ||
let count = 0; | ||
app.addSingleton('mysql', (config, app) => { | ||
const done = app.readyCallback(`createMysql-${count++}`); | ||
return clientManager.create(config, done); | ||
}); | ||
|
||
// 兼容老的写法 | ||
if (mysqlConfig.clients) { | ||
// 增加 end 方法 | ||
app.mysql.end = function(config, callback) { | ||
callback = callback || function() {}; | ||
const clientId = typeof config === 'string' ? config : getClientId(config); | ||
const client = app.mysql.get(clientId); | ||
if (!client) { | ||
return callback(); | ||
} | ||
app.mysql.clients.delete(clientId); | ||
client.end(callback); | ||
}; | ||
app.mysqls = app.mysql; | ||
} | ||
}; | ||
|
||
/** | ||
* 兼容老的配置写法 | ||
* @param {Object} originalConfig 原始配置 | ||
* @return {Object} 新版配置 | ||
*/ | ||
function compact(originalConfig) { | ||
const config = { | ||
app: originalConfig.app, | ||
agent: originalConfig.agent, | ||
}; | ||
|
||
if (Array.isArray(originalConfig.clients)) { | ||
config.clients = {}; | ||
for (const client of originalConfig.clients) { | ||
config.clients[getClientId(client)] = client; | ||
} | ||
// config.default = originalConfig.default; // 原来配置多实例不支持 default | ||
return config; | ||
} | ||
|
||
if (originalConfig.host) { | ||
config.client = originalConfig; | ||
config.default = originalConfig.default; // 原来配置单实例有默认值 | ||
delete config.client.default; | ||
delete config.client.agent; | ||
delete config.client.app; | ||
return config; | ||
} | ||
|
||
return originalConfig; | ||
} | ||
|
||
/** | ||
* 获取 clientId | ||
* @param {Object} config 配置对象 | ||
* @return {String} 配置 id | ||
*/ | ||
function getClientId(config) { | ||
return config.clientId || `${config.host}#${config.port}#${config.database}#${config.user}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const pedding = require('pedding'); | ||
const request = require('supertest'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/multiclient.test.js', () => { | ||
describe('old config', () => { | ||
let app; | ||
before(done => { | ||
app = mm.app({ | ||
baseDir: 'apps/mysqlapp-multi-client', | ||
plugin: 'mysql', | ||
}); | ||
app.ready(done); | ||
}); | ||
|
||
it('should multi client work', done => { | ||
request(app.callback()) | ||
.get('/') | ||
.expect({ | ||
hasRows: true, | ||
}) | ||
.expect(200, done); | ||
}); | ||
|
||
it('should close all mysql client work', done => { | ||
done = pedding(Object.keys(app.config.mysql.clients).length, done); | ||
for (const key in app.config.mysql.clients) { | ||
app.mysqls.end(key, done); | ||
} | ||
app.mysqls.end({ host: 'not exists host' }); | ||
app.mysqls.end('db1'); | ||
}); | ||
}); | ||
|
||
describe('new config', () => { | ||
let app; | ||
before(done => { | ||
app = mm.app({ | ||
baseDir: 'apps/mysqlapp-multi-client-new', | ||
plugin: 'mysql', | ||
}); | ||
app.ready(done); | ||
}); | ||
|
||
it('should multi client work', done => { | ||
request(app.callback()) | ||
.get('/') | ||
.expect({ | ||
hasRows: true, | ||
}) | ||
.expect(200, done); | ||
}); | ||
|
||
it('should close all mysql client work', done => { | ||
done = pedding(Object.keys(app.config.mysql.clients).length, done); | ||
for (const key in app.config.mysql.clients) { | ||
app.mysqls.end(key, done); | ||
} | ||
app.mysqls.end({ host: 'not exists host' }); | ||
app.mysqls.end('db1'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = function* () { | ||
const rows = yield this.app.mysql.query('show tables'); | ||
this.body = { | ||
hasRows: rows.length > 0, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.get('/', app.controller.home); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
exports.mysql = { | ||
host: '127.0.0.1', | ||
port: 3307, | ||
user: 'root', | ||
password: '', | ||
database: 'test', | ||
// host: env.ALI_SDK_RDS_HOST || 'localhost', | ||
// port: env.ALI_SDK_RDS_PORT || 3306, | ||
// user: env.ALI_SDK_RDS_USER || 'root', | ||
// password: env.ALI_SDK_RDS_PASSWORD || '', | ||
// database: env.ALI_SDK_RDS_DATABASE || 'test', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "npmweb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
exports.mysql = { | ||
host: '127.0.0.1', | ||
port: 3307, | ||
user: 'root', | ||
password: '', | ||
database: 'test', | ||
app:false | ||
// host: env.ALI_SDK_RDS_HOST || 'localhost', | ||
// port: env.ALI_SDK_RDS_PORT || 3306, | ||
// user: env.ALI_SDK_RDS_USER || 'root', | ||
// password: env.ALI_SDK_RDS_PASSWORD || '', | ||
// database: env.ALI_SDK_RDS_DATABASE || 'test', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "mysqlapp" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const co = require('co'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = function(agent) { | ||
agent.mysql1 = agent.mysql.createInstance(agent.config.mysql1); | ||
const done = agent.readyCallback('agent-mysql'); | ||
const p = path.join(__dirname, 'run/agent_result.json'); | ||
fs.existsSync(p) && fs.unlinkSync(p); | ||
|
||
co(function* () { | ||
const result = yield agent.mysql1.query('select now() as currentTime;'); | ||
fs.writeFileSync(p, JSON.stringify(result)); | ||
}).then(done, done); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.mysql1 = app.mysql.createInstance(app.config.mysql1); | ||
}; |
10 changes: 10 additions & 0 deletions
10
test/fixtures/apps/mysqlapp-dynamic/app/controller/home.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
module.exports = function* () { | ||
const users = yield this.service.user.list(this); | ||
|
||
this.body = { | ||
status: 'success', | ||
users: users, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.get('/', app.controller.home); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
exports.list = function* (ctx) { | ||
return yield ctx.app.mysql1.query('select * from npm_auth'); | ||
}; |
Oops, something went wrong.