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 mysql doc #234
docs: add mysql doc #234
Conversation
@mansonchor 来看看? |
|
||
# 访问 MySQL 实现数据库操作 | ||
|
||
在 WEB 应用方面 MySQL 是最常见,最好的关系型数据库之一。包括阿里巴巴在内,很多网站的开发都选择 MySQL 作为网站数据库。本篇文档介绍了如何使用 egg 框架及其插件来访问数据库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文档里面统一写 web
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
包括阿里巴巴在内,很多网站的开发都选择 MySQL 作为网站数据库。
去掉阿里巴巴好了。
非常多网站会选择 MySQL 作为网站数据库。
|
||
在 WEB 应用方面 MySQL 是最常见,最好的关系型数据库之一。包括阿里巴巴在内,很多网站的开发都选择 MySQL 作为网站数据库。本篇文档介绍了如何使用 egg 框架及其插件来访问数据库。 | ||
|
||
## egg-MySQL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
呃。。。这个就写 egg-mysql
|
||
## egg-MySQL | ||
|
||
我们提供了 [egg-MySQL](https://github.com/eggjs/egg-mysql)插件来访问 MySQL 数据库,这个插件既可以访问普通的 MySQL 数据库,也可以访问阿里云提供的在线数据库服务 [RDS](https://www.aliyun.com/product/rds?spm=5176.8142029.388261.35.puj0DS)(ApsaraDB for RDS,简称RDS)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要给阿里云打广告,去掉吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个插件既可以访问普通的 MySQL 数据库,也可以访问阿里云提供的在线数据库服务
这个插件既可以访问普通的 MySQL 数据库,也可以访问基于 MySQL 协议的在线数据库服务
|
||
## 安装与配置 | ||
|
||
### 安装插件 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
安装不需要写了,只需要提示在 package.json 里面依赖这个模块
// 数据库名 | ||
database: 'test', | ||
}, | ||
// ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
db2 的配置示例也加上
Current coverage is 97.54% (diff: 100%)
|
8f613ce
to
8c4745b
Compare
title: MySQL | ||
--- | ||
|
||
# 访问 MySQL 实现数据库操作 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
title
和 第一个 #
应该同名吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在不需要再正文中写标题了,所有的章节从 h2 开始
|
||
## egg-mysql | ||
|
||
我们提供了 [egg-mysql](https://github.com/eggjs/egg-mysql)插件来访问 MySQL 数据库。这个插件既可以访问普通的 MySQL 数据库,也可以访问基于 MySQL 协议的在线数据库服务。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件来访问
前面加个空格
|
||
## 安装与配置 | ||
|
||
在 package.json 里面依赖 egg-MySQL模块,并通过 `config/plugin.js` 配置启动 MySQL 插件: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg-MySQL
-> egg-mysql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议不要直接让用户写 pkg, 提供:
$ npm i egg-mysql --save
参考下 quickstart
|
||
下面是一个 service 中访问 MySQL数据库的例子。 | ||
|
||
更多 service 层的介绍,可以参考 egg 文档中的 service 一节。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg 文档中的 service 一节。
-> [service](../basics/service.md)
|
||
更多 service 层的介绍,可以参考 egg 文档中的 service 一节。 | ||
|
||
```javascript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
统一用 js
module.exports = app => { | ||
class User extends app.Service { | ||
// 默认不需要提供构造函数。 | ||
// constructor(ctx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段不需要了, 这里不是演示 service
|
||
* find(uid) { | ||
// 假如 我们拿到用户 id 从数据库获取用户详细信息 | ||
const user = this.app.mysql.query(`select * from user where uid = ${uid}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要 yield ?
|
||
之后可以通过 controller 获取 service 层拿到的数据。 | ||
|
||
```javascript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js
|
||
因此,对于一个事务来讲,一定伴随着 beginTransaction, commit 或 rollback ,分别代表事务的开始,成功和失败回滚。 | ||
|
||
egg-MySQL 提供了两种类型的事务。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg-mysql, 其他地方也查下
// 用户名 | ||
user: 'test_user', | ||
// 密码 | ||
password: 'test_password', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件好像有个 encryptPassword
属性,开源的这个还有保留么?
这对密码安全保证比较有好处
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mansonchor 去掉了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
const post = yield this.app.mysql.get('posts', { id: 12 }); // 在 post 表中,搜索 id 为 12 的记录 | ||
// 查询 | ||
const results = yield this.app.mysql.select('posts',{ // 搜索 post 表 | ||
where: { status: 'draft' }, // 条件 : status 为 draft |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉 Read
这个操作介绍有点简单,其实 select
这个方法下的每个字段都可以单独介绍一些最佳实践和例子
例如 where
,是支持 WHERE IN
语法糖的
// same like "WHERE status IN('draft', 'pass')"
where: { status: ['draft', 'pass'] }
不过不知道这种介绍是放到 egg-mysql 插件的文档里,还是放这里比较好? @fengmk2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应用层开发需要了解的知识都在这里写完
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那就在这里补上,如果今天来不及完善 我之后再补 PR
// if error throw on scope, will auto rollback | ||
``` | ||
|
||
## 乐观锁与悲观锁 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果不是框架本身控制这个事情
这个介绍是否没有必要可以去掉?
|
||
## 安装与配置 | ||
|
||
在 package.json 里面依赖 egg-MySQL模块,并通过 `config/plugin.js` 配置启动 MySQL 插件: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg-mysql 模块
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接写出来吧:
{
"dependencies": {
"egg-mysql": "^1.0.1"
}
}
|
||
* find(uid) { | ||
// 假如 我们拿到用户 id 从数据库获取用户详细信息 | ||
const user = this.app.mysql.query(`select * from user where uid = ${uid}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要在例子里面自己拼 sql!教坏新手
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
而且要在文档中强调一下,写 sql 时的注意事项,怎么样防止 sql 注入
}; | ||
``` | ||
|
||
## 异步处理 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
异步处理与 mysql 插件没有太大关系吧?
|
||
## 直接执行 sql 语句 | ||
|
||
插件本身也支持拼接与直接执行 sql 语句。使用 `query` 可以执行合法的 sql 语句。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
强调不允许自己拼 sql,有什么危害
// if error throw on scope, will auto rollback | ||
``` | ||
|
||
## 乐观锁与悲观锁 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
乐观锁和悲观锁没必要在这里说吧
@jtyjty99999 提的建议还在修改么? |
8c4745b
to
aa53511
Compare
|
||
我们提供了 [egg-mysql](https://github.com/eggjs/egg-mysql) 插件来访问 MySQL 数据库。这个插件既可以访问普通的 MySQL 数据库,也可以访问基于 MySQL 协议的在线数据库服务。 | ||
|
||
## 安装与配置 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### 安装与配置
}; | ||
``` | ||
|
||
在 `config/config.${env}.js` 配置各个环境的数据库连接信息: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
接信息:
接信息。
|
||
#### 单数据源 | ||
|
||
如果你的应用只需要访问一个 MySQL 数据库实例,可以如下配置: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果你的应用
如果我们的应用
|
||
#### 多数据源 | ||
|
||
如果你的应用需要访问多个 MySQL 数据源,可以按照如下配置: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
|
||
由于对 MySQL 数据库的访问操作属于 web 层中的数据处理层,因此我们强烈建议将这部分代码放在 service 层中维护。 | ||
|
||
下面是一个 service 中访问 MySQL数据库的例子。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MySQL 数据库的例子
少了一个空格
```js | ||
// app/service/user.js | ||
module.exports = app => { | ||
class User extends app.Service { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return class User extends app.Service {}
}; | ||
``` | ||
|
||
## 如何编写 crud 语句 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRUD
otherField: 'other field value', | ||
modifiedAt: this.app.mysql.literals.now, // `now()` on db server | ||
}; | ||
const result = yield this.app.mysql.update('posts', row); // 更新 posts 表中的记录 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
等价的 SQL 语句写出来吧,像上面那样
|
||
## 使用 ORM | ||
|
||
// 等待补充 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mansonchor @jtyjty99999 这些都补充上吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用ORM
和 使用DAO
不知道要写啥,不是我加这个栏目没概念
这个等 @jtyjty99999 补吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个我先去掉,后面再新开文档单独写 ORM 和 DAO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fengmk2 嗯,现在主要是配套插件还没跟上,等ok了再仔细写写这块
如果需要调用mysql内置的函数(或表达式),可以使用`Literal`。 | ||
|
||
#### 内置表达式 | ||
- NOW(): 数据库当前系统时间,通过`app.mysql.literals.now`获取。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOW(): 数据库当前系统时间,通过
app.mysql.literals.now
获取。
NOW()
:数据库当前系统时间,通过 app.mysql.literals.now
获取。
中英文标点不要混用,都是有中文标点符号
|
||
```js | ||
yield this.app.mysql.insert(table, { | ||
create_time: this.app.mysql.literals.now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_time: this.app.mysql.literals.now,
少了逗号
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果对象末行规范加 逗号的话,我统一扫一遍加下
``` | ||
|
||
#### 自定义表达式 | ||
下例展示了如何调用mysql内置的`CONCAT(s1, ...sn)`函数,做字符串拼接。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
的
CONCAT(s1, ...sn)
函
需要加上空格
fb3d561
to
a4bd002
Compare
@fengmk2 再看看 |
rebase master |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
我将 todo 的去掉合并。
Checklist
npm test
passesAffected core subsystem(s)
Description of change