Skip to content

Commit 5b07935

Browse files
authored
Merge pull request #133 from sourcefuse/automated-docs-sync/loopback4-audit-log
Sync loopback4-audit-log Docs
2 parents 175a2a4 + 4615357 commit 5b07935

File tree

1 file changed

+108
-34
lines changed
  • docs/arc-api-docs/extensions/loopback4-audit-log

1 file changed

+108
-34
lines changed

docs/arc-api-docs/extensions/loopback4-audit-log/README.md

Lines changed: 108 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ npm install @sourceloop/audit-log
4444

4545
In order to use this component into your LoopBack application, please follow below steps.
4646

47-
- Add audit model class as Entity.
47+
- If you wish to modify the one provided by this extension create your own model class just the one shown below. You can use `lb4 model` command to create new model
4848

4949
```ts
5050
import {Entity, model, property} from '@loopback/repository';
@@ -160,7 +160,8 @@ export class AuditDataSource
160160
}
161161
```
162162

163-
- Using `lb4 repository` command, create a repository file. After that, change the inject paramater as below so as to refer to correct data source name.
163+
- If you have a custom model and you wish to use that in your repository class you can
164+
Using `lb4 repository` command, create a repository file. After that, change the inject paramater as below so as to refer to correct data source name.
164165
`@inject(`datasources.\${AuditDbSourceName}`) dataSource: AuditDataSource,`
165166

166167
One example below.
@@ -212,14 +213,64 @@ export class GroupRepository extends AuditRepositoryMixin<
212213
@inject('datasources.pgdb') dataSource: PgdbDataSource,
213214
@inject.getter(AuthenticationBindings.CURRENT_USER)
214215
public getCurrentUser: Getter<IAuthUser>,
215-
@repository.getter('AuditLogRepository')
216+
@repository.getter(AuditLogRepository)
216217
public getAuditLogRepository: Getter<AuditLogRepository>,
217218
) {
218219
super(Group, dataSource, getCurrentUser);
219220
}
220221
}
221222
```
222223

224+
and also bind the component of this extension in your application.ts
225+
226+
```ts
227+
import {AuditLogComponent} from '@sourceloop/audit-log';
228+
229+
this.component(AuditLogComponent);
230+
```
231+
232+
- The above code uses the default repository provided by this extension
233+
- If you wish to use your custom repository and model class do the following
234+
235+
```ts
236+
import {repository} from '@loopback/repository';
237+
import {Group, GroupRelations} from '../models';
238+
import {PgdbDataSource} from '../datasources';
239+
import {inject, Getter, Constructor} from '@loopback/core';
240+
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
241+
import {AuditRepositoryMixin} from '@sourceloop/audit-log';
242+
import {CustomAuditLogRepository} from './audit-log.repository';
243+
import {CustomAuditLogModel} from '../models';
244+
245+
const groupAuditOpts: IAuditMixinOptions = {
246+
actionKey: 'Group_Logs',
247+
};
248+
249+
export class GroupRepository extends AuditRepositoryMixin<
250+
Group,
251+
typeof Group.prototype.id,
252+
GroupRelations,
253+
string,
254+
Constructor<
255+
DefaultCrudRepository<Group, typeof Group.prototype.id, GroupRelations>
256+
>,
257+
// pass the below two parameters when you want your custom model to be used
258+
// otheriwise the default model and repository will be used
259+
CustomAuditLogModel,
260+
CustomAuditLogRepository
261+
>(DefaultCrudRepository, groupAuditOpts) {
262+
constructor(
263+
@inject('datasources.pgdb') dataSource: PgdbDataSource,
264+
@inject.getter(AuthenticationBindings.CURRENT_USER)
265+
public getCurrentUser: Getter<IAuthUser>,
266+
@repository.getter(CustomAuditLogRepository)
267+
public getAuditLogRepository: Getter<CustomAuditLogRepository>,
268+
) {
269+
super(Group, dataSource, getCurrentUser);
270+
}
271+
}
272+
```
273+
223274
You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function. You can also pass some dynamic values to the extra columns of the audit-log table via the method options.
224275

225276
```ts
@@ -245,7 +296,7 @@ This will create all insert, update, delete audits for this model.
245296
create(data, {noAudit: true});
246297
```
247298

248-
- The Actor field is now configurable and can save any string type value in the field.
299+
- The Actor field is configurable and can save any string type value in the field.
249300
Though the default value will be userId a developer can save any string field from the current User that is being passed.
250301

251302
```ts
@@ -283,6 +334,15 @@ this.bind(AuthServiceBindings.ActorIdKey).to('username');
283334
public actorIdKey?: ActorId,
284335
```
285336

337+
#### Making current user not mandatory
338+
339+
Incase you dont have current user binded in your application context and wish to log the activities within your application then in that case you can pass the actor id along with the
340+
options just like
341+
342+
```ts
343+
await productRepo.create(product, {noAudit: false, actorId: 'userId'});
344+
```
345+
286346
- The package exposes a conditional mixin for your repository classes. Just extend your repository class with `ConditionalAuditRepositoryMixin`, for all those repositories where you need audit data based on condition whether `ADD_AUDIT_LOG_MIXIN` is set true. See an example below. For a model `Group`, here we are extending the `GroupRepository` with `AuditRepositoryMixin`.
287347

288348
```ts
@@ -291,8 +351,10 @@ import {Group, GroupRelations} from '../models';
291351
import {PgdbDataSource} from '../datasources';
292352
import {inject, Getter, Constructor} from '@loopback/core';
293353
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
294-
import {ConditionalAuditRepositoryMixin} from '@sourceloop/audit-log';
295-
import {AuditLogRepository} from './audit-log.repository';
354+
import {
355+
ConditionalAuditRepositoryMixin,
356+
AuditLogRepository,
357+
} from '@sourceloop/audit-log';
296358

