@ubs-platform/crud-base 6.8.0 → 6.9.0
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.
- package/base-crud-service.d.ts +9 -0
- package/base-crud-service.js +75 -13
- package/base-crud-service.js.map +1 -1
- package/package.json +1 -1
package/base-crud-service.d.ts
CHANGED
|
@@ -3,8 +3,13 @@ import { FilterQuery } from 'mongoose';
|
|
|
3
3
|
import { BaseCrudKlass } from './base-crud-klass';
|
|
4
4
|
import { UserAuthBackendDTO } from '@ubs-platform/users-common';
|
|
5
5
|
import { IRepositoryWrap } from './repository-wrap';
|
|
6
|
+
import { CacheManagerService } from '@ubs-platform/cache-manager';
|
|
6
7
|
export declare abstract class BaseCrudService<MODEL, ID, INPUT, OUTPUT, SEARCH> extends BaseCrudKlass {
|
|
7
8
|
m: IRepositoryWrap<MODEL, ID, any>;
|
|
9
|
+
protected readonly isCacheEnabled = true;
|
|
10
|
+
protected readonly cacheLiveTimeMs = 3000;
|
|
11
|
+
protected readonly cacheKeyPrefix: string;
|
|
12
|
+
protected readonly cacheManager?: CacheManagerService;
|
|
8
13
|
constructor(m: IRepositoryWrap<MODEL, ID, any>);
|
|
9
14
|
abstract generateNewModel(): MODEL;
|
|
10
15
|
abstract getIdFieldNameFromInput(i: INPUT): ID;
|
|
@@ -19,6 +24,10 @@ export declare abstract class BaseCrudService<MODEL, ID, INPUT, OUTPUT, SEARCH>
|
|
|
19
24
|
create(input: INPUT, user?: UserAuthBackendDTO): Promise<OUTPUT>;
|
|
20
25
|
edit(input: INPUT, user?: UserAuthBackendDTO): Promise<OUTPUT>;
|
|
21
26
|
remove(id: ID, ruser?: UserAuthBackendDTO): Promise<OUTPUT>;
|
|
27
|
+
protected withShortCache<T>(key: string, cb: () => Promise<T>): Promise<T>;
|
|
28
|
+
protected buildCacheKey(method: string, ...parts: unknown[]): string;
|
|
29
|
+
protected serializeCachePart(part: unknown): string;
|
|
30
|
+
protected invalidateSelfCache(): void;
|
|
22
31
|
afterCreate(m: OUTPUT, input: INPUT, user?: UserAuthBackendDTO): Promise<void>;
|
|
23
32
|
beforeCreateOrEdit(model: MODEL, input: INPUT, mode: 'EDIT' | 'CREATE', user?: UserAuthBackendDTO): Promise<void>;
|
|
24
33
|
beforeRemove(model: MODEL, user?: UserAuthBackendDTO): Promise<void>;
|
package/base-crud-service.js
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.BaseCrudService = void 0;
|
|
4
13
|
const base_crud_klass_1 = require("./base-crud-klass");
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const cache_manager_1 = require("@ubs-platform/cache-manager");
|
|
16
|
+
const crypto_1 = require("crypto");
|
|
5
17
|
class BaseCrudService extends base_crud_klass_1.BaseCrudKlass {
|
|
6
18
|
constructor(m) {
|
|
7
19
|
super();
|
|
8
20
|
this.m = m;
|
|
21
|
+
this.isCacheEnabled = true;
|
|
22
|
+
this.cacheLiveTimeMs = 3000;
|
|
23
|
+
const klassName = this.constructor?.name?.trim();
|
|
24
|
+
this.cacheKeyPrefix = klassName
|
|
25
|
+
? `crud:${klassName}`
|
|
26
|
+
: `crud:${(0, crypto_1.randomUUID)()}`;
|
|
9
27
|
}
|
|
10
28
|
async convertAndReturnTheList(list, user) {
|
|
11
29
|
const outputList = [];
|
|
@@ -16,29 +34,38 @@ class BaseCrudService extends base_crud_klass_1.BaseCrudKlass {
|
|
|
16
34
|
return outputList;
|
|
17
35
|
}
|
|
18
36
|
async searchPagination(searchAndPagination, user) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
return this.withShortCache(this.buildCacheKey('searchPagination', searchAndPagination, user), async () => {
|
|
38
|
+
const page = searchAndPagination?.page || 0, size = searchAndPagination?.size || 10;
|
|
39
|
+
let s = await this.searchParams(searchAndPagination, user);
|
|
40
|
+
let sort;
|
|
41
|
+
if (searchAndPagination?.sortBy &&
|
|
42
|
+
searchAndPagination.sortRotation) {
|
|
43
|
+
sort = {};
|
|
44
|
+
sort[searchAndPagination.sortBy] =
|
|
45
|
+
searchAndPagination.sortRotation;
|
|
46
|
+
}
|
|
47
|
+
return (await this.m.modelSearch(size, page, sort, {
|
|
48
|
+
$match: s,
|
|
49
|
+
})).mapAsync((a) => this.toOutput(a));
|
|
50
|
+
});
|
|
29
51
|
}
|
|
30
52
|
async fetchAll(s, user) {
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
return this.withShortCache(this.buildCacheKey('fetchAll', s, user), async () => {
|
|
54
|
+
const list = await this.m.find(await this.searchParams(s, user));
|
|
55
|
+
return await this.convertAndReturnTheList(list, user);
|
|
56
|
+
});
|
|
33
57
|
}
|
|
34
58
|
async fetchOne(id, user) {
|
|
35
|
-
return this.
|
|
59
|
+
return this.withShortCache(this.buildCacheKey('fetchOne', id, user), async () => {
|
|
60
|
+
return this.toOutput((await this.m.findById(id)));
|
|
61
|
+
});
|
|
36
62
|
}
|
|
37
63
|
async create(input, user) {
|
|
38
64
|
let newModel = this.generateNewModel();
|
|
39
65
|
await this.moveIntoModel(newModel, input);
|
|
40
66
|
await this.beforeCreateOrEdit(newModel, input, 'CREATE', user);
|
|
41
67
|
await this.m.saveModel(newModel);
|
|
68
|
+
this.invalidateSelfCache();
|
|
42
69
|
const out = await this.toOutput(newModel);
|
|
43
70
|
await this.afterCreate(out, input, user);
|
|
44
71
|
return out;
|
|
@@ -48,18 +75,53 @@ class BaseCrudService extends base_crud_klass_1.BaseCrudKlass {
|
|
|
48
75
|
const newModel = await this.moveIntoModel(newModelFirst, input);
|
|
49
76
|
await this.beforeCreateOrEdit(newModel, input, 'EDIT', user);
|
|
50
77
|
await this.m.saveModel(newModel);
|
|
78
|
+
this.invalidateSelfCache();
|
|
51
79
|
return this.toOutput(newModel);
|
|
52
80
|
}
|
|
53
81
|
async remove(id, ruser) {
|
|
54
82
|
let ac = (await this.m.findById(id));
|
|
55
83
|
await this.beforeRemove(ac, ruser);
|
|
56
84
|
await this.m.deleteById(id);
|
|
85
|
+
this.invalidateSelfCache();
|
|
57
86
|
return this.toOutput(ac);
|
|
58
87
|
}
|
|
88
|
+
async withShortCache(key, cb) {
|
|
89
|
+
if (!this.isCacheEnabled || !this.cacheManager) {
|
|
90
|
+
return cb();
|
|
91
|
+
}
|
|
92
|
+
return this.cacheManager.getOrCallAsync(key, cb, {
|
|
93
|
+
livetime: this.cacheLiveTimeMs,
|
|
94
|
+
livetimeExtending: 'ON_GET',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
buildCacheKey(method, ...parts) {
|
|
98
|
+
const serialized = parts.map((part) => this.serializeCachePart(part));
|
|
99
|
+
return `${this.cacheKeyPrefix}:${method}:${serialized.join('|')}`;
|
|
100
|
+
}
|
|
101
|
+
serializeCachePart(part) {
|
|
102
|
+
try {
|
|
103
|
+
return JSON.stringify(part);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return String(part);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
invalidateSelfCache() {
|
|
110
|
+
if (!this.isCacheEnabled || !this.cacheManager) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const escapedPrefix = this.cacheKeyPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
114
|
+
this.cacheManager.invalidateRegex(new RegExp(`^${escapedPrefix}:`));
|
|
115
|
+
}
|
|
59
116
|
async afterCreate(m, input, user) { }
|
|
60
117
|
async beforeCreateOrEdit(model, input, mode, user) { }
|
|
61
118
|
async beforeRemove(model, user) {
|
|
62
119
|
}
|
|
63
120
|
}
|
|
64
121
|
exports.BaseCrudService = BaseCrudService;
|
|
122
|
+
__decorate([
|
|
123
|
+
(0, common_1.Optional)(),
|
|
124
|
+
(0, common_1.Inject)(cache_manager_1.CacheManagerService),
|
|
125
|
+
__metadata("design:type", cache_manager_1.CacheManagerService)
|
|
126
|
+
], BaseCrudService.prototype, "cacheManager", void 0);
|
|
65
127
|
//# sourceMappingURL=base-crud-service.js.map
|
package/base-crud-service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-crud-service.js","sourceRoot":"","sources":["../../../../../libs/crud-base/src/base-crud-service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base-crud-service.js","sourceRoot":"","sources":["../../../../../libs/crud-base/src/base-crud-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,uDAAkD;AAGlD,2CAAkD;AAClD,+DAAkE;AAClE,mCAAoC;AAGpC,MAAsB,eAMpB,SAAQ,+BAAa;IASnB,YAAmB,CAAkC;QACjD,KAAK,EAAE,CAAC;QADO,MAAC,GAAD,CAAC,CAAiC;QARlC,mBAAc,GAAG,IAAI,CAAC;QACtB,oBAAe,GAAG,IAAI,CAAC;QAStC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QACjD,IAAI,CAAC,cAAc,GAAG,SAA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C;CAEJ;AA5KD,0CA4KC;AA/JsB;IAFlB,IAAA,iBAAQ,GAAE;IACV,IAAA,eAAM,EAAC,mCAAmB,CAAC;8BACM,mCAAmB;qDAAC"}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@ubs-platform/crud-base","repository":{"url":"https://github.com/ubs-platform/ubs-mona-mr"},"homepage":"https://github.com/ubs-platform/ubs-mona-mr","version":"6.
|
|
1
|
+
{"name":"@ubs-platform/crud-base","repository":{"url":"https://github.com/ubs-platform/ubs-mona-mr"},"homepage":"https://github.com/ubs-platform/ubs-mona-mr","version":"6.9.0","iksir":{"type":"LIBRARY","libraryMode":"PEER","buildSubFolder":""},"peerDependencies":{"@nestjs/common":"^11.1.19","@ubs-platform/users-microservice-helper":"6.9.0","@ubs-platform/users-roles":"6.9.0","@ubs-platform/cache-manager":"6.9.0","@ubs-platform/crud-base-common":"6.9.0"}}
|