@nest-omni/core 4.1.3-20 → 4.1.3-23
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/audit/audit.module.d.ts +1 -0
- package/audit/audit.module.js +5 -3
- package/audit/controllers/audit.controller.d.ts +3 -11
- package/audit/controllers/audit.controller.js +12 -19
- package/audit/decorators/audit-operation.decorator.d.ts +0 -7
- package/audit/decorators/audit-operation.decorator.js +0 -7
- package/audit/dto/audit-action-query.dto.d.ts +13 -0
- package/audit/dto/audit-action-query.dto.js +77 -0
- package/audit/dto/index.d.ts +1 -0
- package/audit/dto/index.js +1 -0
- package/audit/entities/entity-audit-log.entity.d.ts +1 -4
- package/audit/entities/entity-audit-log.entity.js +1 -17
- package/audit/entities/manual-operation-log.entity.d.ts +0 -2
- package/audit/entities/manual-operation-log.entity.js +0 -8
- package/audit/enums/audit.enums.d.ts +0 -8
- package/audit/enums/audit.enums.js +1 -10
- package/audit/examples/decorator-value-mapping.example.d.ts +70 -0
- package/audit/examples/decorator-value-mapping.example.js +414 -0
- package/audit/index.d.ts +1 -0
- package/audit/index.js +5 -1
- package/audit/interceptors/audit.interceptor.d.ts +1 -0
- package/audit/interceptors/audit.interceptor.js +19 -11
- package/audit/interfaces/audit.interfaces.d.ts +2 -17
- package/audit/services/audit-context.service.d.ts +9 -0
- package/audit/services/entity-audit.service.d.ts +65 -24
- package/audit/services/entity-audit.service.js +280 -93
- package/audit/services/manual-audit-log.service.d.ts +0 -1
- package/audit/services/manual-audit-log.service.js +1 -3
- package/audit/subscribers/entity-audit.subscriber.d.ts +1 -0
- package/audit/subscribers/entity-audit.subscriber.js +22 -5
- package/cache/cache.module.d.ts +7 -2
- package/cache/cache.module.js +9 -7
- package/cache/cache.service.d.ts +4 -4
- package/cache/cache.service.js +5 -5
- package/cache/entities/index.d.ts +1 -0
- package/cache/entities/index.js +17 -0
- package/cache/entities/typeorm-cache.entity.d.ts +71 -0
- package/cache/entities/typeorm-cache.entity.js +110 -0
- package/cache/index.d.ts +2 -1
- package/cache/index.js +19 -2
- package/cache/providers/index.d.ts +2 -1
- package/cache/providers/index.js +2 -1
- package/cache/providers/lrucache.provider.d.ts +76 -0
- package/cache/providers/lrucache.provider.js +226 -0
- package/cache/providers/typeorm-cache.provider.d.ts +211 -0
- package/cache/providers/typeorm-cache.provider.js +483 -0
- package/common/boilerplate.polyfill.d.ts +1 -0
- package/common/boilerplate.polyfill.js +17 -0
- package/common/helpers/validation-metadata-helper.d.ts +55 -0
- package/common/helpers/validation-metadata-helper.js +60 -0
- package/common/index.d.ts +1 -0
- package/common/index.js +4 -0
- package/decorators/field.decorators.d.ts +71 -2
- package/decorators/field.decorators.js +147 -18
- package/decorators/transform.decorators.d.ts +0 -2
- package/decorators/transform.decorators.js +0 -23
- package/filters/bad-request.filter.js +19 -4
- package/http-client/examples/axios-config-extended.example.d.ts +17 -0
- package/http-client/examples/axios-config-extended.example.js +313 -0
- package/http-client/examples/index.d.ts +2 -0
- package/http-client/examples/index.js +2 -0
- package/http-client/examples/ssl-certificate.example.d.ts +47 -0
- package/http-client/examples/ssl-certificate.example.js +431 -0
- package/http-client/index.d.ts +1 -1
- package/http-client/interfaces/http-client-config.interface.d.ts +73 -0
- package/http-client/services/http-client.service.js +46 -5
- package/http-client/utils/context-extractor.util.js +2 -0
- package/ip-filter/constants.d.ts +21 -0
- package/ip-filter/constants.js +24 -0
- package/ip-filter/decorators/index.d.ts +1 -0
- package/ip-filter/decorators/index.js +17 -0
- package/ip-filter/decorators/ip-filter.decorator.d.ts +58 -0
- package/ip-filter/decorators/ip-filter.decorator.js +79 -0
- package/ip-filter/guards/index.d.ts +1 -0
- package/ip-filter/guards/index.js +17 -0
- package/ip-filter/guards/ip-filter.guard.d.ts +62 -0
- package/ip-filter/guards/ip-filter.guard.js +174 -0
- package/ip-filter/index.d.ts +7 -0
- package/ip-filter/index.js +23 -0
- package/ip-filter/interfaces/index.d.ts +4 -0
- package/ip-filter/interfaces/index.js +20 -0
- package/ip-filter/interfaces/ip-filter-async-options.interface.d.ts +15 -0
- package/ip-filter/interfaces/ip-filter-async-options.interface.js +2 -0
- package/ip-filter/interfaces/ip-filter-metadata.interface.d.ts +26 -0
- package/ip-filter/interfaces/ip-filter-metadata.interface.js +2 -0
- package/ip-filter/interfaces/ip-filter-options.interface.d.ts +34 -0
- package/ip-filter/interfaces/ip-filter-options.interface.js +2 -0
- package/ip-filter/interfaces/ip-rule.interface.d.ts +36 -0
- package/ip-filter/interfaces/ip-rule.interface.js +2 -0
- package/ip-filter/ip-filter.module.d.ts +55 -0
- package/ip-filter/ip-filter.module.js +105 -0
- package/ip-filter/services/index.d.ts +1 -0
- package/ip-filter/services/index.js +17 -0
- package/ip-filter/services/ip-filter.service.d.ts +92 -0
- package/ip-filter/services/ip-filter.service.js +238 -0
- package/ip-filter/utils/index.d.ts +1 -0
- package/ip-filter/utils/index.js +17 -0
- package/ip-filter/utils/ip-utils.d.ts +61 -0
- package/ip-filter/utils/ip-utils.js +162 -0
- package/package.json +23 -24
- package/providers/context.provider.d.ts +9 -0
- package/providers/context.provider.js +13 -0
- package/setup/bootstrap.setup.d.ts +1 -1
- package/setup/bootstrap.setup.js +1 -1
- package/shared/service-registry.module.js +0 -1
- package/cache/providers/memory-cache.provider.d.ts +0 -69
- package/cache/providers/memory-cache.provider.js +0 -237
|
@@ -0,0 +1,483 @@
|
|
|
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
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.TypeormCacheProvider = void 0;
|
|
25
|
+
const common_1 = require("@nestjs/common");
|
|
26
|
+
const typeorm_1 = require("typeorm");
|
|
27
|
+
const base_cache_provider_1 = require("./base-cache.provider");
|
|
28
|
+
const entities_1 = require("../entities");
|
|
29
|
+
/**
|
|
30
|
+
* TypeORM Cache Provider
|
|
31
|
+
*
|
|
32
|
+
* Database-backed cache provider using TypeORM for persistent caching.
|
|
33
|
+
* This is an L3 cache layer that provides:
|
|
34
|
+
* - Persistent cache storage in database
|
|
35
|
+
* - Cross-instance cache sharing
|
|
36
|
+
* - Automatic expiration handling
|
|
37
|
+
* - Namespace isolation
|
|
38
|
+
*
|
|
39
|
+
* Unlike in-memory caches, this provider survives application restarts
|
|
40
|
+
* and can be shared across multiple application instances.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // In your module:
|
|
45
|
+
* @Module({
|
|
46
|
+
* providers: [
|
|
47
|
+
* {
|
|
48
|
+
* provide: 'TYPEORM_CACHE',
|
|
49
|
+
* useFactory: (dataSource: DataSource) => {
|
|
50
|
+
* return new TypeormCacheProvider({
|
|
51
|
+
* duration: 60000,
|
|
52
|
+
* namespace: 'app:cache',
|
|
53
|
+
* dataSourceName: 'default',
|
|
54
|
+
* });
|
|
55
|
+
* },
|
|
56
|
+
* inject: [DataSource],
|
|
57
|
+
* },
|
|
58
|
+
* ],
|
|
59
|
+
* })
|
|
60
|
+
* export class AppModule {}
|
|
61
|
+
*
|
|
62
|
+
* // In your service:
|
|
63
|
+
* @Injectable()
|
|
64
|
+
* export class UserService {
|
|
65
|
+
* constructor(@Inject('TYPEORM_CACHE') private cache: TypeormCacheProvider) {}
|
|
66
|
+
*
|
|
67
|
+
* async getUser(id: string) {
|
|
68
|
+
* // Try cache first
|
|
69
|
+
* const cached = await this.cache.get(`user:${id}`);
|
|
70
|
+
* if (cached) return cached;
|
|
71
|
+
*
|
|
72
|
+
* // Cache miss - fetch from database
|
|
73
|
+
* const user = await this.userRepository.findOne(id);
|
|
74
|
+
*
|
|
75
|
+
* // Store in cache
|
|
76
|
+
* await this.cache.set(`user:${id}`, user, 60000);
|
|
77
|
+
*
|
|
78
|
+
* return user;
|
|
79
|
+
* }
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @see https://typeorm.io/#caching
|
|
84
|
+
*/
|
|
85
|
+
let TypeormCacheProvider = class TypeormCacheProvider extends base_cache_provider_1.BaseCacheProvider {
|
|
86
|
+
constructor(injectedDataSource, options) {
|
|
87
|
+
super();
|
|
88
|
+
this.injectedDataSource = injectedDataSource;
|
|
89
|
+
const { duration = 60000, enabled = true, dataSourceName = 'default', namespace = 'cache', autoCleanup = true, cleanupInterval = 300000, // 5 minutes
|
|
90
|
+
} = options || {};
|
|
91
|
+
this.defaultDuration = duration;
|
|
92
|
+
this.enabled = enabled;
|
|
93
|
+
this.namespace = namespace;
|
|
94
|
+
this.dataSourceName = dataSourceName;
|
|
95
|
+
this.autoCleanup = autoCleanup;
|
|
96
|
+
this.cleanupInterval = cleanupInterval;
|
|
97
|
+
if (this.injectedDataSource) {
|
|
98
|
+
this.dataSource = this.injectedDataSource;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get DataSource for cache operations
|
|
103
|
+
* Uses @nest-omni/transaction's getDataSource utility
|
|
104
|
+
*/
|
|
105
|
+
getDataSource() {
|
|
106
|
+
if (this.dataSource) {
|
|
107
|
+
return this.dataSource;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
// Try to use getDataSource from @nest-omni/transaction
|
|
111
|
+
const { getDataSource } = require('@nest-omni/transaction');
|
|
112
|
+
this.dataSource = getDataSource(this.dataSourceName);
|
|
113
|
+
if (!this.dataSource) {
|
|
114
|
+
throw new Error(`DataSource '${this.dataSourceName}' not found`);
|
|
115
|
+
}
|
|
116
|
+
return this.dataSource;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
throw new Error(`TypeormCacheProvider requires DataSource '${this.dataSourceName}' to be registered. ` +
|
|
120
|
+
`Use DataSourceUtil.registerDataSource('${this.dataSourceName}', dataSource) or inject it directly.`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get repository for cache operations
|
|
125
|
+
*/
|
|
126
|
+
getRepository() {
|
|
127
|
+
const dataSource = this.getDataSource();
|
|
128
|
+
return dataSource.getRepository(entities_1.TypeormCacheEntity);
|
|
129
|
+
}
|
|
130
|
+
getName() {
|
|
131
|
+
return 'TypeORM';
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get cached value by key
|
|
135
|
+
*/
|
|
136
|
+
get(key) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
if (!this.enabled)
|
|
139
|
+
return null;
|
|
140
|
+
try {
|
|
141
|
+
const repository = this.getRepository();
|
|
142
|
+
const fullKey = this.getFullKey(key);
|
|
143
|
+
const cacheEntry = yield repository.findOne({
|
|
144
|
+
where: { key: fullKey, namespace: this.namespace },
|
|
145
|
+
});
|
|
146
|
+
if (!cacheEntry) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
// Check if expired
|
|
150
|
+
if (cacheEntry.isExpired()) {
|
|
151
|
+
// Clean up expired entry
|
|
152
|
+
yield repository.remove(cacheEntry);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
// Parse and return cached value
|
|
156
|
+
return JSON.parse(cacheEntry.value);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
console.error(`TypeormCacheProvider.get() failed for key '${key}':`, error);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Set cache value with optional TTL
|
|
166
|
+
*/
|
|
167
|
+
set(key, value, ttl) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
if (!this.enabled)
|
|
170
|
+
return;
|
|
171
|
+
try {
|
|
172
|
+
const repository = this.getRepository();
|
|
173
|
+
const fullKey = this.getFullKey(key);
|
|
174
|
+
const duration = ttl || this.defaultDuration;
|
|
175
|
+
const expiresAt = duration > 0 ? new Date(Date.now() + duration) : null;
|
|
176
|
+
// Check if entry already exists
|
|
177
|
+
const existing = yield repository.findOne({
|
|
178
|
+
where: { key: fullKey, namespace: this.namespace },
|
|
179
|
+
});
|
|
180
|
+
if (existing) {
|
|
181
|
+
// Update existing entry
|
|
182
|
+
existing.updateValue(JSON.stringify(value), duration);
|
|
183
|
+
yield repository.save(existing);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Create new entry
|
|
187
|
+
const cacheEntry = repository.create({
|
|
188
|
+
key: fullKey,
|
|
189
|
+
namespace: this.namespace,
|
|
190
|
+
value: JSON.stringify(value),
|
|
191
|
+
expires_at: expiresAt,
|
|
192
|
+
ttl: duration > 0 ? Math.floor(duration / 1000) : null,
|
|
193
|
+
});
|
|
194
|
+
yield repository.save(cacheEntry);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
console.error(`TypeormCacheProvider.set() failed for key '${key}':`, error);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Delete cache entry by key
|
|
204
|
+
*/
|
|
205
|
+
delete(key) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
207
|
+
if (!this.enabled)
|
|
208
|
+
return;
|
|
209
|
+
try {
|
|
210
|
+
const repository = this.getRepository();
|
|
211
|
+
const keys = Array.isArray(key) ? key : [key];
|
|
212
|
+
const fullKeys = keys.map((k) => this.getFullKey(k));
|
|
213
|
+
yield repository.delete({
|
|
214
|
+
key: (0, typeorm_1.In)(fullKeys),
|
|
215
|
+
namespace: this.namespace,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
console.error('TypeormCacheProvider.delete() failed:', error);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Delete cache entries matching a pattern
|
|
225
|
+
* Uses SQL LIKE for pattern matching
|
|
226
|
+
*/
|
|
227
|
+
deletePattern(pattern) {
|
|
228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
if (!this.enabled)
|
|
230
|
+
return 0;
|
|
231
|
+
try {
|
|
232
|
+
const repository = this.getRepository();
|
|
233
|
+
const fullPattern = this.getFullKey(pattern);
|
|
234
|
+
// Use LIKE for pattern matching
|
|
235
|
+
const result = yield repository
|
|
236
|
+
.createQueryBuilder()
|
|
237
|
+
.delete()
|
|
238
|
+
.where('namespace = :namespace', { namespace: this.namespace })
|
|
239
|
+
.andWhere('key LIKE :pattern', { pattern: fullPattern })
|
|
240
|
+
.execute();
|
|
241
|
+
return result.affected || 0;
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
console.error('TypeormCacheProvider.deletePattern() failed:', error);
|
|
245
|
+
return 0;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Clear all cache entries in namespace
|
|
251
|
+
*/
|
|
252
|
+
clear() {
|
|
253
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
if (!this.enabled)
|
|
255
|
+
return;
|
|
256
|
+
try {
|
|
257
|
+
const repository = this.getRepository();
|
|
258
|
+
yield repository.delete({
|
|
259
|
+
namespace: this.namespace,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
console.error('TypeormCacheProvider.clear() failed:', error);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Check if key exists and is not expired
|
|
269
|
+
*/
|
|
270
|
+
has(key) {
|
|
271
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
272
|
+
if (!this.enabled)
|
|
273
|
+
return false;
|
|
274
|
+
try {
|
|
275
|
+
const value = yield this.get(key);
|
|
276
|
+
return value !== null;
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Get multiple values by keys
|
|
285
|
+
*/
|
|
286
|
+
mget(keys) {
|
|
287
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
+
if (!this.enabled || keys.length === 0) {
|
|
289
|
+
return keys.map(() => null);
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
const repository = this.getRepository();
|
|
293
|
+
const fullKeys = keys.map((k) => this.getFullKey(k));
|
|
294
|
+
const cacheEntries = yield repository.find({
|
|
295
|
+
where: {
|
|
296
|
+
key: (0, typeorm_1.In)(fullKeys),
|
|
297
|
+
namespace: this.namespace,
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
// Create map for quick lookup
|
|
301
|
+
const entryMap = new Map();
|
|
302
|
+
for (const entry of cacheEntries) {
|
|
303
|
+
if (entry.isValid()) {
|
|
304
|
+
entryMap.set(entry.key, JSON.parse(entry.value));
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
// Clean up expired entry
|
|
308
|
+
yield repository.remove(entry);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// Return values in same order as input keys
|
|
312
|
+
return keys.map((key) => entryMap.get(this.getFullKey(key)) || null);
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
console.error('TypeormCacheProvider.mget() failed:', error);
|
|
316
|
+
return keys.map(() => null);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Set multiple cache entries
|
|
322
|
+
*/
|
|
323
|
+
mset(items, ttl) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
if (!this.enabled || items.length === 0)
|
|
326
|
+
return;
|
|
327
|
+
try {
|
|
328
|
+
const repository = this.getRepository();
|
|
329
|
+
const duration = ttl || this.defaultDuration;
|
|
330
|
+
const expiresAt = duration > 0 ? new Date(Date.now() + duration) : null;
|
|
331
|
+
for (const item of items) {
|
|
332
|
+
const fullKey = this.getFullKey(item.key);
|
|
333
|
+
const existing = yield repository.findOne({
|
|
334
|
+
where: { key: fullKey, namespace: this.namespace },
|
|
335
|
+
});
|
|
336
|
+
if (existing) {
|
|
337
|
+
existing.updateValue(JSON.stringify(item.value), duration);
|
|
338
|
+
yield repository.save(existing);
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const cacheEntry = repository.create({
|
|
342
|
+
key: fullKey,
|
|
343
|
+
namespace: this.namespace,
|
|
344
|
+
value: JSON.stringify(item.value),
|
|
345
|
+
expires_at: expiresAt,
|
|
346
|
+
ttl: duration > 0 ? Math.floor(duration / 1000) : null,
|
|
347
|
+
});
|
|
348
|
+
yield repository.save(cacheEntry);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
console.error('TypeormCacheProvider.mset() failed:', error);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get cache configuration
|
|
359
|
+
*/
|
|
360
|
+
getCacheConfig() {
|
|
361
|
+
return {
|
|
362
|
+
duration: this.defaultDuration,
|
|
363
|
+
type: 'database',
|
|
364
|
+
enabled: this.enabled,
|
|
365
|
+
namespace: this.namespace,
|
|
366
|
+
dataSourceName: this.dataSourceName,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Get statistics about cache entries
|
|
371
|
+
*/
|
|
372
|
+
getStats() {
|
|
373
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
374
|
+
try {
|
|
375
|
+
const repository = this.getRepository();
|
|
376
|
+
const [total, expired] = yield Promise.all([
|
|
377
|
+
repository.count({
|
|
378
|
+
where: { namespace: this.namespace },
|
|
379
|
+
}),
|
|
380
|
+
repository
|
|
381
|
+
.createQueryBuilder('cache')
|
|
382
|
+
.where('cache.namespace = :namespace', { namespace: this.namespace })
|
|
383
|
+
.andWhere('cache.expires_at < :now', { now: new Date() })
|
|
384
|
+
.getCount(),
|
|
385
|
+
]);
|
|
386
|
+
return {
|
|
387
|
+
totalEntries: total,
|
|
388
|
+
expiredEntries: expired,
|
|
389
|
+
validEntries: total - expired,
|
|
390
|
+
enabled: this.enabled,
|
|
391
|
+
defaultDuration: this.defaultDuration,
|
|
392
|
+
type: 'database',
|
|
393
|
+
namespace: this.namespace,
|
|
394
|
+
dataSourceName: this.dataSourceName,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
catch (error) {
|
|
398
|
+
return {
|
|
399
|
+
totalEntries: 0,
|
|
400
|
+
expiredEntries: 0,
|
|
401
|
+
validEntries: 0,
|
|
402
|
+
enabled: this.enabled,
|
|
403
|
+
defaultDuration: this.defaultDuration,
|
|
404
|
+
type: 'database',
|
|
405
|
+
namespace: this.namespace,
|
|
406
|
+
dataSourceName: this.dataSourceName,
|
|
407
|
+
error: error instanceof Error ? error.message : String(error),
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Clean up expired entries
|
|
414
|
+
*/
|
|
415
|
+
cleanupExpired() {
|
|
416
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
417
|
+
try {
|
|
418
|
+
const repository = this.getRepository();
|
|
419
|
+
const result = yield repository
|
|
420
|
+
.createQueryBuilder()
|
|
421
|
+
.delete()
|
|
422
|
+
.where('namespace = :namespace', { namespace: this.namespace })
|
|
423
|
+
.andWhere('expires_at < :now', { now: new Date() })
|
|
424
|
+
.execute();
|
|
425
|
+
return result.affected || 0;
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
console.error('TypeormCacheProvider.cleanupExpired() failed:', error);
|
|
429
|
+
return 0;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Enable or disable caching
|
|
435
|
+
*/
|
|
436
|
+
setEnabled(enabled) {
|
|
437
|
+
this.enabled = enabled;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Set namespace
|
|
441
|
+
*/
|
|
442
|
+
setNamespace(namespace) {
|
|
443
|
+
this.namespace = namespace;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Get full key with namespace
|
|
447
|
+
*/
|
|
448
|
+
getFullKey(key) {
|
|
449
|
+
return key;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Start automatic cleanup of expired entries
|
|
453
|
+
*/
|
|
454
|
+
startAutoCleanup() {
|
|
455
|
+
if (!this.autoCleanup || this.cleanupTimer) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
this.cleanupTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
459
|
+
yield this.cleanupExpired();
|
|
460
|
+
}), this.cleanupInterval);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Stop automatic cleanup
|
|
464
|
+
*/
|
|
465
|
+
stopAutoCleanup() {
|
|
466
|
+
if (this.cleanupTimer) {
|
|
467
|
+
clearInterval(this.cleanupTimer);
|
|
468
|
+
this.cleanupTimer = undefined;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
onModuleDestroy() {
|
|
472
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
473
|
+
this.stopAutoCleanup();
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
exports.TypeormCacheProvider = TypeormCacheProvider;
|
|
478
|
+
exports.TypeormCacheProvider = TypeormCacheProvider = __decorate([
|
|
479
|
+
(0, common_1.Injectable)(),
|
|
480
|
+
__param(0, (0, common_1.Optional)()),
|
|
481
|
+
__param(0, (0, common_1.Inject)('TYPEORM_CACHE_DATASOURCE')),
|
|
482
|
+
__metadata("design:paramtypes", [typeorm_1.DataSource, Object])
|
|
483
|
+
], TypeormCacheProvider);
|
|
@@ -167,3 +167,20 @@ typeorm_1.SelectQueryBuilder.prototype.eachBatch = function (callback, options)
|
|
|
167
167
|
}
|
|
168
168
|
});
|
|
169
169
|
};
|
|
170
|
+
typeorm_1.SelectQueryBuilder.prototype.findField = function (field) {
|
|
171
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
172
|
+
const alias = this.alias;
|
|
173
|
+
const fieldName = String(field);
|
|
174
|
+
const results = yield this.clone()
|
|
175
|
+
.select(`${alias}.${fieldName}`, fieldName)
|
|
176
|
+
.getRawMany();
|
|
177
|
+
if (results.length === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const values = results.map((result) => result[fieldName]);
|
|
181
|
+
if (values.length === 1) {
|
|
182
|
+
return values[0];
|
|
183
|
+
}
|
|
184
|
+
return values;
|
|
185
|
+
});
|
|
186
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Constructor } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 字段元数据接口(简化版)
|
|
4
|
+
* 用于在验证错误时生成友好的错误消息
|
|
5
|
+
*/
|
|
6
|
+
export interface ValidationMetadata {
|
|
7
|
+
/** 字段名称 */
|
|
8
|
+
fieldName?: string;
|
|
9
|
+
/** 字段标签(多语言)- 使用 fieldLabel 避免命名冲突 */
|
|
10
|
+
label?: string | {
|
|
11
|
+
zh?: string;
|
|
12
|
+
en?: string;
|
|
13
|
+
[lang: string]: string | undefined;
|
|
14
|
+
};
|
|
15
|
+
/** 字段描述(多语言)- 使用 fieldDescription 避免命名冲突 */
|
|
16
|
+
description?: string | {
|
|
17
|
+
zh?: string;
|
|
18
|
+
en?: string;
|
|
19
|
+
[lang: string]: string | undefined;
|
|
20
|
+
};
|
|
21
|
+
/** 是否为数组 */
|
|
22
|
+
each?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 存储验证元数据到装饰器
|
|
26
|
+
*
|
|
27
|
+
* @param target - 目标类
|
|
28
|
+
* @param propertyKey - 属性名
|
|
29
|
+
* @param metadata - 验证元数据
|
|
30
|
+
*/
|
|
31
|
+
export declare function setValidationMetadata(target: Constructor<any>, propertyKey: string, metadata: ValidationMetadata): void;
|
|
32
|
+
/**
|
|
33
|
+
* 获取验证元数据
|
|
34
|
+
*
|
|
35
|
+
* @param target - 目标类
|
|
36
|
+
* @param propertyKey - 属性名
|
|
37
|
+
* @returns 验证元数据
|
|
38
|
+
*/
|
|
39
|
+
export declare function getValidationMetadata(target: Constructor<any>, propertyKey: string): ValidationMetadata | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* 获取所有验证元数据
|
|
42
|
+
*
|
|
43
|
+
* @param target - 目标类
|
|
44
|
+
* @returns 所有验证元数据
|
|
45
|
+
*/
|
|
46
|
+
export declare function getAllValidationMetadata(target: Constructor<any>): Record<string, ValidationMetadata>;
|
|
47
|
+
/**
|
|
48
|
+
* 获取字段的友好标签
|
|
49
|
+
* 根据语言环境返回对应的标签
|
|
50
|
+
*
|
|
51
|
+
* @param metadata - 验证元数据
|
|
52
|
+
* @param lang - 语言 (zh, en, 等)
|
|
53
|
+
* @returns 字段标签
|
|
54
|
+
*/
|
|
55
|
+
export declare function getFieldLabelForValidation(metadata: ValidationMetadata, lang?: string): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setValidationMetadata = setValidationMetadata;
|
|
4
|
+
exports.getValidationMetadata = getValidationMetadata;
|
|
5
|
+
exports.getAllValidationMetadata = getAllValidationMetadata;
|
|
6
|
+
exports.getFieldLabelForValidation = getFieldLabelForValidation;
|
|
7
|
+
/**
|
|
8
|
+
* 验证元数据存储键
|
|
9
|
+
*/
|
|
10
|
+
const VALIDATION_METADATA_KEY = 'VALIDATION_METADATA_OPTIONS';
|
|
11
|
+
/**
|
|
12
|
+
* 存储验证元数据到装饰器
|
|
13
|
+
*
|
|
14
|
+
* @param target - 目标类
|
|
15
|
+
* @param propertyKey - 属性名
|
|
16
|
+
* @param metadata - 验证元数据
|
|
17
|
+
*/
|
|
18
|
+
function setValidationMetadata(target, propertyKey, metadata) {
|
|
19
|
+
const existingMetadata = Reflect.getMetadata(VALIDATION_METADATA_KEY, target) || {};
|
|
20
|
+
existingMetadata[propertyKey] = metadata;
|
|
21
|
+
Reflect.defineMetadata(VALIDATION_METADATA_KEY, existingMetadata, target);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 获取验证元数据
|
|
25
|
+
*
|
|
26
|
+
* @param target - 目标类
|
|
27
|
+
* @param propertyKey - 属性名
|
|
28
|
+
* @returns 验证元数据
|
|
29
|
+
*/
|
|
30
|
+
function getValidationMetadata(target, propertyKey) {
|
|
31
|
+
const allMetadata = Reflect.getMetadata(VALIDATION_METADATA_KEY, target);
|
|
32
|
+
const metadata = allMetadata === null || allMetadata === void 0 ? void 0 : allMetadata[propertyKey];
|
|
33
|
+
return metadata;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 获取所有验证元数据
|
|
37
|
+
*
|
|
38
|
+
* @param target - 目标类
|
|
39
|
+
* @returns 所有验证元数据
|
|
40
|
+
*/
|
|
41
|
+
function getAllValidationMetadata(target) {
|
|
42
|
+
return Reflect.getMetadata(VALIDATION_METADATA_KEY, target) || {};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 获取字段的友好标签
|
|
46
|
+
* 根据语言环境返回对应的标签
|
|
47
|
+
*
|
|
48
|
+
* @param metadata - 验证元数据
|
|
49
|
+
* @param lang - 语言 (zh, en, 等)
|
|
50
|
+
* @returns 字段标签
|
|
51
|
+
*/
|
|
52
|
+
function getFieldLabelForValidation(metadata, lang = 'zh') {
|
|
53
|
+
if (!metadata.label) {
|
|
54
|
+
return metadata.fieldName || '';
|
|
55
|
+
}
|
|
56
|
+
if (typeof metadata.label === 'string') {
|
|
57
|
+
return metadata.label;
|
|
58
|
+
}
|
|
59
|
+
return metadata.label[lang] || metadata.label.zh || metadata.label.en || metadata.fieldName || '';
|
|
60
|
+
}
|
package/common/index.d.ts
CHANGED
package/common/index.js
CHANGED
|
@@ -20,3 +20,7 @@ __exportStar(require("./dto"), exports);
|
|
|
20
20
|
__exportStar(require("./types"), exports);
|
|
21
21
|
__exportStar(require("./snake-naming.strategy"), exports);
|
|
22
22
|
__exportStar(require("./boilerplate.polyfill"), exports);
|
|
23
|
+
// ========================================
|
|
24
|
+
// Validation Metadata Helper
|
|
25
|
+
// ========================================
|
|
26
|
+
__exportStar(require("./helpers/validation-metadata-helper"), exports);
|