Registry / database / mikro-orm-soft-delete

mikro-orm-soft-delete

JSON →
library1.0.0-alpha.1jsnpmunverified

Declarative soft-delete solution for MikroORM v6 (v1.x) and v5 (v0.x). v1.0.0-alpha.1 targets ORM v6 and requires registering SoftDeleteHandler as an extension. Active development under alpha. Uses decorators (@SoftDeletable) and filters to automatically intercept delete queries. Key differentiator: minimal configuration, supports custom field names and default values, inheritance, and cascade remove.

npm install mikro-orm-soft-delete
INSTALL
IMPORT
SIG · MIKRO-ORM-SOFT-DEL
M
mikro-orm-soft-delete
databasejavascriptv1.0.0-alpha.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SoftDeleteHandler
import { SoftDeleteHandler } from 'mikro-orm-soft-delete'
const { SoftDeleteHandler } = require('mikro-orm-soft-delete')
ESM-only since v1; CommonJS require may not work depending on bundler.
SoftDeletable
import { SoftDeletable } from 'mikro-orm-soft-delete'
import SoftDeletable from 'mikro-orm-soft-delete'
Named export, not default. Used as a decorator.
SOFT_DELETABLE_FILTER
import { SOFT_DELETABLE_FILTER } from 'mikro-orm-soft-delete'
Constant for disabling the filter in queries.

Initializes MikroORM with soft-delete extension, defines a soft-deletable entity, performs soft delete, and queries with/without filter.

import { MikroORM, BaseEntity, Entity, PrimaryKey, Property } from '@mikro-orm/core'; import { SoftDeleteHandler, SoftDeletable, SOFT_DELETABLE_FILTER } from 'mikro-orm-soft-delete'; @SoftDeletable(() => User, 'deletedAt', () => new Date()) @Entity() export class User extends BaseEntity { @PrimaryKey() id: number; @Property({ nullable: true }) deletedAt?: Date; } const orm = await MikroORM.init({ entities: [User], dbName: 'test', type: 'sqlite', extensions: [SoftDeleteHandler], }); await orm.schema.refreshDatabase(); const repo = orm.em.getRepository(User); const user = repo.create({}); await repo.flush(); // Soft delete repo.remove(user); await repo.flush(); console.log(user.deletedAt); // Date object // Query excluding soft-deleted (default filter) const users = await repo.findAll(); console.log(users.length); // 0 // Query including soft-deleted const allUsers = await repo.findAll({ filters: { [SOFT_DELETABLE_FILTER]: false } }); console.log(allUsers.length); // 1
Debug
Known issues
breakingv1.x drops generic type parameters on base entities; new mandatory SoftDeleteHandler extension registration.
fix
Update to v1: remove generic type params from base entities and add 'extensions: [SoftDeleteHandler]' to MikroORM.init().
affects: >=1.0.0 <2.0.0
gotchaSoftDeletable decorator's third argument (value) must be a function returning the delete mark value, e.g., () => new Date(). If omitted, the filter uses null; mismatch with default field value causes filter to fail.
fix
Provide value function matching your field type, and specify 'valueInitial' option if default is not null.
affects: >=0.0.0
gotchaCascade remove and orphanRemoval work with repo.remove() but not when removing items from collections (orphanRemoval cascades). The library cannot intercept those deletions.
fix
Use repo.remove() for explicit deletions; avoid orphanRemoval on collections or handle deletions manually.
affects: >=0.0.0
deprecatedv0.x (for MikroORM v5) is no longer maintained; v1.x is alpha and may introduce breaking changes.
fix
Migrate to v1.x and MikroORM v6.x.
affects: >=0.0.0 <1.0.0
Errors
Common errors & fixes
Error: No handler registered for 'SoftDeleteHandler'. Make sure to add it to the 'extensions' array in MikroORM.init()
Missing extension registration in MikroORM config when using v1.x.
fix
Add 'extensions: [SoftDeleteHandler]' to your MikroORM.init() call.
TypeError: Class extends value undefined is not a constructor or null
Base entity generic parameters used in v1 which removed them.
fix
Remove generic type parameters from base entities (e.g., change 'class MyEntity extends BaseEntity<...>' to 'class MyEntity extends BaseEntity').
ReferenceError: Cannot access 'User' before initialization
Decorator @SoftDeletable(() => User, ...) uses forward reference; entity class not yet defined.
fix
Use arrow function returning the entity class (not the class itself) to defer resolution. Already done correctly in example.
Upgrade
Version history
1.0.0-alpha.1latest on npm
Audit
Dependencies
@mikro-orm/corerequiredPeer dependency; required to initialize MikroORM and use decorators/filters
Agent activity
4 hits · last 30 days
node
4
Resources
mikro-orm-soft-delete — npm install mikro-orm-soft-delete · libregistry