|
18 | 18 | npm install loopback4-soft-delete
|
19 | 19 | ```
|
20 | 20 |
|
21 |
| -**NOTE** - With latest version 3.0.0, you also need to install [loopback4-authentication](https://github.com/sourcefuse/loopback4-authentication) for using deleted_by feature added. |
22 |
| - |
23 |
| -```sh |
24 |
| -npm install loopback4-soft-delete |
25 |
| -``` |
26 |
| - |
27 | 21 | ## Quick Starter
|
28 | 22 |
|
29 | 23 | For 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.
|
@@ -197,6 +191,51 @@ export class ItemRepository extends SoftCrudRepositoryMixin<
|
197 | 191 | }
|
198 | 192 | ```
|
199 | 193 |
|
| 194 | +### deletedBy |
| 195 | + |
| 196 | +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 IAuthUser type. However, if you want to use some other attribute of user model other than id, you can do it by sending extra options to repository methods - deleteById, delete and deleteAll. Here is an example. |
| 197 | + |
| 198 | +```ts |
| 199 | +import {Getter, inject} from '@loopback/core'; |
| 200 | +import {SoftCrudRepository} from 'loopback4-soft-delete'; |
| 201 | +import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; |
| 202 | + |
| 203 | +import {PgdbDataSource} from '../datasources'; |
| 204 | +import {User, UserRelations} from '../models'; |
| 205 | + |
| 206 | +export class UserRepository extends SoftCrudRepository< |
| 207 | + User, |
| 208 | + typeof User.prototype.id, |
| 209 | + UserRelations |
| 210 | +> { |
| 211 | + constructor( |
| 212 | + @inject('datasources.pgdb') dataSource: PgdbDataSource, |
| 213 | + @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true}) |
| 214 | + protected readonly getCurrentUser: Getter<IAuthUser | undefined>, |
| 215 | + ) { |
| 216 | + super(User, dataSource, getCurrentUser); |
| 217 | + } |
| 218 | + |
| 219 | + async delete(entity: User, options?: Options): Promise<void> { |
| 220 | + return super.delete(entity, { |
| 221 | + userIdentifierKey: 'userTenantId', |
| 222 | + }); |
| 223 | + } |
| 224 | + |
| 225 | + async deleteAll(where?: Where<User>, options?: Options): Promise<Count> { |
| 226 | + return super.deleteAll(where, { |
| 227 | + userIdentifierKey: 'userTenantId', |
| 228 | + }); |
| 229 | + } |
| 230 | + |
| 231 | + async deleteById(id: string, options?: Options): Promise<void> { |
| 232 | + return super.deleteById(id, { |
| 233 | + userIdentifierKey: 'userTenantId', |
| 234 | + }); |
| 235 | + } |
| 236 | +} |
| 237 | +``` |
| 238 | + |
200 | 239 | ## License
|
201 | 240 |
|
202 | 241 | [MIT](https://github.com/sourcefuse/loopback4-soft-delete/blob/master/LICENSE)
|
0 commit comments