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,508 +0,0 @@
1
- /**
2
- * insertBatch 操作实现(改进版 - 支持重试机制)
3
- * 分批批量插入大量文档到集合
4
- */
5
-
6
- const { createError, ErrorCodes } = require('../../errors');
7
- const CacheFactory = require('../../cache');
8
- const { isInTransaction, getTransactionFromSession } = require('../common/transaction-aware');
9
- const { convertObjectIdStrings } = require('../../utils/objectid-converter');
10
-
11
- /**
12
- * 创建 insertBatch 操作
13
- * @param {Object} context - 模块上下文
14
- * @param {Object} context.insertMany - insertMany 方法实例
15
- * @param {Object} context.cache - 缓存实例
16
- * @param {Object} context.logger - 日志实例
17
- * @param {Object} context.defaults - 默认配置
18
- * @param {string} context.collectionName - 集合名称
19
- * @param {string} context.effectiveDbName - 数据库名称
20
- * @param {string} context.instanceId - 实例ID
21
- * @returns {Object} 包含 insertBatch 方法的对象
22
- */
23
- function createInsertBatchOps(context) {
24
- const { insertMany, cache, logger, defaults, collectionName, effectiveDbName: databaseName, instanceId } = context;
25
-
26
- /**
27
- * 分批批量插入多个文档
28
- * @param {Array<Object>} documents - 要插入的文档数组(必需)
29
- * @param {Object} [options] - 操作选项
30
- * @param {number} [options.batchSize=1000] - 每批插入的文档数量
31
- * @param {number} [options.concurrency=1] - 并发批次数(1=串行,>1=并行)
32
- * @param {boolean} [options.ordered=false] - 批次内是否按顺序插入
33
- * @param {Function} [options.onProgress] - 进度回调函数 (progress) => {}
34
- * @param {string} [options.onError='stop'] - 错误处理策略: 'stop'/'skip'/'collect'/'retry'
35
- * @param {number} [options.retryAttempts=3] - 失败批次最大重试次数(onError='retry'时有效)
36
- * @param {number} [options.retryDelay=1000] - 重试延迟时间(毫秒)
37
- * @param {Function} [options.onRetry] - 重试回调函数 (retryInfo) => {}
38
- * @param {Object} [options.writeConcern] - 写关注选项
39
- * @param {boolean} [options.bypassDocumentValidation] - 是否绕过文档验证
40
- * @param {string} [options.comment] - 操作注释(用于日志追踪)
41
- * @returns {Promise<Object>} 插入结果 { acknowledged, totalCount, insertedCount, batchCount, errors, retries, insertedIds }
42
- */
43
- const insertBatch = async function insertBatch(documents, options = {}) {
44
- const startTime = Date.now();
45
-
46
- // 1. 参数验证
47
- if (!Array.isArray(documents)) {
48
- throw createError(
49
- ErrorCodes.DOCUMENTS_REQUIRED,
50
- 'documents 必须是数组类型',
51
- [{ field: 'documents', type: 'array.required', message: 'documents 是必需参数且必须是数组' }]
52
- );
53
- }
54
-
55
- if (documents.length === 0) {
56
- throw createError(
57
- ErrorCodes.DOCUMENTS_REQUIRED,
58
- 'documents 数组不能为空',
59
- [{ field: 'documents', type: 'array.min', message: 'documents 至少需要包含一个文档' }]
60
- );
61
- }
62
-
63
- // ✅ v1.3.0: 自动转换 ObjectId 字符串
64
- const convertedDocuments = documents.map(doc => convertObjectIdStrings(doc, 'document', 0, new WeakSet(), {
65
- logger: context.logger,
66
- excludeFields: context.autoConvertConfig?.excludeFields,
67
- customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
68
- maxDepth: context.autoConvertConfig?.maxDepth
69
- }));
70
-
71
- // 2. 提取选项
72
- const {
73
- batchSize = 1000,
74
- concurrency = 1,
75
- ordered = false,
76
- onProgress,
77
- onError = 'stop',
78
- retryAttempts = 3,
79
- retryDelay = 1000,
80
- onRetry,
81
- ...insertOptions
82
- } = options;
83
-
84
- // 验证参数
85
- if (typeof batchSize !== 'number' || batchSize < 1) {
86
- throw createError(
87
- ErrorCodes.INVALID_PARAMETER,
88
- 'batchSize 必须是大于 0 的数字',
89
- [{ field: 'batchSize', type: 'number.min', message: 'batchSize 必须大于 0' }]
90
- );
91
- }
92
-
93
- if (typeof concurrency !== 'number' || concurrency < 1) {
94
- throw createError(
95
- ErrorCodes.INVALID_PARAMETER,
96
- 'concurrency 必须是大于 0 的数字',
97
- [{ field: 'concurrency', type: 'number.min', message: 'concurrency 必须大于 0' }]
98
- );
99
- }
100
-
101
- const validErrorStrategies = ['stop', 'skip', 'collect', 'retry'];
102
- if (!validErrorStrategies.includes(onError)) {
103
- throw createError(
104
- ErrorCodes.INVALID_PARAMETER,
105
- `onError 必须是以下值之一: ${validErrorStrategies.join(', ')}`,
106
- [{ field: 'onError', type: 'enum', message: `有效值: ${validErrorStrategies.join(', ')}` }]
107
- );
108
- }
109
-
110
- if (typeof retryAttempts !== 'number' || retryAttempts < 0) {
111
- throw createError(
112
- ErrorCodes.INVALID_PARAMETER,
113
- 'retryAttempts 必须是非负数',
114
- [{ field: 'retryAttempts', type: 'number.min', message: 'retryAttempts 必须 >= 0' }]
115
- );
116
- }
117
-
118
- if (typeof retryDelay !== 'number' || retryDelay < 0) {
119
- throw createError(
120
- ErrorCodes.INVALID_PARAMETER,
121
- 'retryDelay 必须是非负数',
122
- [{ field: 'retryDelay', type: 'number.min', message: 'retryDelay 必须 >= 0' }]
123
- );
124
- }
125
-
126
- // 3. 构建操作上下文
127
- const operation = 'insertBatch';
128
- const ns = `${databaseName}.${collectionName}`;
129
- const totalCount = convertedDocuments.length;
130
- const totalBatches = Math.ceil(totalCount / batchSize);
131
-
132
- // 结果统计
133
- const result = {
134
- acknowledged: true,
135
- totalCount,
136
- insertedCount: 0,
137
- batchCount: totalBatches,
138
- errors: [],
139
- retries: [], // 新增:重试记录
140
- insertedIds: {}
141
- };
142
-
143
- logger.debug(`[${operation}] 开始分批插入`, {
144
- ns,
145
- totalCount,
146
- batchSize,
147
- totalBatches,
148
- concurrency,
149
- onError,
150
- retryAttempts: onError === 'retry' ? retryAttempts : 0
151
- });
152
-
153
- try {
154
- // 4. 分割文档为批次
155
- const batches = [];
156
- for (let i = 0; i < convertedDocuments.length; i += batchSize) {
157
- batches.push({
158
- index: batches.length,
159
- documents: convertedDocuments.slice(i, Math.min(i + batchSize, convertedDocuments.length)),
160
- startIndex: i
161
- });
162
- }
163
-
164
- // 5. 执行批次插入
165
- if (concurrency === 1) {
166
- await executeSequential(batches, result, insertOptions, onProgress, {
167
- onError,
168
- retryAttempts,
169
- retryDelay,
170
- onRetry,
171
- logger,
172
- insertMany
173
- });
174
- } else {
175
- await executeConcurrent(batches, result, insertOptions, onProgress, concurrency, {
176
- onError,
177
- retryAttempts,
178
- retryDelay,
179
- onRetry,
180
- logger,
181
- insertMany
182
- });
183
- }
184
-
185
- // 6. 最终缓存失效
186
- if (result.insertedCount > 0 && cache) {
187
- try {
188
- const nsObj = {
189
- iid: instanceId,
190
- type: 'mongodb',
191
- db: databaseName,
192
- collection: collectionName
193
- };
194
- const pattern = CacheFactory.buildNamespacePattern(nsObj);
195
- const deleted = await cache.delPattern(pattern);
196
-
197
- if (deleted > 0) {
198
- logger.debug(`[${operation}] 自动失效缓存: ${nsObj.db}.${nsObj.collection}, 删除 ${deleted} 个缓存键`);
199
- }
200
- } catch (cacheErr) {
201
- logger.warn(`[${operation}] 缓存失效失败: ${cacheErr.message}`, { ns, error: cacheErr });
202
- }
203
- }
204
-
205
- // 7. 记录完成日志
206
- const duration = Date.now() - startTime;
207
- const slowThreshold = defaults.slowThreshold || 5000;
208
-
209
- if (duration > slowThreshold) {
210
- logger.warn(`[${operation}] 慢操作警告`, {
211
- ns,
212
- duration,
213
- threshold: slowThreshold,
214
- totalCount,
215
- insertedCount: result.insertedCount,
216
- batchCount: result.batchCount,
217
- errorCount: result.errors.length,
218
- retryCount: result.retries.length,
219
- comment: insertOptions.comment
220
- });
221
- } else {
222
- logger.debug(`[${operation}] 操作完成`, {
223
- ns,
224
- duration,
225
- totalCount,
226
- insertedCount: result.insertedCount,
227
- batchCount: result.batchCount,
228
- errorCount: result.errors.length,
229
- retryCount: result.retries.length
230
- });
231
- }
232
-
233
- // 8. 如果有错误且策略是 stop,抛出错误
234
- if (result.errors.length > 0 && onError === 'stop') {
235
- throw createError(
236
- ErrorCodes.WRITE_ERROR,
237
- `insertBatch 操作失败: 在批次 ${result.errors[0].batchIndex + 1}/${totalBatches} 遇到错误`,
238
- result.errors,
239
- result.errors[0].error
240
- );
241
- }
242
-
243
- return result;
244
-
245
- } catch (error) {
246
- const duration = Date.now() - startTime;
247
-
248
- logger.error(`[${operation}] 操作失败`, {
249
- ns,
250
- duration,
251
- error: error.message,
252
- code: error.code,
253
- totalCount,
254
- insertedCount: result.insertedCount,
255
- errorCount: result.errors.length,
256
- retryCount: result.retries.length
257
- });
258
-
259
- if (error.code === ErrorCodes.WRITE_ERROR && result.errors.length > 0) {
260
- throw error;
261
- }
262
-
263
- throw createError(
264
- ErrorCodes.WRITE_ERROR,
265
- `insertBatch 操作失败: ${error.message}`,
266
- null,
267
- error
268
- );
269
- }
270
- };
271
-
272
- /**
273
- * 执行单个批次插入(带重试)
274
- */
275
- async function executeBatchWithRetry(batch, insertOptions, retryContext) {
276
- const { onError, retryAttempts, retryDelay, onRetry, logger, insertMany } = retryContext;
277
-
278
- let lastError = null;
279
- let attempts = 0;
280
- const maxAttempts = onError === 'retry' ? retryAttempts + 1 : 1;
281
-
282
- while (attempts < maxAttempts) {
283
- try {
284
- const batchResult = await insertMany(batch.documents, {
285
- ...insertOptions,
286
- ordered: insertOptions.ordered !== undefined ? insertOptions.ordered : false
287
- });
288
-
289
- // 成功
290
- return {
291
- success: true,
292
- batch,
293
- result: batchResult,
294
- attempts
295
- };
296
-
297
- } catch (error) {
298
- lastError = error;
299
- attempts++;
300
-
301
- // 如果还可以重试
302
- if (attempts < maxAttempts) {
303
- logger.warn(`[insertBatch] 批次 ${batch.index + 1} 失败,准备重试 (${attempts}/${retryAttempts})`, {
304
- error: error.message,
305
- batchIndex: batch.index
306
- });
307
-
308
- // 触发重试回调
309
- if (onRetry) {
310
- onRetry({
311
- batchIndex: batch.index,
312
- attempt: attempts,
313
- maxAttempts: retryAttempts,
314
- error,
315
- nextRetryDelay: retryDelay
316
- });
317
- }
318
-
319
- // 延迟后重试
320
- if (retryDelay > 0) {
321
- await new Promise(resolve => setTimeout(resolve, retryDelay));
322
- }
323
- } else {
324
- // 重试次数用尽
325
- logger.error(`[insertBatch] 批次 ${batch.index + 1} 重试失败,已达最大重试次数`, {
326
- error: error.message,
327
- attempts,
328
- batchIndex: batch.index
329
- });
330
- }
331
- }
332
- }
333
-
334
- // 所有重试都失败
335
- return {
336
- success: false,
337
- batch,
338
- error: lastError,
339
- attempts
340
- };
341
- }
342
-
343
- /**
344
- * 串行执行批次插入
345
- */
346
- async function executeSequential(batches, result, insertOptions, onProgress, retryContext) {
347
- for (const batch of batches) {
348
- const batchResult = await executeBatchWithRetry(batch, insertOptions, retryContext);
349
-
350
- if (batchResult.success) {
351
- // 更新结果
352
- result.insertedCount += batchResult.result.insertedCount;
353
-
354
- // 合并 insertedIds
355
- if (batchResult.result.insertedIds) {
356
- for (const [localIndex, id] of Object.entries(batchResult.result.insertedIds)) {
357
- const globalIndex = batch.startIndex + parseInt(localIndex);
358
- result.insertedIds[globalIndex] = id;
359
- }
360
- }
361
-
362
- // 记录重试信息
363
- if (batchResult.attempts > 0) {
364
- result.retries.push({
365
- batchIndex: batch.index,
366
- attempts: batchResult.attempts,
367
- success: true
368
- });
369
- }
370
-
371
- } else {
372
- const errorInfo = {
373
- batchIndex: batch.index,
374
- batchStartIndex: batch.startIndex,
375
- batchSize: batch.documents.length,
376
- error: batchResult.error,
377
- code: batchResult.error.code,
378
- message: batchResult.error.message,
379
- attempts: batchResult.attempts
380
- };
381
-
382
- result.errors.push(errorInfo);
383
-
384
- // 记录重试信息
385
- if (batchResult.attempts > 1) {
386
- result.retries.push({
387
- batchIndex: batch.index,
388
- attempts: batchResult.attempts,
389
- success: false
390
- });
391
- }
392
-
393
- // 根据错误策略处理
394
- if (retryContext.onError === 'stop') {
395
- throw batchResult.error;
396
- } else if (retryContext.onError === 'skip' || retryContext.onError === 'retry') {
397
- retryContext.logger.warn(`[insertBatch] 跳过失败批次 ${batch.index + 1}/${batches.length}`, errorInfo);
398
- } else if (retryContext.onError === 'collect') {
399
- retryContext.logger.warn(`[insertBatch] 收集错误,继续执行 ${batch.index + 1}/${batches.length}`, errorInfo);
400
- }
401
- }
402
-
403
- // 触发进度回调
404
- if (onProgress) {
405
- onProgress({
406
- currentBatch: batch.index + 1,
407
- totalBatches: batches.length,
408
- inserted: result.insertedCount,
409
- total: result.totalCount,
410
- percentage: Math.round((result.insertedCount / result.totalCount) * 100),
411
- errors: result.errors.length,
412
- retries: result.retries.length
413
- });
414
- }
415
- }
416
- }
417
-
418
- /**
419
- * 并发执行批次插入
420
- */
421
- async function executeConcurrent(batches, result, insertOptions, onProgress, concurrency, retryContext) {
422
- const executing = [];
423
- const results = [];
424
-
425
- for (let i = 0; i < batches.length; i++) {
426
- const batch = batches[i];
427
-
428
- const promise = executeBatchWithRetry(batch, insertOptions, retryContext);
429
-
430
- results.push(promise);
431
- executing.push(promise);
432
-
433
- // 控制并发数量
434
- if (executing.length >= concurrency) {
435
- await Promise.race(executing);
436
- executing.splice(0, executing.findIndex(p => p === promise) + 1);
437
- }
438
-
439
- // 等待当前批次
440
- const batchResult = await promise;
441
-
442
- // 更新结果
443
- if (batchResult.success) {
444
- result.insertedCount += batchResult.result.insertedCount;
445
-
446
- if (batchResult.result.insertedIds) {
447
- for (const [localIndex, id] of Object.entries(batchResult.result.insertedIds)) {
448
- const globalIndex = batch.startIndex + parseInt(localIndex);
449
- result.insertedIds[globalIndex] = id;
450
- }
451
- }
452
-
453
- if (batchResult.attempts > 0) {
454
- result.retries.push({
455
- batchIndex: batch.index,
456
- attempts: batchResult.attempts,
457
- success: true
458
- });
459
- }
460
- } else {
461
- const errorInfo = {
462
- batchIndex: batch.index,
463
- batchStartIndex: batch.startIndex,
464
- batchSize: batch.documents.length,
465
- error: batchResult.error,
466
- code: batchResult.error.code,
467
- message: batchResult.error.message,
468
- attempts: batchResult.attempts
469
- };
470
-
471
- result.errors.push(errorInfo);
472
-
473
- if (batchResult.attempts > 1) {
474
- result.retries.push({
475
- batchIndex: batch.index,
476
- attempts: batchResult.attempts,
477
- success: false
478
- });
479
- }
480
-
481
- if (retryContext.onError === 'stop') {
482
- throw batchResult.error;
483
- }
484
- }
485
-
486
- // 触发进度回调
487
- if (onProgress) {
488
- onProgress({
489
- currentBatch: i + 1,
490
- totalBatches: batches.length,
491
- inserted: result.insertedCount,
492
- total: result.totalCount,
493
- percentage: Math.round((result.insertedCount / result.totalCount) * 100),
494
- errors: result.errors.length,
495
- retries: result.retries.length
496
- });
497
- }
498
- }
499
-
500
- await Promise.all(results);
501
- }
502
-
503
- return { insertBatch };
504
- }
505
-
506
- module.exports = { createInsertBatchOps };
507
-
508
-