mikro-orm-plus
v1.0.0
Published
Some useful features for MikroORM
Downloads
9
Readme
mikro-orm-plus
Some useful features for MikroORM
Installation
$ npm install mikro-orm-plus
$ yarn add mikro-orm-plus
$ pnpm install mikro-orm-plus
@CreateDateProperty
Special property that is automatically set to the entity's insertion time. You don't need to write a value into this property - it will be automatically set.
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { CreateDateProperty } from 'mikro-orm-plus';
@Entity()
export class User {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@CreateDateProperty()
createdAt: Date;
}
@UpdateDateProperty
Special property that is automatically set to the entity's update time. You don't need to write a value into this property - it will be automatically set.
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { UpdateDateProperty } from 'mikro-orm-plus';
@Entity()
export class User {
@PrimaryKey()
id: number;
@Property()
name: string;
@UpdateDateProperty()
updatedAt: Date;
}
@DeleteDateProperty
Special property that is automatically set to the entity's delete time. You don't need to write a value into this property - it will be automatically set.
This feature is base on mikro-orm-soft-delete, modified as a property decorator implementation.
Soft deletable entity define:
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { DeleteDateProperty } from 'mikro-orm-plus';
@Entity()
export class User {
@PrimaryKey()
id: number;
@Property()
name: string;
@DeleteDateProperty()
deletedAt: Date;
}
how to softly delete?
Consistent with mikroOrm's api, just use em.remove()
or em.removeAndFlush()
to delete entity.
await em.removeAndFlush(user);
How to find soft deleted entity?
You need disabled soft delete filter, and then you can find soft deleted entity.
em.find(User, {...}, { filters: { [SOFT_DELETABLE_FILTER]: false } });
em.find(User, {...}, { filters: false }); // if you are sure that there are no other filters enabled
How to hard delete entity?
You can use em.nativeDelete()
to hard delete entity.