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
@@ -1,121 +0,0 @@
1
- /**
2
- * Model 关系定义相关类型
3
- * @module types/model/relations
4
- * @since v1.2.0
5
- */
6
-
7
- /**
8
- * 关系配置(MongoDB 原生风格)
9
- * @since v1.2.0
10
- */
11
- export interface RelationConfig {
12
- /**
13
- * 关联的集合名称(MongoDB 原生集合名)
14
- * @example 'posts'
15
- */
16
- from: string;
17
-
18
- /**
19
- * 本地字段名(用于匹配的字段)
20
- * @example '_id'
21
- */
22
- localField: string;
23
-
24
- /**
25
- * 外部字段名(关联集合中的字段)
26
- * @example 'authorId'
27
- */
28
- foreignField: string;
29
-
30
- /**
31
- * 返回类型
32
- * - true: 返回单个文档或 null(one-to-one)
33
- * - false: 返回数组(one-to-many)
34
- * @default false
35
- */
36
- single?: boolean;
37
- }
38
-
39
- /**
40
- * Populate 配置
41
- * @since v1.2.0
42
- */
43
- export interface PopulateConfig {
44
- /**
45
- * 关系路径(relations 中定义的名称)
46
- * @example 'posts'
47
- */
48
- path: string;
49
-
50
- /**
51
- * 字段选择(空格分隔)
52
- * @example 'title content createdAt'
53
- */
54
- select?: string;
55
-
56
- /**
57
- * 排序规则
58
- * @example { createdAt: -1 }
59
- */
60
- sort?: Record<string, 1 | -1>;
61
-
62
- /**
63
- * 限制返回数量
64
- * @example 10
65
- */
66
- limit?: number;
67
-
68
- /**
69
- * 跳过数量
70
- * @example 20
71
- */
72
- skip?: number;
73
-
74
- /**
75
- * 额外过滤条件
76
- * @example { status: 'published' }
77
- */
78
- match?: any;
79
-
80
- /**
81
- * 嵌套 populate(支持多层关联)
82
- * @example
83
- * // 字符串形式
84
- * populate: 'comments'
85
- *
86
- * // 对象形式
87
- * populate: { path: 'comments', select: 'content' }
88
- *
89
- * // 数组形式
90
- * populate: ['comments', 'likes']
91
- */
92
- populate?: string | PopulateConfig | (string | PopulateConfig)[];
93
- }
94
-
95
- /**
96
- * PopulateProxy - 支持链式 populate 调用
97
- * @since v1.2.0
98
- */
99
- export interface PopulateProxy<T = any> extends Promise<T> {
100
- /**
101
- * 添加 populate 路径(链式调用)
102
- *
103
- * @param path - 关系路径或配置对象
104
- * @returns PopulateProxy 实例,支持继续链式调用
105
- *
106
- * @example
107
- * // 字符串形式
108
- * User.findOne({ _id }).populate('posts')
109
- *
110
- * // 对象形式
111
- * User.findOne({ _id }).populate({ path: 'posts', select: 'title' })
112
- *
113
- * // 链式调用
114
- * User.findOne({ _id })
115
- * .populate('profile')
116
- * .populate({ path: 'posts', limit: 10 })
117
- */
118
- populate(path: string): PopulateProxy<T>;
119
- populate(config: PopulateConfig): PopulateProxy<T>;
120
- }
121
-
@@ -1,32 +0,0 @@
1
- /**
2
- * Model 虚拟字段相关类型
3
- * @module types/model/virtuals
4
- * @since v1.0.6
5
- */
6
-
7
- /**
8
- * 虚拟字段配置
9
- * @since v1.0.6
10
- */
11
- export interface VirtualConfig {
12
- /**
13
- * getter 函数(必需)
14
- * @example
15
- * get: function() {
16
- * return `${this.firstName} ${this.lastName}`;
17
- * }
18
- */
19
- get: (this: any) => any;
20
-
21
- /**
22
- * setter 函数(可选)
23
- * @example
24
- * set: function(value) {
25
- * const parts = value.split(' ');
26
- * this.firstName = parts[0];
27
- * this.lastName = parts[1];
28
- * }
29
- */
30
- set?: (this: any, value: any) => void;
31
- }
32
-
@@ -1,245 +0,0 @@
1
- /**
2
- * MonSQLize 主类类型定义
3
- * @module types/monsqlize
4
- */
5
-
6
- import type { TransactionOptions } from './options';
7
- import type { DbAccessor, HealthView, Collection } from './collection';
8
- import type { CacheLike } from './cache';
9
- import type { Transaction } from './transaction';
10
- import type { Lock, LockOptions } from './lock';
11
- import type { ExpressionFunction } from './base';
12
- import type { MetaInfo } from './pagination';
13
- import type { ModelInstance } from './model';
14
-
15
- /**
16
- * MonSQLize 主类
17
- */
18
- export interface MonSQLize {
19
-
20
- /**
21
- * 连接数据库
22
- */
23
- connect(): Promise<DbAccessor>;
24
-
25
- /**
26
- * 获取缓存实例
27
- */
28
- getCache(): CacheLike;
29
-
30
- /**
31
- * 获取默认配置
32
- */
33
- getDefaults(): {
34
- maxTimeMS?: number;
35
- findLimit?: number;
36
- namespace?: { instanceId?: string; scope?: 'database' | 'connection' };
37
- slowQueryMs?: number;
38
- };
39
-
40
- /**
41
- * 关闭连接
42
- */
43
- close(): Promise<void>;
44
-
45
- /**
46
- * 健康检查
47
- */
48
- health(): Promise<HealthView>;
49
-
50
- /**
51
- * 获取 Model 实例(缓存复用)
52
- *
53
- * 同一 collectionName 多次调用返回同一实例。
54
- * Model.redefine() / Model.undefine() 后自动失效,close() 后全部清空。
55
- *
56
- * @param collectionName - 已注册的集合名称
57
- * @returns ModelInstance 实例
58
- * @throws 数据库未连接(NOT_CONNECTED)
59
- * @throws Model 未定义(MODEL_NOT_DEFINED)
60
- * @since 1.0.3
61
- */
62
- model(collectionName: string): ModelInstance;
63
-
64
- /**
65
- * 获取原始集合实例
66
- *
67
- * @param collectionName - 集合名称
68
- * @returns Collection 实例
69
- * @throws 数据库未连接(NOT_CONNECTED)
70
- */
71
- collection(collectionName: string): Collection;
72
-
73
- /**
74
- * 获取限定池/库的 collection(公开底层方法)
75
- *
76
- * opts.pool / opts.database 均为可选;均不填时退化为默认路由。
77
- *
78
- * @param collectionName - MongoDB 集合名
79
- * @param opts.pool - 连接池名称(可选)
80
- * @param opts.database - 数据库名称(可选)
81
- * @throws NOT_CONNECTED / NO_POOL_MANAGER / POOL_NOT_FOUND
82
- * @since 1.3.0
83
- */
84
- scopedCollection(
85
- collectionName: string,
86
- opts?: { pool?: string; database?: string }
87
- ): ReturnType<MonSQLize['collection']>;
88
-
89
- /**
90
- * 获取限定池/库的 Model 实例(不走 _modelInstances 缓存)
91
- *
92
- * connection 合并语义:opts 字段优先,definition.connection 作 fallback。
93
- *
94
- * @param key - 已注册的 Model key(或 alias)
95
- * @param opts - 路由选项,字段优先于 definition.connection
96
- * @throws NOT_CONNECTED / MODEL_NOT_DEFINED / NO_POOL_MANAGER / POOL_NOT_FOUND
97
- * @since 1.3.0
98
- */
99
- scopedModel(
100
- key: string,
101
- opts?: { pool?: string; database?: string }
102
- ): ReturnType<MonSQLize['model']>;
103
-
104
- /**
105
- * 获取指定连接池的链式访问器
106
- *
107
- * 立即校验:NOT_CONNECTED / NO_POOL_MANAGER / POOL_NOT_FOUND。
108
- *
109
- * @param poolName - 连接池名称
110
- * @throws NOT_CONNECTED / NO_POOL_MANAGER / POOL_NOT_FOUND
111
- * @since 1.3.0
112
- */
113
- pool(poolName: string): {
114
- collection: (name: string) => ReturnType<MonSQLize['collection']>;
115
- model: (key: string) => ReturnType<MonSQLize['model']>;
116
- use: (dbName: string) => {
117
- collection: (name: string) => ReturnType<MonSQLize['collection']>;
118
- model: (key: string) => ReturnType<MonSQLize['model']>;
119
- };
120
- };
121
-
122
- /**
123
- * 获取「默认池 + 指定库」访问器
124
- *
125
- * 适用于单连接多库场景(无需配置多连接池)。
126
- *
127
- * @param dbName - 数据库名称
128
- * @throws NOT_CONNECTED
129
- * @since 1.3.0
130
- */
131
- use(dbName: string): {
132
- collection: (name: string) => ReturnType<MonSQLize['collection']>;
133
- model: (key: string) => ReturnType<MonSQLize['model']>;
134
- };
135
-
136
-
137
- // ============================================================================
138
- // 事件系统
139
- // ============================================================================
140
-
141
- /** 事件订阅 */
142
- on(event: 'connected', handler: (payload: { type: string; db: string; scope?: string; iid?: string }) => void): void;
143
- on(event: 'closed', handler: (payload: { type: string; db: string; iid?: string }) => void): void;
144
- on(event: 'error', handler: (payload: { type: string; db: string; error: string; iid?: string }) => void): void;
145
- on(event: 'slow-query', handler: (meta: MetaInfo) => void): void;
146
- on(event: 'query', handler: (meta: MetaInfo) => void): void;
147
- on(event: string, handler: (payload: any) => void): void;
148
-
149
- /** 一次性事件订阅 */
150
- once(event: string, handler: (payload: any) => void): void;
151
-
152
- /** 取消事件订阅 */
153
- off(event: string, handler: (payload: any) => void): void;
154
-
155
- /** 触发事件 */
156
- emit(event: string, payload: any): void;
157
-
158
- // ============================================================================
159
- // 事务管理
160
- // ============================================================================
161
-
162
- /**
163
- * 创建手动事务会话
164
- * @since v0.2.0
165
- */
166
- startSession(options?: TransactionOptions): Promise<Transaction>;
167
-
168
- /**
169
- * 自动管理事务生命周期(推荐)
170
- * @since v0.2.0
171
- */
172
- withTransaction<T = any>(
173
- callback: (transaction: Transaction) => Promise<T>,
174
- options?: TransactionOptions
175
- ): Promise<T>;
176
-
177
- // ============================================================================
178
- // 业务锁 API
179
- // ============================================================================
180
-
181
- /**
182
- * 业务锁:自动管理锁生命周期(推荐)
183
- * @since v1.4.0
184
- */
185
- withLock?<T = any>(
186
- key: string,
187
- callback: () => Promise<T>,
188
- options?: LockOptions
189
- ): Promise<T>;
190
-
191
- /**
192
- * 业务锁:手动获取锁(阻塞重试)
193
- * @since v1.4.0
194
- */
195
- acquireLock?(key: string, options?: LockOptions): Promise<Lock>;
196
-
197
- /**
198
- * 业务锁:尝试获取锁(不阻塞)
199
- * @since v1.4.0
200
- */
201
- tryAcquireLock?(key: string, options?: Omit<LockOptions, 'retryTimes'>): Promise<Lock | null>;
202
-
203
- /**
204
- * 获取慢查询日志
205
- * @since v1.3.1
206
- */
207
- getSlowQueryLogs(
208
- filter?: Record<string, any>,
209
- options?: {
210
- sort?: Record<string, 1 | -1>;
211
- limit?: number;
212
- skip?: number;
213
- }
214
- ): Promise<Array<{
215
- queryHash: string;
216
- database: string;
217
- collection: string;
218
- operation: string;
219
- count: number;
220
- totalTimeMs: number;
221
- avgTimeMs: number;
222
- maxTimeMs: number;
223
- minTimeMs: number;
224
- firstSeen: Date;
225
- lastSeen: Date;
226
- }>>;
227
- }
228
-
229
- /**
230
- * MonSQLize 命名空间(静态成员)
231
- */
232
- export declare namespace MonSQLize {
233
- /**
234
- * 统一表达式创建函数(静态方法)
235
- * @since v1.0.9
236
- */
237
- const expr: ExpressionFunction;
238
-
239
- /**
240
- * 统一表达式创建函数(完整版别名)
241
- * @since v1.0.9
242
- */
243
- const createExpression: ExpressionFunction;
244
- }
245
-
package/types/options.ts DELETED
@@ -1,192 +0,0 @@
1
- /**
2
- * 配置选项类型定义
3
- * @module types/options
4
- */
5
-
6
- import type { DbType, LoggerLike } from './base';
7
- import type { CacheLike, MemoryCacheOptions, MultiLevelCacheOptions } from './cache';
8
-
9
- /**
10
- * SSH 隧道配置
11
- * @since v1.3.0
12
- */
13
- export interface SSHConfig {
14
- /** SSH 服务器地址 */
15
- host: string;
16
- /** SSH 服务器端口(默认 22) */
17
- port?: number;
18
- /** SSH 用户名 */
19
- username: string;
20
- /** SSH 密码(与 privateKey 二选一) */
21
- password?: string;
22
- /** SSH 私钥(字符串或 Buffer,与 password 二选一) */
23
- privateKey?: string | Buffer;
24
- /** 私钥密码(如果私钥有加密) */
25
- passphrase?: string;
26
- /** 连接超时时间(毫秒,默认 30000) */
27
- readyTimeout?: number;
28
- /** 保持连接的间隔时间(毫秒,默认 10000) */
29
- keepaliveInterval?: number;
30
- /** 目标数据库主机(相对于 SSH 服务器,默认 'localhost') */
31
- dstHost?: string;
32
- /** 目标数据库端口(相对于 SSH 服务器) */
33
- dstPort?: number;
34
- }
35
-
36
- /**
37
- * 事务选项配置
38
- * @since v0.2.0
39
- */
40
- export interface TransactionOptions {
41
- /** 读关注级别(Read Concern) */
42
- readConcern?: {
43
- level: 'local' | 'majority' | 'snapshot' | 'linearizable' | 'available';
44
- };
45
- /** 读偏好(Read Preference) */
46
- readPreference?: 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
47
- /** 因果一致性(Causal Consistency) */
48
- causalConsistency?: boolean;
49
- /** 事务最大执行时间(毫秒) */
50
- maxDuration?: number;
51
- /** 是否启用自动重试 */
52
- enableRetry?: boolean;
53
- /** 最大重试次数 */
54
- maxRetries?: number;
55
- /** 重试延迟(毫秒) */
56
- retryDelay?: number;
57
- /** 重试退避系数 */
58
- retryBackoff?: number;
59
- /** 是否启用缓存锁 */
60
- enableCacheLock?: boolean;
61
- /** 缓存锁清理间隔(毫秒) */
62
- lockCleanupInterval?: number;
63
- }
64
-
65
- /**
66
- * MonSQLize 基础配置选项
67
- */
68
- export interface BaseOptions {
69
- type: DbType;
70
- databaseName: string;
71
- config: any;
72
- /** SSH 隧道配置(v1.3.0+) */
73
- ssh?: SSHConfig;
74
- cache?: CacheLike | MemoryCacheOptions | MultiLevelCacheOptions | object;
75
- logger?: LoggerLike;
76
- maxTimeMS?: number; // 全局默认查询超时(毫秒)
77
- findLimit?: number; // 全局默认 find limit(未传 limit 时使用;0 表示不限制)
78
- namespace?: { instanceId?: string; scope?: 'database' | 'connection' };
79
- slowQueryMs?: number; // 慢查询日志阈值(毫秒),默认 500
80
- /** MongoDB 副本集读偏好(全局配置)
81
- * - 'primary': 仅读主节点(默认)
82
- * - 'primaryPreferred': 优先读主节点,主节点不可用时读从节点
83
- * - 'secondary': 仅读从节点
84
- * - 'secondaryPreferred': 优先读从节点,从节点不可用时读主节点
85
- * - 'nearest': 读最近的节点(低延迟)
86
- */
87
- readPreference?: 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
88
- /**
89
- * 事务配置(MongoDB Transaction API)
90
- * 需要 MongoDB 4.0+ 且部署在副本集或分片集群上
91
- * @since v0.2.0
92
- */
93
- transaction?: TransactionOptions | false;
94
- // 统一默认(新增可选项)
95
- findPageMaxLimit?: number; // 深分页页大小上限(默认 500)
96
- cursorSecret?: string; // 可选:游标签名密钥(如启用 HMAC 验签)
97
- log?: {
98
- slowQueryTag?: { event?: string; code?: string };
99
- formatSlowQuery?: (meta: any) => any;
100
- };
101
- /**
102
- * ObjectId 自动转换配置(v1.3.0+)
103
- * 自动将字符串 _id 转换为 ObjectId
104
- * @default true(MongoDB)
105
- * @since v1.3.0
106
- */
107
- autoConvertObjectId?: boolean | {
108
- /** 是否启用(默认 true) */
109
- enabled?: boolean;
110
- /** 排除字段列表 */
111
- excludeFields?: string[];
112
- /** 自定义字段模式 */
113
- customFieldPatterns?: string[];
114
- /** 最大转换深度(默认 10) */
115
- maxDepth?: number;
116
- /** 日志级别(默认 'warn') */
117
- logLevel?: 'info' | 'warn' | 'error';
118
- };
119
- /**
120
- * Count 队列配置(高并发控制)
121
- * 避免大量并发 count 查询压垮数据库
122
- * @default { enabled: true, concurrency: CPU核心数 }
123
- * @since v1.0.0
124
- */
125
- countQueue?: boolean | {
126
- /** 是否启用队列(默认 true) */
127
- enabled?: boolean;
128
- /** 并发数(默认 CPU 核心数,最少 4,最多 16) */
129
- concurrency?: number;
130
- /** 最大队列长度(默认 10000) */
131
- maxQueueSize?: number;
132
- /** 超时时间(毫秒,默认 60000) */
133
- timeout?: number;
134
- };
135
- /**
136
- * Model 自动加载配置(v1.4.0+)
137
- * 自动扫描指定目录加载 Model 定义文件
138
- * @default undefined(不启用)
139
- * @since v1.4.0
140
- */
141
- models?: string | {
142
- /** Model 文件目录路径(相对或绝对路径) */
143
- path: string;
144
- /** 文件名模式(支持 glob 格式,默认 '*.model.{js,ts,mjs,cjs}') */
145
- pattern?: string;
146
- /** 是否递归扫描子目录(默认 false) */
147
- recursive?: boolean;
148
- };
149
- /**
150
- * Change Stream 数据同步配置(v1.0.8+)
151
- * 实时同步数据到备份库
152
- * @requires MongoDB Replica Set
153
- * @requires MongoDB 4.0+
154
- * @since v1.0.8
155
- */
156
- sync?: {
157
- /** 是否启用同步 */
158
- enabled: boolean;
159
- /** 备份目标数组 */
160
- targets: Array<{
161
- /** 目标名称 */
162
- name: string;
163
- /** MongoDB URI */
164
- uri: string;
165
- /** 要同步的集合(['*'] 表示全部) */
166
- collections?: string[];
167
- /** 健康检查配置 */
168
- healthCheck?: {
169
- enabled?: boolean;
170
- interval?: number;
171
- timeout?: number;
172
- retries?: number;
173
- };
174
- }>;
175
- /** 要监听的集合(可选,默认全部) */
176
- collections?: string[];
177
- /** Resume Token 配置 */
178
- resumeToken?: {
179
- /** 存储类型('file' | 'redis') */
180
- storage?: 'file' | 'redis';
181
- /** 文件路径(storage='file' 时使用) */
182
- path?: string;
183
- /** Redis 实例(storage='redis' 时使用) */
184
- redis?: any;
185
- };
186
- /** 事件过滤函数 */
187
- filter?: (event: any) => boolean;
188
- /** 数据转换函数 */
189
- transform?: (document: any) => any;
190
- };
191
- }
192
-