npm install loopback4-soft-deleteFor 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.
The package exports following classes and mixins:
- SoftDeleteEntity - To add required soft delete props in the model.
- SoftCrud仓库 - Class providing soft crud capabilitiies (to be used in place of
DefaultCrud仓库). - SoftCrud仓库Mixin - Mixin accepting any respository that extends the DefaultCrud仓库 to add soft delete functionality to. Can be used as a wrapper for
DefaultCrud仓库,DefaultTransactional仓库etc. - SoftDeleteEntityMixin
- DefaultTransactionSoftCrud仓库 (Deprecated) - Class providing soft crud capabilitiies. To be used in place of
DefaultTransactional仓库. - SequelizeSoftCrud仓库 - Class providing soft crud capabilitiies for @loopback/sequelize package. To be used in place of
SequelizeCrud仓库.
已关注 are more details on the usage of above artifcats:
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.
An abstract base class providing soft delete capabilities for projects using @loopback/sequelize package.
All the other workings are similar to SoftCrud仓库, except it's imported using directory import syntax from loopback4-soft-delete/sequelize.
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 DefaultCrud仓库 class from @loopback/repository.
Note:
DefaultTransactionSoftCrud仓库is deprecated in favour of SoftCrud仓库Mixin and will be removed in future releases.
An abstract base class similar to SoftCrud仓库 but with transaction support.
This class is a wrapper over DefaultTransactional仓库 class from @loopback/repository.
In order to use this extension in your application, please follow below steps.
- 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
}- Extend repositories with SoftCrud仓库 class replacing DefaultCrud仓库. Like below:
import {Getter, inject} from '@loopback/core';
import {SoftCrud仓库} from 'loopback4-soft-delete';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';
export class User仓库 extends SoftCrud仓库<
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);
}
}- For transaction support, use the
SoftCrud仓库Mixinand wrap it aroundDefaultTransactional仓库. Like below:
import {Getter, inject} from '@loopback/core';
import {SoftCrud仓库} from 'loopback4-soft-delete';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';
export class User仓库 extends SoftCrud仓库Mixin<
User,
typeof User.prototype.id,
UserRelations
>(DefaultTransactional仓库) {
constructor(
@inject('datasources.pgdb') dataSource: PgdbDataSource,
@inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
protected readonly getCurrentUser: Getter<IAuthUser | undefined>,
) {
super(User, dataSource, getCurrentUser);
}
}The package also provides the following mixins which can be used for soft delete functionality:
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',
},
}) {}The soft deleted properties added by SoftDeleteEntityMixin are represented by IBaseEntity interface.
interface IBaseEntity {
deleted?: boolean;
deletedOn?: Date;
deletedBy?: string;
}You can make use of this mixin to get the soft delete functionality for DefaultCrud仓库 or any respository that extends the DefaultCrud仓库. You need to extend your repository with this mixin and provide DefaultCrud仓库 (or any repository that extends DefaultCrud仓库) as input. This means that this same mixin can also be used to provide soft delete functionality for DefaultTransactionSoftCrud仓库 (as DefaultTransactionSoftCrud仓库 extends DefaultCrud仓库). You will have to inject the getter for IAuthUser in the contructor of your repository.
import {Constructor, Getter, inject} from '@loopback/core';
import {DefaultCrud仓库} from '@loopback/repository';
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
import {SoftCrud仓库Mixin} from 'loopback4-soft-delete';
import {TestDataSource} from '../datasources';
import {ItemSoftDelete, ItemSoftDeleteRelations} from '../models';
export class Item仓库 extends SoftCrud仓库Mixin<
ItemSoftDelete,
typeof ItemSoftDelete.prototype.id,
Constructor<
DefaultCrud仓库<
ItemSoftDelete,
typeof ItemSoftDelete.prototype.id,
ItemSoftDeleteRelations
>
>,
ItemSoftDeleteRelations
>(DefaultCrud仓库) {
constructor(
@inject('datasources.test') dataSource: TestDataSource,
@inject.getter(AuthenticationBindings.CURRENT_USER)
public getCurrentUser: Getter<IAuthUser>,
) {
super(ItemSoftDelete, dataSource);
}
}已关注 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 SoftCrud仓库Mixin:
findAll- This method is similar tofind, but it returns entries including soft deleted ones.deleteHard- This method is used to perform a hard delete on a specified entity.deleteByIdHard- This method is used to perform a hard delete of an entity based on the provided ID.findByIdIncludeSoftDelete- This method is similar tofindById, but it returns the entity even if it is soft deleted.deleteAllHard- This method is used to perform a hard delete of multiple entities based on a specified condition.findOneIncludeSoftDelete- This method is similar tofindOne, but it returns a single entity even if it is soft deleted.countAll- This method is similar tocount, but it returns the total count of all entities including soft deleted ones.
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 {SoftCrud仓库, IUser} from 'loopback4-soft-delete';
import {AuthenticationBindings} from 'loopback4-authentication';
import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';
export class User仓库 extends SoftCrud仓库<
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);
}
}