297359
const groupAuditOpts: IAuditMixinOptions = {
298360
actionKey: 'Group_Logs',
@@ -318,15 +380,6 @@ export class GroupRepository extends ConditionalAuditRepositoryMixin(
318380
}
319381
```
320382

321-
### Making current user not mandatory
322-
323-
Incase you dont have current user binded in your application context and wish to log the activities within your application then in that case you can pass the actor id along with the
324-
options just like
325-
326-
```ts
327-
await productRepo.create(product, {noAudit: false, actorId: 'userId'});
328-
```
329-
330383
## Using with Sequelize ORM
331384

332385
This extension provides support to both juggler (the default loopback ORM) and sequelize.
@@ -336,33 +389,50 @@ If your loopback project is already using `SequelizeCrudRepository` from [@loopb
336389
1. The import statements should have the suffix `/sequelize`, like below:
337390

338391
```ts
339-
import {
340-
AuditRepositoryMixin,
341-
AuditLogRepository,
342-
} from '@sourceloop/audit-log/sequelize';
343-
```
344-
345-
2. The Audit datasource's parent class should be `SequelizeDataSource`.
346-
347-
```ts
348-
import {SequelizeDataSource} from '@loopback/sequelize';
392+
import {repository} from '@loopback/repository';
393+
import {Group, GroupRelations} from '../models';
394+
import {PgdbDataSource} from '../datasources';
395+
import {inject, Getter, Constructor} from '@loopback/core';
396+
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
397+
import {AuditRepositoryMixin} from '@sourceloop/audit-log';
398+
import {AuditLogRepository as SequelizeAuditLogRepository} from '@sourceloop/auidt-log/sequelize';
349399

350-
export class AuditDataSource
351-
extends SequelizeDataSource
352-
implements LifeCycleObserver
353-
{
354-
static dataSourceName = AuditDbSourceName;
355-
static readonly defaultConfig = config;
400+
const groupAuditOpts: IAuditMixinOptions = {
401+
actionKey: 'Group_Logs',
402+
};
356403

404+
export class GroupRepository extends AuditRepositoryMixin<
405+
Group,
406+
typeof Group.prototype.id,
407+
GroupRelations,
408+
string,
409+
Constructor<
410+
DefaultCrudRepository<Group, typeof Group.prototype.id, GroupRelations>
411+
>,
412+
AuditLog,
413+
SequelizeAuditLogRepository
414+
>(DefaultCrudRepository, groupAuditOpts) {
357415
constructor(
358-
@inject('datasources.config.audit', {optional: true})
359-
dsConfig: object = config,
416+
@inject('datasources.pgdb') dataSource: PgdbDataSource,
417+
@inject.getter(AuthenticationBindings.CURRENT_USER)
418+
public getCurrentUser: Getter<IAuthUser>,
419+
@repository.getter(AuditLogRepository)
420+
public getAuditLogRepository: Getter<SequelizeAuditLogRepository>,
360421
) {
361-
super(dsConfig);
422+
super(Group, dataSource, getCurrentUser);
362423
}
363424
}
364425
```
365426

427+
Also add the following to your application.ts file to bind the Sequelize repository
428+
429+
```ts
430+
import {AuditLog} from '@sourceloop/auidt-log/';
431+
import {AuditLogRepository} from '@sourceloop/audit-log/sequelize';
432+
433+
this.repositories = [AuditLogRepository<AuditLog>];
434+
```
435+
366436
## Feedback
367437

368438
If you've noticed a bug or have a question or have a feature request, [search the issue tracker](https://github.com/sourcefuse/loopback4-audit-log/issues) to see if someone else in the community has already created a ticket.
@@ -381,3 +451,7 @@ Code of conduct guidelines [here](https://github.com/sourcefuse/loopback4-audit-
381451
## License
382452

383453
[MIT](https://github.com/sourcefuse/loopback4-audit-log/blob/master/LICENSE)
454+
455+
```
456+
457+
```

0 commit comments

Comments
 (0)