Created
June 17, 2019 21:12
-
-
Save B4nan/91cde4f99a88ee340dff79dec157d3b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const res = await fetch('api.example.com/book/123'); | |
| const book = res.json(); | |
| console.log(book.version); // prints the current version | |
| // user does some changes and calls the PUT handler | |
| const changes = { title: 'new title' }; | |
| await fetch('api.example.com/book/123', { | |
| method: 'PUT', | |
| body: { | |
| ...changes, | |
| version: book.version, | |
| }, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GET /book/:id | |
| async findOne(req, res) { | |
| const book = await this.em.findOne(Book, +req.query.id); | |
| res.json(book); | |
| } | |
| // PUT /book/:id | |
| async update(req, res) { | |
| const book = await this.em.findOne(Book, +req.query.id, { lockMode: LockMode.OPTIMISTIC, lockVersion: req.body.version }); | |
| book.assign(req.body); | |
| await this.em.flush(); | |
| res.json(book); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment