@drax/audit-back 0.43.0 → 0.44.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,24 @@
1
- import AuditRepository from '../../repository/AuditRepository.js';
1
+ import AuditMongoRepository from '../../repository/mongo/AuditMongoRepository.js';
2
+ import AuditSqliteRepository from "../../repository/sqlite/AuditSqliteRepository.js";
2
3
  import { AuditService } from '../../services/AuditService.js';
3
4
  import { AuditBaseSchema } from "../../schemas/AuditSchema.js";
5
+ import { COMMON, CommonConfig, DraxConfig } from "@drax/common-back";
4
6
  class AuditServiceFactory {
5
7
  static get instance() {
6
8
  if (!AuditServiceFactory.service) {
7
- const repository = new AuditRepository();
9
+ let repository;
10
+ switch (DraxConfig.getOrLoad(CommonConfig.DbEngine)) {
11
+ case COMMON.DB_ENGINES.MONGODB:
12
+ repository = new AuditMongoRepository();
13
+ break;
14
+ case COMMON.DB_ENGINES.SQLITE:
15
+ const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile);
16
+ repository = new AuditSqliteRepository(dbFile, false);
17
+ repository.build();
18
+ break;
19
+ default:
20
+ throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
21
+ }
8
22
  const schema = AuditBaseSchema;
9
23
  AuditServiceFactory.service = new AuditService(repository, schema);
10
24
  }
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import AuditPermissions from './permissions/AuditPermissions.js';
2
2
  import AuditSchema from './schemas/AuditSchema.js';
3
3
  import AuditModel from './models/AuditModel.js';
4
- import AuditRepository from './repository/AuditRepository.js';
4
+ import AuditMongoRepository from './repository/mongo/AuditMongoRepository.js';
5
+ import AuditSqliteRepository from './repository/sqlite/AuditSqliteRepository.js';
5
6
  import AuditService from './services/AuditService.js';
6
7
  import AuditServiceFactory from './factory/services/AuditServiceFactory.js';
7
8
  import AuditController from './controllers/AuditController.js';
8
9
  import AuditRoutes from './routes/AuditRoutes.js';
9
10
  import { RegisterCrudEvent } from './utils/RegisterCrudEvent.js';
10
- export { AuditPermissions, AuditSchema, AuditModel, AuditRepository, AuditService, AuditServiceFactory, AuditController, AuditRoutes, RegisterCrudEvent };
11
+ export { AuditPermissions, AuditSchema, AuditModel, AuditMongoRepository, AuditSqliteRepository, AuditService, AuditServiceFactory, AuditController, AuditRoutes, RegisterCrudEvent };
@@ -0,0 +1,12 @@
1
+ import { AbstractMongoRepository } from "@drax/crud-back";
2
+ import { AuditModel } from "../../models/AuditModel.js";
3
+ class AuditMongoRepository extends AbstractMongoRepository {
4
+ constructor() {
5
+ super();
6
+ this._model = AuditModel;
7
+ this._searchFields = ['entity', 'action', 'sessionId', 'requestId'];
8
+ this._populateFields = [];
9
+ }
10
+ }
11
+ export default AuditMongoRepository;
12
+ export { AuditMongoRepository };
@@ -0,0 +1,65 @@
1
+ import { AbstractSqliteRepository } from "@drax/crud-back";
2
+ class AuditSqliteRepository extends AbstractSqliteRepository {
3
+ constructor() {
4
+ super(...arguments);
5
+ this.tableName = 'audits';
6
+ this.searchFields = [];
7
+ this.booleanFields = [];
8
+ this.identifier = '_id';
9
+ this.populateFields = [];
10
+ this.tableFields = [
11
+ { name: "entity", type: "TEXT", unique: false, primary: false },
12
+ { name: "resourceId", type: "TEXT", unique: false, primary: false },
13
+ { name: "user", type: "TEXT", unique: false, primary: false },
14
+ { name: "action", type: "TEXT", unique: false, primary: false },
15
+ { name: "ip", type: "TEXT", unique: false, primary: false },
16
+ { name: "userAgent", type: "TEXT", unique: false, primary: false },
17
+ { name: "changes", type: "TEXT", unique: false, primary: false },
18
+ { name: "sessionId", type: "TEXT", unique: false, primary: false },
19
+ { name: "requestId", type: "TEXT", unique: false, primary: false },
20
+ { name: "detail", type: "TEXT", unique: false, primary: false },
21
+ { name: "tenant", type: "TEXT", unique: false, primary: false },
22
+ { name: "apiKey", type: "TEXT", unique: false, primary: false },
23
+ { name: "createdAt", type: "TEXT", unique: false, primary: false },
24
+ { name: "updatedAt", type: "TEXT", unique: false, primary: false },
25
+ ];
26
+ this.verbose = false;
27
+ }
28
+ async prepareData(data) {
29
+ if (!data) {
30
+ return;
31
+ }
32
+ if (data && data.changes) {
33
+ data.changes = JSON.stringify(data.changes);
34
+ }
35
+ if (data && data.user) {
36
+ data.user = JSON.stringify(data.user);
37
+ }
38
+ if (data && data.tenant) {
39
+ data.tenant = JSON.stringify(data.tenant);
40
+ }
41
+ if (data && data.apiKey) {
42
+ data.apiKey = JSON.stringify(data.apiKey);
43
+ }
44
+ }
45
+ async prepareItem(item) {
46
+ if (!item) {
47
+ return;
48
+ }
49
+ if (item && item.changes) {
50
+ item.changes = JSON.parse(item.changes);
51
+ }
52
+ if (item && item.user) {
53
+ item.user = JSON.parse(item.user);
54
+ }
55
+ if (item && item.tenant) {
56
+ item.tenant = JSON.parse(item.tenant);
57
+ }
58
+ if (item && item.apiKey) {
59
+ item.apiKey = JSON.parse(item.apiKey);
60
+ }
61
+ return item;
62
+ }
63
+ }
64
+ export default AuditSqliteRepository;
65
+ export { AuditSqliteRepository };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.43.0",
6
+ "version": "0.44.1",
7
7
  "description": "Audit backend",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -22,8 +22,8 @@
22
22
  "author": "Cristian Incarnato & Drax Team",
23
23
  "license": "ISC",
24
24
  "dependencies": {
25
- "@drax/crud-back": "^0.43.0",
26
- "@drax/crud-share": "^0.43.0",
25
+ "@drax/crud-back": "^0.44.1",
26
+ "@drax/crud-share": "^0.44.1",
27
27
  "mongoose": "^8.6.3",
28
28
  "mongoose-paginate-v2": "^1.8.3"
29
29
  },
@@ -39,5 +39,5 @@
39
39
  "tsc-alias": "^1.8.10",
40
40
  "typescript": "^5.6.2"
41
41
  },
42
- "gitHead": "3451a1c66eaf3facd4fef67d96750bf5d38a1d5c"
42
+ "gitHead": "f80d56ce3769fe36bf69f1903efa0a4290de06a2"
43
43
  }
@@ -1,14 +1,29 @@
1
1
 
2
- import AuditRepository from '../../repository/AuditRepository.js'
2
+ import AuditMongoRepository from '../../repository/mongo/AuditMongoRepository.js'
3
+ import AuditSqliteRepository from "../../repository/sqlite/AuditSqliteRepository.js";
4
+ import type {IAuditRepository} from "../../interfaces/IAuditRepository";
3
5
  import {AuditService} from '../../services/AuditService.js'
4
6
  import {AuditBaseSchema} from "../../schemas/AuditSchema.js";
7
+ import {COMMON, CommonConfig, DraxConfig} from "@drax/common-back";
5
8
 
6
9
  class AuditServiceFactory {
7
10
  private static service: AuditService;
8
11
 
9
12
  public static get instance(): AuditService {
10
13
  if (!AuditServiceFactory.service) {
11
- const repository = new AuditRepository();
14
+ let repository: IAuditRepository
15
+ switch (DraxConfig.getOrLoad(CommonConfig.DbEngine)) {
16
+ case COMMON.DB_ENGINES.MONGODB:
17
+ repository = new AuditMongoRepository()
18
+ break;
19
+ case COMMON.DB_ENGINES.SQLITE:
20
+ const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile)
21
+ repository = new AuditSqliteRepository(dbFile, false)
22
+ repository.build()
23
+ break;
24
+ default:
25
+ throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
26
+ }
12
27
  const schema = AuditBaseSchema;
13
28
  AuditServiceFactory.service = new AuditService(repository, schema);
14
29
  }
package/src/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import AuditPermissions from './permissions/AuditPermissions.js'
2
2
  import AuditSchema from './schemas/AuditSchema.js'
3
3
  import AuditModel from './models/AuditModel.js'
4
- import AuditRepository from './repository/AuditRepository.js'
4
+ import AuditMongoRepository from './repository/mongo/AuditMongoRepository.js'
5
+ import AuditSqliteRepository from './repository/sqlite/AuditSqliteRepository.js'
5
6
  import AuditService from './services/AuditService.js'
6
7
  import AuditServiceFactory from './factory/services/AuditServiceFactory.js'
7
8
  import AuditController from './controllers/AuditController.js'
@@ -17,7 +18,8 @@ export {
17
18
  AuditPermissions,
18
19
  AuditSchema,
19
20
  AuditModel,
20
- AuditRepository,
21
+ AuditMongoRepository,
22
+ AuditSqliteRepository,
21
23
  AuditService,
22
24
  AuditServiceFactory,
23
25
  AuditController,
@@ -1,7 +1,7 @@
1
1
 
2
2
  import {AbstractMongoRepository} from "@drax/crud-back";
3
- import {AuditModel} from "../models/AuditModel.js";
4
- import type {IAuditRepository} from '../interfaces/IAuditRepository'
3
+ import {AuditModel} from "../../models/AuditModel.js";
4
+ import type {IAuditRepository} from '../../interfaces/IAuditRepository'
5
5
  import type {IAudit, IAuditBase} from "@drax/audit-share";
6
6
 
7
7
 
@@ -0,0 +1,93 @@
1
+ import type {IAuditRepository} from '../../interfaces/IAuditRepository'
2
+ import {IAudit, IAuditBase} from "@drax/audit-share";
3
+ import {
4
+ SqliteTableField,
5
+ } from "@drax/common-back";
6
+ import {AbstractSqliteRepository} from "@drax/crud-back";
7
+
8
+
9
+ class AuditSqliteRepository extends AbstractSqliteRepository<IAudit, IAuditBase, IAuditBase> implements IAuditRepository {
10
+
11
+ protected db: any;
12
+ protected tableName: string = 'audits';
13
+ protected dataBaseFile: string;
14
+ protected searchFields: string[] = [];
15
+ protected booleanFields: string[] = [];
16
+ protected identifier: string = '_id';
17
+ protected populateFields = []
18
+ protected tableFields: SqliteTableField[] = [
19
+ {name: "entity", type: "TEXT", unique: false, primary: false},
20
+ {name: "resourceId", type: "TEXT", unique: false, primary: false},
21
+ {name: "user", type: "TEXT", unique: false, primary: false},
22
+ {name: "action", type: "TEXT", unique: false, primary: false},
23
+ {name: "ip", type: "TEXT", unique: false, primary: false},
24
+ {name: "userAgent", type: "TEXT", unique: false, primary: false},
25
+ {name: "changes", type: "TEXT", unique: false, primary: false},
26
+ {name: "sessionId", type: "TEXT", unique: false, primary: false},
27
+ {name: "requestId", type: "TEXT", unique: false, primary: false},
28
+ {name: "detail", type: "TEXT", unique: false, primary: false},
29
+ {name: "tenant", type: "TEXT", unique: false, primary: false},
30
+ {name: "apiKey", type: "TEXT", unique: false, primary: false},
31
+ {name: "createdAt", type: "TEXT", unique: false, primary: false},
32
+ {name: "updatedAt", type: "TEXT", unique: false, primary: false},
33
+ ]
34
+ protected verbose: boolean = false;
35
+
36
+
37
+ async prepareData(data: any) {
38
+ if (!data) {
39
+ return
40
+ }
41
+
42
+ if (data && data.changes) {
43
+ data.changes = JSON.stringify(data.changes)
44
+ }
45
+
46
+ if (data && data.user) {
47
+ data.user = JSON.stringify(data.user)
48
+ }
49
+
50
+ if (data && data.tenant) {
51
+ data.tenant = JSON.stringify(data.tenant)
52
+ }
53
+
54
+
55
+ if (data && data.apiKey) {
56
+ data.apiKey = JSON.stringify(data.apiKey)
57
+ }
58
+
59
+
60
+
61
+ }
62
+
63
+ async prepareItem(item: any) {
64
+
65
+ if (!item) {
66
+ return
67
+ }
68
+
69
+ if (item && item.changes) {
70
+ item.changes = JSON.parse(item.changes)
71
+ }
72
+
73
+
74
+ if (item && item.user) {
75
+ item.user = JSON.parse(item.user)
76
+ }
77
+
78
+ if (item && item.tenant) {
79
+ item.tenant = JSON.parse(item.tenant)
80
+ }
81
+
82
+ if (item && item.apiKey) {
83
+ item.apiKey = JSON.parse(item.apiKey)
84
+ }
85
+
86
+
87
+ return item;
88
+ }
89
+
90
+ }
91
+
92
+ export default AuditSqliteRepository
93
+ export {AuditSqliteRepository}