Skip to content

Instantly share code, notes, and snippets.

@B4nan
Created June 17, 2019 21:12
Show Gist options
  • Select an option

  • Save B4nan/91cde4f99a88ee340dff79dec157d3b0 to your computer and use it in GitHub Desktop.

Select an option

Save B4nan/91cde4f99a88ee340dff79dec157d3b0 to your computer and use it in GitHub Desktop.
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,
},
});
// 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