Skip to content

Commit 4cf6b9c

Browse files
feat(entity): add mixin for SoftDeleteEntity and DefaultCrudRepository (#80)
1 parent 779436d commit 4cf6b9c

12 files changed

+1483
-224
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,80 @@ export class UserRepository extends DefaultTransactionSoftCrudRepository<
123123
}
124124
```
125125

126+
The package also provides the following mixins which can be used for soft delete functionality:
127+
128+
- **SoftDeleteEntityMixin**: This mixin adds the soft delete properties to your model. The properties added are represented by the IBaseEntity interface:
129+
130+
```ts
131+
interface IBaseEntity {
132+
deleted?: boolean;
133+
deletedOn?: Date;
134+
deletedBy?: string;
135+
}
136+
```
137+
138+
There is also an option to provide config for the @property decorator for all these properties.
139+
Usage of SoftDeleteEntityMixin is as follows:
140+
141+
```ts
142+
class Item extends Entity {
143+
@property({
144+
type: 'number',
145+
id: true,
146+
generated: true,
147+
})
148+
id?: number;
149+
150+
@property({
151+
type: 'string',
152+
required: true,
153+
})
154+
name: string;
155+
156+
constructor(data?: Partial<Item>) {
157+
super(data);
158+
}
159+
}
160+
161+
@model()
162+
export class ItemSoftDelete extends SoftDeleteEntityMixin(Item, {
163+
deletedBy: {name: 'deleted_by_userid'},
164+
}) {}
165+
```
166+
167+
- **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.
168+
Example:
169+
170+
```ts
171+
import {Constructor, Getter, inject} from '@loopback/core';
172+
import {DefaultCrudRepository} from '@loopback/repository';
173+
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
174+
import {SoftCrudRepositoryMixin} from 'loopback4-soft-delete';
175+
import {TestDataSource} from '../datasources';
176+
import {ItemSoftDelete, ItemSoftDeleteRelations} from '../models';
177+
178+
export class ItemRepository extends SoftCrudRepositoryMixin<
179+
ItemSoftDelete,
180+
typeof ItemSoftDelete.prototype.id,
181+
Constructor<
182+
DefaultCrudRepository<
183+
ItemSoftDelete,
184+
typeof ItemSoftDelete.prototype.id,
185+
ItemSoftDeleteRelations
186+
>
187+
>,
188+
ItemSoftDeleteRelations
189+
>(DefaultCrudRepository) {
190+
constructor(
191+
@inject('datasources.test') dataSource: TestDataSource,
192+
@inject.getter(AuthenticationBindings.CURRENT_USER)
193+
public getCurrentUser: Getter<IAuthUser>,
194+
) {
195+
super(ItemSoftDelete, dataSource);
196+
}
197+
}
198+
```
199+
126200
## License
127201

128202
[MIT](https://github.com/sourcefuse/loopback4-soft-delete/blob/master/LICENSE)

0 commit comments

Comments
 (0)