@@ -137,17 +137,17 @@ agent.mysql.get('db1').query(sql);
137
137
138
138
``` js
139
139
// 插入
140
- const result = yield app .mysql .insert (' posts' , { title: ' Hello World' });
140
+ const result = await app .mysql .insert (' posts' , { title: ' Hello World' });
141
141
const insertSuccess = result .affectedRows === 1 ;
142
142
```
143
143
144
144
### Read
145
145
146
146
``` js
147
147
// 获得一个
148
- const post = yield app .mysql .get (' posts' , { id: 12 });
148
+ const post = await app .mysql .get (' posts' , { id: 12 });
149
149
// 查询
150
- const results = yield app .mysql .select (' posts' ,{
150
+ const results = await app .mysql .select (' posts' ,{
151
151
where: { status: ' draft' },
152
152
orders: [[' created_at' ,' desc' ], [' id' ,' desc' ]],
153
153
limit: 10 ,
@@ -165,14 +165,14 @@ const row = {
165
165
otherField: ' other field value' ,
166
166
modifiedAt: app .mysql .literals .now , // `now()` on db server
167
167
};
168
- const result = yield app .mysql .update (' posts' , row);
168
+ const result = await app .mysql .update (' posts' , row);
169
169
const updateSuccess = result .affectedRows === 1 ;
170
170
```
171
171
172
172
### Delete
173
173
174
174
``` js
175
- const result = yield app .mysql .delete (' table-name' , {
175
+ const result = await app .mysql .delete (' table-name' , {
176
176
name: ' fengmk2'
177
177
});
178
178
```
@@ -185,32 +185,32 @@ const result = yield app.mysql.delete('table-name', {
185
185
- 缺点:手写代码比较多,不是每个人都能写好。忘记了捕获异常和 cleanup 都会导致严重 bug。
186
186
187
187
``` js
188
- const conn = yield app .mysql .beginTransaction ();
188
+ const conn = await app .mysql .beginTransaction ();
189
189
190
190
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 ();
194
194
} catch (err) {
195
195
// error, rollback
196
- yield conn .rollback (); // rollback call won't throw err
196
+ await conn .rollback (); // rollback call won't throw err
197
197
throw err;
198
198
}
199
199
```
200
200
201
201
### 自动控制:Transaction with scope
202
202
203
- - API:` * beginTransactionScope(scope, ctx)`
203
+ - API:` async beginTransactionScope(scope, ctx)`
204
204
- ` scope ` : 一个 generatorFunction,在这个函数里面执行这次事务的所有 sql 语句。
205
205
- ` ctx ` : 当前请求的上下文对象,传入 ctx 可以保证即便在出现事务嵌套的情况下,一次请求中同时只有一个激活状态的事务。
206
206
- 优点:使用简单,不容易犯错,就感觉事务不存在的样子。
207
207
- 缺点:整个事务要么成功,要么失败,无法做细粒度控制。
208
208
209
209
``` js
210
- const result = yield app .mysql .beginTransactionScope (function * (conn ) {
210
+ const result = await app .mysql .beginTransactionScope (async (conn ) => {
211
211
// 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);
214
214
return { success: true };
215
215
}, ctx); // ctx 是当前请求的上下文,如果是在 service 文件中,可以从 `this.ctx` 获取到
216
216
// if error throw on scope, will auto rollback
@@ -220,7 +220,7 @@ const result = yield app.mysql.beginTransactionScope(function* (conn) {
220
220
221
221
### 自定义SQL拼接
222
222
``` 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]);
224
224
```
225
225
226
226
### 表达式(Literal)
@@ -230,7 +230,7 @@ const results = yield app.mysql.query('update posts set hits = (hits + ?) where
230
230
- NOW(): 数据库当前系统时间,通过` app.mysql.literals.now ` 获取。
231
231
232
232
``` js
233
- yield app .mysql .insert (table, {
233
+ await app .mysql .insert (table, {
234
234
create_time: app .mysql .literals .now
235
235
});
236
236
@@ -244,7 +244,7 @@ yield app.mysql.insert(table, {
244
244
const Literal = app .mysql .literals .Literal ;
245
245
const first = ' James' ;
246
246
const last = ' Bond' ;
247
- yield app .mysql .insert (table, {
247
+ await app .mysql .insert (table, {
248
248
id: 123 ,
249
249
fullname: new Literal (` CONCAT("${ first} ", "${ last} "` ),
250
250
});
@@ -272,4 +272,4 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
272
272
273
273
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 ` .
274
274
275
- <!-- GITCONTRIBUTOR_END -->
275
+ <!-- GITCONTRIBUTOR_END -->
0 commit comments