Skip to content

Instantly share code, notes, and snippets.

View B4nan's full-sized avatar

Martin Adámek B4nan

View GitHub Profile
@B4nan
B4nan / query-conditions.ts
Last active January 13, 2020 12:06
MikroORM 3: complex query with auto-join
const book = await orm.em.findOne(Book, {
author: {
name: 'Jon Snow',
address: {
street: 'Downing Street',
},
},
}, ['author.address']);
console.log(book.author.name); // 'Jon Snow'
@B4nan
B4nan / user.ts
Created January 13, 2020 11:43
MikroORM 3: entity definition
@Entity()
export class User implements IdEntity<User> {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@OneToOne()
@B4nan
B4nan / DateType.ts
Last active January 13, 2020 10:39
MikroORM 3: Custom Types
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);
@B4nan
B4nan / package.json
Created January 13, 2020 09:53
MikroORM CLI setup
{
"name": "your-app",
"dependencies": { ... },
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/mikro-orm.config.js"
]
}
@B4nan
B4nan / cli.sh
Created January 13, 2020 09:51
MikroORM CLI
$ 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
@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
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 / 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!');
}
@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 / 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;
// ...
}