{"id":29943387,"url":"https://github.com/sourcefuse/loopback4-soft-delete","last_synced_at":"2025-08-03T02:17:00.169Z","repository":{"id":34846423,"uuid":"184415389","full_name":"sourcefuse/loopback4-soft-delete","owner":"sourcefuse","description":"A loopback4 extension for soft delete feature","archived":false,"fork":false,"pushed_at":"2025-07-22T08:28:10.000Z","size":4609,"stargazers_count":40,"open_issues_count":4,"forks_count":27,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-07-26T21:29:12.094Z","etag":null,"topics":["arcbysf","loopback","loopback-extension","loopback-next","loopback4","loopback4-extension","soft-delete","soft-deletes"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/loopback4-soft-delete","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sourcefuse.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-05-01T12:49:59.000Z","updated_at":"2025-04-04T03:31:42.000Z","dependencies_parsed_at":"2024-03-12T11:46:39.910Z","dependency_job_id":"8e30d505-2b51-4e58-a680-2cd6b2806c02","html_url":"https://github.com/sourcefuse/loopback4-soft-delete","commit_stats":{"total_commits":153,"total_committers":26,"mean_commits":5.884615384615385,"dds":0.7124183006535948,"last_synced_commit":"f80f6e5ff8fe4817a4a8e0623f7be175913e2484"},"previous_names":[],"tags_count":56,"template":false,"template_full_name":null,"purl":"pkg:github/sourcefuse/loopback4-soft-delete","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcefuse%2Floopback4-soft-delete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcefuse%2Floopback4-soft-delete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcefuse%2Floopback4-soft-delete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcefuse%2Floopback4-soft-delete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sourcefuse","download_url":"https://codeload.github.com/sourcefuse/loopback4-soft-delete/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourcefuse%2Floopback4-soft-delete/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268484362,"owners_count":24257660,"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","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["arcbysf","loopback","loopback-extension","loopback-next","loopback4","loopback4-extension","soft-delete","soft-deletes"],"created_at":"2025-08-03T02:15:14.158Z","updated_at":"2025-08-03T02:17:00.154Z","avatar_url":"https://github.com/sourcefuse.png","language":"TypeScript","readme":"\u003ca href=\"https://sourcefuse.github.io/arc-docs/arc-api-docs\" target=\"_blank\"\u003e\u003cimg src=\"https://github.com/sourcefuse/loopback4-microservice-catalog/blob/master/docs/assets/logo-dark-bg.png?raw=true\" alt=\"ARC By SourceFuse logo\" title=\"ARC By SourceFuse\" align=\"right\" width=\"150\" /\u003e\u003c/a\u003e\n\n# [loopback4-soft-delete](https://github.com/sourcefuse/loopback4-soft-delete)\n\n\u003cp align=\"left\"\u003e\n\u003ca href=\"https://www.npmjs.com/package/loopback4-soft-delete\"\u003e\n\u003cimg src=\"https://img.shields.io/npm/v/loopback4-soft-delete.svg\" alt=\"npm version\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://sonarcloud.io/summary/new_code?id=sourcefuse_loopback4-soft-delete\" target=\"_blank\"\u003e\n\u003cimg alt=\"Sonar Quality Gate\" src=\"https://img.shields.io/sonar/quality_gate/sourcefuse_loopback4-soft-delete?server=https%3A%2F%2Fsonarcloud.io\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://app.snyk.io/org/ashishkaushik/reporting?context[page]=issues-detail\u0026project_target=%255B%2522sourcefuse%252Floopback4-soft-delete%2522%255D\u0026project_origin=%255B%2522github%2522%255D\u0026issue_status=%255B%2522Open%2522%255D\u0026issue_by=Severity\u0026table_issues_detail_cols=SCORE%257CCVE%257CCWE%257CPROJECT%257CEXPLOIT%2520MATURITY%257CAUTO%2520FIXABLE%257CINTRODUCED%257CSNYK%2520PRODUCT\u0026v=1\"\u003e\n\u003cimg alt=\"Synk Status\" src=\"https://img.shields.io/badge/SYNK_SECURITY-MONITORED-GREEN\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/sourcefuse/loopback4-soft-delete/graphs/contributors\" target=\"_blank\"\u003e\n\u003cimg alt=\"GitHub contributors\" src=\"https://img.shields.io/github/contributors/sourcefuse/loopback4-soft-delete\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/loopback4-soft-delete\" target=\"_blank\"\u003e\n\u003cimg alt=\"downloads\" src=\"https://img.shields.io/npm/dw/loopback4-soft-delete.svg\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/sourcefuse/loopback4-soft-delete/blob/master/LICENSE\"\u003e\n\u003cimg src=\"https://img.shields.io/github/license/sourcefuse/loopback4-soft-delete.svg\" alt=\"License\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://loopback.io/\" target=\"_blank\"\u003e\n\u003cimg alt=\"Powered By LoopBack 4\" src=\"https://img.shields.io/badge/Powered%20by-LoopBack 4-brightgreen\" /\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n## Install\n\n```sh\nnpm install loopback4-soft-delete\n```\n\n## Quick Starter\n\nFor a quick starter guide, you can refer to our [loopback 4 starter](https://github.com/sourcefuse/loopback4-starter) application which utilizes this package for soft-deletes in a multi-tenant application.\n\n## Usage\n\nThe package exports following classes and mixins:\n\n1. [SoftDeleteEntity](#softdeleteentity) - To add required soft delete props in the model.\n2. [SoftCrudRepository](#softcrudrepository) - Class providing soft crud capabilitiies (to be used in place of `DefaultCrudRepository`).\n3. [SoftCrudRepositoryMixin](#softcrudrepositorymixin) - Mixin accepting any respository that extends the DefaultCrudRepository to add soft delete functionality to. Can be used as a wrapper for `DefaultCrudRepository`, `DefaultTransactionalRepository` etc.\n4. [SoftDeleteEntityMixin](#softdeleteentitymixin)\n5. [DefaultTransactionSoftCrudRepository](#defaulttransactionsoftcrudrepository-deprecated) (Deprecated) - Class providing soft crud capabilitiies. To be used in place of `DefaultTransactionalRepository`.\n6. [SequelizeSoftCrudRepository](#sequelizesoftcrudrepository) - Class providing soft crud capabilitiies for [@loopback/sequelize](https://www.npmjs.com/package/@loopback/sequelize) package. To be used in place of `SequelizeCrudRepository`.\n\nFollowing are more details on the usage of above artifcats:\n\n### SoftDeleteEntity\n\nAn abstract base class for all models which require soft delete feature.\nThis class is a wrapper over Entity class from [@loopback/repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository) adding three attributes to the model class for handling soft-delete, namely, deleted, deletedOn, deletedBy.\nThe column names needed to be there in DB within that table are - 'deleted', 'deleted_on', 'deleted_by'.\nIf you are using auto-migration of loopback 4, then, you may not need to do anything specific to add this column.\nIf not, then please add these columns to the DB table.\n\n### SequelizeSoftCrudRepository\n\nAn abstract base class providing soft delete capabilities for projects using [@loopback/sequelize](https://www.npmjs.com/package/@loopback/sequelize) package.\nAll the other workings are similar to [SoftCrudRepository](#softcrudrepository), except it's imported using directory import syntax from `loopback4-soft-delete/sequelize`.\n\n### SoftCrudRepository\n\nAn abstract base class for all repositories which require soft delete feature.\nThis class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses, However if there is a need to query soft deleted entries as well, there is an options to achieve that and you can use findAll() in place of find() , findOneIncludeSoftDelete() in place of findOne() and findByIdIncludeSoftDelete() in place of findById(), these will give you the responses including soft deleted entries.\nThis class is a wrapper over DefaultCrudRepository class from [@loopback/repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository).\n\n### DefaultTransactionSoftCrudRepository (Deprecated)\n\n\u003e Note: `DefaultTransactionSoftCrudRepository` is deprecated in favour of [SoftCrudRepositoryMixin](#softcrudrepositorymixin) and will be removed in future releases.\n\nAn abstract base class similar to [SoftCrudRepository](#softcrudrepository) but with transaction support.\n\nThis class is a wrapper over `DefaultTransactionalRepository` class from [@loopback/repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository).\n\nIn order to use this extension in your application, please follow below steps.\n\n1. Extend models with SoftDeleteEntity class replacing Entity. Like below:\n\n```ts\nimport {model, property} from '@loopback/repository';\nimport {SoftDeleteEntity} from 'loopback4-soft-delete';\n\n@model({\n  name: 'users',\n})\nexport class User extends SoftDeleteEntity {\n  @property({\n    type: 'number',\n    id: true,\n  })\n  id?: number;\n\n  // .... More properties\n}\n```\n\n2. Extend repositories with SoftCrudRepository class replacing DefaultCrudRepository. Like below:\n\n```ts\nimport {Getter, inject} from '@loopback/core';\nimport {SoftCrudRepository} from 'loopback4-soft-delete';\nimport {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';\n\nimport {PgdbDataSource} from '../datasources';\nimport {User, UserRelations} from '../models';\n\nexport class UserRepository extends SoftCrudRepository\u003c\n  User,\n  typeof User.prototype.id,\n  UserRelations\n\u003e {\n  constructor(\n    @inject('datasources.pgdb') dataSource: PgdbDataSource,\n    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})\n    protected readonly getCurrentUser: Getter\u003cIAuthUser | undefined\u003e,\n  ) {\n    super(User, dataSource, getCurrentUser);\n  }\n}\n```\n\n3. For transaction support, use the `SoftCrudRepositoryMixin` and wrap it around `DefaultTransactionalRepository`. Like below:\n\n```ts\nimport {Getter, inject} from '@loopback/core';\nimport {SoftCrudRepository} from 'loopback4-soft-delete';\nimport {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';\n\nimport {PgdbDataSource} from '../datasources';\nimport {User, UserRelations} from '../models';\n\nexport class UserRepository extends SoftCrudRepositoryMixin\u003c\n  User,\n  typeof User.prototype.id,\n  UserRelations\n\u003e(DefaultTransactionalRepository) {\n  constructor(\n    @inject('datasources.pgdb') dataSource: PgdbDataSource,\n    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})\n    protected readonly getCurrentUser: Getter\u003cIAuthUser | undefined\u003e,\n  ) {\n    super(User, dataSource, getCurrentUser);\n  }\n}\n```\n\n## Mixins Usage\n\nThe package also provides the following mixins which can be used for soft delete functionality:\n\n### SoftDeleteEntityMixin\n\nThis mixin adds the soft delete properties to your model. The properties added are represented by the [IBaseEntity](#ibaseentity) interface:\n\nThere is also an option to provide config for the `@property` decorator for all these properties.\n\nUsage of `SoftDeleteEntityMixin` is as follows:\n\n```ts\nclass Item extends Entity {\n  @property({\n    type: 'number',\n    id: true,\n    generated: true,\n  })\n  id?: number;\n\n  @property({\n    type: 'string',\n    required: true,\n  })\n  name: string;\n\n  constructor(data?: Partial\u003cItem\u003e) {\n    super(data);\n  }\n}\n\n@model()\nexport class ItemSoftDelete extends SoftDeleteEntityMixin(Item, {\n  deletedBy: {\n    name: 'deleted_by_userid',\n  },\n}) {}\n```\n\n#### IBaseEntity\n\nThe soft deleted properties added by [SoftDeleteEntityMixin](#softdeleteentitymixin) are represented by `IBaseEntity` interface.\n\n```ts\ninterface IBaseEntity {\n  deleted?: boolean;\n  deletedOn?: Date;\n  deletedBy?: string;\n}\n```\n\n### SoftCrudRepositoryMixin\n\nYou can make use of this mixin to get the soft delete functionality for `DefaultCrudRepository` or any respository that extends the `DefaultCrudRepository`. You need to extend your repository with this mixin and provide DefaultCrudRepository (or any repository that extends DefaultCrudRepository) as input. This means that this same mixin can also be used to provide soft delete functionality for DefaultTransactionSoftCrudRepository (as DefaultTransactionSoftCrudRepository extends DefaultCrudRepository). You will have to inject the getter for IAuthUser in the contructor of your repository.\n\n#### Example:\n\n```ts\nimport {Constructor, Getter, inject} from '@loopback/core';\nimport {DefaultCrudRepository} from '@loopback/repository';\nimport {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';\nimport {SoftCrudRepositoryMixin} from 'loopback4-soft-delete';\nimport {TestDataSource} from '../datasources';\nimport {ItemSoftDelete, ItemSoftDeleteRelations} from '../models';\n\nexport class ItemRepository extends SoftCrudRepositoryMixin\u003c\n  ItemSoftDelete,\n  typeof ItemSoftDelete.prototype.id,\n  Constructor\u003c\n    DefaultCrudRepository\u003c\n      ItemSoftDelete,\n      typeof ItemSoftDelete.prototype.id,\n      ItemSoftDeleteRelations\n    \u003e\n  \u003e,\n  ItemSoftDeleteRelations\n\u003e(DefaultCrudRepository) {\n  constructor(\n    @inject('datasources.test') dataSource: TestDataSource,\n    @inject.getter(AuthenticationBindings.CURRENT_USER)\n    public getCurrentUser: Getter\u003cIAuthUser\u003e,\n  ) {\n    super(ItemSoftDelete, dataSource);\n  }\n}\n```\n\n## Additional Repository Methods\n\nFollowing are some additional methods that you can use when working with repositories in your application, either by extending the base repositories provided or by using the [SoftCrudRepositoryMixin](#softcrudrepositorymixin):\n\n1. `findAll` - This method is similar to `find`, but it returns entries including soft deleted ones.\n2. `deleteHard` - This method is used to perform a hard delete on a specified entity.\n3. `deleteByIdHard` - This method is used to perform a hard delete of an entity based on the provided ID.\n4. `findByIdIncludeSoftDelete` - This method is similar to `findById`, but it returns the entity even if it is soft deleted.\n5. `deleteAllHard` - This method is used to perform a hard delete of multiple entities based on a specified condition.\n6. `findOneIncludeSoftDelete` - This method is similar to `findOne`, but it returns a single entity even if it is soft deleted.\n7. `countAll` - This method is similar to `count`, but it returns the total count of all entities including soft deleted ones.\n\n### deletedBy\n\nWhenever any entry is deleted using deleteById, delete and deleteAll repository methods, it also sets deletedBy column with a value with user id whoever is logged in currently. Hence it uses a Getter function of IUser type. However, if you want to use some other attribute of user model other than id, you can do it by overriding deletedByIdKey. Here is an example.\n\n```ts\nimport {Getter, inject} from '@loopback/core';\nimport {SoftCrudRepository, IUser} from 'loopback4-soft-delete';\nimport {AuthenticationBindings} from 'loopback4-authentication';\n\nimport {PgdbDataSource} from '../datasources';\nimport {User, UserRelations} from '../models';\n\nexport class UserRepository extends SoftCrudRepository\u003c\n  User,\n  typeof User.prototype.id,\n  UserRelations\n\u003e {\n  constructor(\n    @inject('datasources.pgdb') dataSource: PgdbDataSource,\n    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})\n    protected readonly getCurrentUser: Getter\u003cUser | undefined\u003e,\n  ) {\n    super(User, dataSource, getCurrentUser);\n  }\n}\n```\n\nModel class for custom identifier case. Notice the `getIdentifier() method` and `IUser` interface implemented.\n\n```ts\n@model()\nclass User extends SoftDeleteEntity implements IUser {\n  @property({\n    id: true,\n  })\n  id: string;\n\n  @property()\n  username: string;\n\n  getIdentifier() {\n    return this.username;\n  }\n\n  constructor(data?: Partial\u003cUser\u003e) {\n    super(data);\n  }\n}\n```\n\n## License\n\n[MIT](https://github.com/sourcefuse/loopback4-soft-delete/blob/master/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourcefuse%2Floopback4-soft-delete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsourcefuse%2Floopback4-soft-delete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourcefuse%2Floopback4-soft-delete/lists"}