Coder Social home page Coder Social logo

jedliu / mikro-orm-soft-delete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thenightmarex/mikro-orm-soft-delete

0.0 0.0 0.0 121 KB

Generic soft delete solution for MikroORM.

License: Apache License 2.0

JavaScript 20.91% TypeScript 79.09%

mikro-orm-soft-delete's Introduction

Mikro ORM Soft Delete

Generic soft delete solution for MikroORM.

npm i mikro-orm-soft-delete

Inspired by: mikro-orm/mikro-orm#1492 (comment)

Tutorial

Basic

It is so simple to enable soft delete with this package that you only need one example to understand what's going on:

@SoftDeletable(() => User, "deletedAt", () => new Date())
@Entity()
export class User extends BaseEntity<User, "id"> {
  @PrimaryKey()
  id: number;

  @Property({ nullable: true })
  deletedAt?: Date;
}

This means that:

  • A filter with conditions { deletedAt: null } has been defined on User and enabled by default, so that those deleted entities will be filtered out by default. The filter can be disabled by:
    repo.find({ ... }, { filters: { [SOFT_DELETABLE_FILTER]: false } });
    repo.find({ ... }, { filters: false }); // if you are sure that there are no other filters enabled
  • When you try to delete a User entity, it will not be actually deleted from the database, and its deletedAt property will be set to a newly instantiated Date. You can find that delete statements are replaced with update ones with MikroORM's debug mode on.
    repo.remove(user);
    await repo.flush();
    user.id !== undefined; // true
    user.deletedAt === true; // true
  • cascade: [Cascade.Remove] and orphanRemoval: true still work fine with repo.remove(). But you must avoid removing items from collections when using orphanRemoval because we cannot catch the deletions caused by it.

Inheritance

If you want all your entities to be soft deletable, you can create a SoftDeletableBaseEntity and make all your other entity classes extend it:

@SoftDeletable(() => SoftDeletableBaseEntity, "deletedAt", () => new Date())
export abstract class SoftDeletableBaseEntity<
  T,
  PK extends keyof T,
> extends BaseEntity<T, PK> {
  @Property({ nullable: true })
  deletedAt?: Date;
}

Hard Deleting

Currently it's impossible to perform perfect hard deletes. As a workaround, we can hard delete entities using the native API:

em.nativeDelete(...);

Known Issues

mikro-orm-soft-delete's People

Contributors

thenightmarex avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.