monsqlize 1.3.0 → 2.0.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/CHANGELOG.md +56 -60
- package/LICENSE +201 -21
- package/README.md +537 -1828
- package/changelogs/README.md +160 -0
- package/changelogs/v2.0.0.md +222 -0
- package/dist/cjs/index.cjs +10600 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10650 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +526 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +67 -69
- package/index.d.ts +0 -206
- package/index.mjs +0 -52
- package/lib/cache-invalidation.js +0 -279
- package/lib/cache.js +0 -530
- package/lib/common/cursor.js +0 -59
- package/lib/common/docs-urls.js +0 -73
- package/lib/common/index-options.js +0 -223
- package/lib/common/log.js +0 -61
- package/lib/common/namespace.js +0 -22
- package/lib/common/normalize.js +0 -34
- package/lib/common/page-result.js +0 -43
- package/lib/common/runner.js +0 -57
- package/lib/common/server-features.js +0 -232
- package/lib/common/shape-builders.js +0 -27
- package/lib/common/validation.js +0 -113
- package/lib/connect.js +0 -99
- package/lib/constants.js +0 -55
- package/lib/count-queue.js +0 -188
- package/lib/distributed-cache-invalidator.js +0 -260
- package/lib/errors.js +0 -168
- package/lib/expression/cache/ExpressionCache.js +0 -114
- package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
- package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
- package/lib/expression/detector.js +0 -84
- package/lib/expression/factory.js +0 -29
- package/lib/expression/index.js +0 -19
- package/lib/function-cache.js +0 -533
- package/lib/index.js +0 -1251
- package/lib/infrastructure/ConnectionPoolManager.js +0 -464
- package/lib/infrastructure/HealthChecker.js +0 -281
- package/lib/infrastructure/PoolConfig.js +0 -199
- package/lib/infrastructure/PoolSelector.js +0 -225
- package/lib/infrastructure/PoolStats.js +0 -244
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
- package/lib/infrastructure/ssh-tunnel.js +0 -41
- package/lib/infrastructure/uri-parser.js +0 -36
- package/lib/lock/Lock.js +0 -67
- package/lib/lock/errors.js +0 -28
- package/lib/lock/index.js +0 -13
- package/lib/logger.js +0 -225
- package/lib/model/examples/test.js +0 -311
- package/lib/model/features/defaults.js +0 -161
- package/lib/model/features/populate.js +0 -568
- package/lib/model/features/relations.js +0 -120
- package/lib/model/features/soft-delete.js +0 -349
- package/lib/model/features/version.js +0 -157
- package/lib/model/features/virtuals.js +0 -219
- package/lib/model/index.js +0 -1265
- package/lib/mongodb/common/accessor-helpers.js +0 -59
- package/lib/mongodb/common/agg-pipeline.js +0 -36
- package/lib/mongodb/common/aggregation-validator.js +0 -127
- package/lib/mongodb/common/iid.js +0 -28
- package/lib/mongodb/common/lexicographic-expr.js +0 -53
- package/lib/mongodb/common/shape.js +0 -32
- package/lib/mongodb/common/sort.js +0 -39
- package/lib/mongodb/common/transaction-aware.js +0 -25
- package/lib/mongodb/connect.js +0 -234
- package/lib/mongodb/index.js +0 -639
- package/lib/mongodb/management/admin-ops.js +0 -200
- package/lib/mongodb/management/bookmark-ops.js +0 -167
- package/lib/mongodb/management/cache-ops.js +0 -50
- package/lib/mongodb/management/collection-ops.js +0 -387
- package/lib/mongodb/management/database-ops.js +0 -202
- package/lib/mongodb/management/index-ops.js +0 -475
- package/lib/mongodb/management/index.js +0 -17
- package/lib/mongodb/management/namespace.js +0 -31
- package/lib/mongodb/management/validation-ops.js +0 -268
- package/lib/mongodb/queries/aggregate.js +0 -172
- package/lib/mongodb/queries/chain.js +0 -631
- package/lib/mongodb/queries/count.js +0 -99
- package/lib/mongodb/queries/distinct.js +0 -78
- package/lib/mongodb/queries/find-and-count.js +0 -193
- package/lib/mongodb/queries/find-by-ids.js +0 -236
- package/lib/mongodb/queries/find-one-by-id.js +0 -171
- package/lib/mongodb/queries/find-one.js +0 -71
- package/lib/mongodb/queries/find-page.js +0 -618
- package/lib/mongodb/queries/find.js +0 -171
- package/lib/mongodb/queries/index.js +0 -51
- package/lib/mongodb/queries/watch.js +0 -538
- package/lib/mongodb/writes/common/batch-retry.js +0 -65
- package/lib/mongodb/writes/delete-batch.js +0 -323
- package/lib/mongodb/writes/delete-many.js +0 -181
- package/lib/mongodb/writes/delete-one.js +0 -173
- package/lib/mongodb/writes/find-one-and-delete.js +0 -203
- package/lib/mongodb/writes/find-one-and-replace.js +0 -239
- package/lib/mongodb/writes/find-one-and-update.js +0 -240
- package/lib/mongodb/writes/increment-one.js +0 -259
- package/lib/mongodb/writes/index.js +0 -46
- package/lib/mongodb/writes/insert-batch.js +0 -508
- package/lib/mongodb/writes/insert-many.js +0 -223
- package/lib/mongodb/writes/insert-one.js +0 -169
- package/lib/mongodb/writes/replace-one.js +0 -226
- package/lib/mongodb/writes/result-handler.js +0 -237
- package/lib/mongodb/writes/update-batch.js +0 -416
- package/lib/mongodb/writes/update-many.js +0 -275
- package/lib/mongodb/writes/update-one.js +0 -273
- package/lib/mongodb/writes/upsert-one.js +0 -203
- package/lib/multi-level-cache.js +0 -244
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -267
- package/lib/saga/SagaContext.js +0 -67
- package/lib/saga/SagaDefinition.js +0 -32
- package/lib/saga/SagaExecutor.js +0 -201
- package/lib/saga/SagaOrchestrator.js +0 -186
- package/lib/saga/index.js +0 -11
- package/lib/slow-query-log/base-storage.js +0 -70
- package/lib/slow-query-log/batch-queue.js +0 -97
- package/lib/slow-query-log/config-manager.js +0 -196
- package/lib/slow-query-log/index.js +0 -238
- package/lib/slow-query-log/mongodb-storage.js +0 -324
- package/lib/slow-query-log/query-hash.js +0 -39
- package/lib/sync/ChangeStreamSyncManager.js +0 -405
- package/lib/sync/ResumeTokenStore.js +0 -192
- package/lib/sync/SyncConfig.js +0 -127
- package/lib/sync/SyncTarget.js +0 -240
- package/lib/sync/index.js +0 -20
- package/lib/transaction/CacheLockManager.js +0 -162
- package/lib/transaction/DistributedCacheLockManager.js +0 -475
- package/lib/transaction/Transaction.js +0 -315
- package/lib/transaction/TransactionManager.js +0 -267
- package/lib/transaction/index.js +0 -11
- package/lib/utils/objectid-converter.js +0 -632
- package/types/README.md +0 -122
- package/types/base.ts +0 -94
- package/types/batch.ts +0 -187
- package/types/cache.ts +0 -71
- package/types/chain.ts +0 -254
- package/types/collection.ts +0 -357
- package/types/expression.ts +0 -109
- package/types/function-cache.d.ts +0 -135
- package/types/lock.ts +0 -95
- package/types/model/definition.ts +0 -152
- package/types/model/index.ts +0 -10
- package/types/model/instance.ts +0 -121
- package/types/model/relations.ts +0 -121
- package/types/model/virtuals.ts +0 -32
- package/types/monsqlize.ts +0 -245
- package/types/options.ts +0 -192
- package/types/pagination.ts +0 -154
- package/types/pool.ts +0 -125
- package/types/query.ts +0 -71
- package/types/saga.ts +0 -125
- package/types/stream.ts +0 -64
- package/types/sync.ts +0 -79
- package/types/transaction.ts +0 -79
- package/types/write.ts +0 -77
package/types/expression.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 统一表达式系统类型定义
|
|
3
|
-
* @module types/expression
|
|
4
|
-
* @since v1.0.9
|
|
5
|
-
* @updated v1.1.0 - 新增49个操作符,实现100% MongoDB支持
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 支持的统一表达式操作符(122个 - 100% MongoDB支持)
|
|
10
|
-
* @since v1.0.9
|
|
11
|
-
* @updated v1.1.0
|
|
12
|
-
*/
|
|
13
|
-
export namespace UnifiedExpressionOperators {
|
|
14
|
-
/** 三元运算符 */
|
|
15
|
-
export type TernaryOperator = '? :';
|
|
16
|
-
|
|
17
|
-
/** 空值合并运算符 */
|
|
18
|
-
export type NullishCoalescing = '??';
|
|
19
|
-
|
|
20
|
-
/** 比较运算符 */
|
|
21
|
-
export type ComparisonOperators = '>' | '>=' | '<' | '<=' | '===' | '!==';
|
|
22
|
-
|
|
23
|
-
/** 逻辑运算符 */
|
|
24
|
-
export type LogicalOperators = '&&' | '||' | 'NOT';
|
|
25
|
-
|
|
26
|
-
/** 算术运算符 */
|
|
27
|
-
export type ArithmeticOperators = '+' | '-' | '*' | '/' | '%';
|
|
28
|
-
|
|
29
|
-
/** 数学函数(8个)*/
|
|
30
|
-
export type MathFunctions = 'ABS' | 'CEIL' | 'FLOOR' | 'ROUND' | 'SQRT' | 'POW' | 'LOG' | 'LOG10';
|
|
31
|
-
|
|
32
|
-
/** 字符串基础函数(12个)*/
|
|
33
|
-
export type StringBasicFunctions = 'CONCAT' | 'UPPER' | 'LOWER' | 'TRIM' | 'SUBSTR' | 'LENGTH' |
|
|
34
|
-
'SPLIT' | 'REPLACE' | 'INDEX_OF_STR' | 'LTRIM' | 'RTRIM' | 'SUBSTR_CP';
|
|
35
|
-
|
|
36
|
-
/** 字符串扩展函数(3个)v1.1.0 */
|
|
37
|
-
export type StringExtendedFunctions = 'STR_LEN_BYTES' | 'STR_LEN_CP' | 'SUBSTR_BYTES';
|
|
38
|
-
|
|
39
|
-
/** 数组基础函数(13个)*/
|
|
40
|
-
export type ArrayBasicFunctions = 'SIZE' | 'ARRAY_ELEM_AT' | 'IN' | 'SLICE' | 'FIRST' | 'LAST' |
|
|
41
|
-
'FILTER' | 'MAP' | 'INDEX_OF' | 'CONCAT_ARRAYS';
|
|
42
|
-
|
|
43
|
-
/** 数组扩展函数(4个)v1.1.0 */
|
|
44
|
-
export type ArrayExtendedFunctions = 'REDUCE' | 'ZIP' | 'REVERSE_ARRAY' | 'RANGE';
|
|
45
|
-
|
|
46
|
-
/** 日期基础函数(6个)*/
|
|
47
|
-
export type DateBasicFunctions = 'YEAR' | 'MONTH' | 'DAY_OF_MONTH' | 'HOUR' | 'MINUTE' | 'SECOND';
|
|
48
|
-
|
|
49
|
-
/** 日期高级函数(5个)v1.1.0 */
|
|
50
|
-
export type DateAdvancedFunctions = 'DATE_ADD' | 'DATE_SUBTRACT' | 'DATE_DIFF' | 'DATE_TO_STRING' | 'DATE_FROM_STRING';
|
|
51
|
-
|
|
52
|
-
/** 日期扩展函数(8个)v1.1.0 */
|
|
53
|
-
export type DateExtendedFunctions = 'DATE_FROM_PARTS' | 'DATE_TO_PARTS' | 'ISO_WEEK' | 'ISO_WEEK_YEAR' |
|
|
54
|
-
'ISO_DAY_OF_WEEK' | 'DAY_OF_WEEK' | 'DAY_OF_YEAR' | 'WEEK';
|
|
55
|
-
|
|
56
|
-
/** 类型检查函数(5个)*/
|
|
57
|
-
export type TypeCheckFunctions = 'TYPE' | 'IS_NUMBER' | 'IS_ARRAY' | 'EXISTS' | 'NOT';
|
|
58
|
-
|
|
59
|
-
/** 类型转换基础函数(3个)*/
|
|
60
|
-
export type TypeConversionBasicFunctions = 'TO_INT' | 'TO_STRING' | 'OBJECT_TO_ARRAY' | 'ARRAY_TO_OBJECT';
|
|
61
|
-
|
|
62
|
-
/** 类型转换扩展函数(7个)v1.1.0 */
|
|
63
|
-
export type TypeConversionExtendedFunctions = 'TO_BOOL' | 'TO_DATE' | 'TO_DOUBLE' | 'TO_DECIMAL' |
|
|
64
|
-
'TO_LONG' | 'TO_OBJECT_ID' | 'CONVERT';
|
|
65
|
-
|
|
66
|
-
/** 逻辑扩展函数(2个)v1.1.0 */
|
|
67
|
-
export type LogicalExtendedFunctions = 'ALL_ELEMENTS_TRUE' | 'ANY_ELEMENT_TRUE';
|
|
68
|
-
|
|
69
|
-
/** 条件函数(3个)*/
|
|
70
|
-
export type ConditionalFunctions = 'SWITCH' | 'COND' | 'IF_NULL';
|
|
71
|
-
|
|
72
|
-
/** 对象操作函数(5个)*/
|
|
73
|
-
export type ObjectOperationFunctions = 'MERGE_OBJECTS' | 'SET_FIELD' | 'UNSET_FIELD' | 'GET_FIELD' | 'OBJECT_TO_ARRAY';
|
|
74
|
-
|
|
75
|
-
/** 集合操作函数(5个)*/
|
|
76
|
-
export type SetOperationFunctions = 'SET_UNION' | 'SET_DIFFERENCE' | 'SET_EQUALS' | 'SET_INTERSECTION' | 'SET_IS_SUBSET';
|
|
77
|
-
|
|
78
|
-
/** 高级操作函数(4个)v1.1.0 */
|
|
79
|
-
export type AdvancedOperationFunctions = 'LET' | 'LITERAL' | 'RAND' | 'SAMPLE_RATE' | 'REGEX';
|
|
80
|
-
|
|
81
|
-
/** 聚合累加器(7个)*/
|
|
82
|
-
export type AggregateFunctions = 'SUM' | 'AVG' | 'MAX' | 'MIN' | 'COUNT' | 'PUSH' | 'ADD_TO_SET';
|
|
83
|
-
|
|
84
|
-
/** 所有操作符联合类型 */
|
|
85
|
-
export type AllOperators =
|
|
86
|
-
| TernaryOperator
|
|
87
|
-
| NullishCoalescing
|
|
88
|
-
| ComparisonOperators
|
|
89
|
-
| LogicalOperators
|
|
90
|
-
| ArithmeticOperators
|
|
91
|
-
| MathFunctions
|
|
92
|
-
| StringBasicFunctions
|
|
93
|
-
| StringExtendedFunctions
|
|
94
|
-
| ArrayBasicFunctions
|
|
95
|
-
| ArrayExtendedFunctions
|
|
96
|
-
| DateBasicFunctions
|
|
97
|
-
| DateAdvancedFunctions
|
|
98
|
-
| DateExtendedFunctions
|
|
99
|
-
| TypeCheckFunctions
|
|
100
|
-
| TypeConversionBasicFunctions
|
|
101
|
-
| TypeConversionExtendedFunctions
|
|
102
|
-
| LogicalExtendedFunctions
|
|
103
|
-
| ConditionalFunctions
|
|
104
|
-
| ObjectOperationFunctions
|
|
105
|
-
| SetOperationFunctions
|
|
106
|
-
| AdvancedOperationFunctions
|
|
107
|
-
| AggregateFunctions;
|
|
108
|
-
}
|
|
109
|
-
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 函数缓存类型定义
|
|
3
|
-
* 🆕 v1.1.4: 通用函数缓存
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* withCache 配置选项
|
|
8
|
-
*/
|
|
9
|
-
export interface WithCacheOptions<T extends (...args: any[]) => any> {
|
|
10
|
-
/** 缓存时间(毫秒) */
|
|
11
|
-
ttl?: number;
|
|
12
|
-
|
|
13
|
-
/** 自定义键生成函数 */
|
|
14
|
-
keyBuilder?: (...args: Parameters<T>) => string;
|
|
15
|
-
|
|
16
|
-
/** 缓存实例 */
|
|
17
|
-
cache?: import('./cache').CacheLike;
|
|
18
|
-
|
|
19
|
-
/** 命名空间 */
|
|
20
|
-
namespace?: string;
|
|
21
|
-
|
|
22
|
-
/** 条件缓存函数 */
|
|
23
|
-
condition?: (result: Awaited<ReturnType<T>>) => boolean;
|
|
24
|
-
|
|
25
|
-
/** 启用统计 */
|
|
26
|
-
enableStats?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 缓存统计信息
|
|
31
|
-
*/
|
|
32
|
-
export interface CacheStats {
|
|
33
|
-
/** 命中次数 */
|
|
34
|
-
hits: number;
|
|
35
|
-
|
|
36
|
-
/** 未命中次数 */
|
|
37
|
-
misses: number;
|
|
38
|
-
|
|
39
|
-
/** 错误次数 */
|
|
40
|
-
errors: number;
|
|
41
|
-
|
|
42
|
-
/** 总调用次数 */
|
|
43
|
-
calls: number;
|
|
44
|
-
|
|
45
|
-
/** 总耗时(毫秒) */
|
|
46
|
-
totalTime: number;
|
|
47
|
-
|
|
48
|
-
/** 命中率 */
|
|
49
|
-
hitRate: number;
|
|
50
|
-
|
|
51
|
-
/** 平均耗时(毫秒) */
|
|
52
|
-
avgTime: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* FunctionCache 配置选项
|
|
57
|
-
*/
|
|
58
|
-
export interface FunctionCacheOptions {
|
|
59
|
-
/** 命名空间 */
|
|
60
|
-
namespace?: string;
|
|
61
|
-
|
|
62
|
-
/** 默认 TTL(毫秒) */
|
|
63
|
-
defaultTTL?: number;
|
|
64
|
-
|
|
65
|
-
/** 启用统计 */
|
|
66
|
-
enableStats?: boolean;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 包装后的函数(带统计方法)
|
|
71
|
-
*/
|
|
72
|
-
export type CachedFunction<T extends (...args: any[]) => any> = T & {
|
|
73
|
-
getCacheStats(): CacheStats;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 为函数添加缓存能力
|
|
78
|
-
*/
|
|
79
|
-
export function withCache<T extends (...args: any[]) => any>(
|
|
80
|
-
fn: T,
|
|
81
|
-
options?: WithCacheOptions<T>
|
|
82
|
-
): CachedFunction<T>;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 函数缓存管理类
|
|
86
|
-
*/
|
|
87
|
-
export class FunctionCache {
|
|
88
|
-
constructor(msq?: any, options?: FunctionCacheOptions);
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 注册函数
|
|
92
|
-
* 🔧 v1.1.4-hotfix: 改为异步方法
|
|
93
|
-
*/
|
|
94
|
-
register<T extends (...args: any[]) => any>(
|
|
95
|
-
name: string,
|
|
96
|
-
fn: T,
|
|
97
|
-
options?: WithCacheOptions<T>
|
|
98
|
-
): Promise<void>;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 执行函数
|
|
102
|
-
*/
|
|
103
|
-
execute<T = any>(name: string, ...args: any[]): Promise<T>;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 失效缓存
|
|
107
|
-
*/
|
|
108
|
-
invalidate(name: string, ...args: any[]): Promise<void>;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 批量失效缓存
|
|
112
|
-
*/
|
|
113
|
-
invalidatePattern(pattern: string): Promise<number>;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 获取统计信息
|
|
117
|
-
*/
|
|
118
|
-
getStats(name?: string): CacheStats | Record<string, CacheStats> | null;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 列出所有已注册的函数
|
|
122
|
-
*/
|
|
123
|
-
list(): string[];
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* 重置统计信息
|
|
127
|
-
*/
|
|
128
|
-
resetStats(name?: string): void;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 清空所有已注册的函数
|
|
132
|
-
*/
|
|
133
|
-
clear(): void;
|
|
134
|
-
}
|
|
135
|
-
|
package/types/lock.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 业务锁相关类型定义
|
|
3
|
-
* @module types/lock
|
|
4
|
-
* @since v1.4.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 业务锁配置选项
|
|
9
|
-
* @since v1.4.0
|
|
10
|
-
*/
|
|
11
|
-
export interface LockOptions {
|
|
12
|
-
/** 锁过期时间(毫秒),默认 10000 */
|
|
13
|
-
ttl?: number;
|
|
14
|
-
/** 获取锁失败时的重试次数,默认 3 */
|
|
15
|
-
retryTimes?: number;
|
|
16
|
-
/** 重试间隔(毫秒),默认 100 */
|
|
17
|
-
retryDelay?: number;
|
|
18
|
-
/** Redis 不可用时是否降级为无锁执行,默认 false */
|
|
19
|
-
fallbackToNoLock?: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 锁对象
|
|
24
|
-
* 表示一个已获取的锁,提供释放和续期方法
|
|
25
|
-
* @since v1.4.0
|
|
26
|
-
*/
|
|
27
|
-
export interface Lock {
|
|
28
|
-
/** 锁的 Key */
|
|
29
|
-
readonly key: string;
|
|
30
|
-
/** 锁的唯一ID */
|
|
31
|
-
readonly lockId: string;
|
|
32
|
-
/** 是否已释放 */
|
|
33
|
-
readonly released: boolean;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 释放锁
|
|
37
|
-
* @returns Promise<boolean> 是否成功释放
|
|
38
|
-
*/
|
|
39
|
-
release(): Promise<boolean>;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 续期(延长 TTL)
|
|
43
|
-
* @param ttl - 新的过期时间(毫秒),默认使用原TTL
|
|
44
|
-
* @returns Promise<boolean> 是否成功续期
|
|
45
|
-
*/
|
|
46
|
-
renew(ttl?: number): Promise<boolean>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 检查锁是否仍被持有
|
|
50
|
-
* @returns boolean
|
|
51
|
-
*/
|
|
52
|
-
isHeld(): boolean;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 获取锁持有时间(毫秒)
|
|
56
|
-
* @returns number
|
|
57
|
-
*/
|
|
58
|
-
getHoldTime(): number;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 锁统计信息
|
|
63
|
-
* @since v1.4.0
|
|
64
|
-
*/
|
|
65
|
-
export interface LockStats {
|
|
66
|
-
/** 成功获取锁的次数 */
|
|
67
|
-
locksAcquired: number;
|
|
68
|
-
/** 成功释放锁的次数 */
|
|
69
|
-
locksReleased: number;
|
|
70
|
-
/** 锁检查次数 */
|
|
71
|
-
lockChecks: number;
|
|
72
|
-
/** 错误次数 */
|
|
73
|
-
errors: number;
|
|
74
|
-
/** 锁键前缀 */
|
|
75
|
-
lockKeyPrefix: string;
|
|
76
|
-
/** 锁最大持续时间 */
|
|
77
|
-
maxDuration: number;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 锁获取失败错误
|
|
82
|
-
* @since v1.4.0
|
|
83
|
-
*/
|
|
84
|
-
export interface LockAcquireError extends Error {
|
|
85
|
-
readonly code: 'LOCK_ACQUIRE_FAILED';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 锁超时错误
|
|
90
|
-
* @since v1.4.0
|
|
91
|
-
*/
|
|
92
|
-
export interface LockTimeoutError extends Error {
|
|
93
|
-
readonly code: 'LOCK_TIMEOUT';
|
|
94
|
-
}
|
|
95
|
-
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Model 定义相关类型
|
|
3
|
-
* @module types/model/definition
|
|
4
|
-
* @since v1.0.3
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Schema DSL 函数类型
|
|
9
|
-
*/
|
|
10
|
-
export type SchemaDSL = (dsl: any) => any;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 默认值类型
|
|
14
|
-
* @since v1.0.6
|
|
15
|
-
*/
|
|
16
|
-
export type DefaultValue<T = any> = T | ((context?: any, doc?: any) => T);
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 钩子上下文
|
|
20
|
-
*/
|
|
21
|
-
export interface HookContext {
|
|
22
|
-
operation: string;
|
|
23
|
-
collection: string;
|
|
24
|
-
data?: any;
|
|
25
|
-
filter?: any;
|
|
26
|
-
update?: any;
|
|
27
|
-
result?: any;
|
|
28
|
-
error?: Error;
|
|
29
|
-
[key: string]: any;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 验证结果
|
|
34
|
-
*/
|
|
35
|
-
export interface ValidationResult {
|
|
36
|
-
valid: boolean;
|
|
37
|
-
errors?: Array<{
|
|
38
|
-
field: string;
|
|
39
|
-
message: string;
|
|
40
|
-
value?: any;
|
|
41
|
-
}>;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Model 定义配置
|
|
46
|
-
*/
|
|
47
|
-
export interface ModelDefinition<T = any> {
|
|
48
|
-
/**
|
|
49
|
-
* 枚举配置(可被 schema 引用)
|
|
50
|
-
* @example
|
|
51
|
-
* enums: {
|
|
52
|
-
* role: 'admin|user|guest',
|
|
53
|
-
* status: 'active|inactive'
|
|
54
|
-
* }
|
|
55
|
-
*/
|
|
56
|
-
enums?: Record<string, string>;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Schema 定义(数据验证规则)
|
|
60
|
-
* 使用 function 时,this 自动绑定到 definition,可访问 this.enums
|
|
61
|
-
*/
|
|
62
|
-
schema?: SchemaDSL | Record<string, any>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 默认值配置
|
|
66
|
-
* @example
|
|
67
|
-
* defaults: {
|
|
68
|
-
* status: 'active',
|
|
69
|
-
* createdAt: () => new Date()
|
|
70
|
-
* }
|
|
71
|
-
*/
|
|
72
|
-
defaults?: Record<string, DefaultValue>;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 钩子函数配置
|
|
76
|
-
*/
|
|
77
|
-
hooks?: {
|
|
78
|
-
beforeCreate?: (context: HookContext) => Promise<void> | void;
|
|
79
|
-
afterCreate?: (context: HookContext) => Promise<void> | void;
|
|
80
|
-
beforeUpdate?: (context: HookContext) => Promise<void> | void;
|
|
81
|
-
afterUpdate?: (context: HookContext) => Promise<void> | void;
|
|
82
|
-
beforeDelete?: (context: HookContext) => Promise<void> | void;
|
|
83
|
-
afterDelete?: (context: HookContext) => Promise<void> | void;
|
|
84
|
-
beforeFind?: (context: HookContext) => Promise<void> | void;
|
|
85
|
-
afterFind?: (context: HookContext) => Promise<void> | void;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 方法配置(实例方法)
|
|
90
|
-
* @example
|
|
91
|
-
* methods: {
|
|
92
|
-
* fullName: function() {
|
|
93
|
-
* return `${this.firstName} ${this.lastName}`;
|
|
94
|
-
* }
|
|
95
|
-
* }
|
|
96
|
-
*/
|
|
97
|
-
methods?: Record<string, (this: any, ...args: any[]) => any>;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 静态方法配置(Model 静态方法)
|
|
101
|
-
* @example
|
|
102
|
-
* statics: {
|
|
103
|
-
* findByRole: async function(role) {
|
|
104
|
-
* return this.find({ role });
|
|
105
|
-
* }
|
|
106
|
-
* }
|
|
107
|
-
*/
|
|
108
|
-
statics?: Record<string, (...args: any[]) => any>;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 数据源绑定配置(v1.2.2+)
|
|
112
|
-
*
|
|
113
|
-
* 声明此 Model 使用哪个连接池和/或数据库。
|
|
114
|
-
* 不配置时行为与 v1.2.1 完全相同(向后兼容)。
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* // 绑定到指定连接池 + 数据库
|
|
118
|
-
* connection: { pool: 'analytics', database: 'reports_db' }
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* // 只切换数据库(使用默认连接池)
|
|
122
|
-
* connection: { database: 'logs_db' }
|
|
123
|
-
*
|
|
124
|
-
* @since v1.2.2
|
|
125
|
-
*/
|
|
126
|
-
connection?: ModelConnection;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Model 数据源绑定配置
|
|
131
|
-
*
|
|
132
|
-
* 声明 Model 使用哪个连接池(pool)和/或数据库(database)。
|
|
133
|
-
* 两个字段均为可选:
|
|
134
|
-
* - 只指定 pool → 使用该池 + 实例初始化时的 databaseName
|
|
135
|
-
* - 只指定 database → 使用默认连接池 + 指定数据库
|
|
136
|
-
* - 两者都指定 → 使用指定池 + 指定数据库
|
|
137
|
-
* - 均不指定 → 与原来完全相同(向后兼容)
|
|
138
|
-
*
|
|
139
|
-
* @since v1.2.2
|
|
140
|
-
*/
|
|
141
|
-
export interface ModelConnection {
|
|
142
|
-
/**
|
|
143
|
-
* 连接池名称,须与 MonSQLize 构造函数 `pools[].name` 对应
|
|
144
|
-
*/
|
|
145
|
-
pool?: string;
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 数据库名称,不指定时使用实例初始化时的 `databaseName`
|
|
149
|
-
*/
|
|
150
|
-
database?: string;
|
|
151
|
-
}
|
|
152
|
-
|
package/types/model/index.ts
DELETED
package/types/model/instance.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Model 实例和静态类相关类型
|
|
3
|
-
* @module types/model/instance
|
|
4
|
-
* @since v1.0.3
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { ModelDefinition, ValidationResult } from "./definition";
|
|
8
|
-
import type { PopulateProxy } from "./relations";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Model 注册表条目(Model.get() 的返回值结构)
|
|
12
|
-
* @since 1.1.7
|
|
13
|
-
*/
|
|
14
|
-
export interface RegisteredModel<T = any> {
|
|
15
|
-
collectionName: string;
|
|
16
|
-
definition: ModelDefinition<T>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Model 实例接口
|
|
21
|
-
* 文档实例的扩展方法
|
|
22
|
-
*/
|
|
23
|
-
export interface ModelInstance<T = any> {
|
|
24
|
-
/**
|
|
25
|
-
* 保存文档(插入或更新)
|
|
26
|
-
*/
|
|
27
|
-
save(): Promise<T>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 删除文档
|
|
31
|
-
*/
|
|
32
|
-
remove(): Promise<boolean>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 验证文档
|
|
36
|
-
*/
|
|
37
|
-
validate(): Promise<ValidationResult>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 关联查询(populate)
|
|
41
|
-
*/
|
|
42
|
-
populate(path: string): PopulateProxy<T>;
|
|
43
|
-
populate(config: any): PopulateProxy<T>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 转换为普通对象
|
|
47
|
-
*/
|
|
48
|
-
toObject(): T;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 转换为 JSON
|
|
52
|
-
*/
|
|
53
|
-
toJSON(): any;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Model 命名空间(静态方法)
|
|
58
|
-
*/
|
|
59
|
-
export declare namespace Model {
|
|
60
|
-
/**
|
|
61
|
-
* 注册一个 Model 定义
|
|
62
|
-
*
|
|
63
|
-
* @param collectionName - 集合名称
|
|
64
|
-
* @param definition - Model 定义对象
|
|
65
|
-
* @throws {Error} 集合名称无效、schema 未定义、Model 已存在
|
|
66
|
-
*/
|
|
67
|
-
function define<T = any>(
|
|
68
|
-
collectionName: string,
|
|
69
|
-
definition: ModelDefinition<T>,
|
|
70
|
-
): void;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 获取已注册的 Model 条目
|
|
74
|
-
*
|
|
75
|
-
* @param collectionName - 集合名称
|
|
76
|
-
* @returns 注册条目 { collectionName, definition },如果不存在返回 undefined
|
|
77
|
-
*/
|
|
78
|
-
function get<T = any>(collectionName: string): RegisteredModel<T> | undefined;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 检查 Model 是否已注册
|
|
82
|
-
*
|
|
83
|
-
* @param collectionName - 集合名称
|
|
84
|
-
* @returns boolean
|
|
85
|
-
*/
|
|
86
|
-
function has(collectionName: string): boolean;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 列出所有已注册的 Model 名称
|
|
90
|
-
*
|
|
91
|
-
* @returns 集合名称数组
|
|
92
|
-
*/
|
|
93
|
-
function list(): string[];
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 注销已注册的 Model 定义
|
|
97
|
-
*
|
|
98
|
-
* 从全局注册表中移除指定的 Model 定义。
|
|
99
|
-
* 已实例化的 ModelInstance 不受影响,仅影响后续通过 msq.model() 获取的新实例。
|
|
100
|
-
*
|
|
101
|
-
* @param collectionName - 集合名称
|
|
102
|
-
* @returns 如果成功移除返回 true,如果 Model 不存在返回 false
|
|
103
|
-
* @since 1.1.7
|
|
104
|
-
*/
|
|
105
|
-
function undefine(collectionName: string): boolean;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 重新定义已注册的 Model(undefine + define 的组合操作)
|
|
109
|
-
*
|
|
110
|
-
* 如果 Model 不存在,行为等同于 define()(首次注册)。
|
|
111
|
-
* 注意:如果新的 definition 校验失败,旧定义将被移除。
|
|
112
|
-
*
|
|
113
|
-
* @param collectionName - 集合名称
|
|
114
|
-
* @param definition - 新的 Model 定义对象
|
|
115
|
-
* @since 1.1.7
|
|
116
|
-
*/
|
|
117
|
-
function redefine<T = any>(
|
|
118
|
-
collectionName: string,
|
|
119
|
-
definition: ModelDefinition<T>,
|
|
120
|
-
): void;
|
|
121
|
-
}
|