Skip to content

Instantly share code, notes, and snippets.

View B4nan's full-sized avatar

Martin Adámek B4nan

View GitHub Profile
@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 / 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 / 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 / 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-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 / working-with-collections.ts
Created February 26, 2019 19:39
Working with collections in MikroORM
// find author and populate his books collection
const author = orm.em.findOne(Author, '...', ['books']);
for (const book of author.books) {
console.log(book); // instance of Book
}
author.books.add(book);
console.log(author.books.contains(book)); // true
author.books.remove(book);
@B4nan
B4nan / using-repository.ts
Last active April 30, 2019 16:24
Using entity repository in MikroORM
import { QueryOrder } from 'mikro-orm';
const booksRepository = orm.em.getRepository(Book);
// with sorting, limit and offset parameters, populating author references
const books = await booksRepository.find({ author: '...' }, ['author'], { title: QueryOrder.DESC }, 2, 1);
// or with options object
const books = await booksRepository.find({ author: '...' }, {
populate: ['author'],
@B4nan
B4nan / entity-references.ts
Last active April 30, 2019 16:24
Using entity references in MikroORM
const book = orm.em.findOne(Book, '...');
console.log(book.author); // reference with ID only, instance of Author entity
// this will get the same reference as we already have in `book.author`
const author = orm.em.getReference(Author, book.author.id);
console.log(author.id); // accessing the id will not trigger any db call
console.log(author.isInitialized()); // false
console.log(author.name); // undefined
console.log(author === book.author); // true
@B4nan
B4nan / identity-map.ts
Last active April 30, 2019 16:24
Usage of Identity Map in MikroORM
const authorRepository = orm.em.getRepository(Author);
const jon = await authorRepository.findOne({ name: 'Jon Snow' }, ['books']);
const jon2 = await authorRepository.findOne({ email: '[email protected]' });
const authors = await authorRepository.findAll(['books']);
// identity map in action
console.log(jon === authors[0]); // true
console.log(jon === jon2); // true
// as we always have one instance, books will be populated also here
@B4nan
B4nan / 01-installation.sh
Last active April 30, 2019 16:23
Installation of MikroORM
# using yarn
$ yarn add mikro-orm mongodb # for mongo
$ yarn add mikro-orm mysql2 # for mysql
$ yarn add mikro-orm pg # for postgresql
$ yarn add mikro-orm sqlite # for sqlite
# or npm
$ npm i -s mikro-orm mongodb # for mongo
$ npm i -s mikro-orm mysql2 # for mysql
$ npm i -s mikro-orm pg # for postgresql