Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

loopback4-soft-delete

sourcefuse14.3kMIT10.0.3TypeScript support: included

A loopback-next extension for soft delete feature.

loopback-extension, loopback

readme

ARC By SourceFuse logo

loopback4-soft-delete

npm version Sonar Quality Gate Synk Status GitHub contributors downloads License Powered By LoopBack 4

Install

npm install loopback4-soft-delete

Quick Starter

For a quick starter guide, you can refer to our loopback 4 starter application which utilizes this package for soft-deletes in a multi-tenant application.

Usage

The package exports following classes and mixins:

  1. SoftDeleteEntity - To add required soft delete props in the model.
  2. SoftCrudRepository - Class providing soft crud capabilitiies (to be used in place of DefaultCrudRepository).
  3. 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.
  4. SoftDeleteEntityMixin
  5. DefaultTransactionSoftCrudRepository (Deprecated) - Class providing soft crud capabilitiies. To be used in place of DefaultTransactionalRepository.
  6. SequelizeSoftCrudRepository - Class providing soft crud capabilitiies for @loopback/sequelize package. To be used in place of SequelizeCrudRepository.

Following are more details on the usage of above artifcats:

SoftDeleteEntity

An abstract base class for all models which require soft delete feature. This class is a wrapper over Entity class from @loopback/repository adding three attributes to the model class for handling soft-delete, namely, deleted, deletedOn, deletedBy. The column names needed to be there in DB within that table are - 'deleted', 'deleted_on', 'deleted_by'. If you are using auto-migration of loopback 4, then, you may not need to do anything specific to add this column. If not, then please add these columns to the DB table.

SequelizeSoftCrudRepository

An abstract base class providing soft delete capabilities for projects using @loopback/sequelize package. All the other workings are similar to SoftCrudRepository, except it's imported using directory import syntax from loopback4-soft-delete/sequelize.

SoftCrudRepository

An abstract base class for all repositories which require soft delete feature. This 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. This class is a wrapper over DefaultCrudRepository class from @loopback/repository.

DefaultTransactionSoftCrudRepository (Deprecated)

Note: DefaultTransactionSoftCrudRepository is deprecated in favour of SoftCrudRepositoryMixin and will be removed in future releases.

An abstract base class similar to SoftCrudRepository but with transaction support.

This class is a wrapper over DefaultTransactionalRepository class from @loopback/repository.

In order to use this extension in your application, please follow below steps.

  1. Extend models with SoftDeleteEntity class replacing Entity. Like below:
import {model, property} from '@loopback/repository';
import {SoftDeleteEntity} from 'loopback4-soft-delete';

@model({
  name: 'users',
})
export class User extends SoftDeleteEntity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  // .... More properties
}
  1. Extend repositories with SoftCrudRepository class replacing DefaultCrudRepository. Like below:
import {Getter, inject} from '@loopback/core';
import {SoftCrudRepository} from 'loopback4-soft-delete';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';

import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';

export class UserRepository extends SoftCrudRepository<
  User,
  typeof User.prototype.id,
  UserRelations
> {
  constructor(
    @inject('datasources.pgdb') dataSource: PgdbDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
    protected readonly getCurrentUser: Getter<IAuthUser | undefined>,
  ) {
    super(User, dataSource, getCurrentUser);
  }
}
  1. For transaction support, use the SoftCrudRepositoryMixin and wrap it around DefaultTransactionalRepository. Like below:
import {Getter, inject} from '@loopback/core';
import {SoftCrudRepository} from 'loopback4-soft-delete';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';

import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';

export class UserRepository extends SoftCrudRepositoryMixin<
  User,
  typeof User.prototype.id,
  UserRelations
>(DefaultTransactionalRepository) {
  constructor(
    @inject('datasources.pgdb') dataSource: PgdbDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
    protected readonly getCurrentUser: Getter<IAuthUser | undefined>,
  ) {
    super(User, dataSource, getCurrentUser);
  }
}

Mixins Usage

The package also provides the following mixins which can be used for soft delete functionality:

SoftDeleteEntityMixin

This mixin adds the soft delete properties to your model. The properties added are represented by the IBaseEntity interface:

