{"id":21553158,"url":"https://github.com/char2sgu/mikro-orm-soft-delete","last_synced_at":"2025-04-10T07:50:18.635Z","repository":{"id":57297067,"uuid":"446054300","full_name":"Char2sGu/mikro-orm-soft-delete","owner":"Char2sGu","description":"Generic soft delete solution for MikroORM.","archived":false,"fork":false,"pushed_at":"2024-01-31T16:58:52.000Z","size":738,"stargazers_count":32,"open_issues_count":5,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T08:04:20.446Z","etag":null,"topics":["mikroorm","soft-delete","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Char2sGu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-01-09T10:19:24.000Z","updated_at":"2024-09-18T09:33:07.000Z","dependencies_parsed_at":"2024-04-08T01:59:52.574Z","dependency_job_id":null,"html_url":"https://github.com/Char2sGu/mikro-orm-soft-delete","commit_stats":null,"previous_names":["char2sgu/mikro-orm-soft-delete","thenightmarex/mikro-orm-soft-delete"],"tags_count":4,"template":false,"template_full_name":"Char2sGu/typescript-package-boilerplate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Char2sGu%2Fmikro-orm-soft-delete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Char2sGu%2Fmikro-orm-soft-delete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Char2sGu%2Fmikro-orm-soft-delete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Char2sGu%2Fmikro-orm-soft-delete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Char2sGu","download_url":"https://codeload.github.com/Char2sGu/mikro-orm-soft-delete/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248182010,"owners_count":21060891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mikroorm","soft-delete","typescript"],"created_at":"2024-11-24T07:09:42.260Z","updated_at":"2025-04-10T07:50:18.615Z","avatar_url":"https://github.com/Char2sGu.png","language":"TypeScript","readme":"# Mikro ORM Soft Delete\n\nThe declarative soft-delete solution for MikroORM.\n\n```\nnpm i mikro-orm-soft-delete\n```\n\n\u003e Inspired by: https://github.com/mikro-orm/mikro-orm/issues/1492#issuecomment-785394397\n\n## Compatibilities\n\n| Library Version | ORM Version |\n| --------------- | ----------- |\n| v1.x.x          | v6.x.x      |\n| v0.x.x          | v5.x.x      |\n\n## Migrating to v1\n\n- It is now mandatory to register `SoftDeleteHandler` as an extension for this library to work. See [Initialization](#initialization) for details.\n- Base entities no longer accept any generic type parameters.\n\n## Tutorial\n\n### Initialization\n\nTo enable soft-delete for your `MikroORM` instance, register `SoftDeleteHandler` as an [extension](https://mikro-orm.io/docs/configuration#extensions) in the initialization config:\n\n```ts\nimport { SoftDeleteHandler } from \"mikro-orm-soft-delete\";\n\nawait MikroORM.init({\n  // ...\n  extensions: [SoftDeleteHandler],\n  // ...\n});\n```\n\n### Basics\n\nPut a `SoftDeletable` decorator on your entity definition to make it soft-deletable:\n\n```ts\nimport { SoftDeletable } from \"mikro-orm-soft-delete\";\n\n@SoftDeletable(() =\u003e User, \"deletedAt\", () =\u003e new Date())\n@Entity()\nexport class User extends BaseEntity {\n  @PrimaryKey()\n  id: number;\n\n  @Property({ nullable: true })\n  deletedAt?: Date;\n}\n```\n\nThe above code snippet means that:\n\n- A filter with conditions `{ deletedAt: null }` is applied to `User` and enabled by default, so that those soft-deleted entities will be excluded from your queries. This filter could be disabled by:\n  ```ts\n  import { SOFT_DELETABLE_FILTER } from \"mikro-orm-soft-delete\";\n  repo.find({ ... }, { filters: { [SOFT_DELETABLE_FILTER]: false } });\n  repo.find({ ... }, { filters: false }); // if you are sure that there are no other filters enabled\n  ```\n- When an deletion command is executed on a `User` entity, its `deletedAt` field will be set to a newly instantiated `Date`. You could find all `delete` statements replaced with `update` ones under MikroORM's debugging mode:\n  ```ts\n  repo.remove(user);\n  await repo.flush();\n  user.id !== undefined; // true\n  user.deletedAt instanceof Date; // true\n  ```\n- `cascade: [Cascade.Remove]` and `orphanRemoval: true` still work with `repo.remove()`. But you would have to avoid removing items from collections when using `orphanRemoval` as it's currently not possible to intercept deletions caused by these operations.\n\n### Config API\n\nAside from passing the parameters by positions, there is also an object-based API that accepts a config object instead:\n\n```ts\n@SoftDeletable({\n  type: () =\u003e User,\n  field: 'deletedAt',\n  value: () =\u003e new Date(),\n})\n```\n\n### Default Field Value\n\nBy default, a `null` value is used in the filter to exclude soft-deleted entities: `{ deletedAt: null }`. However, if the default value of the field is not `null`, the query would not work as we expected.\n\nFor example, when the field is `isDeleted` and the default value is `false`, the query `{ isDeleted: null }` would not match any entities.\n\nIn this case, an additional option `valueInitial` needs to be specified:\n\n```ts\n@SoftDeletable({\n  type: () =\u003e User,\n  field: 'isDeleted',\n  value: () =\u003e true,\n  valueInitial: false, // indicating that the default value of `isDeleted` is `false`.\n})\n```\n\n...which would make the query look like `{ isDeleted: false }` to find all the entities that is not soft-deleted.\n\nThis option could also be specified through the 4th argument:\n\n```ts\n@SoftDeletable(() =\u003e User, 'isDeleted', () =\u003e true, false)\n```\n\n### Inheritance\n\nInheritance is supported for the `SoftDeletable` decorator, thus it is possible to create a `SoftDeletableBaseEntity` to make all the sub entity classes soft-deletable:\n\n```ts\n@SoftDeletable(() =\u003e SoftDeletableBaseEntity, \"deletedAt\", () =\u003e new Date())\nexport abstract class SoftDeletableBaseEntity extends BaseEntity {\n  @Property({ nullable: true })\n  deletedAt?: Date;\n}\n```\n\n### Hard Deletions\n\nCurrently it's impossible to hard-delete an entity marked as soft-deletable. As a workaround, the native API could be used for hard-deletions:\n\n```ts\nem.nativeDelete(...);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchar2sgu%2Fmikro-orm-soft-delete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchar2sgu%2Fmikro-orm-soft-delete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchar2sgu%2Fmikro-orm-soft-delete/lists"}