diff --git a/README.md b/README.md index ad15dd2..766edee 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ npm install @sourceloop/audit-log In order to use this component into your LoopBack application, please follow below steps. -- Add audit model class as Entity. +- 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 ```ts import {Entity, model, property} from '@loopback/repository'; @@ -154,7 +154,8 @@ export class AuditDataSource } ``` -- 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. +- If you have a custom model and you wish to use that in your repository class you can + 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. `@inject(`datasources.\${AuditDbSourceName}`) dataSource: AuditDataSource,` One example below. @@ -187,8 +188,7 @@ import {Group, GroupRelations} from '../models'; import {PgdbDataSource} from '../datasources'; import {inject, Getter, Constructor} from '@loopback/core'; import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; -import {AuditRepositoryMixin} from '@sourceloop/audit-log'; -import {AuditLogRepository} from './audit-log.repository'; +import {AuditRepositoryMixin, AuditLogRepository} from '@sourceloop/audit-log'; const groupAuditOpts: IAuditMixinOptions = { actionKey: 'Group_Logs', @@ -207,7 +207,7 @@ export class GroupRepository extends AuditRepositoryMixin< @inject('datasources.pgdb') dataSource: PgdbDataSource, @inject.getter(AuthenticationBindings.CURRENT_USER) public getCurrentUser: Getter, - @repository.getter('AuditLogRepository') + @repository.getter(AuditLogRepository) public getAuditLogRepository: Getter, ) { super(Group, dataSource, getCurrentUser); @@ -215,6 +215,56 @@ export class GroupRepository extends AuditRepositoryMixin< } ``` +and also bind the component of this extension in your application.ts + +```ts +import {AuditLogComponent} from '@sourceloop/audit-log'; + +this.component(AuditLogComponent); +``` + +- The above code uses the default repository provided by this extension +- If you wish to use your custom repository and model class do the following + +```ts +import {repository} from '@loopback/repository'; +import {Group, GroupRelations} from '../models'; +import {PgdbDataSource} from '../datasources'; +import {inject, Getter, Constructor} from '@loopback/core'; +import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; +import {AuditRepositoryMixin} from '@sourceloop/audit-log'; +import {CustomAuditLogRepository} from './audit-log.repository'; +import {CustomAuditLogModel} from '../models'; + +const groupAuditOpts: IAuditMixinOptions = { + actionKey: 'Group_Logs', +}; + +export class GroupRepository extends AuditRepositoryMixin< + Group, + typeof Group.prototype.id, + GroupRelations, + string, + Constructor< + DefaultCrudRepository + >, + // pass the below two parameters when you want your custom model to be used + // otheriwise the default model and repository will be used + CustomAuditLogModel, + CustomAuditLogRepository +>(DefaultCrudRepository, groupAuditOpts) { + constructor( + @inject('datasources.pgdb') dataSource: PgdbDataSource, + @inject.getter(AuthenticationBindings.CURRENT_USER) + public getCurrentUser: Getter, + @repository.getter(CustomAuditLogRepository) + public getAuditLogRepository: Getter, + ) { + super(Group, dataSource, getCurrentUser); + } +} +``` + You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function. Make sure you provide `getCurrentUser` and `getAuditLogRepository` Getter functions in constructor. @@ -227,7 +277,7 @@ This will create all insert, update, delete audits for this model. create(data, {noAudit: true}); ``` -- The Actor field is now configurable and can save any string type value in the field. +- The Actor field is configurable and can save any string type value in the field. Though the default value will be userId a developer can save any string field from the current User that is being passed. ```ts @@ -265,6 +315,15 @@ this.bind(AuthServiceBindings.ActorIdKey).to('username'); public actorIdKey?: ActorId, ``` +#### Making current user not mandatory + +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 +options just like + +```ts +await productRepo.create(product, {noAudit: false, actorId: 'userId'}); +``` + - 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`. ```ts @@ -273,8 +332,10 @@ import {Group, GroupRelations} from '../models'; import {PgdbDataSource} from '../datasources'; import {inject, Getter, Constructor} from '@loopback/core'; import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; -import {ConditionalAuditRepositoryMixin} from '@sourceloop/audit-log'; -import {AuditLogRepository} from './audit-log.repository'; +import { + ConditionalAuditRepositoryMixin, + AuditLogRepository, +} from '@sourceloop/audit-log'; const groupAuditOpts: IAuditMixinOptions = { actionKey: 'Group_Logs', @@ -300,15 +361,6 @@ export class GroupRepository extends ConditionalAuditRepositoryMixin( } ``` -### Making current user not mandatory - -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 -options just like - -```ts -await productRepo.create(product, {noAudit: false, actorId: 'userId'}); -``` - ## Using with Sequelize ORM This extension provides support to both juggler (the default loopback ORM) and sequelize. @@ -318,33 +370,50 @@ If your loopback project is already using `SequelizeCrudRepository` from [@loopb 1. The import statements should have the suffix `/sequelize`, like below: ```ts -import { - AuditRepositoryMixin, - AuditLogRepository, -} from '@sourceloop/audit-log/sequelize'; -``` - -2. The Audit datasource's parent class should be `SequelizeDataSource`. - -```ts -import {SequelizeDataSource} from '@loopback/sequelize'; +import {repository} from '@loopback/repository'; +import {Group, GroupRelations} from '../models'; +import {PgdbDataSource} from '../datasources'; +import {inject, Getter, Constructor} from '@loopback/core'; +import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; +import {AuditRepositoryMixin} from '@sourceloop/audit-log'; +import {AuditLogRepository as SequelizeAuditLogRepository} from '@sourceloop/auidt-log/sequelize'; -export class AuditDataSource - extends SequelizeDataSource - implements LifeCycleObserver -{ - static dataSourceName = AuditDbSourceName; - static readonly defaultConfig = config; +const groupAuditOpts: IAuditMixinOptions = { + actionKey: 'Group_Logs', +}; +export class GroupRepository extends AuditRepositoryMixin< + Group, + typeof Group.prototype.id, + GroupRelations, + string, + Constructor< + DefaultCrudRepository + >, + AuditLog, + SequelizeAuditLogRepository +>(DefaultCrudRepository, groupAuditOpts) { constructor( - @inject('datasources.config.audit', {optional: true}) - dsConfig: object = config, + @inject('datasources.pgdb') dataSource: PgdbDataSource, + @inject.getter(AuthenticationBindings.CURRENT_USER) + public getCurrentUser: Getter, + @repository.getter(AuditLogRepository) + public getAuditLogRepository: Getter, ) { - super(dsConfig); + super(Group, dataSource, getCurrentUser); } } ``` +Also add the following to your application.ts file to bind the Sequelize repository + +```ts +import {AuditLog} from '@sourceloop/auidt-log/'; +import {AuditLogRepository} from '@sourceloop/auidt-log/sequelize'; + +this.repositories = [AuditLogRepository]; +``` + ## Feedback 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. @@ -363,3 +432,7 @@ Code of conduct guidelines [here](https://github.com/sourcefuse/loopback4-audit- ## License [MIT](https://github.com/sourcefuse/loopback4-audit-log/blob/master/LICENSE) + +``` + +``` diff --git a/docs/README.md b/docs/README.md index ad15dd2..766edee 100644 --- a/docs/README.md +++ b/docs/README.md @@ -38,7 +38,7 @@ npm install @sourceloop/audit-log In order to use this component into your LoopBack application, please follow below steps. -- Add audit model class as Entity. +- 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 ```ts import {Entity, model, property} from '@loopback/repository'; @@ -154,7 +154,8 @@ export class AuditDataSource } ``` -- 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. +- If you have a custom model and you wish to use that in your repository class you can + 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. `@inject(`datasources.\${AuditDbSourceName}`) dataSource: AuditDataSource,` One example below. @@ -187,8 +188,7 @@ import {Group, GroupRelations} from '../models'; import {PgdbDataSource} from '../datasources'; import {inject, Getter, Constructor} from '@loopback/core'; import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; -import {AuditRepositoryMixin} from '@sourceloop/audit-log'; -import {AuditLogRepository} from './audit-log.repository'; +import {AuditRepositoryMixin, AuditLogRepository} from '@sourceloop/audit-log'; const groupAuditOpts: IAuditMixinOptions = { actionKey: 'Group_Logs', @@ -207,7 +207,7 @@ export class GroupRepository extends AuditRepositoryMixin< @inject('datasources.pgdb') dataSource: PgdbDataSource, @inject.getter(AuthenticationBindings.CURRENT_USER) public getCurrentUser: Getter, - @repository.getter('AuditLogRepository') + @repository.getter(AuditLogRepository) public getAuditLogRepository: Getter, ) { super(Group, dataSource, getCurrentUser); @@ -215,6 +215,56 @@ export class GroupRepository extends AuditRepositoryMixin< } ``` +and also bind the component of this extension in your application.ts + +```ts +import {AuditLogComponent} from '@sourceloop/audit-log'; + +this.component(AuditLogComponent); +``` + +- The above code uses the default repository provided by this extension +- If you wish to use your custom repository and model class do the following + +```ts +import {repository} from '@loopback/repository'; +import {Group, GroupRelations} from '../models'; +import {PgdbDataSource} from '../datasources'; +import {inject, Getter, Constructor} from '@loopback/core'; +import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; +import {AuditRepositoryMixin} from '@sourceloop/audit-log'; +import {CustomAuditLogRepository} from './audit-log.repository'; +import {CustomAuditLogModel} from '../models'; + +const groupAuditOpts: IAuditMixinOptions = { + actionKey: 'Group_Logs', +}; + +export class GroupRepository extends AuditRepositoryMixin< + Group, + typeof Group.prototype.id, + GroupRelations, + string, + Constructor< + DefaultCrudRepository + >, + // pass the below two parameters when you want your custom model to be used + // otheriwise the default model and repository will be used + CustomAuditLogModel, + CustomAuditLogRepository +>(DefaultCrudRepository, groupAuditOpts) { + constructor( + @inject('datasources.pgdb') dataSource: PgdbDataSource, + @inject.getter(AuthenticationBindings.CURRENT_USER) + public getCurrentUser: Getter, + @repository.getter(CustomAuditLogRepository) + public getAuditLogRepository: Getter, + ) { + super(Group, dataSource, getCurrentUser); + } +} +``` + You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function. Make sure you provide `getCurrentUser` and `getAuditLogRepository` Getter functions in constructor. @@ -227,7 +277,7 @@ This will create all insert, update, delete audits for this model. create(data, {noAudit: true}); ``` -- The Actor field is now configurable and can save any string type value in the field. +- The Actor field is configurable and can save any string type value in the field. Though the default value will be userId a developer can save any string field from the current User that is being passed. ```ts @@ -265,6 +315,15 @@ this.bind(AuthServiceBindings.ActorIdKey).to('username'); public actorIdKey?: ActorId, ``` +#### Making current user not mandatory + +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 +options just like + +```ts +await productRepo.create(product, {noAudit: false, actorId: 'userId'}); +``` + - 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`. ```ts @@ -273,8 +332,10 @@ import {Group, GroupRelations} from '../models'; import {PgdbDataSource} from '../datasources'; import {inject, Getter, Constructor} from '@loopback/core'; import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; -import {ConditionalAuditRepositoryMixin} from '@sourceloop/audit-log'; -import {AuditLogRepository} from './audit-log.repository'; +import { + ConditionalAuditRepositoryMixin, + AuditLogRepository, +} from '@sourceloop/audit-log'; const groupAuditOpts: IAuditMixinOptions = { actionKey: 'Group_Logs', @@ -300,15 +361,6 @@ export class GroupRepository extends ConditionalAuditRepositoryMixin( } ``` -### Making current user not mandatory - -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 -options just like - -```ts -await productRepo.create(product, {noAudit: false, actorId: 'userId'}); -``` - ## Using with Sequelize ORM This extension provides support to both juggler (the default loopback ORM) and sequelize. @@ -318,33 +370,50 @@ If your loopback project is already using `SequelizeCrudRepository` from [@loopb 1. The import statements should have the suffix `/sequelize`, like below: ```ts -import { - AuditRepositoryMixin, - AuditLogRepository, -} from '@sourceloop/audit-log/sequelize'; -``` - -2. The Audit datasource's parent class should be `SequelizeDataSource`. - -```ts -import {SequelizeDataSource} from '@loopback/sequelize'; +import {repository} from '@loopback/repository'; +import {Group, GroupRelations} from '../models'; +import {PgdbDataSource} from '../datasources'; +import {inject, Getter, Constructor} from '@loopback/core'; +import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication'; +import {AuditRepositoryMixin} from '@sourceloop/audit-log'; +import {AuditLogRepository as SequelizeAuditLogRepository} from '@sourceloop/auidt-log/sequelize'; -export class AuditDataSource - extends SequelizeDataSource - implements LifeCycleObserver -{ - static dataSourceName = AuditDbSourceName; - static readonly defaultConfig = config; +const groupAuditOpts: IAuditMixinOptions = { + actionKey: 'Group_Logs', +}; +export class GroupRepository extends AuditRepositoryMixin< + Group, + typeof Group.prototype.id, + GroupRelations, + string, + Constructor< + DefaultCrudRepository + >, + AuditLog, + SequelizeAuditLogRepository +>(DefaultCrudRepository, groupAuditOpts) { constructor( - @inject('datasources.config.audit', {optional: true}) - dsConfig: object = config, + @inject('datasources.pgdb') dataSource: PgdbDataSource, + @inject.getter(AuthenticationBindings.CURRENT_USER) + public getCurrentUser: Getter, + @repository.getter(AuditLogRepository) + public getAuditLogRepository: Getter, ) { - super(dsConfig); + super(Group, dataSource, getCurrentUser); } } ``` +Also add the following to your application.ts file to bind the Sequelize repository + +```ts +import {AuditLog} from '@sourceloop/auidt-log/'; +import {AuditLogRepository} from '@sourceloop/auidt-log/sequelize'; + +this.repositories = [AuditLogRepository]; +``` + ## Feedback 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. @@ -363,3 +432,7 @@ Code of conduct guidelines [here](https://github.com/sourcefuse/loopback4-audit- ## License [MIT](https://github.com/sourcefuse/loopback4-audit-log/blob/master/LICENSE) + +``` + +``` diff --git a/package-lock.json b/package-lock.json index 8f4d36f..1fd71c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sourceloop/audit-log", - "version": "8.0.1", + "version": "8.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sourceloop/audit-log", - "version": "8.0.1", + "version": "8.0.2", "dependencies": { "@loopback/boot": "^7.0.2", "@loopback/core": "^6.0.2", diff --git a/src/__tests__/acceptance/fixtures/dummy-application.ts b/src/__tests__/acceptance/fixtures/dummy-application.ts index 72cd0da..b014489 100644 --- a/src/__tests__/acceptance/fixtures/dummy-application.ts +++ b/src/__tests__/acceptance/fixtures/dummy-application.ts @@ -9,10 +9,10 @@ import {RestApplication} from '@loopback/rest'; import {TenantUtilitiesBindings} from '@sourceloop/core'; import * as path from 'path'; import {TestRepository} from './repositories/test.repository'; -import {AuditLogRepository} from '../../../repositories'; import {TestAuditLogErrorRepository} from './repositories/audit-error.repository'; import {TestErrorRepository} from './repositories/test-error.repository'; import {AuditLogComponent} from '../../../component'; +import {TestAuditLogRepository} from './repositories/audit.repository'; export {ApplicationConfig}; @@ -29,7 +29,7 @@ export class DummyAuditServiceApplication extends BootMixin( this.component(AuditLogComponent); - this.repository(AuditLogRepository); + this.repository(TestAuditLogRepository); this.repository(TestRepository); this.repository(TestAuditLogErrorRepository); this.repository(TestErrorRepository); diff --git a/src/__tests__/acceptance/fixtures/models/audit.model.ts b/src/__tests__/acceptance/fixtures/models/audit.model.ts index 569e663..8317d3f 100644 --- a/src/__tests__/acceptance/fixtures/models/audit.model.ts +++ b/src/__tests__/acceptance/fixtures/models/audit.model.ts @@ -54,13 +54,6 @@ export class TestAuditLog extends Entity { }) actor: string; - @property({ - name: 'tenant_id', - type: 'string', - required: true, - }) - tenantId: string; - @property({ type: 'object', }) diff --git a/src/__tests__/acceptance/fixtures/repositories/audit.repository.ts b/src/__tests__/acceptance/fixtures/repositories/audit.repository.ts index 1ddcd6b..a3355ff 100644 --- a/src/__tests__/acceptance/fixtures/repositories/audit.repository.ts +++ b/src/__tests__/acceptance/fixtures/repositories/audit.repository.ts @@ -1,18 +1,13 @@ -import {DefaultCrudRepository, Entity, juggler} from '@loopback/repository'; +import {DefaultCrudRepository, juggler} from '@loopback/repository'; import {TestAuditLog} from '../models/audit.model'; -import {tenantGuard} from '@sourceloop/core'; import {inject} from '@loopback/core'; -@tenantGuard() export class TestAuditLogRepository extends DefaultCrudRepository< TestAuditLog, - typeof TestAuditLog.prototype.id + typeof TestAuditLog.prototype.id, + {} > { - constructor( - @inject('datasources.AuditDB') dataSource: juggler.DataSource, - @inject('models.AuditLog') - private readonly auditLog: typeof Entity & {prototype: TestAuditLog}, - ) { - super(auditLog, dataSource); + constructor(@inject('datasources.AuditDB') dataSource: juggler.DataSource) { + super(TestAuditLog, dataSource); } } diff --git a/src/__tests__/acceptance/fixtures/repositories/test-error.repository.ts b/src/__tests__/acceptance/fixtures/repositories/test-error.repository.ts index efd657e..4d243ea 100644 --- a/src/__tests__/acceptance/fixtures/repositories/test-error.repository.ts +++ b/src/__tests__/acceptance/fixtures/repositories/test-error.repository.ts @@ -1,6 +1,5 @@ import {Constructor, Getter, inject} from '@loopback/core'; import {DefaultCrudRepository, repository} from '@loopback/repository'; - import { AuditLogRepository, AuditRepositoryMixin, @@ -8,14 +7,11 @@ import { } from '../../../..'; import {TestDataSource} from '../datasources/test.datasource'; import {TestModel, TestModelRelations} from '../models/test.model'; - -import {AuditLogRepository as SequelizeAuditLogRepository} from '../../../../repositories/sequelize'; import {IAuthUserWithPermissions} from '@sourceloop/core'; export const testAuditOpts: IAuditMixinOptions = { actionKey: 'Item_Logs', }; - export class TestErrorRepository extends AuditRepositoryMixin< TestModel, typeof TestModel.prototype.id, @@ -34,9 +30,7 @@ export class TestErrorRepository extends AuditRepositoryMixin< @inject.getter('sf.userAuthentication.currentUser') public getCurrentUser: Getter, @repository.getter('TestAuditLogErrorRepository') - public getAuditLogRepository: Getter< - AuditLogRepository | SequelizeAuditLogRepository - >, + public getAuditLogRepository: Getter, ) { super(TestModel, dataSource); } diff --git a/src/__tests__/acceptance/fixtures/repositories/test.repository.ts b/src/__tests__/acceptance/fixtures/repositories/test.repository.ts index 4f19e7c..839e8af 100644 --- a/src/__tests__/acceptance/fixtures/repositories/test.repository.ts +++ b/src/__tests__/acceptance/fixtures/repositories/test.repository.ts @@ -1,16 +1,13 @@ import {Constructor, Getter, inject} from '@loopback/core'; import {DefaultCrudRepository, repository} from '@loopback/repository'; - -import { - AuditLogRepository, - AuditRepositoryMixin, - IAuditMixinOptions, -} from '../../../..'; import {TestDataSource} from '../datasources/test.datasource'; import {TestModel, TestModelRelations} from '../models/test.model'; - -import {AuditLogRepository as SequelizeAuditLogRepository} from '../../../../repositories/sequelize'; import {IAuthUserWithPermissions} from '@sourceloop/core'; +import {AuditRepositoryMixin} from '../../../../mixins'; +import {IAuditMixinOptions} from '../../../../types'; +import {AuditLogRepository} from '../../../../repositories'; +import {TestAuditLog} from '../models/audit.model'; +import {TestAuditLogRepository} from './audit.repository'; export const testAuditOpts: IAuditMixinOptions = { actionKey: 'Item_Logs', @@ -27,16 +24,16 @@ export class TestRepository extends AuditRepositoryMixin< typeof TestModel.prototype.id, TestModelRelations > - > + >, + TestAuditLog, + TestAuditLogRepository >(DefaultCrudRepository, testAuditOpts) { constructor( @inject('datasources.test') dataSource: TestDataSource, @inject.getter('sf.userAuthentication.currentUser') public getCurrentUser: Getter, @repository.getter('TestAuditLogRepository') - public getAuditLogRepository: Getter< - AuditLogRepository | SequelizeAuditLogRepository - >, + public getAuditLogRepository: Getter, ) { super(TestModel, dataSource); } diff --git a/src/component.ts b/src/component.ts index ec20f67..f4eddf3 100644 --- a/src/component.ts +++ b/src/component.ts @@ -1,13 +1,15 @@ import {Component, CoreBindings, ProviderMap, inject} from '@loopback/core'; -import {Class, Model} from '@loopback/repository'; +import {Class, Model, Repository} from '@loopback/repository'; import {AuditLog} from './models'; import {AuditLog as TenantAuditLog} from './models/tenant-support'; import { ITenantUtilitiesConfig, TenantUtilitiesBindings, TenantUtilitiesComponent, + tenantGuard, } from '@sourceloop/core'; import {RestApplication} from '@loopback/rest'; +import {AuditLogRepository} from './repositories'; export class AuditLogComponent implements Component { constructor( @@ -19,10 +21,21 @@ export class AuditLogComponent implements Component { this.application.component(TenantUtilitiesComponent); if (this.config?.useSingleTenant) { this.models = [AuditLog]; + this.repositories = [AuditLogRepository]; } else { this.models = [TenantAuditLog]; + this.repositories = [tenantGuard()(AuditLogRepository)]; } } providers?: ProviderMap = {}; + /** + * An optional list of Model classes to bind for dependency injection + * via `app.model()` API. + */ models?: Class[]; + /** + * An optional list of Repository classes to bind for dependency injection + * via `app.repository()` API. + */ + repositories?: Class>[]; //define repositories } diff --git a/src/mixins/audit.mixin.ts b/src/mixins/audit.mixin.ts index ca607dc..17286c4 100644 --- a/src/mixins/audit.mixin.ts +++ b/src/mixins/audit.mixin.ts @@ -2,13 +2,13 @@ import { AnyObject, Count, DataObject, + DefaultCrudRepository, Entity, Where, } from '@loopback/repository'; import {cloneDeep, isArray, keyBy} from 'lodash'; import {Action, AuditLog} from '../models'; import {AuditLogRepository} from '../repositories'; -import {AuditLogRepository as SequelizeAuditLogRepository} from '../repositories/sequelize'; import { AbstractConstructor, ActorId, @@ -18,6 +18,7 @@ import { IAuditMixinOptions, User, } from '../types'; +import {SequelizeCrudRepository} from '@loopback/sequelize'; // NOSONAR - ignore camelCase naming convention export function AuditRepositoryMixin< @@ -26,17 +27,23 @@ export function AuditRepositoryMixin< Relations extends object, UserID, R extends AuditMixinBase, + LogModel extends AuditLog = AuditLog, + Repo extends + | DefaultCrudRepository + | SequelizeCrudRepository< + LogModel, + typeof AuditLog.prototype.id, + {} + > = AuditLogRepository, >( superClass: R, opts: IAuditMixinOptions, -): R & AbstractConstructor> { +): R & AbstractConstructor> { abstract class MixedRepository extends superClass - implements IAuditMixin + implements IAuditMixin { - getAuditLogRepository: () => Promise< - AuditLogRepository | SequelizeAuditLogRepository - >; + getAuditLogRepository: () => Promise; getCurrentUser?: () => Promise; actorIdKey?: ActorId; diff --git a/src/repositories/audit.repository.ts b/src/repositories/audit.repository.ts index d6bf83d..8790501 100644 --- a/src/repositories/audit.repository.ts +++ b/src/repositories/audit.repository.ts @@ -1,19 +1,15 @@ import {inject} from '@loopback/core'; import {juggler, DefaultCrudRepository, Entity} from '@loopback/repository'; - -import {AuditLog} from '../models/tenant-support'; +import {AuditLog} from '../models'; import {AuditDbSourceName} from '../types'; -import {tenantGuard} from '@sourceloop/core'; -@tenantGuard() -export class AuditLogRepository extends DefaultCrudRepository< - AuditLog, - typeof AuditLog.prototype.id -> { +export class AuditLogRepository< + LogModel extends AuditLog = AuditLog, +> extends DefaultCrudRepository { constructor( @inject(`datasources.${AuditDbSourceName}`) dataSource: juggler.DataSource, @inject('models.AuditLog') - private readonly auditLog: typeof Entity & {prototype: AuditLog}, + auditLog: typeof Entity & {prototype: LogModel}, ) { super(auditLog, dataSource); } diff --git a/src/repositories/sequelize/audit.repository.ts b/src/repositories/sequelize/audit.repository.ts index c6eddc6..c2307ad 100644 --- a/src/repositories/sequelize/audit.repository.ts +++ b/src/repositories/sequelize/audit.repository.ts @@ -1,22 +1,19 @@ import {inject} from '@loopback/core'; -import {AuditLog} from '../../models/tenant-support'; import {AuditDbSourceName} from '../../types'; import { SequelizeCrudRepository, SequelizeDataSource, } from '@loopback/sequelize'; import {Entity} from '@loopback/repository'; -import {tenantGuard} from '@sourceloop/core'; +import {AuditLog} from '../../models'; -@tenantGuard() -export class AuditLogRepository extends SequelizeCrudRepository< - AuditLog, - typeof AuditLog.prototype.id -> { +export class AuditLogRepository< + LogModel extends AuditLog = AuditLog, +> extends SequelizeCrudRepository { constructor( @inject(`datasources.${AuditDbSourceName}`) dataSource: SequelizeDataSource, @inject('models.AuditLog') - private readonly auditLog: typeof Entity & {prototype: AuditLog}, + auditLog: typeof Entity & {prototype: LogModel}, ) { super(auditLog, dataSource); } diff --git a/src/types.ts b/src/types.ts index 744cbc4..147ce05 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,3 @@ -import {AuditLogRepository} from './repositories'; -import {AuditLogRepository as SequelizeAuditLogRepository} from './repositories/sequelize'; import { Count, DataObject, @@ -12,10 +10,8 @@ import { export const AuditDbSourceName = 'AuditDB'; // eslint-disable-next-line @typescript-eslint/no-unused-vars -export interface IAuditMixin { - getAuditLogRepository: () => Promise< - AuditLogRepository | SequelizeAuditLogRepository - >; +export interface IAuditMixin { + getAuditLogRepository: () => Promise; getCurrentUser?: () => Promise; actorIdKey?: ActorId; }