@nest-omni/core 4.1.3-2 → 4.1.3-4
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 +10 -0
- package/audit/audit.module.js +15 -0
- package/audit/controllers/audit.controller.d.ts +24 -0
- package/audit/controllers/audit.controller.js +24 -0
- package/audit/decorators/audit-controller.decorator.d.ts +8 -0
- package/audit/decorators/audit-controller.decorator.js +9 -0
- package/audit/decorators/audit-operation.decorator.d.ts +45 -0
- package/audit/decorators/audit-operation.decorator.js +49 -0
- package/audit/decorators/entity-audit.decorator.d.ts +8 -0
- package/audit/decorators/entity-audit.decorator.js +9 -0
- package/audit/dto/audit-log-query.dto.d.ts +3 -0
- package/audit/dto/audit-log-query.dto.js +3 -0
- package/audit/dto/begin-transaction.dto.d.ts +3 -0
- package/audit/dto/begin-transaction.dto.js +3 -0
- package/audit/dto/compare-entities.dto.d.ts +3 -0
- package/audit/dto/compare-entities.dto.js +3 -0
- package/audit/dto/pre-check-restore.dto.d.ts +3 -0
- package/audit/dto/pre-check-restore.dto.js +3 -0
- package/audit/dto/restore-entity.dto.d.ts +3 -0
- package/audit/dto/restore-entity.dto.js +3 -0
- package/audit/entities/entity-audit-log.entity.d.ts +3 -0
- package/audit/entities/entity-audit-log.entity.js +3 -0
- package/audit/entities/entity-transaction.entity.d.ts +3 -0
- package/audit/entities/entity-transaction.entity.js +3 -0
- package/audit/entities/manual-operation-log.entity.d.ts +4 -0
- package/audit/entities/manual-operation-log.entity.js +4 -0
- package/audit/entities/operation-template.entity.d.ts +4 -0
- package/audit/entities/operation-template.entity.js +4 -0
- package/audit/enums/audit.enums.d.ts +17 -2
- package/audit/enums/audit.enums.js +15 -0
- package/audit/index.js +10 -0
- package/audit/interceptors/audit.interceptor.d.ts +15 -0
- package/audit/interceptors/audit.interceptor.js +23 -1
- package/audit/interfaces/audit.interfaces.d.ts +42 -0
- package/audit/services/audit-context.service.d.ts +15 -0
- package/audit/services/audit-context.service.js +15 -0
- package/audit/services/audit-strategy.service.d.ts +6 -0
- package/audit/services/audit-strategy.service.js +13 -0
- package/audit/services/entity-audit.service.d.ts +57 -0
- package/audit/services/entity-audit.service.js +91 -0
- package/audit/services/manual-audit-log.service.d.ts +124 -0
- package/audit/services/manual-audit-log.service.js +138 -0
- package/audit/services/multi-database.service.d.ts +12 -0
- package/audit/services/multi-database.service.js +12 -0
- package/audit/services/operation-description.service.d.ts +59 -0
- package/audit/services/operation-description.service.js +76 -2
- package/audit/services/transaction-audit.service.d.ts +30 -0
- package/audit/services/transaction-audit.service.js +47 -0
- package/audit/subscribers/entity-audit.subscriber.d.ts +15 -0
- package/audit/subscribers/entity-audit.subscriber.js +29 -1
- package/cache/cache-metrics.service.d.ts +67 -0
- package/cache/cache-metrics.service.js +68 -4
- package/cache/cache-serialization.service.d.ts +31 -0
- package/cache/cache-serialization.service.js +25 -0
- package/cache/cache.constants.d.ts +9 -0
- package/cache/cache.constants.js +9 -0
- package/cache/cache.health.d.ts +26 -0
- package/cache/cache.health.js +30 -0
- package/cache/cache.module.d.ts +86 -0
- package/cache/cache.module.js +71 -0
- package/cache/cache.service.d.ts +140 -0
- package/cache/cache.service.js +157 -0
- package/cache/cache.warmup.service.d.ts +39 -0
- package/cache/cache.warmup.service.js +32 -0
- package/cache/decorators/cache-evict.decorator.d.ts +47 -0
- package/cache/decorators/cache-evict.decorator.js +56 -0
- package/cache/decorators/cache-put.decorator.d.ts +34 -0
- package/cache/decorators/cache-put.decorator.js +39 -0
- package/cache/decorators/cacheable.decorator.d.ts +40 -0
- package/cache/decorators/cacheable.decorator.js +55 -0
- package/cache/dependencies/callback.dependency.d.ts +33 -0
- package/cache/dependencies/callback.dependency.js +39 -1
- package/cache/dependencies/chain.dependency.d.ts +28 -0
- package/cache/dependencies/chain.dependency.js +34 -0
- package/cache/dependencies/db.dependency.d.ts +45 -0
- package/cache/dependencies/db.dependency.js +48 -1
- package/cache/dependencies/file.dependency.d.ts +32 -0
- package/cache/dependencies/file.dependency.js +34 -0
- package/cache/dependencies/tag.dependency.d.ts +36 -0
- package/cache/dependencies/tag.dependency.js +36 -0
- package/cache/dependencies/time.dependency.d.ts +43 -0
- package/cache/dependencies/time.dependency.js +43 -0
- package/cache/examples/basic-usage.d.ts +15 -0
- package/cache/examples/basic-usage.js +62 -8
- package/cache/index.js +9 -0
- package/cache/interfaces/cache-dependency.interface.d.ts +53 -0
- package/cache/interfaces/cache-options.interface.d.ts +81 -0
- package/cache/interfaces/cache-options.interface.js +6 -0
- package/cache/interfaces/cache-provider.interface.d.ts +78 -0
- package/cache/providers/base-cache.provider.d.ts +14 -0
- package/cache/providers/base-cache.provider.js +16 -0
- package/cache/providers/cls-cache.provider.d.ts +20 -0
- package/cache/providers/cls-cache.provider.js +28 -0
- package/cache/providers/memory-cache.provider.d.ts +23 -0
- package/cache/providers/memory-cache.provider.js +26 -0
- package/cache/providers/redis-cache.provider.d.ts +26 -0
- package/cache/providers/redis-cache.provider.js +29 -0
- package/cache/utils/dependency-manager.util.d.ts +52 -0
- package/cache/utils/dependency-manager.util.js +59 -0
- package/cache/utils/key-generator.util.d.ts +42 -0
- package/cache/utils/key-generator.util.js +53 -1
- package/common/abstract.entity.d.ts +14 -0
- package/common/abstract.entity.js +14 -0
- package/common/boilerplate.polyfill.d.ts +142 -0
- package/common/boilerplate.polyfill.js +17 -0
- package/common/dto/dto-container.d.ts +16 -0
- package/common/dto/dto-container.js +20 -0
- package/common/dto/dto-decorators.d.ts +18 -0
- package/common/dto/dto-decorators.js +14 -0
- package/common/dto/dto-extensions.d.ts +11 -0
- package/common/dto/dto-extensions.js +9 -0
- package/common/dto/dto-service-accessor.d.ts +17 -0
- package/common/dto/dto-service-accessor.js +18 -0
- package/common/dto/dto-transformer.d.ts +12 -0
- package/common/dto/dto-transformer.js +9 -0
- package/common/dto/index.js +2 -0
- package/common/examples/paginate-and-map.example.d.ts +6 -0
- package/common/examples/paginate-and-map.example.js +26 -0
- package/common/utils.d.ts +15 -0
- package/common/utils.js +15 -0
- package/constants/language-code.js +1 -0
- package/decorators/field.decorators.js +8 -1
- package/decorators/property.decorators.js +1 -0
- package/decorators/public-route.decorator.js +1 -0
- package/decorators/transform.decorators.d.ts +27 -0
- package/decorators/transform.decorators.js +29 -0
- package/decorators/translate.decorator.js +1 -0
- package/decorators/user.decorator.js +1 -0
- package/decorators/validator.decorators.d.ts +8 -18
- package/decorators/validator.decorators.js +22 -190
- package/filters/constraint-errors.js +1 -0
- package/helpers/common.helper.d.ts +13 -0
- package/helpers/common.helper.js +13 -0
- package/http-client/config/http-client.config.d.ts +15 -0
- package/http-client/config/http-client.config.js +25 -9
- package/http-client/decorators/http-client.decorators.d.ts +63 -0
- package/http-client/decorators/http-client.decorators.js +71 -3
- package/http-client/entities/http-log.entity.d.ts +229 -0
- package/http-client/entities/http-log.entity.js +6 -1
- package/http-client/errors/http-client.errors.d.ts +57 -0
- package/http-client/errors/http-client.errors.js +58 -0
- package/http-client/examples/advanced-usage.example.d.ts +41 -0
- package/http-client/examples/advanced-usage.example.js +68 -24
- package/http-client/examples/auth-with-waiting-lock.example.d.ts +31 -0
- package/http-client/examples/auth-with-waiting-lock.example.js +52 -5
- package/http-client/examples/basic-usage.example.d.ts +60 -0
- package/http-client/examples/basic-usage.example.js +60 -0
- package/http-client/examples/multi-api-configuration.example.d.ts +60 -0
- package/http-client/examples/multi-api-configuration.example.js +76 -5
- package/http-client/http-client.module.d.ts +13 -0
- package/http-client/http-client.module.js +19 -0
- package/http-client/index.js +8 -0
- package/http-client/interfaces/api-client-config.interface.d.ts +125 -0
- package/http-client/interfaces/api-client-config.interface.js +3 -0
- package/http-client/interfaces/http-client-config.interface.d.ts +60 -0
- package/http-client/services/api-client-registry.service.d.ts +57 -0
- package/http-client/services/api-client-registry.service.js +84 -1
- package/http-client/services/cache.service.d.ts +52 -0
- package/http-client/services/cache.service.js +72 -3
- package/http-client/services/circuit-breaker.service.d.ts +46 -0
- package/http-client/services/circuit-breaker.service.js +52 -0
- package/http-client/services/http-client.service.d.ts +67 -0
- package/http-client/services/http-client.service.js +105 -4
- package/http-client/services/http-log-query.service.d.ts +83 -0
- package/http-client/services/http-log-query.service.js +122 -1
- package/http-client/services/http-replay.service.d.ts +101 -0
- package/http-client/services/http-replay.service.js +86 -0
- package/http-client/services/log-cleanup.service.d.ts +63 -0
- package/http-client/services/log-cleanup.service.js +54 -2
- package/http-client/services/logging.service.d.ts +40 -0
- package/http-client/services/logging.service.js +53 -0
- package/http-client/utils/call-stack-extractor.util.d.ts +37 -0
- package/http-client/utils/call-stack-extractor.util.js +48 -0
- package/http-client/utils/context-extractor.util.d.ts +49 -0
- package/http-client/utils/context-extractor.util.js +52 -0
- package/http-client/utils/curl-generator.util.d.ts +21 -0
- package/http-client/utils/curl-generator.util.js +44 -3
- package/http-client/utils/request-id.util.d.ts +18 -0
- package/http-client/utils/request-id.util.js +20 -0
- package/http-client/utils/retry-recorder.util.d.ts +42 -0
- package/http-client/utils/retry-recorder.util.js +44 -0
- package/index.js +8 -0
- package/interceptors/translation-interceptor.service.js +5 -0
- package/package.json +1 -1
- package/providers/context.provider.js +2 -0
- package/providers/generator.provider.d.ts +4 -0
- package/providers/generator.provider.js +4 -0
- package/redis-lock/examples/lock-strategy.examples.d.ts +89 -0
- package/redis-lock/examples/lock-strategy.examples.js +130 -15
- package/redis-lock/index.js +3 -0
- package/redis-lock/redis-lock.decorator.d.ts +101 -0
- package/redis-lock/redis-lock.decorator.js +120 -0
- package/redis-lock/redis-lock.module.d.ts +60 -0
- package/redis-lock/redis-lock.module.js +46 -0
- package/redis-lock/redis-lock.service.d.ts +251 -0
- package/redis-lock/redis-lock.service.js +219 -3
- package/setup/bootstrap.setup.js +20 -0
- package/setup/mode.setup.d.ts +44 -0
- package/setup/mode.setup.js +44 -0
- package/setup/schedule.decorator.d.ts +226 -0
- package/setup/schedule.decorator.js +214 -1
- package/setup/worker.decorator.d.ts +86 -0
- package/setup/worker.decorator.js +88 -0
- package/shared/serviceRegistryModule.js +5 -1
- package/shared/services/api-config.service.d.ts +3 -0
- package/shared/services/api-config.service.js +20 -9
- package/validator-json/decorators.d.ts +17 -0
- package/validator-json/decorators.js +17 -2
- package/validator-json/default.d.ts +6 -0
- package/validator-json/default.js +30 -2
- package/validator-json/defaultConverters.js +1 -0
- package/validator-json/options.d.ts +23 -0
- package/validators/common-validators.d.ts +143 -0
- package/validators/common-validators.js +249 -0
- package/validators/custom-validate.examples.d.ts +23 -0
- package/validators/custom-validate.examples.js +78 -6
- package/validators/custom-validate.validator.d.ts +107 -0
- package/validators/custom-validate.validator.js +84 -0
- package/validators/index.d.ts +1 -0
- package/validators/index.js +1 -0
- package/validators/is-exists.validator.d.ts +11 -0
- package/validators/is-exists.validator.js +22 -0
- package/validators/is-unique.validator.d.ts +11 -0
- package/validators/is-unique.validator.js +31 -3
- package/validators/skip-empty.validator.d.ts +5 -0
- package/validators/skip-empty.validator.js +5 -0
- package/vault/interfaces/vault-options.interface.d.ts +9 -0
- package/vault/vault-config.loader.d.ts +30 -0
- package/vault/vault-config.loader.js +48 -1
- package/vault/vault-config.service.d.ts +53 -0
- package/vault/vault-config.service.js +57 -0
- package/vault/vault.module.d.ts +4 -0
- package/vault/vault.module.js +4 -0
- package/decorators/examples/validation-decorators.example.d.ts +0 -69
- package/decorators/examples/validation-decorators.example.js +0 -331
|
@@ -1,15 +1,60 @@
|
|
|
1
1
|
import { DataSource } from 'typeorm';
|
|
2
2
|
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
3
|
+
/**
|
|
4
|
+
* Database-based cache dependency
|
|
5
|
+
*
|
|
6
|
+
* This dependency monitors database query results. When the query result changes,
|
|
7
|
+
* the cache becomes invalid.
|
|
8
|
+
*
|
|
9
|
+
* Common use case: Monitor MAX(updated_at) or COUNT(*) to detect data changes.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Invalidate cache when user data changes
|
|
14
|
+
* @Cacheable({
|
|
15
|
+
* key: (id) => `user:${id}:profile`,
|
|
16
|
+
* dependencies: [
|
|
17
|
+
* new DbDependency(
|
|
18
|
+
* 'SELECT updated_at FROM users WHERE id = ?',
|
|
19
|
+
* (id) => [id]
|
|
20
|
+
* )
|
|
21
|
+
* ]
|
|
22
|
+
* })
|
|
23
|
+
* async getUserProfile(id: string) { }
|
|
24
|
+
*
|
|
25
|
+
* // Invalidate when any user in tenant changes
|
|
26
|
+
* @Cacheable({
|
|
27
|
+
* key: (tenantId) => `users:list:${tenantId}`,
|
|
28
|
+
* dependencies: [
|
|
29
|
+
* new DbDependency(
|
|
30
|
+
* 'SELECT MAX(updated_at) FROM users WHERE tenant_id = ?',
|
|
31
|
+
* (tenantId) => [tenantId]
|
|
32
|
+
* )
|
|
33
|
+
* ]
|
|
34
|
+
* })
|
|
35
|
+
* async getUserList(tenantId: string) { }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
3
38
|
export declare class DbDependency implements CacheDependency {
|
|
4
39
|
private readonly sql;
|
|
5
40
|
private readonly paramsFactory?;
|
|
6
41
|
private readonly name?;
|
|
7
42
|
private static dataSource;
|
|
8
43
|
constructor(sql: string, paramsFactory?: (...args: any[]) => any[], name?: string);
|
|
44
|
+
/**
|
|
45
|
+
* Set the TypeORM DataSource for database queries
|
|
46
|
+
* This should be called once during application initialization
|
|
47
|
+
*/
|
|
9
48
|
static setDataSource(dataSource: DataSource): void;
|
|
49
|
+
/**
|
|
50
|
+
* Get the current DataSource
|
|
51
|
+
*/
|
|
10
52
|
static getDataSource(): DataSource | null;
|
|
11
53
|
getKey(): string;
|
|
12
54
|
getData(): Promise<any>;
|
|
13
55
|
isChanged(oldData: any): Promise<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Simple hash code generation for strings
|
|
58
|
+
*/
|
|
14
59
|
private hashCode;
|
|
15
60
|
}
|
|
@@ -10,6 +10,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.DbDependency = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Database-based cache dependency
|
|
15
|
+
*
|
|
16
|
+
* This dependency monitors database query results. When the query result changes,
|
|
17
|
+
* the cache becomes invalid.
|
|
18
|
+
*
|
|
19
|
+
* Common use case: Monitor MAX(updated_at) or COUNT(*) to detect data changes.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Invalidate cache when user data changes
|
|
24
|
+
* @Cacheable({
|
|
25
|
+
* key: (id) => `user:${id}:profile`,
|
|
26
|
+
* dependencies: [
|
|
27
|
+
* new DbDependency(
|
|
28
|
+
* 'SELECT updated_at FROM users WHERE id = ?',
|
|
29
|
+
* (id) => [id]
|
|
30
|
+
* )
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
* async getUserProfile(id: string) { }
|
|
34
|
+
*
|
|
35
|
+
* // Invalidate when any user in tenant changes
|
|
36
|
+
* @Cacheable({
|
|
37
|
+
* key: (tenantId) => `users:list:${tenantId}`,
|
|
38
|
+
* dependencies: [
|
|
39
|
+
* new DbDependency(
|
|
40
|
+
* 'SELECT MAX(updated_at) FROM users WHERE tenant_id = ?',
|
|
41
|
+
* (tenantId) => [tenantId]
|
|
42
|
+
* )
|
|
43
|
+
* ]
|
|
44
|
+
* })
|
|
45
|
+
* async getUserList(tenantId: string) { }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
13
48
|
class DbDependency {
|
|
14
49
|
constructor(sql, paramsFactory, name) {
|
|
15
50
|
this.sql = sql;
|
|
@@ -19,9 +54,16 @@ class DbDependency {
|
|
|
19
54
|
throw new Error('DbDependency requires a valid SQL query');
|
|
20
55
|
}
|
|
21
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Set the TypeORM DataSource for database queries
|
|
59
|
+
* This should be called once during application initialization
|
|
60
|
+
*/
|
|
22
61
|
static setDataSource(dataSource) {
|
|
23
62
|
this.dataSource = dataSource;
|
|
24
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Get the current DataSource
|
|
66
|
+
*/
|
|
25
67
|
static getDataSource() {
|
|
26
68
|
return this.dataSource;
|
|
27
69
|
}
|
|
@@ -39,6 +81,7 @@ class DbDependency {
|
|
|
39
81
|
try {
|
|
40
82
|
const params = ((_a = this.paramsFactory) === null || _a === void 0 ? void 0 : _a.call(this)) || [];
|
|
41
83
|
const result = yield DbDependency.dataSource.query(this.sql, params);
|
|
84
|
+
// Serialize result for comparison
|
|
42
85
|
return JSON.stringify(result);
|
|
43
86
|
}
|
|
44
87
|
catch (error) {
|
|
@@ -53,16 +96,20 @@ class DbDependency {
|
|
|
53
96
|
return oldData !== currentData;
|
|
54
97
|
}
|
|
55
98
|
catch (_a) {
|
|
99
|
+
// If query fails, consider it changed to be safe
|
|
56
100
|
return true;
|
|
57
101
|
}
|
|
58
102
|
});
|
|
59
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Simple hash code generation for strings
|
|
106
|
+
*/
|
|
60
107
|
hashCode(str) {
|
|
61
108
|
let hash = 0;
|
|
62
109
|
for (let i = 0; i < str.length; i++) {
|
|
63
110
|
const char = str.charCodeAt(i);
|
|
64
111
|
hash = (hash << 5) - hash + char;
|
|
65
|
-
hash = hash & hash;
|
|
112
|
+
hash = hash & hash; // Convert to 32bit integer
|
|
66
113
|
}
|
|
67
114
|
return Math.abs(hash).toString(36);
|
|
68
115
|
}
|
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
/**
|
|
3
|
+
* File-based cache dependency
|
|
4
|
+
*
|
|
5
|
+
* This dependency monitors file modification time. When the file is modified,
|
|
6
|
+
* the cache becomes invalid.
|
|
7
|
+
*
|
|
8
|
+
* Useful for configuration files, templates, or other file-based resources.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Cache template, invalidate when template file changes
|
|
13
|
+
* @Cacheable({
|
|
14
|
+
* key: (name) => `template:${name}`,
|
|
15
|
+
* dependencies: [
|
|
16
|
+
* new FileDependency((name) => `./templates/${name}.html`)
|
|
17
|
+
* ]
|
|
18
|
+
* })
|
|
19
|
+
* async getTemplate(name: string) { }
|
|
20
|
+
*
|
|
21
|
+
* // Cache config, invalidate when config file changes
|
|
22
|
+
* @Cacheable({
|
|
23
|
+
* key: 'app:config',
|
|
24
|
+
* dependencies: [
|
|
25
|
+
* new FileDependency('./config/app.json')
|
|
26
|
+
* ]
|
|
27
|
+
* })
|
|
28
|
+
* async getConfig() { }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
2
31
|
export declare class FileDependency implements CacheDependency {
|
|
3
32
|
private readonly filePath;
|
|
4
33
|
constructor(filePath: string | ((...args: any[]) => string));
|
|
@@ -13,5 +42,8 @@ export declare class FileDependency implements CacheDependency {
|
|
|
13
42
|
mtime: number;
|
|
14
43
|
size: number;
|
|
15
44
|
} | null): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the file path (handle dynamic paths)
|
|
47
|
+
*/
|
|
16
48
|
private resolveFilePath;
|
|
17
49
|
}
|
|
@@ -11,6 +11,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.FileDependency = void 0;
|
|
13
13
|
const node_fs_1 = require("node:fs");
|
|
14
|
+
/**
|
|
15
|
+
* File-based cache dependency
|
|
16
|
+
*
|
|
17
|
+
* This dependency monitors file modification time. When the file is modified,
|
|
18
|
+
* the cache becomes invalid.
|
|
19
|
+
*
|
|
20
|
+
* Useful for configuration files, templates, or other file-based resources.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Cache template, invalidate when template file changes
|
|
25
|
+
* @Cacheable({
|
|
26
|
+
* key: (name) => `template:${name}`,
|
|
27
|
+
* dependencies: [
|
|
28
|
+
* new FileDependency((name) => `./templates/${name}.html`)
|
|
29
|
+
* ]
|
|
30
|
+
* })
|
|
31
|
+
* async getTemplate(name: string) { }
|
|
32
|
+
*
|
|
33
|
+
* // Cache config, invalidate when config file changes
|
|
34
|
+
* @Cacheable({
|
|
35
|
+
* key: 'app:config',
|
|
36
|
+
* dependencies: [
|
|
37
|
+
* new FileDependency('./config/app.json')
|
|
38
|
+
* ]
|
|
39
|
+
* })
|
|
40
|
+
* async getConfig() { }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
14
43
|
class FileDependency {
|
|
15
44
|
constructor(filePath) {
|
|
16
45
|
if (!filePath) {
|
|
@@ -42,15 +71,20 @@ class FileDependency {
|
|
|
42
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
72
|
try {
|
|
44
73
|
const currentData = yield this.getData();
|
|
74
|
+
// File path changed or mtime changed or size changed
|
|
45
75
|
return (currentData.path !== oldData.path ||
|
|
46
76
|
currentData.mtime !== oldData.mtime ||
|
|
47
77
|
currentData.size !== oldData.size);
|
|
48
78
|
}
|
|
49
79
|
catch (_a) {
|
|
80
|
+
// If we can't access file, consider it changed
|
|
50
81
|
return true;
|
|
51
82
|
}
|
|
52
83
|
});
|
|
53
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Resolve the file path (handle dynamic paths)
|
|
87
|
+
*/
|
|
54
88
|
resolveFilePath() {
|
|
55
89
|
return __awaiter(this, void 0, void 0, function* () {
|
|
56
90
|
if (typeof this.filePath === 'function') {
|
|
@@ -1,13 +1,49 @@
|
|
|
1
1
|
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
/**
|
|
3
|
+
* Tag-based cache dependency
|
|
4
|
+
*
|
|
5
|
+
* This dependency allows you to invalidate multiple cache entries by tags.
|
|
6
|
+
* When a tag is invalidated, all cache entries depending on that tag become invalid.
|
|
7
|
+
*
|
|
8
|
+
* This is the most commonly used dependency type for managing related cache entries.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Cache user list with tag dependency
|
|
13
|
+
* @Cacheable({
|
|
14
|
+
* key: (tenantId) => `users:list:${tenantId}`,
|
|
15
|
+
* dependencies: [new TagDependency(['user-list', `tenant:${tenantId}`])]
|
|
16
|
+
* })
|
|
17
|
+
* async getUserList(tenantId: string) { }
|
|
18
|
+
*
|
|
19
|
+
* // When creating a new user, invalidate the tag
|
|
20
|
+
* await cacheService.invalidateTags(['user-list']);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
2
23
|
export declare class TagDependency implements CacheDependency {
|
|
3
24
|
private readonly tags;
|
|
4
25
|
private static readonly PREFIX;
|
|
5
26
|
private static tagVersions;
|
|
6
27
|
constructor(tags: string[]);
|
|
28
|
+
/**
|
|
29
|
+
* Get current version of a tag
|
|
30
|
+
*/
|
|
7
31
|
static getTagVersion(tag: string): number;
|
|
32
|
+
/**
|
|
33
|
+
* Invalidate a tag (increment its version)
|
|
34
|
+
*/
|
|
8
35
|
static invalidateTag(tag: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Invalidate multiple tags
|
|
38
|
+
*/
|
|
9
39
|
static invalidateTags(tags: string[]): void;
|
|
40
|
+
/**
|
|
41
|
+
* Reset all tag versions (useful for testing)
|
|
42
|
+
*/
|
|
10
43
|
static resetAllTags(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get all tags and their versions
|
|
46
|
+
*/
|
|
11
47
|
static getAllTags(): Map<string, number>;
|
|
12
48
|
getKey(): string;
|
|
13
49
|
getData(): Promise<Record<string, number>>;
|
|
@@ -10,6 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.TagDependency = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Tag-based cache dependency
|
|
15
|
+
*
|
|
16
|
+
* This dependency allows you to invalidate multiple cache entries by tags.
|
|
17
|
+
* When a tag is invalidated, all cache entries depending on that tag become invalid.
|
|
18
|
+
*
|
|
19
|
+
* This is the most commonly used dependency type for managing related cache entries.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Cache user list with tag dependency
|
|
24
|
+
* @Cacheable({
|
|
25
|
+
* key: (tenantId) => `users:list:${tenantId}`,
|
|
26
|
+
* dependencies: [new TagDependency(['user-list', `tenant:${tenantId}`])]
|
|
27
|
+
* })
|
|
28
|
+
* async getUserList(tenantId: string) { }
|
|
29
|
+
*
|
|
30
|
+
* // When creating a new user, invalidate the tag
|
|
31
|
+
* await cacheService.invalidateTags(['user-list']);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
13
34
|
class TagDependency {
|
|
14
35
|
constructor(tags) {
|
|
15
36
|
this.tags = tags;
|
|
@@ -17,21 +38,36 @@ class TagDependency {
|
|
|
17
38
|
throw new Error('TagDependency requires at least one tag');
|
|
18
39
|
}
|
|
19
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Get current version of a tag
|
|
43
|
+
*/
|
|
20
44
|
static getTagVersion(tag) {
|
|
21
45
|
return this.tagVersions.get(tag) || 0;
|
|
22
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Invalidate a tag (increment its version)
|
|
49
|
+
*/
|
|
23
50
|
static invalidateTag(tag) {
|
|
24
51
|
const currentVersion = this.tagVersions.get(tag) || 0;
|
|
25
52
|
this.tagVersions.set(tag, currentVersion + 1);
|
|
26
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Invalidate multiple tags
|
|
56
|
+
*/
|
|
27
57
|
static invalidateTags(tags) {
|
|
28
58
|
for (const tag of tags) {
|
|
29
59
|
this.invalidateTag(tag);
|
|
30
60
|
}
|
|
31
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Reset all tag versions (useful for testing)
|
|
64
|
+
*/
|
|
32
65
|
static resetAllTags() {
|
|
33
66
|
this.tagVersions.clear();
|
|
34
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Get all tags and their versions
|
|
70
|
+
*/
|
|
35
71
|
static getAllTags() {
|
|
36
72
|
return new Map(this.tagVersions);
|
|
37
73
|
}
|
|
@@ -1,4 +1,38 @@
|
|
|
1
1
|
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
/**
|
|
3
|
+
* Time-based cache dependency
|
|
4
|
+
*
|
|
5
|
+
* This dependency invalidates cache after a specific duration.
|
|
6
|
+
* Unlike TTL which is checked on read, this is checked as a dependency.
|
|
7
|
+
*
|
|
8
|
+
* Useful for implementing complex time-based invalidation logic.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Cache for 5 minutes
|
|
13
|
+
* @Cacheable({
|
|
14
|
+
* key: 'daily:report',
|
|
15
|
+
* dependencies: [
|
|
16
|
+
* new TimeDependency(5 * 60 * 1000) // 5 minutes
|
|
17
|
+
* ]
|
|
18
|
+
* })
|
|
19
|
+
* async getDailyReport() { }
|
|
20
|
+
*
|
|
21
|
+
* // Cache until midnight
|
|
22
|
+
* @Cacheable({
|
|
23
|
+
* key: 'today:stats',
|
|
24
|
+
* dependencies: [
|
|
25
|
+
* new TimeDependency(() => {
|
|
26
|
+
* const now = new Date();
|
|
27
|
+
* const midnight = new Date(now);
|
|
28
|
+
* midnight.setHours(24, 0, 0, 0);
|
|
29
|
+
* return midnight.getTime() - now.getTime();
|
|
30
|
+
* })
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
* async getTodayStats() { }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
2
36
|
export declare class TimeDependency implements CacheDependency {
|
|
3
37
|
private readonly duration;
|
|
4
38
|
private createdAt;
|
|
@@ -13,7 +47,16 @@ export declare class TimeDependency implements CacheDependency {
|
|
|
13
47
|
duration: number;
|
|
14
48
|
}): Promise<boolean>;
|
|
15
49
|
reset(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Get remaining time in milliseconds
|
|
52
|
+
*/
|
|
16
53
|
getRemainingTime(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Check if dependency has expired
|
|
56
|
+
*/
|
|
17
57
|
isExpired(): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Resolve duration value (handle dynamic duration)
|
|
60
|
+
*/
|
|
18
61
|
private resolveDuration;
|
|
19
62
|
}
|
|
@@ -10,6 +10,40 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.TimeDependency = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Time-based cache dependency
|
|
15
|
+
*
|
|
16
|
+
* This dependency invalidates cache after a specific duration.
|
|
17
|
+
* Unlike TTL which is checked on read, this is checked as a dependency.
|
|
18
|
+
*
|
|
19
|
+
* Useful for implementing complex time-based invalidation logic.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Cache for 5 minutes
|
|
24
|
+
* @Cacheable({
|
|
25
|
+
* key: 'daily:report',
|
|
26
|
+
* dependencies: [
|
|
27
|
+
* new TimeDependency(5 * 60 * 1000) // 5 minutes
|
|
28
|
+
* ]
|
|
29
|
+
* })
|
|
30
|
+
* async getDailyReport() { }
|
|
31
|
+
*
|
|
32
|
+
* // Cache until midnight
|
|
33
|
+
* @Cacheable({
|
|
34
|
+
* key: 'today:stats',
|
|
35
|
+
* dependencies: [
|
|
36
|
+
* new TimeDependency(() => {
|
|
37
|
+
* const now = new Date();
|
|
38
|
+
* const midnight = new Date(now);
|
|
39
|
+
* midnight.setHours(24, 0, 0, 0);
|
|
40
|
+
* return midnight.getTime() - now.getTime();
|
|
41
|
+
* })
|
|
42
|
+
* ]
|
|
43
|
+
* })
|
|
44
|
+
* async getTodayStats() { }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
13
47
|
class TimeDependency {
|
|
14
48
|
constructor(duration) {
|
|
15
49
|
if (typeof duration !== 'number' && typeof duration !== 'function') {
|
|
@@ -48,15 +82,24 @@ class TimeDependency {
|
|
|
48
82
|
this.createdAt = Date.now();
|
|
49
83
|
});
|
|
50
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Get remaining time in milliseconds
|
|
87
|
+
*/
|
|
51
88
|
getRemainingTime() {
|
|
52
89
|
const duration = this.resolveDuration();
|
|
53
90
|
const elapsed = Date.now() - this.createdAt;
|
|
54
91
|
const remaining = duration - elapsed;
|
|
55
92
|
return Math.max(0, remaining);
|
|
56
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if dependency has expired
|
|
96
|
+
*/
|
|
57
97
|
isExpired() {
|
|
58
98
|
return this.getRemainingTime() === 0;
|
|
59
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Resolve duration value (handle dynamic duration)
|
|
102
|
+
*/
|
|
60
103
|
resolveDuration() {
|
|
61
104
|
if (typeof this.duration === 'function') {
|
|
62
105
|
const value = this.duration();
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基础缓存使用示例
|
|
3
|
+
*/
|
|
1
4
|
import { CacheService } from '../index';
|
|
2
5
|
interface User {
|
|
3
6
|
id: string;
|
|
@@ -25,6 +28,9 @@ declare class UserRepository {
|
|
|
25
28
|
declare class PostRepository {
|
|
26
29
|
findByAuthor(authorId: string): Promise<Post[]>;
|
|
27
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* 用户服务示例
|
|
33
|
+
*/
|
|
28
34
|
export declare class UserService {
|
|
29
35
|
private cacheService;
|
|
30
36
|
private userRepository;
|
|
@@ -39,6 +45,9 @@ export declare class UserService {
|
|
|
39
45
|
cacheLargeData(id: string): Promise<void>;
|
|
40
46
|
getLargeData(id: string): Promise<any>;
|
|
41
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* 文章服务示例 - 展示依赖使用
|
|
50
|
+
*/
|
|
42
51
|
export declare class PostService {
|
|
43
52
|
private cacheService;
|
|
44
53
|
private postRepository;
|
|
@@ -47,6 +56,9 @@ export declare class PostService {
|
|
|
47
56
|
getFeaturedPosts(): Promise<Post[]>;
|
|
48
57
|
private getConfigVersion;
|
|
49
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* 缓存统计和监控示例
|
|
61
|
+
*/
|
|
50
62
|
export declare class CacheMonitoringService {
|
|
51
63
|
private cacheService;
|
|
52
64
|
constructor(cacheService: CacheService);
|
|
@@ -54,5 +66,8 @@ export declare class CacheMonitoringService {
|
|
|
54
66
|
resetStats(): void;
|
|
55
67
|
startMonitoring(intervalMs?: number): void;
|
|
56
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* 使用示例
|
|
71
|
+
*/
|
|
57
72
|
declare function demonstrateCacheUsage(): Promise<void>;
|
|
58
73
|
export { demonstrateCacheUsage };
|