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 = await orm.em.findOne(Book, { | |
| author: { | |
| name: 'Jon Snow', | |
| address: { | |
| street: 'Downing Street', | |
| }, | |
| }, | |
| }, ['author.address']); | |
| console.log(book.author.name); // 'Jon Snow' |
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
| @Entity() | |
| export class User implements IdEntity<User> { | |
| @PrimaryKey() | |
| id!: number; | |
| @Property() | |
| name!: string; | |
| @OneToOne() |
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 { Type, Platform, EntityProperty, ValidationError } from 'mikro-orm'; | |
| export class DateType extends Type { | |
| convertToDatabaseValue(value: any, platform: Platform): any { | |
| return value.toISOString().substr(0, 10); | |
| } | |
| convertToJSValue(value: any, platform: Platform): any { | |
| return new Date(value); |
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
| { | |
| "name": "your-app", | |
| "dependencies": { ... }, | |
| "mikro-orm": { | |
| "useTsNode": true, | |
| "configPaths": [ | |
| "./src/mikro-orm.config.ts", | |
| "./dist/mikro-orm.config.js" | |
| ] | |
| } |
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
| $ npx mikro-orm | |
| Usage: mikro-orm <command> [options] | |
| Commands: | |
| mikro-orm cache:clear Clear metadata cache | |
| mikro-orm cache:generate Generate metadata cache for production | |
| mikro-orm generate-entities Generate entities based on current database | |
| schema | |
| mikro-orm database:import <file> Imports the SQL file to the database | |
| mikro-orm schema:create Create database schema based on current |
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 |
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
| 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 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
| export class User { | |
| // ... | |
| @Property({ version: true }) | |
| version: number; | |
| // ... | |
| } |