Skip to content

Commit

Permalink
feat(sequelize): add sequelize support
Browse files Browse the repository at this point in the history
modified mixin argument type to accept non default crud repositories
added directory import syntax to provide sequelize specific artifacts

GH-69
  • Loading branch information
shubhamp-sf committed May 25, 2023
1 parent 11209e5 commit db8a0b9
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 16 deletions.
1 change: 1 addition & 0 deletions .cz-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
{name: 'core'},
{name: 'maintenance'},
{name: 'mixin'},
{name: 'sequelize'},
],

appendBranchNameToCommitMessage: true,
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,42 @@ export class GroupRepository extends ConditionalAuditRepositoryMixin(
}
```

## Using with Sequelize ORM

This extension provides support to both juggler (the default loopback ORM) and sequelize.

If your loopback project is already using `SequelizeCrudRepository` from [@loopback/sequelize](https://www.npmjs.com/package/@loopback/sequelize) or equivalent add on repositories from sourceloop packages like [`SequelizeUserModifyCrudRepository`](https://sourcefuse.github.io/arc-docs/arc-api-docs/packages/core/#sequelizeusermodifycrudrepository). You'll need to make just two changes:

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';

export class AuditDataSource
extends SequelizeDataSource
implements LifeCycleObserver
{
static dataSourceName = AuditDbSourceName;
static readonly defaultConfig = config;

constructor(
@inject('datasources.config.audit', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
```

## 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.
Expand Down
162 changes: 157 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./sequelize": {
"types": "./dist/index.sequelize.d.ts",
"default": "./dist/index.sequelize.js"
}
},
"typesVersions": {
"*": {
"sequelize": [
"./dist/index.sequelize.d.ts"
]
}
},
"engines": {
"node": ">=16"
},
Expand Down Expand Up @@ -46,6 +60,7 @@
"@loopback/boot": "^5.0.9",
"@loopback/core": "^4.0.9",
"@loopback/repository": "^5.1.5",
"@loopback/sequelize": "^0.2.0",
"jsdom": "^20.0.3",
"lodash": "^4.17.21",
"tslib": "^2.4.1"
Expand Down
5 changes: 5 additions & 0 deletions src/index.sequelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './component';
export * from './mixins';
export * from './types';
export * from './models';
export * from './repositories/sequelize';
16 changes: 7 additions & 9 deletions src/mixins/audit.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {
Count,
DataObject,
DefaultCrudRepository,
Entity,
Where,
} from '@loopback/repository';
import {Count, DataObject, Entity, Where} from '@loopback/repository';
import {keyBy, cloneDeep} from 'lodash';

import {Action, AuditLog} from '../models';
import {AuditLogRepository} from '../repositories';
import {AuditLogRepository as SequelizeAuditLogRepository} from '../repositories/sequelize';
import {
AbstractConstructor,
AuditMixinBase,
AuditOptions,
IAuditMixin,
IAuditMixinOptions,
Expand All @@ -23,7 +19,7 @@ export function AuditRepositoryMixin<
Relations extends object,
UserID,
//sonarignore:end
R extends AbstractConstructor<DefaultCrudRepository<M, ID, Relations>>,
R extends AuditMixinBase<M, ID, Relations>,
>(
superClass: R,
opts: IAuditMixinOptions,
Expand All @@ -32,7 +28,9 @@ export function AuditRepositoryMixin<
extends superClass
implements IAuditMixin<UserID>
{
getAuditLogRepository: () => Promise<AuditLogRepository>;
getAuditLogRepository: () => Promise<
AuditLogRepository | SequelizeAuditLogRepository
>;
getCurrentUser?: () => Promise<{id?: UserID}>;

async create(
Expand Down
Loading

0 comments on commit db8a0b9

Please sign in to comment.