There is also an option to provide config for the @property decorator for all these properties.

Usage of SoftDeleteEntityMixin is as follows:

class Item extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  id?: number;

  @property({
    type: 'string',
    required: true,
  })
  name: string;

  constructor(data?: Partial<Item>) {
    super(data);
  }
}

@model()
export class ItemSoftDelete extends SoftDeleteEntityMixin(Item, {
  deletedBy: {
    name: 'deleted_by_userid',
  },
}) {}

IBaseEntity

The soft deleted properties added by SoftDeleteEntityMixin are represented by IBaseEntity interface.

interface IBaseEntity {
  deleted?: boolean;
  deletedOn?: Date;
  deletedBy?: string;
}

SoftCrudRepositoryMixin

You 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.

Example:

import {Constructor, Getter, inject} from '@loopback/core';
import {DefaultCrudRepository} from '@loopback/repository';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
import {SoftCrudRepositoryMixin} from 'loopback4-soft-delete';
import {TestDataSource} from '../datasources';
import {ItemSoftDelete, ItemSoftDeleteRelations} from '../models';

export class ItemRepository extends SoftCrudRepositoryMixin<
  ItemSoftDelete,
  typeof ItemSoftDelete.prototype.id,
  Constructor<
    DefaultCrudRepository<
      ItemSoftDelete,
      typeof ItemSoftDelete.prototype.id,
      ItemSoftDeleteRelations
    >
  >,
  ItemSoftDeleteRelations
>(DefaultCrudRepository) {
  constructor(
    @inject('datasources.test') dataSource: TestDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER)
    public getCurrentUser: Getter<IAuthUser>,
  ) {
    super(ItemSoftDelete, dataSource);
  }
}

Additional Repository Methods

Following 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:

  1. findAll - This method is similar to find, but it returns entries including soft deleted ones.
  2. deleteHard - This method is used to perform a hard delete on a specified entity.
  3. deleteByIdHard - This method is used to perform a hard delete of an entity based on the provided ID.
  4. findByIdIncludeSoftDelete - This method is similar to findById, but it returns the entity even if it is soft deleted.
  5. deleteAllHard - This method is used to perform a hard delete of multiple entities based on a specified condition.
  6. findOneIncludeSoftDelete - This method is similar to findOne, but it returns a single entity even if it is soft deleted.
  7. countAll - This method is similar to count, but it returns the total count of all entities including soft deleted ones.

deletedBy

Whenever 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.

import {Getter, inject} from '@loopback/core';
import {SoftCrudRepository, IUser} from 'loopback4-soft-delete';
import {AuthenticationBindings} from 'loopback4-authentication';

import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';

export class UserRepository extends SoftCrudRepository<
  User,
  typeof User.prototype.id,
  UserRelations
> {
  constructor(
    @inject('datasources.pgdb') dataSource: PgdbDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
    protected readonly getCurrentUser: Getter<User | undefined>,
  ) {
    super(User, dataSource, getCurrentUser);
  }
}

Model class for custom identifier case. Notice the getIdentifier() method and IUser interface implemented.

@model()
class User extends SoftDeleteEntity implements IUser {
  @property({
    id: true,
  })
  id: string;

  @property()
  username: string;

  getIdentifier() {
    return this.username;
  }

  constructor(data?: Partial<User>) {
    super(data);
  }
}

License

MIT

changelog

Release v10.0.2 June 4, 2024

