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.
Files changed (172) hide show
  1. package/CHANGELOG.md +56 -60
  2. package/LICENSE +201 -21
  3. package/README.md +537 -1828
  4. package/changelogs/README.md +160 -0
  5. package/changelogs/v2.0.0.md +222 -0
  6. package/dist/cjs/index.cjs +10600 -0
  7. package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
  8. package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
  9. package/dist/cjs/transaction/Transaction.cjs +158 -0
  10. package/dist/cjs/transaction/TransactionManager.cjs +298 -0
  11. package/dist/esm/index.mjs +10650 -0
  12. package/dist/types/base.d.ts +81 -0
  13. package/dist/types/collection.d.ts +1031 -0
  14. package/dist/types/expression.d.ts +115 -0
  15. package/dist/types/index.d.ts +23 -0
  16. package/dist/types/lock.d.ts +74 -0
  17. package/dist/types/model.d.ts +526 -0
  18. package/dist/types/mongodb.d.ts +49 -0
  19. package/dist/types/monsqlize.d.ts +491 -0
  20. package/dist/types/pool.d.ts +84 -0
  21. package/dist/types/runtime.d.ts +362 -0
  22. package/dist/types/saga.d.ts +143 -0
  23. package/dist/types/slow-query-log.d.ts +126 -0
  24. package/dist/types/sync.d.ts +103 -0
  25. package/dist/types/transaction.d.ts +132 -0
  26. package/package.json +67 -69
  27. package/index.d.ts +0 -206
  28. package/index.mjs +0 -52
  29. package/lib/cache-invalidation.js +0 -279
  30. package/lib/cache.js +0 -530
  31. package/lib/common/cursor.js +0 -59
  32. package/lib/common/docs-urls.js +0 -73
  33. package/lib/common/index-options.js +0 -223
  34. package/lib/common/log.js +0 -61
  35. package/lib/common/namespace.js +0 -22
  36. package/lib/common/normalize.js +0 -34
  37. package/lib/common/page-result.js +0 -43
  38. package/lib/common/runner.js +0 -57
  39. package/lib/common/server-features.js +0 -232
  40. package/lib/common/shape-builders.js +0 -27
  41. package/lib/common/validation.js +0 -113
  42. package/lib/connect.js +0 -99
  43. package/lib/constants.js +0 -55
  44. package/lib/count-queue.js +0 -188
  45. package/lib/distributed-cache-invalidator.js +0 -260
  46. package/lib/errors.js +0 -168
  47. package/lib/expression/cache/ExpressionCache.js +0 -114
  48. package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
  49. package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
  50. package/lib/expression/detector.js +0 -84
  51. package/lib/expression/factory.js +0 -29
  52. package/lib/expression/index.js +0 -19
  53. package/lib/function-cache.js +0 -533
  54. package/lib/index.js +0 -1251
  55. package/lib/infrastructure/ConnectionPoolManager.js +0 -464
  56. package/lib/infrastructure/HealthChecker.js +0 -281
  57. package/lib/infrastructure/PoolConfig.js +0 -199
  58. package/lib/infrastructure/PoolSelector.js +0 -225
  59. package/lib/infrastructure/PoolStats.js +0 -244
  60. package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
  61. package/lib/infrastructure/ssh-tunnel.js +0 -41
  62. package/lib/infrastructure/uri-parser.js +0 -36
  63. package/lib/lock/Lock.js +0 -67
  64. package/lib/lock/errors.js +0 -28
  65. package/lib/lock/index.js +0 -13
  66. package/lib/logger.js +0 -225
  67. package/lib/model/examples/test.js +0 -311
  68. package/lib/model/features/defaults.js +0 -161
  69. package/lib/model/features/populate.js +0 -568
  70. package/lib/model/features/relations.js +0 -120
  71. package/lib/model/features/soft-delete.js +0 -349
  72. package/lib/model/features/version.js +0 -157
  73. package/lib/model/features/virtuals.js +0 -219
  74. package/lib/model/index.js +0 -1265
  75. package/lib/mongodb/common/accessor-helpers.js +0 -59
  76. package/lib/mongodb/common/agg-pipeline.js +0 -36
  77. package/lib/mongodb/common/aggregation-validator.js +0 -127
  78. package/lib/mongodb/common/iid.js +0 -28
  79. package/lib/mongodb/common/lexicographic-expr.js +0 -53
  80. package/lib/mongodb/common/shape.js +0 -32
  81. package/lib/mongodb/common/sort.js +0 -39
  82. package/lib/mongodb/common/transaction-aware.js +0 -25
  83. package/lib/mongodb/connect.js +0 -234
  84. package/lib/mongodb/index.js +0 -639
  85. package/lib/mongodb/management/admin-ops.js +0 -200
  86. package/lib/mongodb/management/bookmark-ops.js +0 -167
  87. package/lib/mongodb/management/cache-ops.js +0 -50
  88. package/lib/mongodb/management/collection-ops.js +0 -387
  89. package/lib/mongodb/management/database-ops.js +0 -202
  90. package/lib/mongodb/management/index-ops.js +0 -475
  91. package/lib/mongodb/management/index.js +0 -17
  92. package/lib/mongodb/management/namespace.js +0 -31
  93. package/lib/mongodb/management/validation-ops.js +0 -268
  94. package/lib/mongodb/queries/aggregate.js +0 -172
  95. package/lib/mongodb/queries/chain.js +0 -631
  96. package/lib/mongodb/queries/count.js +0 -99
  97. package/lib/mongodb/queries/distinct.js +0 -78
  98. package/lib/mongodb/queries/find-and-count.js +0 -193
  99. package/lib/mongodb/queries/find-by-ids.js +0 -236
  100. package/lib/mongodb/queries/find-one-by-id.js +0 -171
  101. package/lib/mongodb/queries/find-one.js +0 -71
  102. package/lib/mongodb/queries/find-page.js +0 -618
  103. package/lib/mongodb/queries/find.js +0 -171
  104. package/lib/mongodb/queries/index.js +0 -51
  105. package/lib/mongodb/queries/watch.js +0 -538
  106. package/lib/mongodb/writes/common/batch-retry.js +0 -65
  107. package/lib/mongodb/writes/delete-batch.js +0 -323
  108. package/lib/mongodb/writes/delete-many.js +0 -181
  109. package/lib/mongodb/writes/delete-one.js +0 -173
  110. package/lib/mongodb/writes/find-one-and-delete.js +0 -203
  111. package/lib/mongodb/writes/find-one-and-replace.js +0 -239
  112. package/lib/mongodb/writes/find-one-and-update.js +0 -240
  113. package/lib/mongodb/writes/increment-one.js +0 -259
  114. package/lib/mongodb/writes/index.js +0 -46
  115. package/lib/mongodb/writes/insert-batch.js +0 -508
  116. package/lib/mongodb/writes/insert-many.js +0 -223
  117. package/lib/mongodb/writes/insert-one.js +0 -169
  118. package/lib/mongodb/writes/replace-one.js +0 -226
  119. package/lib/mongodb/writes/result-handler.js +0 -237
  120. package/lib/mongodb/writes/update-batch.js +0 -416
  121. package/lib/mongodb/writes/update-many.js +0 -275
  122. package/lib/mongodb/writes/update-one.js +0 -273
  123. package/lib/mongodb/writes/upsert-one.js +0 -203
  124. package/lib/multi-level-cache.js +0 -244
  125. package/lib/operators.js +0 -330
  126. package/lib/redis-cache-adapter.js +0 -267
  127. package/lib/saga/SagaContext.js +0 -67
  128. package/lib/saga/SagaDefinition.js +0 -32
  129. package/lib/saga/SagaExecutor.js +0 -201
  130. package/lib/saga/SagaOrchestrator.js +0 -186
  131. package/lib/saga/index.js +0 -11
  132. package/lib/slow-query-log/base-storage.js +0 -70
  133. package/lib/slow-query-log/batch-queue.js +0 -97
  134. package/lib/slow-query-log/config-manager.js +0 -196
  135. package/lib/slow-query-log/index.js +0 -238
  136. package/lib/slow-query-log/mongodb-storage.js +0 -324
  137. package/lib/slow-query-log/query-hash.js +0 -39
  138. package/lib/sync/ChangeStreamSyncManager.js +0 -405
  139. package/lib/sync/ResumeTokenStore.js +0 -192
  140. package/lib/sync/SyncConfig.js +0 -127
  141. package/lib/sync/SyncTarget.js +0 -240
  142. package/lib/sync/index.js +0 -20
  143. package/lib/transaction/CacheLockManager.js +0 -162
  144. package/lib/transaction/DistributedCacheLockManager.js +0 -475
  145. package/lib/transaction/Transaction.js +0 -315
  146. package/lib/transaction/TransactionManager.js +0 -267
  147. package/lib/transaction/index.js +0 -11
  148. package/lib/utils/objectid-converter.js +0 -632
  149. package/types/README.md +0 -122
  150. package/types/base.ts +0 -94
  151. package/types/batch.ts +0 -187
  152. package/types/cache.ts +0 -71
  153. package/types/chain.ts +0 -254
  154. package/types/collection.ts +0 -357
  155. package/types/expression.ts +0 -109
  156. package/types/function-cache.d.ts +0 -135
  157. package/types/lock.ts +0 -95
  158. package/types/model/definition.ts +0 -152
  159. package/types/model/index.ts +0 -10
  160. package/types/model/instance.ts +0 -121
  161. package/types/model/relations.ts +0 -121
  162. package/types/model/virtuals.ts +0 -32
  163. package/types/monsqlize.ts +0 -245
  164. package/types/options.ts +0 -192
  165. package/types/pagination.ts +0 -154
  166. package/types/pool.ts +0 -125
  167. package/types/query.ts +0 -71
  168. package/types/saga.ts +0 -125
  169. package/types/stream.ts +0 -64
  170. package/types/sync.ts +0 -79
  171. package/types/transaction.ts +0 -79
  172. package/types/write.ts +0 -77
