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
| 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
| 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
| 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
| 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
| // 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); |
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
| 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'], |
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 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 |
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 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 |
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
| # 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 |