Welcome to the June 4, 2024 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

  • :- chore(deps): loopback version updates was commited on June 4, 2024 by Surbhi

    • loopback version updates

    • GH-213

    • Description

    • loopback version updates

    • Fixes #213

    • Type of change

    • Please delete options that are not relevant.

      • <input disabled="" type="checkbox"> Bug fix (non-breaking change which fixes an issue)
      • <input disabled="" type="checkbox"> New feature (non-breaking change which adds functionality)
      • <input disabled="" type="checkbox"> Breaking change (fix or feature that would cause existing
    • functionality

    • to not work as expected)

      • <input disabled="" type="checkbox"> Intermediate change (work in progress)
    • How Has This Been Tested ?

    • Please describe the tests that you ran to verify your changes. Provide

    • instructions so we can reproduce. Please also list any relevant details

    • for

    • your test configuration

      • <input disabled="" type="checkbox"> Test A
      • <input disabled="" type="checkbox"> Test B
    • Checklist:

      • <input disabled="" type="checkbox"> Performed a self-review of my own code
      • <input disabled="" type="checkbox"> npm test passes on your machine
      • <input disabled="" type="checkbox"> New tests added or existing tests modified to cover all changes
      • <input disabled="" type="checkbox"> Code conforms with the style guide
      • <input disabled="" type="checkbox"> API Documentation in code was updated

Clink on the above links to understand the changes in detail.


Release v10.0.1 May 15, 2024

Welcome to the May 15, 2024 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

  • :- fix(ci-cd): add step to generate changelog was commited on May 15, 2024 by yeshamavani

    • add the missing ci checks for pr

    • GH-211

    • Description

    • add step to generate changelog

    • add the missing ci checks for pr

    • Fixes #211

    • Type of change

    • Please delete options that are not relevant.

      • <input disabled="" type="checkbox"> Bug fix (non-breaking change which fixes an issue)
      • <input disabled="" type="checkbox"> New feature (non-breaking change which adds functionality)
      • <input disabled="" type="checkbox"> Breaking change (fix or feature that would cause existing
    • functionality

    • to not work as expected)

      • <input disabled="" type="checkbox"> Intermediate change (work in progress)
    • Checklist:

      • <input disabled="" type="checkbox"> Performed a self-review of my own code
      • <input disabled="" type="checkbox"> npm test passes on your machine
      • <input disabled="" type="checkbox"> New tests added or existing tests modified to cover all changes
      • <input disabled="" type="checkbox"> Code conforms with the style guide
      • <input disabled="" type="checkbox"> API Documentation in code was updated
  • :- docs(chore): copy the Readme to root as well was commited on May 13, 2024 by arpit1503khanna

    • GH-209

    • Description

    • Copy the Readme to root as well

    • Fixes #209

    • Type of change

    • Please delete options that are not relevant.

      • <input disabled="" type="checkbox"> Bug fix (non-breaking change which fixes an issue)
      • <input disabled="" type="checkbox"> New feature (non-breaking change which adds functionality)
      • <input disabled="" type="checkbox"> Breaking change (fix or feature that would cause existing
    • functionality

    • to not work as expected)

      • <input disabled="" type="checkbox"> Intermediate change (work in progress)
    • How Has This Been Tested ?

    • Please describe the tests that you ran to verify your changes. Provide

    • instructions so we can reproduce. Please also list any relevant details

    • for

    • your test configuration

      • <input disabled="" type="checkbox"> Test A
      • <input disabled="" type="checkbox"> Test B
    • Checklist:

      • <input disabled="" type="checkbox"> Performed a self-review of my own code
      • <input disabled="" type="checkbox"> npm test passes on your machine
      • <input disabled="" type="checkbox"> New tests added or existing tests modified to cover all changes
      • <input disabled="" type="checkbox"> Code conforms with the style guide
      • <input disabled="" type="checkbox"> API Documentation in code was updated
  • :- chore(ci-cd): update readme path in sync docs action was commited on March 14, 2024 by yeshamavani

    • since the readme moved to docs folder

    • GH-00

    • Description

    • update readme path in sync docs action

    • Fixes # (issue)

    • Type of change

    • Please delete options that are not relevant.

      • <input disabled="" type="checkbox"> Bug fix (non-breaking change which fixes an issue)
      • <input disabled="" type="checkbox"> New feature (non-breaking change which adds functionality)
      • <input disabled="" type="checkbox"> Breaking change (fix or feature that would cause existing
    • functionality

    • to not work as expected)

      • <input disabled="" type="checkbox"> Intermediate change (work in progress)
    • Checklist:

      • <input disabled="" type="checkbox"> Performed a self-review of my own code
      • <input disabled="" type="checkbox"> npm test passes on your machine
      • <input disabled="" type="checkbox"> New tests added or existing tests modified to cover all changes
      • <input disabled="" type="checkbox"> Code conforms with the style guide
      • <input disabled="" type="checkbox"> API Documentation in code was updated