package/types/README.md DELETED
@@ -1,122 +0,0 @@
1
- # Types 模块说明
2
-
3
- 本目录包含 monSQLize 的所有 TypeScript 类型定义,已按功能模块拆分。
4
-
5
- ## 📁 目录结构
6
-
7
- ```
8
- types/
9
- ├── base.ts # 基础类型(ErrorCodes, LoggerLike, ExpressionObject)
10
- ├── expression.ts # 统一表达式操作符(67个操作符)
11
- ├── cache.ts # 缓存接口(CacheLike, MultiLevelCache)
12
- ├── options.ts # 配置选项(BaseOptions, SSH, Transaction)
13
- ├── query.ts # 查询选项(Find, Count, Aggregate, Distinct)
14
- ├── write.ts # 写操作(InsertOne, InsertMany, WriteConcern)
15
- ├── batch.ts # 批量操作(InsertBatch, UpdateBatch, DeleteBatch)
16
- ├── pagination.ts # 分页系统(FindPage, PageResult, Bookmark)
17
- ├── stream.ts # 流式查询(Stream, Explain)
18
- ├── transaction.ts # 事务(Transaction, MongoSession)
19
- ├── lock.ts # 业务锁(Lock, LockOptions)
20
- ├── chain.ts # 链式调用(FindChain, AggregateChain)
21
- ├── pool.ts # 连接池(ConnectionPoolManager, PoolConfig)
22
- ├── saga.ts # Saga事务(SagaOrchestrator, SagaDefinition)
23
- ├── sync.ts # 数据同步(Change Stream, SyncConfig)
24
- ├── collection.ts # Collection API(CollectionAccessor, 所有查询方法)
25
- ├── monsqlize.ts # MonSQLize主类
26
- └── model/ # Model 层
27
- ├── definition.ts # Model定义(ModelDefinition, Schema)
28
- ├── relations.ts # 关系定义(Populate, RelationConfig)
29
- ├── virtuals.ts # 虚拟字段(VirtualConfig)
30
- ├── instance.ts # Model实例和静态方法
31
- └── index.ts # Model 类型汇总
32
- ```
33
-
34
- ## 🔍 模块依赖关系
35
-
36
- ```
37
- base.ts (基础)
38
-
39
- options.ts, query.ts (配置)
40
-
41
- write.ts, batch.ts, pagination.ts, stream.ts, chain.ts (操作)
42
-
43
- transaction.ts, lock.ts, pool.ts, saga.ts, sync.ts (功能)
44
-
45
- collection.ts (集合)
46
-
47
- model/* (Model层)
48
-
49
- monsqlize.ts (主类)
50
-
51
- index.d.ts (统一导出)
52
- ```
53
-
54
- ## 📖 使用指南
55
-
56
- ### 导入类型
57
-
58
- 所有类型统一从 `monsqlize` 模块导入:
59
-
60
- ```typescript
61
- import type { FindOptions, CollectionAccessor, Model } from 'monsqlize';
62
- ```
63
-
64
- ### 查找类型定义
65
-
66
- 1. **按功能查找**:参考上面的目录结构
67
- 2. **使用 IDE**:使用 VS Code 的"转到定义"功能(F12)
68
- 3. **全局搜索**:在 `types/` 目录中搜索类型名称
69
-
70
- ## 🛠️ 开发指南
71
-
72
- ### 修改类型定义
73
-
74
- 1. 找到对应的模块文件(如 `types/query.ts`)
75
- 2. 修改类型定义
76
- 3. 如果是新类型,需要在 `index.d.ts` 中添加导出
77
- 4. 运行 `npx tsc --noEmit` 验证编译通过
78
- 5. 运行 `npm test` 验证测试通过
79
-
80
- ### 添加新类型
81
-
82
- 1. 选择合适的模块文件(或创建新模块)
83
- 2. 添加类型定义
84
- 3. 在 `index.d.ts` 中添加导出语句
85
- ```typescript
86
- export import NewType = ModuleName.NewType;
87
- ```
88
- 4. 验证编译和测试
89
- 5. 更新本 README
90
-
91
- ### 模块划分原则
92
-
93
- 1. **单一职责**:每个文件只包含相关的类型
94
- 2. **依赖清晰**:避免循环依赖
95
- 3. **大小适中**:单个文件不超过 500 行
96
- 4. **命名规范**:使用 kebab-case(如 `multi-level-cache.ts`)
97
-
98
- ## 📦 版本历史
99
-
100
- - **v1.0.10** (2026-01-19): 将 index.d.ts 拆分为 21 个模块(2932 行 → 21 文件)
101
- - **v1.0.9**: 原始单文件结构(index.d.ts 2932 行)
102
-
103
- ## 🔗 相关文档
104
-
105
- - [CHANGELOG.md](../CHANGELOG.md)
106
- - [CONTRIBUTING.md](../CONTRIBUTING.md)
107
- - [实施方案](../plans/refactoring/ref-types-modularization-v1.0.10-revised.md)
108
-
109
- ## 📊 统计信息
110
-
111
- - **总文件数**: 21 个
112
- - **总代码行数**: ~2500 行(包含注释)
113
- - **模块数**: 17 个主模块 + 4 个 Model 子模块
114
- - **导出类型数**: 100+ 个
115
-
116
- ## ✅ 质量保证
117
-
118
- - ✅ 所有模块通过 TypeScript 编译
119
- - ✅ 保持向后兼容(原有导入方式不变)
120
- - ✅ 完整的依赖关系管理
121
- - ✅ 清晰的模块职责划分
122
-
package/types/base.ts DELETED
@@ -1,94 +0,0 @@
1
- /**
2
- * 基础类型定义
3
- * 包含核心类型、错误码、日志接口等
4
- * @module types/base
5
- */
6
-
7
- /**
8
- * 数据库类型
9
- */
10
- export type DbType = 'mongodb';
11
-
12
- /**
13
- * 表达式对象
14
- * 由 expr() 函数创建,包含原始表达式字符串和编译标记
15
- * @since v1.0.9
16
- */
17
- export interface ExpressionObject {
18
- /** 原始表达式字符串 */
19
- __expr__: string;
20
- /** 是否已编译(内部使用) */
21
- __compiled__: boolean;
22
- }
23
-
24
- /**
25
- * 表达式编译上下文
26
- * @since v1.0.9
27
- */
28
- export type ExpressionContext = '$match' | '$project' | '$group' | 'unknown';
29
-
30
- /**
31
- * 统一表达式创建函数
32
- * @since v1.0.9
33
- */
34
- export type ExpressionFunction = (expr: string) => ExpressionObject;
35
-
36
- /**
37
- * 日志记录器接口
38
- * v2.0 新增:支持 traceId 和结构化日志
39
- */
40
- export interface LoggerLike {
41
- debug?: (...args: any[]) => void;
42
- info?: (...args: any[]) => void;
43
- warn?: (...args: any[]) => void;
44
- error?: (...args: any[]) => void;
45
- withTraceId?: (fn: Function, traceId?: string) => any;
46
- getTraceId?: () => string | null;
47
- }
48
-
49
- /**
50
- * 日志选项
51
- */
52
- export interface LoggerOptions {
53
- /** 是否使用结构化日志(JSON 格式) */
54
- structured?: boolean;
55
- /** 是否启用 traceId(分布式追踪) */
56
- enableTraceId?: boolean;
57
- }
58
-
59
- /**
60
- * 统一错误码
61
- */
62
- export const enum ErrorCodes {
63
- VALIDATION_ERROR = 'VALIDATION_ERROR',
64
- INVALID_COLLECTION_NAME = 'INVALID_COLLECTION_NAME',
65
- INVALID_DATABASE_NAME = 'INVALID_DATABASE_NAME',
66
- INVALID_CURSOR = 'INVALID_CURSOR',
67
- CURSOR_SORT_MISMATCH = 'CURSOR_SORT_MISMATCH',
68
- JUMP_TOO_FAR = 'JUMP_TOO_FAR',
69
- STREAM_NO_JUMP = 'STREAM_NO_JUMP',
70
- STREAM_NO_TOTALS = 'STREAM_NO_TOTALS',
71
- NOT_CONNECTED = 'NOT_CONNECTED',
72
- NO_POOL_MANAGER = 'NO_POOL_MANAGER',
73
- POOL_NOT_FOUND = 'POOL_NOT_FOUND',
74
- MODEL_NOT_DEFINED = 'MODEL_NOT_DEFINED',
75
- CONNECTION_TIMEOUT = 'CONNECTION_TIMEOUT',
76
- CONNECTION_FAILED = 'CONNECTION_FAILED',
77
- CONNECTION_CLOSED = 'CONNECTION_CLOSED',
78
- DATABASE_ERROR = 'DATABASE_ERROR',
79
- QUERY_TIMEOUT = 'QUERY_TIMEOUT',
80
- CACHE_ERROR = 'CACHE_ERROR',
81
- CACHE_TIMEOUT = 'CACHE_TIMEOUT',
82
- INVALID_CONFIG = 'INVALID_CONFIG',
83
- UNSUPPORTED_DATABASE = 'UNSUPPORTED_DATABASE',
84
- }
85
-
86
- /**
87
- * 标准错误接口
88
- */
89
- export interface MonSQLizeError extends Error {
90
- code: string;
91
- details?: any[];
92
- cause?: Error;
93
- }
94
-
package/types/batch.ts DELETED
@@ -1,187 +0,0 @@
1
- /**
2
- * 批量操作相关类型定义
3
- * @module types/batch
4
- * @since v1.0.0
5
- */
6
-
7
- import type { WriteConcern } from './write';
8
-
9
- /**
10
- * 批量操作进度信息
11
- */
12
- export interface BatchProgress {
13
- /** 当前批次号(从1开始) */
14
- currentBatch: number;
15
- /** 总批次数 */
16
- totalBatches: number;
17
- /** 已处理数量 */
18
- inserted?: number;
19
- modified?: number;
20
- deleted?: number;
21
- /** 总数量 */
22
- total: number | null;
23
- /** 完成百分比(0-100) */
24
- percentage: number | null;
25
- /** 错误数量 */
26
- errors: number;
27
- /** 重试数量 */
28
- retries: number;
29
- }
30
-
31
- /**
32
- * 重试信息
33
- */
34
- export interface RetryInfo {
35
- /** 批次索引(从0开始) */
36
- batchIndex: number;
37
- /** 当前重试次数 */
38
- attempt: number;
39
- /** 最大重试次数 */
40
- maxAttempts: number;
41
- /** 错误信息 */
42
- error: Error;
43
- }
44
-
45
- /**
46
- * insertBatch 选项
47
- * @since v1.0.0
48
- */
49
- export interface InsertBatchOptions {
50
- /** 每批插入的文档数量(默认 1000) */
51
- batchSize?: number;
52
- /** 并发批次数(1=串行,>1=并行,默认 1) */
53
- concurrency?: number;
54
- /** 批次内是否按顺序插入(默认 false) */
55
- ordered?: boolean;
56
- /** 进度回调函数 */
57
- onProgress?: (progress: BatchProgress) => void;
58
- /** 错误处理策略: 'stop'|'skip'|'collect'|'retry'(默认 'stop') */
59
- onError?: 'stop' | 'skip' | 'collect' | 'retry';
60
- /** 失败批次最大重试次数(onError='retry'时有效,默认 3) */
61
- retryAttempts?: number;
62
- /** 重试延迟时间(毫秒,默认 1000) */
63
- retryDelay?: number;
64
- /** 重试回调函数 */
65
- onRetry?: (retryInfo: RetryInfo) => void;
66
- /** 写确认级别 */
67
- writeConcern?: WriteConcern;
68
- /** 是否绕过文档验证(默认 false) */
69
- bypassDocumentValidation?: boolean;
70
- /** 操作注释(用于日志追踪) */
71
- comment?: string;
72
- }
73
-
74
- /**
75
- * updateBatch 选项
76
- * @since v1.0.0
77
- */
78
- export interface UpdateBatchOptions {
79
- /** 每批更新的文档数量(默认 1000) */
80
- batchSize?: number;
81
- /** 是否预先 count 总数(用于进度百分比,默认 true) */
82
- estimateProgress?: boolean;
83
- /** 进度回调函数 */
84
- onProgress?: (progress: BatchProgress) => void;
85
- /** 错误处理策略: 'stop'|'skip'|'collect'|'retry'(默认 'stop') */
86
- onError?: 'stop' | 'skip' | 'collect' | 'retry';
87
- /** 失败批次最大重试次数(onError='retry'时有效,默认 3) */
88
- retryAttempts?: number;
89
- /** 重试延迟时间(毫秒,默认 1000) */
90
- retryDelay?: number;
91
- /** 重试回调函数 */
92
- onRetry?: (retryInfo: RetryInfo) => void;
93
- /** 写确认级别 */
94
- writeConcern?: WriteConcern;
95
- /** 未匹配时是否插入(默认 false) */
96
- upsert?: boolean;
97
- /** 数组过滤器 */
98
- arrayFilters?: any[];
99
- /** 操作注释(用于日志追踪) */
100
- comment?: string;
101
- }
102
-
103
- /**
104
- * deleteBatch 选项
105
- * @since v1.0.0
106
- */
107
- export interface DeleteBatchOptions {
108
- /** 每批删除的文档数量(默认 1000) */
109
- batchSize?: number;
110
- /** 是否预先 count 总数(用于进度百分比,默认 true) */
111
- estimateProgress?: boolean;
112
- /** 进度回调函数 */
113
- onProgress?: (progress: BatchProgress) => void;
114
- /** 错误处理策略: 'stop'|'skip'|'collect'|'retry'(默认 'stop') */
115
- onError?: 'stop' | 'skip' | 'collect' | 'retry';
116
- /** 失败批次最大重试次数(onError='retry'时有效,默认 3) */
117
- retryAttempts?: number;
118
- /** 重试延迟时间(毫秒,默认 1000) */
119
- retryDelay?: number;
120
- /** 重试回调函数 */
121
- onRetry?: (retryInfo: RetryInfo) => void;
122
- /** 写确认级别 */
123
- writeConcern?: WriteConcern;
124
- /** 操作注释(用于日志追踪) */
125
- comment?: string;
126
- }
127
-
128
- /**
129
- * insertBatch 返回结果
130
- */
131
- export interface InsertBatchResult {
132
- /** 是否被确认 */
133
- acknowledged: boolean;
134
- /** 总文档数 */
135
- totalCount: number;
136
- /** 成功插入数 */
137
- insertedCount: number;
138
- /** 总批次数 */
139
- batchCount: number;
140
- /** 错误列表 */
141
- errors: Array<{ batchIndex: number; message: string; details?: any }>;
142
- /** 重试记录列表 */
143
- retries: Array<{ batchIndex: number; attempts: number; success: boolean }>;
144
- /** 插入的文档 _id 映射表 */
145
- insertedIds: Record<number, any>;
146
- }
147
-
148
- /**
149
- * updateBatch 返回结果
150
- */
151
- export interface UpdateBatchResult {
152
- /** 是否被确认 */
153
- acknowledged: boolean;
154
- /** 总文档数(estimateProgress=true时有值) */
155
- totalCount: number | null;
156
- /** 匹配文档数 */
157
- matchedCount: number;
158
- /** 成功更新数 */
159
- modifiedCount: number;
160
- /** 插入数(upsert=true时) */
161
- upsertedCount: number;
162
- /** 总批次数 */
163
- batchCount: number;
164
- /** 错误列表 */
165
- errors: Array<{ batchIndex: number; message: string; details?: any }>;
166
- /** 重试记录列表 */
167
- retries: Array<{ batchIndex: number; attempts: number; success: boolean }>;
168
- }
169
-
170
- /**
171
- * deleteBatch 返回结果
172
- */
173
- export interface DeleteBatchResult {
174
- /** 是否被确认 */
175
- acknowledged: boolean;
176
- /** 总文档数(estimateProgress=true时有值) */
177
- totalCount: number | null;
178
- /** 成功删除数 */
179
- deletedCount: number;
180
- /** 总批次数 */
181
- batchCount: number;
182
- /** 错误列表 */
183
- errors: Array<{ batchIndex: number; message: string; details?: any }>;
184
- /** 重试记录列表 */
185
- retries: Array<{ batchIndex: number; attempts: number; success: boolean }>;
186
- }
187
-
package/types/cache.ts DELETED
@@ -1,71 +0,0 @@
1
- /**
2
- * 缓存相关类型定义
3
- * @module types/cache
4
- */
5
-
6
- /**
7
- * 缓存接口
8
- */
9
- export interface CacheLike {
10
- get(key: string): Promise<any>;
11
- set(key: string, val: any, ttl?: number): Promise<void>;
12
- del(key: string): Promise<boolean>;
13
- exists(key: string): Promise<boolean>;
14
- getMany(keys: string[]): Promise<Record<string, any>>;
15
- setMany(obj: Record<string, any>, ttl?: number): Promise<boolean>;
16
- delMany(keys: string[]): Promise<number>;
17
- delPattern(pattern: string): Promise<number>;
18
- clear(): void;
19
- keys(pattern?: string): string[];
20
- getStats?(): any;
21
- }
22
-
23
- /**
24
- * 内置内存缓存的配置(MemoryCacheOptions)
25
- * - maxSize: 最大键数量(默认 100000)
26
- * - maxMemory: 最大内存占用(字节;0 表示不限制)
27
- * - enableStats: 是否启用统计,默认 true
28
- * - autoInvalidate: 是否启用精准缓存失效,默认 false(v1.1.5+)
29
- */
30
- export interface MemoryCacheOptions {
31
- maxSize?: number;
32
- maxMemory?: number;
33
- enableStats?: boolean;
34
- /** 🆕 v1.1.5: 是否启用精准缓存失效(默认 false) */
35
- autoInvalidate?: boolean;
36
- // 允许透传其他实现细节,但不做强约束
37
- [k: string]: any;
38
- }
39
-
40
- /**
41
- * 多层缓存写策略
42
- * - both:本地+远端双写(等待远端完成)
43
- * - local-first-async-remote:本地先返回,远端异步写(降低尾延迟)
44
- */
45
- export type WritePolicy = 'both' | 'local-first-async-remote';
46
-
47
- /**
48
- * 多层缓存策略配置
49
- */
50
- export interface MultiLevelCachePolicy {
51
- writePolicy?: WritePolicy;
52
- /** 远端命中后是否回填本地,默认 true */
53
- backfillLocalOnRemoteHit?: boolean;
54
- }
55
-
56
- /**
57
- * 多层缓存配置对象(配置式启用双层缓存)
58
- * 说明:当 BaseOptions.cache 传入该对象时(且 multiLevel=true),框架会自动创建 MultiLevelCache:
59
- * - local:始终使用内置内存缓存(仅接受配置对象 MemoryCacheOptions)
60
- * - remote:可传 CacheLike 实例(推荐生产),或传配置对象以退化为"内存占位"
61
- * - remote.timeoutMs:远端操作超时(毫秒,默认 50)
62
- * - publish:可选的广播函数(例如用于 pub/sub 触发跨进程本地失效)
63
- */
64
- export interface MultiLevelCacheOptions {
65
- multiLevel: true;
66
- local?: MemoryCacheOptions;
67
- remote?: CacheLike | (MemoryCacheOptions & { timeoutMs?: number });
68
- policy?: MultiLevelCachePolicy;
69
- publish?: (msg: any) => void;
70
- }
71
-