Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add app.js and agent.js extend intro #3

Merged
merged 2 commits into from Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 0 additions & 21 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
Expand Down
24 changes: 12 additions & 12 deletions README.md
Expand Up @@ -56,13 +56,13 @@ exports.mysql = {
// port
port: '3306',
// username
user: 'mobile_pub',
user: 'test_user',
// password
password: 'password',
password: 'test_password',
// database
database: 'mobile_pub',
database: 'test',
},
// load into app, default is open
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
Expand All @@ -82,17 +82,17 @@ app.mysql.query(sql, values); // you can access to simple database instance by u
exports.mysql = {
clients: {
// clientId, access the client instance by app.mysql.get('clientId')
mypay1: {
db1: {
// host
host: 'mysql.com',
// port
port: '3306',
// username
user: 'mobile_pub',
user: 'test_user',
// password
password: 'password',
password: 'test_password',
// database
database: 'mobile_pub',
database: 'test',
},
// ...
},
Expand All @@ -101,7 +101,7 @@ exports.mysql = {

},

// load into app, default is open
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
Expand All @@ -111,10 +111,10 @@ exports.mysql = {
Usage:

```js
const client1 = app.mysql.get('client1');
const client1 = app.mysql.get('db1');
client1.query(sql, values);

const client2 = app.mysql.get('client2');
const client2 = app.mysql.get('db2');
client2.query(sql, values);
```

Expand Down Expand Up @@ -191,7 +191,7 @@ try {
- `scope`: A generatorFunction which will execute all sqls of this transaction.
- `ctx`: The context object of current request, it will ensures that even in the case of a nested transaction, there is only one active transaction in a request at the same time.
- adventage: easy to use, as if there is no transaction in your code.
- disadvantage: all transation will be successful or failed, cannot control precisely
- disadvantage: all transation will be successful or failed, cannot control precisely

```js
const result = yield app.mysql.beginTransactionScope(function* (conn) {
Expand Down
62 changes: 43 additions & 19 deletions README.zh-CN.md
Expand Up @@ -20,21 +20,19 @@
[download-image]: https://img.shields.io/npm/dm/egg-mysql.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-mysql

Aliyun rds client(support mysql portocal) for egg framework
MySQL 插件是为 egg 提供 MySQL 数据库访问的功能

## Install
此插件基于 [ali-rds](https://github.com/ali-sdk/ali-rds) 实现一个简单的配置封装,具体使用方法你还需要阅读 [ali-rds] 的文档。

## 安装

```bash
$ npm i egg-mysql --save
```

MySQL Plugin 是为 egg 提供 MySQL 数据库访问的功能

此插件基于 [ali-rds](https://github.com/ali-sdk/ali-rds) 实现一个简单的配置封装,具体使用方法你还需要阅读 [ali-rds](https://github.com/ali-sdk/ali-rds) 的文档。

## 配置

修改 `${app_root}/config/plugin.js` 启动 MySQL 插件:
通过 `config/plugin.js` 配置启动 MySQL 插件:

```js
exports.mysql = {
Expand All @@ -43,24 +41,24 @@ exports.mysql = {
};
```

在 `${app_root}/config/config.default.js` 配置数据库相关的信息
在 `config/config.${env}.js` 配置各个环境的数据库连接信息

### 单数据源

```js
exports.mysql = {
// 数据库信息配置
// 单数据库信息配置
client: {
// host
host: 'mysql.com',
// 端口号
port: '3306',
// 用户名
user: 'mobile_pub',
user: 'test_user',
// 密码
password: 'password',
password: 'test_password',
// 数据库名
database: 'mobile_pub',
database: 'test',
},
// 是否加载到 app 上,默认开启
app: true,
Expand All @@ -75,24 +73,23 @@ exports.mysql = {
app.mysql.query(sql, values); // 单实例可以直接通过 app.mysql 访问
```


### 多数据源

```js
exports.mysql = {
clients: {
// clientId, 获取client实例,需要通过 app.mysql.get('clientId') 获取
mypay1: {
db1: {
// host
host: 'mysql.com',
// 端口号
port: '3306',
// 用户名
user: 'mobile_pub',
user: 'test_user',
// 密码
password: 'password',
password: 'test_password',
// 数据库名
database: 'mobile_pub',
database: 'test',
},
// ...
},
Expand All @@ -111,13 +108,37 @@ exports.mysql = {
使用方式:

```js
const client1 = app.mysql.get('client1');
const client1 = app.mysql.get('db1');
client1.query(sql, values);

const client2 = app.mysql.get('client2');
const client2 = app.mysql.get('db2');
client2.query(sql, values);
```

## 扩展

### app.js

#### app.mysql

如果开启了 `config.mysql.app = true`,则会在 app 上注入 [ali-rds] 客户端 的 [Singleton 单例](https://github.com/eggjs/egg/blob/master/lib/core/singleton.js)。

```js
app.mysql.query(sql);
app.mysql.get('db1').query(sql);
```

### agent.js

#### agent.mysql

如果开启了 `config.mysql.agent = true`,则会在 agent 上注入 [ali-rds] 客户端 的 [Singleton 单例](https://github.com/eggjs/egg/blob/master/lib/core/singleton.js)。

```js
agent.mysql.query(sql);
agent.mysql.get('db1').query(sql);
```

## CRUD 使用指南

### Create
Expand Down Expand Up @@ -246,3 +267,6 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
## License

[MIT](LICENSE)


[ali-rds]: https://github.com/ali-sdk/ali-rds
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -2,6 +2,7 @@ environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
8 changes: 4 additions & 4 deletions config/config.default.js
Expand Up @@ -5,19 +5,19 @@ exports.mysql = {
database: null,
connectionLimit: 5,
},
// 在 app 上初始化
app: true,
// 在 agent 上初始化
agent: false,
// 单数据库

// Single Database
// client: {
// host: 'host',
// port: 'port',
// user: 'user',
// password: 'password',
// database: 'database',
// },
// 多数据库

// Multi Databases
// clients: {
// db1: {
// host: 'host',
Expand Down
12 changes: 6 additions & 6 deletions lib/mysql.js
Expand Up @@ -6,23 +6,23 @@ const co = require('co');

let count = 0;

module.exports = function(app) {
app.addSingleton('mysql', createMysql);
module.exports = app => {
app.addSingleton('mysql', createOneClient);
};

function createMysql(config, app) {
function createOneClient(config, app) {
const index = count++;
const done = app.readyCallback(`createMysql-${index}`);
const done = app.readyCallback(`egg-mysql-createOneClient-${index}`);
app.coreLogger.info('[egg-mysql] connecting %s@%s:%s/%s',
config.user, config.host, config.port, config.database);
assert(config.host && config.port && config.user && config.database,
`[egg-mysql] 'host: ${config.host}', 'port: ${config.port}', 'user: ${config.user}', 'database: ${config.database}' are required on config`);

const client = rds(config);
client._superQuery = client.query;
client.query = function* () {
client.query = function* query() {
const sql = this.format.apply(this, arguments);
const ins = app.instrument('rds', sql);
const ins = app.instrument('egg-mysql', sql);
const result = yield this._superQuery.apply(this, arguments);
ins.end();
return result;
Expand Down
9 changes: 4 additions & 5 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "egg-mysql",
"version": "1.0.0",
"description": "mysql plugin for egg",
"description": "MySQL plugin for egg",
"eggPlugin": {
"name": "mysql"
},
Expand All @@ -15,13 +15,12 @@
},
"devDependencies": {
"autod": "^2.7.1",
"egg": "~0.1.0",
"egg": "^0.6.2",
"egg-bin": "^1.3.0",
"egg-mock": "^0.0.4",
"eslint": "^3.4.0",
"eslint-config-egg": "^3.1.0",
"pedding": "^1.0.0",
"should": "^11.1.0",
"supertest": "^2.0.0",
"utility": "^1.8.0"
},
Expand All @@ -32,12 +31,12 @@
"test": "npm run lint && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint --ext js . --fix",
"lint": "eslint --ext js .",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"ci": {
"version": "4, 6"
"version": "4, 6, 7"
},
"repository": {
"type": "git",
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions test/fixtures/apps/mysqlapp/app/controller/home.js
@@ -1,11 +1,10 @@
'use strict';

module.exports = function* () {
console.log(9999);
const users = yield this.service.user.list(this);

this.body = {
status: 'success',
users: users,
users,
};
};
1 change: 0 additions & 1 deletion test/mocha.opts

This file was deleted.