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 user = new User(); | |
| user.name = 'George'; | |
| await orm.em.persistAndFlush(user); |
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 author = await orm.em.findOne(Author, id, ['books.tags', 'books.publisher']); | |
| author.books[0].title = 'New book name'; | |
| author.books[0].tags[0].name = 'old'; | |
| author.books[0].tags.add(new BookTag('sale')); | |
| author.books[0].publisher.name = 'New publisher name'; | |
| await orm.em.flush(); |
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
| START TRANSACTION; | |
| INSERT INTO `book_tag` (`name`) VALUES (?); | |
| UPDATE `book` SET `title` = ? WHERE `id` = ?; | |
| DELETE FROM `book_to_book_tag` WHERE `book_id` = ?; | |
| INSERT INTO `book_to_book_tag` (`book_id`, `book_tag_id`) VALUES (?, ?); | |
| INSERT INTO `book_to_book_tag` (`book_id`, `book_tag_id`) VALUES (?, ?); | |
| UPDATE `publisher` SET `name` = ? WHERE `id` = ?; | |
| UPDATE `book_tag` SET `name` = ? WHERE `id` = ?; | |
| COMMIT; |
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
| await orm.em.beginTransaction(); | |
| try { | |
| //... do some work | |
| const user = new User(...); | |
| user.name = 'George'; | |
| await orm.em.persistAndFlush(user); | |
| await orm.em.commit(); | |
| } catch (e) { | |
| await orm.em.rollback(); |
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
| await orm.em.transactional(async _em => { | |
| //... do some work | |
| const user = new User(...); | |
| user.name = 'George'; | |
| _em.persistLater(user); | |
| }); |
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
| export class User { | |
| // ... | |
| @Property({ version: true }) | |
| version: number; | |
| // ... | |
| } |
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 theEntityId = 1; | |
| const expectedVersion = 184; | |
| try { | |
| const entity = await orm.em.findOne(User, theEntityId, { lockMode: LockMode.OPTIMISTIC, lockVersion: expectedVersion }); | |
| // do the work | |
| await orm.em.flush(); | |
| } catch (e) { | |
| console.log('Sorry, but someone else has already changed this entity. Please apply the changes again!'); | |
| } |
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 theEntityId = 1; | |
| const expectedVersion = 184; | |
| const entity = await orm.em.findOne(User, theEntityId); | |
| try { | |
| // assert version | |
| await orm.em.lock(entity, LockMode.OPTIMISTIC, expectedVersion); | |
| } catch (e) { | |
| console.log('Sorry, but someone else has already changed this entity. Please apply the changes again!'); | |
| } |
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, |
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
| await em.transactional(async _em => { | |
| await _em.findOne(Author, id, { lockMode: LockMode.PESSIMISTIC_WRITE }); | |
| }); | |
| // START TRANSACTION | |
| // SELECT `e0`.* FROM `author` AS `e0` WHERE `e0`.`id` = ? FOR UPDATE | |
| // COMMIT |