Skip to content

Instantly share code, notes, and snippets.

View B4nan's full-sized avatar

Martin Adámek B4nan

View GitHub Profile
@B4nan
B4nan / implicit-transaction-simple.ts
Last active June 16, 2019 17:23
Simple example of implicit transactions in MikroORM
const user = new User();
user.name = 'George';
await orm.em.persistAndFlush(user);
@B4nan
B4nan / implicit-transaction-complex.ts
Created June 16, 2019 16:56
More complex example of implicit transaction in MikroORM
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();
@B4nan
B4nan / implicit-transaction-complex.sql
Created June 16, 2019 16:57
Actual SQL queries ran when flushing in implicit-transaction-complex.ts example in MikroORM
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;
@B4nan
B4nan / explicit-transaction-manual.ts
Created June 16, 2019 17:00
Explicit transaction handling in MikroORM
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();
@B4nan
B4nan / explicit-transaction-helper.ts
Last active June 17, 2019 18:51
Explicit transactions via EM.transactional() in MikroORM
await orm.em.transactional(async _em => {
//... do some work
const user = new User(...);
user.name = 'George';
_em.persistLater(user);
});
@B4nan
B4nan / 01-version-prop-int.ts
Last active June 17, 2019 19:24
Defining version properties for optimistic locking in MikroORM
export class User {
// ...
@Property({ version: true })
version: number;
// ...
}
@B4nan
B4nan / find-one-optimistic-lock.ts
Last active June 17, 2019 21:56
Using optimistic lock via em.findOne() in MikroORM
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!');
}
@B4nan
B4nan / em-lock.ts
Created June 17, 2019 19:43
Using optimistic lock via em.lock() in MikroORM
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!');
}
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,
@B4nan
B4nan / 01-pessimistic-lock-write.ts
Last active June 17, 2019 21:27
Perssimistic locking in MikroORM
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