docs: example for custom id when mysql update (#2165)

This commit is contained in:
OnedayLiu 2018-03-26 11:18:57 +08:00 committed by Haoliang Gao
parent 10327e1850
commit a86334c595
2 changed files with 38 additions and 0 deletions

View File

@ -233,6 +233,25 @@ const result = await this.app.mysql.update('posts', row); // update records in '
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ;
// check if update is success or failure
const updateSuccess = result.affectedRows === 1;
// if primary key is your custom id,such as custom_id,you should config it in `where`
const row = {
name: 'fengmk2',
otherField: 'other field value', // any other fields u want to update
modifiedAt: this.app.mysql.literals.now, // `now()` on db server
};
const options = {
where: {
custom_id: 456
}
};
const result = await this.app.mysql.update('posts', row, options); // update records in 'posts'
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ;
// check if update is success or failure
const updateSuccess = result.affectedRows === 1;
```

View File

@ -249,6 +249,25 @@ const result = await this.app.mysql.update('posts', row); // 更新 posts 表中
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ;
// 判断更新成功
const updateSuccess = result.affectedRows === 1;
// 如果主键是自定义的 ID 名称,如 custom_id则需要在 `where` 里面配置
const row = {
name: 'fengmk2',
otherField: 'other field value', // any other fields u want to update
modifiedAt: this.app.mysql.literals.now, // `now()` on db server
};
const options = {
where: {
custom_id: 456
}
};
const result = await this.app.mysql.update('posts', row, options); // 更新 posts 表中的记录
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ;
// 判断更新成功
const updateSuccess = result.affectedRows === 1;
```