Skip to content

Commit 4137cfc

Browse files
committed
📖 DOC: Use async/await isntead of yield
1 parent b103400 commit 4137cfc

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ client2.query(sql, values);
116116

117117
```js
118118
// insert
119-
const result = yield app.mysql.insert('posts', { title: 'Hello World' });
119+
const result = await app.mysql.insert('posts', { title: 'Hello World' });
120120
const insertSuccess = result.affectedRows === 1;
121121
```
122122

123123
### Read
124124

125125
```js
126126
// get
127-
const post = yield app.mysql.get('posts', { id: 12 });
127+
const post = await app.mysql.get('posts', { id: 12 });
128128
// query
129-
const results = yield app.mysql.select('posts',{
129+
const results = await app.mysql.select('posts',{
130130
where: { status: 'draft' },
131131
orders: [['created_at','desc'], ['id','desc']],
132132
limit: 10,
@@ -144,14 +144,14 @@ const row = {
144144
otherField: 'other field value',
145145
modifiedAt: app.mysql.literals.now, // `now()` on db server
146146
};
147-
const result = yield app.mysql.update('posts', row);
147+
const result = await app.mysql.update('posts', row);
148148
const updateSuccess = result.affectedRows === 1;
149149
```
150150

151151
### Delete
152152

153153
```js
154-
const result = yield app.mysql.delete('table-name', {
154+
const result = await app.mysql.delete('table-name', {
155155
name: 'fengmk2'
156156
});
157157
```
@@ -164,32 +164,32 @@ const result = yield app.mysql.delete('table-name', {
164164
- disadventage: more handwritten code, Forgot catching error or cleanup will lead to serious bug.
165165

166166
```js
167-
const conn = yield app.mysql.beginTransaction();
167+
const conn = await app.mysql.beginTransaction();
168168

169169
try {
170-
yield conn.insert(table, row1);
171-
yield conn.update(table, row2);
172-
yield conn.commit();
170+
await conn.insert(table, row1);
171+
await conn.update(table, row2);
172+
await conn.commit();
173173
} catch (err) {
174174
// error, rollback
175-
yield conn.rollback(); // rollback call won't throw err
175+
await conn.rollback(); // rollback call won't throw err
176176
throw err;
177177
}
178178
```
179179

180180
### Automatic control: Transaction with scope
181181

182-
- API:`*beginTransactionScope(scope, ctx)`
182+
- API:`async beginTransactionScope(scope, ctx)`
183183
- `scope`: A generatorFunction which will execute all sqls of this transaction.
184184
- `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.
185185
- adventage: easy to use, as if there is no transaction in your code.
186186
- disadvantage: all transation will be successful or failed, cannot control precisely
187187

188188
```js
189-
const result = yield app.mysql.beginTransactionScope(function* (conn) {
189+
const result = await app.mysql.beginTransactionScope(async (conn) => {
190190
// don't commit or rollback by yourself
191-
yield conn.insert(table, row1);
192-
yield conn.update(table, row2);
191+
await conn.insert(table, row1);
192+
await conn.update(table, row2);
193193
return { success: true };
194194
}, ctx); // ctx is the context of current request, access by `this.ctx`.
195195
// if error throw on scope, will auto rollback
@@ -200,7 +200,7 @@ const result = yield app.mysql.beginTransactionScope(function* (conn) {
200200
### Custom SQL splicing
201201

202202
```js
203-
const results = yield app.mysql.query('update posts set hits = (hits + ?) where id = ?', [1, postId]);
203+
const results = await app.mysql.query('update posts set hits = (hits + ?) where id = ?', [1, postId]);
204204
```
205205

206206
### Literal
@@ -211,7 +211,7 @@ If you want to call literals or functions in mysql , you can use `Literal`.
211211
- NOW(): The database system time, you can obtain by `app.mysql.literals.now`.
212212

213213
```js
214-
yield app.mysql.insert(table, {
214+
await app.mysql.insert(table, {
215215
create_time: app.mysql.literals.now
216216
});
217217

@@ -226,7 +226,7 @@ The following demo showed how to call `CONCAT(s1, ...sn)` funtion in mysql to do
226226
const Literal = app.mysql.literals.Literal;
227227
const first = 'James';
228228
const last = 'Bond';
229-
yield app.mysql.insert(table, {
229+
await app.mysql.insert(table, {
230230
id: 123,
231231
fullname: new Literal(`CONCAT("${first}", "${last}"`),
232232
});

README.zh-CN.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ agent.mysql.get('db1').query(sql);
137137

138138
```js
139139
// 插入
140-
const result = yield app.mysql.insert('posts', { title: 'Hello World' });
140+
const result = await app.mysql.insert('posts', { title: 'Hello World' });
141141
const insertSuccess = result.affectedRows === 1;
142142
```
143143

144144
### Read
145145

146146
```js
147147
// 获得一个
148-
const post = yield app.mysql.get('posts', { id: 12 });
148+
const post = await app.mysql.get('posts', { id: 12 });
149149
// 查询
150-
const results = yield app.mysql.select('posts',{
150+
const results = await app.mysql.select('posts',{
151151
where: { status: 'draft' },
152152
orders: [['created_at','desc'], ['id','desc']],
153153
limit: 10,
@@ -165,14 +165,14 @@ const row = {
165165
otherField: 'other field value',
166166
modifiedAt: app.mysql.literals.now, // `now()` on db server
167167
};
168-
const result = yield app.mysql.update('posts', row);
168+
const result = await app.mysql.update('posts', row);
169169
const updateSuccess = result.affectedRows === 1;
170170
```
171171

172172
### Delete
173173

174174
```js
175-
const result = yield app.mysql.delete('table-name', {
175+
const result = await app.mysql.delete('table-name', {
176176
name: 'fengmk2'
177177
});
178178
```
@@ -185,32 +185,32 @@ const result = yield app.mysql.delete('table-name', {
185185
- 缺点:手写代码比较多,不是每个人都能写好。忘记了捕获异常和 cleanup 都会导致严重 bug。
186186

187187
```js
188-
const conn = yield app.mysql.beginTransaction();
188+
const conn = await app.mysql.beginTransaction();
189189

190190
try {
191-
yield conn.insert(table, row1);
192-
yield conn.update(table, row2);
193-
yield conn.commit();
191+
await conn.insert(table, row1);
192+
await conn.update(table, row2);
193+
await conn.commit();
194194
} catch (err) {
195195
// error, rollback
196-
yield conn.rollback(); // rollback call won't throw err
196+
await conn.rollback(); // rollback call won't throw err
197197
throw err;
198198
}
199199
```
200200

201201
### 自动控制:Transaction with scope
202202

203-
- API:`*beginTransactionScope(scope, ctx)`
203+
- API:`async beginTransactionScope(scope, ctx)`
204204
- `scope`: 一个 generatorFunction,在这个函数里面执行这次事务的所有 sql 语句。
205205
- `ctx`: 当前请求的上下文对象,传入 ctx 可以保证即便在出现事务嵌套的情况下,一次请求中同时只有一个激活状态的事务。
206206
- 优点:使用简单,不容易犯错,就感觉事务不存在的样子。
207207
- 缺点:整个事务要么成功,要么失败,无法做细粒度控制。
208208

209209
```js
210-
const result = yield app.mysql.beginTransactionScope(function* (conn) {
210+
const result = await app.mysql.beginTransactionScope(async (conn) => {
211211
// don't commit or rollback by yourself
212-
yield conn.insert(table, row1);
213-
yield conn.update(table, row2);
212+
await conn.insert(table, row1);
213+
await conn.update(table, row2);
214214
return { success: true };
215215
}, ctx); // ctx 是当前请求的上下文,如果是在 service 文件中,可以从 `this.ctx` 获取到
216216
// if error throw on scope, will auto rollback
@@ -220,7 +220,7 @@ const result = yield app.mysql.beginTransactionScope(function* (conn) {
220220

221221
### 自定义SQL拼接
222222
```js
223-
const results = yield app.mysql.query('update posts set hits = (hits + ?) where id = ?', [1, postId]);
223+
const results = await app.mysql.query('update posts set hits = (hits + ?) where id = ?', [1, postId]);
224224
```
225225

226226
### 表达式(Literal)
@@ -230,7 +230,7 @@ const results = yield app.mysql.query('update posts set hits = (hits + ?) where
230230
- NOW(): 数据库当前系统时间,通过`app.mysql.literals.now`获取。
231231

232232
```js
233-
yield app.mysql.insert(table, {
233+
await app.mysql.insert(table, {
234234
create_time: app.mysql.literals.now
235235
});
236236

@@ -244,7 +244,7 @@ yield app.mysql.insert(table, {
244244
const Literal = app.mysql.literals.Literal;
245245
const first = 'James';
246246
const last = 'Bond';
247-
yield app.mysql.insert(table, {
247+
await app.mysql.insert(table, {
248248
id: 123,
249249
fullname: new Literal(`CONCAT("${first}", "${last}"`),
250250
});
@@ -272,4 +272,4 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
272272

273273
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Jun 03 2022 20:59:40 GMT+0800`.
274274

275-
<!-- GITCONTRIBUTOR_END -->
275+
<!-- GITCONTRIBUTOR_END -->

0 commit comments

Comments
 (0)