Clink on the above links to understand the changes in detail.


Release v8.0.1 July 14, 2023

Welcome to the July 14, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v8.0.0 June 7, 2023

Welcome to the June 7, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.3.2 April 24, 2023

Welcome to the April 24, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.3.1 April 7, 2023

Welcome to the April 7, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.3.0 April 7, 2023

Welcome to the April 7, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.3.0 April 6, 2023

Welcome to the April 6, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.2.3 March 17, 2023

Welcome to the March 17, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.2.2 March 10, 2023

Welcome to the March 10, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.2.1 March 1, 2023

Welcome to the March 1, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.2.0 February 28, 2023

Welcome to the February 28, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

  • :- refactor(chore): remove duplicate code and deprecate DefaultTransactionSoftCrudRepository was commited on February 27, 2023 by Shubham P

      • fix(chore): add #122 changes in mixin and transaction repository
    • add development note to ensure future modifications universal

    • add changes

    • made in PR #122 in

    • soft-crud mixin and transaction-soft-crud repository

    • GH-129

      • refactor(chore): add common service and decorator for filter modification
    • created soft crud service file containing common code among artifacts

    • created softFilter and

    • excludeSoftDeleted decorator for the common tasks

    • GH-128

      • refactor(repository): deprecate DefaultTransactionSoftCrudRepository
    • in favor of SoftCrudRepositoryMixin for improving maintainability

    • GH-132

      • refactor(repository): replace modify functions with custom soft filter
    • builder

    • removed soft-crud service and used custom soft filter builder class providing

    • methods like

    • imposeCondition, fields etc.

    • GH-0

      • refactor(chore): resolve sonar code smells
    • ignore transactional respotory file in sonar as this is deprecated and kept

    • duplicacy of the code

    • just for few months of support

    • add avoid rule for arrowParens in prettier

    • GH-0

Clink on the above links to understand the changes in detail.


Release v7.1.2 February 17, 2023

Welcome to the February 17, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.1.1 February 14, 2023

Welcome to the February 14, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


Release v7.1.0 January 11, 2023

Welcome to the January 11, 2023 release of loopback4-soft-delete. There are many updates in this version that we hope you will like, the key highlights include:

Clink on the above links to understand the changes in detail.


7.0.2 (2022-12-05)

7.0.1 (2022-12-05)

7.0.0 (2022-11-16)

Features

  • repository: add new method to provide user identifier for deletedbyid (aaeb150), closes #0

BREAKING CHANGES

  • repository: change approach for custom identifier for deletedbyid

6.0.1 (2022-11-11)

Bug Fixes

  • repository: type mismatch for Iauthuser (e0c5464), closes #0

6.0.0 (2022-11-07)

Features

  • repository: allow deletedBy id to be configurable (#100) (1be1dc4), closes #99
  • repository: allow deletedBy id to be configurable using class protected property (28de1cd), closes #99

BREAKING CHANGES

  • repository: change approach of deletedById key provider
  • repository: remove dependency of loopback4-authentication

5.3.5 (2022-10-31)

5.3.4 (2022-10-14)

5.3.3 (2022-09-20)

Bug Fixes

  • repository: fixed object assignment for filters (#93) (740da07), closes #91

5.3.2 (2022-09-19)

Bug Fixes

  • repository: add deleted property in fields filter (#92) (142179c), closes #91

5.3.1 (2022-09-09)

5.3.0 (2022-09-05)

Features

  • repository: remove multiple db calls for find by id (#88) (bbc9803), closes #87

5.2.1 (2022-07-28)

5.2.0 (2022-07-21)

Bug Fixes

  • repository): fix(repository: fixed entitytoremove=null with mongodb (#48) (277142d)

Features

  • entity: add mixin for SoftDeleteEntity and DefaultCrudRepository (#80) (4cf6b9c)

5.1.1 (2022-06-21)

5.1.0 (2022-05-25)

Bug Fixes

Features