Skip to content

Commit 1be1dc4

Browse files
authored
feat(repository): allow deletedBy id to be configurable (#100)
BREAKING CHANGE: remove dependency of loopback4-authentication gh-99
1 parent 0a2f219 commit 1be1dc4

11 files changed

+127
-1184
lines changed

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
npm install loopback4-soft-delete
1919
```
2020

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-
2721
## Quick Starter
2822

2923
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<
197191
}
198192
```
199193

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+
200239
## License
201240

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

0 commit comments

Comments
 (0)