monsqlize 1.0.1 → 1.0.5

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 (66) hide show
  1. package/CHANGELOG.md +204 -2464
  2. package/README.md +735 -1198
  3. package/index.d.ts +942 -18
  4. package/lib/cache.js +8 -8
  5. package/lib/common/validation.js +64 -1
  6. package/lib/connect.js +3 -3
  7. package/lib/errors.js +10 -0
  8. package/lib/index.js +173 -9
  9. package/lib/infrastructure/ssh-tunnel-ssh2.js +211 -0
  10. package/lib/infrastructure/ssh-tunnel.js +40 -0
  11. package/lib/infrastructure/uri-parser.js +35 -0
  12. package/lib/lock/Lock.js +66 -0
  13. package/lib/lock/errors.js +27 -0
  14. package/lib/lock/index.js +12 -0
  15. package/lib/logger.js +1 -1
  16. package/lib/model/examples/test.js +225 -29
  17. package/lib/model/features/soft-delete.js +348 -0
  18. package/lib/model/features/version.js +156 -0
  19. package/lib/model/index.js +756 -0
  20. package/lib/mongodb/common/accessor-helpers.js +17 -3
  21. package/lib/mongodb/connect.js +68 -13
  22. package/lib/mongodb/index.js +153 -6
  23. package/lib/mongodb/management/collection-ops.js +4 -4
  24. package/lib/mongodb/management/index-ops.js +18 -18
  25. package/lib/mongodb/management/validation-ops.js +3 -3
  26. package/lib/mongodb/queries/aggregate.js +14 -5
  27. package/lib/mongodb/queries/chain.js +52 -45
  28. package/lib/mongodb/queries/count.js +16 -6
  29. package/lib/mongodb/queries/distinct.js +15 -6
  30. package/lib/mongodb/queries/find-and-count.js +22 -13
  31. package/lib/mongodb/queries/find-by-ids.js +5 -5
  32. package/lib/mongodb/queries/find-one-by-id.js +1 -1
  33. package/lib/mongodb/queries/find-one.js +12 -3
  34. package/lib/mongodb/queries/find-page.js +12 -0
  35. package/lib/mongodb/queries/find.js +15 -6
  36. package/lib/mongodb/queries/watch.js +11 -2
  37. package/lib/mongodb/writes/common/batch-retry.js +64 -0
  38. package/lib/mongodb/writes/delete-batch.js +322 -0
  39. package/lib/mongodb/writes/delete-many.js +20 -11
  40. package/lib/mongodb/writes/delete-one.js +18 -9
  41. package/lib/mongodb/writes/find-one-and-delete.js +19 -10
  42. package/lib/mongodb/writes/find-one-and-replace.js +36 -20
  43. package/lib/mongodb/writes/find-one-and-update.js +36 -20
  44. package/lib/mongodb/writes/increment-one.js +22 -7
  45. package/lib/mongodb/writes/index.js +17 -13
  46. package/lib/mongodb/writes/insert-batch.js +46 -37
  47. package/lib/mongodb/writes/insert-many.js +22 -13
  48. package/lib/mongodb/writes/insert-one.js +18 -9
  49. package/lib/mongodb/writes/replace-one.js +33 -17
  50. package/lib/mongodb/writes/result-handler.js +14 -14
  51. package/lib/mongodb/writes/update-batch.js +358 -0
  52. package/lib/mongodb/writes/update-many.js +34 -18
  53. package/lib/mongodb/writes/update-one.js +33 -17
  54. package/lib/mongodb/writes/upsert-one.js +25 -9
  55. package/lib/operators.js +1 -1
  56. package/lib/redis-cache-adapter.js +3 -3
  57. package/lib/slow-query-log/base-storage.js +69 -0
  58. package/lib/slow-query-log/batch-queue.js +96 -0
  59. package/lib/slow-query-log/config-manager.js +195 -0
  60. package/lib/slow-query-log/index.js +237 -0
  61. package/lib/slow-query-log/mongodb-storage.js +323 -0
  62. package/lib/slow-query-log/query-hash.js +38 -0
  63. package/lib/transaction/DistributedCacheLockManager.js +240 -5
  64. package/lib/transaction/Transaction.js +1 -1
  65. package/lib/utils/objectid-converter.js +566 -0
  66. package/package.json +18 -6
@@ -0,0 +1,358 @@
1
+ /**
2
+ * updateBatch 操作实现
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, convertUpdateDocument } = require('../../utils/objectid-converter');
10
+ const { executeBatchWithRetry } = require('./common/batch-retry');
11
+
12
+ /**
13
+ * 创建 updateBatch 操作
14
+ * @param {Object} context - 模块上下文
15
+ * @returns {Object} 包含 updateBatch 方法的对象
16
+ */
17
+ function createUpdateBatchOps(context) {
18
+ const { db, cache, logger, defaults, collection, effectiveDbName: databaseName, instanceId } = context;
19
+
20
+ const collectionName = collection.collectionName;
21
+ const nativeCollection = collection;
22
+
23
+ /**
24
+ * 分批更新大量匹配的文档
25
+ * @param {Object} filter - 筛选条件(必需)
26
+ * @param {Object} update - 更新操作(必需,使用更新操作符如 $set)
27
+ * @param {Object} [options] - 操作选项
28
+ * @param {number} [options.batchSize=1000] - 每批更新的文档数量
29
+ * @param {boolean} [options.estimateProgress=true] - 是否预先 count 获取总数
30
+ * @param {Function} [options.onProgress] - 进度回调函数 (progress) => {}
31
+ * @param {string} [options.onError='stop'] - 错误处理策略: 'stop'/'skip'/'collect'/'retry'
32
+ * @param {number} [options.retryAttempts=3] - 失败批次最大重试次数
33
+ * @param {number} [options.retryDelay=1000] - 重试延迟时间(毫秒)
34
+ * @param {Function} [options.onRetry] - 重试回调函数 (retryInfo) => {}
35
+ * @param {Object} [options.writeConcern] - 写关注选项
36
+ * @param {string} [options.comment] - 操作注释(用于日志追踪)
37
+ * @returns {Promise<Object>} 更新结果
38
+ *
39
+ * @example
40
+ * const result = await collection('orders').updateBatch(
41
+ * { status: 'pending', createdAt: { $lt: expiredDate } },
42
+ * { $set: { status: 'expired' } },
43
+ * {
44
+ * batchSize: 500,
45
+ * estimateProgress: true,
46
+ * onProgress: (p) => console.log(`进度: ${p.percentage}%`)
47
+ * }
48
+ * );
49
+ */
50
+ const updateBatch = async function updateBatch(filter, update, options = {}) {
51
+ const startTime = Date.now();
52
+
53
+ // 1. 参数验证
54
+ if (!filter || typeof filter !== 'object' || Array.isArray(filter)) {
55
+ throw createError(
56
+ ErrorCodes.INVALID_ARGUMENT,
57
+ 'filter 必须是对象类型',
58
+ [{ field: 'filter', type: 'object.required', message: 'filter 是必需参数且必须是对象' }]
59
+ );
60
+ }
61
+
62
+ if (!update || typeof update !== 'object' || Array.isArray(update)) {
63
+ throw createError(
64
+ ErrorCodes.INVALID_ARGUMENT,
65
+ 'update 必须是对象类型',
66
+ [{ field: 'update', type: 'object.required', message: 'update 是必需参数且必须是对象' }]
67
+ );
68
+ }
69
+
70
+ // 验证 update 包含更新操作符
71
+ const updateKeys = Object.keys(update);
72
+ if (updateKeys.length > 0 && !updateKeys.some(key => key.startsWith('$'))) {
73
+ throw createError(
74
+ ErrorCodes.INVALID_ARGUMENT,
75
+ 'update 必须使用更新操作符(如 $set, $inc 等)',
76
+ [{ field: 'update', type: 'object.invalid', message: '请使用 $set, $inc, $push 等更新操作符' }]
77
+ );
78
+ }
79
+
80
+ // 自动转换 ObjectId 字符串
81
+ const convertedFilter = convertObjectIdStrings(filter, 'filter', 0, new WeakSet(), {
82
+ logger: context.logger,
83
+ excludeFields: context.autoConvertConfig?.excludeFields,
84
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
85
+ maxDepth: context.autoConvertConfig?.maxDepth
86
+ });
87
+
88
+ const convertedUpdate = convertUpdateDocument(update, {
89
+ logger: context.logger,
90
+ excludeFields: context.autoConvertConfig?.excludeFields,
91
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
92
+ maxDepth: context.autoConvertConfig?.maxDepth
93
+ });
94
+
95
+ // 解构选项
96
+ const {
97
+ batchSize = 1000,
98
+ estimateProgress = true,
99
+ onProgress,
100
+ onError = 'stop',
101
+ retryAttempts = 3,
102
+ retryDelay = 1000,
103
+ onRetry,
104
+ writeConcern,
105
+ comment
106
+ } = options;
107
+
108
+ // 2. 预先 count(可选)
109
+ const totalCount = estimateProgress
110
+ ? await nativeCollection.countDocuments(convertedFilter)
111
+ : null;
112
+
113
+ // 3. 初始化结果
114
+ const result = {
115
+ acknowledged: true,
116
+ totalCount,
117
+ matchedCount: 0,
118
+ modifiedCount: 0,
119
+ batchCount: 0,
120
+ errors: [],
121
+ retries: []
122
+ };
123
+
124
+ // 4. 🔴 关键:直接调用 context 的 find 流式方法
125
+ const stream = await context.find(convertedFilter, {
126
+ stream: true,
127
+ batchSize,
128
+ limit: 0, // 🔴 重要:禁用默认的 limit,否则只会查询默认的 10 条
129
+ comment: comment ? `${comment}:updateBatch` : 'updateBatch'
130
+ });
131
+
132
+ let batch = [];
133
+
134
+ return new Promise((resolve, reject) => {
135
+ // 5. 监听数据事件
136
+ stream.on('data', async (doc) => {
137
+ batch.push(doc._id);
138
+
139
+ // 达到批次大小,执行更新
140
+ if (batch.length >= batchSize) {
141
+ stream.pause();
142
+
143
+ try {
144
+ // 🔴 带重试的批量更新
145
+ const batchResult = await executeBatchWithRetry(
146
+ () => nativeCollection.updateMany(
147
+ { _id: { $in: batch } },
148
+ convertedUpdate,
149
+ { writeConcern }
150
+ ),
151
+ { onError, retryAttempts, retryDelay, onRetry, batchIndex: result.batchCount }
152
+ );
153
+
154
+ result.matchedCount += batchResult.result.matchedCount;
155
+ result.modifiedCount += batchResult.result.modifiedCount;
156
+ result.batchCount++;
157
+
158
+ // 记录重试信息
159
+ if (batchResult.attempts > 0) {
160
+ result.retries.push({
161
+ batchIndex: result.batchCount - 1,
162
+ attempts: batchResult.attempts,
163
+ success: true
164
+ });
165
+ }
166
+
167
+ batch = [];
168
+
169
+ // 🔴 进度回调
170
+ if (onProgress) {
171
+ onProgress({
172
+ currentBatch: result.batchCount,
173
+ totalBatches: totalCount ? Math.ceil(totalCount / batchSize) : null,
174
+ matched: result.matchedCount,
175
+ modified: result.modifiedCount,
176
+ total: totalCount,
177
+ percentage: totalCount ? Math.round((result.matchedCount / totalCount) * 100) : null,
178
+ errors: result.errors.length,
179
+ retries: result.retries.length
180
+ });
181
+ }
182
+
183
+ stream.resume();
184
+
185
+ } catch (error) {
186
+ // 🔴 错误处理
187
+ result.errors.push({
188
+ batchIndex: result.batchCount,
189
+ batchSize: batch.length,
190
+ error: error,
191
+ message: error.message
192
+ });
193
+
194
+ if (onError === 'stop') {
195
+ stream.destroy();
196
+ reject(createError(
197
+ ErrorCodes.WRITE_ERROR,
198
+ `updateBatch 操作失败: ${error.message}`,
199
+ null,
200
+ error
201
+ ));
202
+ return;
203
+ }
204
+
205
+ // skip 或 collect:清空批次,继续
206
+ batch = [];
207
+ result.batchCount++;
208
+ stream.resume();
209
+ }
210
+ }
211
+ });
212
+
213
+ // 6. 监听流结束事件
214
+ stream.on('end', async () => {
215
+ // 处理剩余的批次
216
+ if (batch.length > 0) {
217
+ try {
218
+ const batchResult = await executeBatchWithRetry(
219
+ () => nativeCollection.updateMany(
220
+ { _id: { $in: batch } },
221
+ convertedUpdate,
222
+ { writeConcern }
223
+ ),
224
+ { onError, retryAttempts, retryDelay, onRetry, batchIndex: result.batchCount }
225
+ );
226
+
227
+ result.matchedCount += batchResult.result.matchedCount;
228
+ result.modifiedCount += batchResult.result.modifiedCount;
229
+ result.batchCount++;
230
+
231
+ if (batchResult.attempts > 0) {
232
+ result.retries.push({
233
+ batchIndex: result.batchCount - 1,
234
+ attempts: batchResult.attempts,
235
+ success: true
236
+ });
237
+ }
238
+
239
+ // 最后一批的进度回调
240
+ if (onProgress) {
241
+ onProgress({
242
+ currentBatch: result.batchCount,
243
+ matched: result.matchedCount,
244
+ modified: result.modifiedCount,
245
+ total: totalCount,
246
+ percentage: totalCount ? 100 : null,
247
+ errors: result.errors.length,
248
+ retries: result.retries.length
249
+ });
250
+ }
251
+
252
+ } catch (error) {
253
+ result.errors.push({
254
+ batchIndex: result.batchCount,
255
+ batchSize: batch.length,
256
+ error: error,
257
+ message: error.message
258
+ });
259
+ }
260
+ }
261
+
262
+ // 7. 最终缓存失效
263
+ if (cache && result.modifiedCount > 0) {
264
+ try {
265
+ const ns = {
266
+ iid: instanceId,
267
+ type: 'mongodb',
268
+ db: databaseName,
269
+ collection: collectionName
270
+ };
271
+ const pattern = CacheFactory.buildNamespacePattern(ns);
272
+
273
+ // 检查是否在事务中
274
+ if (isInTransaction(options)) {
275
+ const tx = getTransactionFromSession(options.session);
276
+ if (tx && typeof tx.recordInvalidation === 'function') {
277
+ await tx.recordInvalidation(pattern, {
278
+ operation: 'write',
279
+ query: filter || {},
280
+ collection: collectionName
281
+ });
282
+ logger.debug(`[updateBatch] 事务中失效缓存: ${ns.db}.${ns.collection}`);
283
+ } else {
284
+ const deleted = await cache.delPattern(pattern);
285
+ if (deleted > 0) {
286
+ logger.debug(`[updateBatch] 自动失效缓存: ${ns.db}.${ns.collection}, 删除 ${deleted} 个缓存键`);
287
+ }
288
+ }
289
+ } else {
290
+ const deleted = await cache.delPattern(pattern);
291
+ if (deleted > 0) {
292
+ logger.debug(`[updateBatch] 自动失效缓存: ${ns.db}.${ns.collection}, 删除 ${deleted} 个缓存键`);
293
+ }
294
+ }
295
+ } catch (cacheErr) {
296
+ logger.warn(`[updateBatch] 缓存失效失败: ${cacheErr.message}`);
297
+ }
298
+ }
299
+
300
+ // 8. 慢查询日志
301
+ const duration = Date.now() - startTime;
302
+ const slowQueryMs = defaults.slowQueryMs || 1000;
303
+
304
+ if (duration > slowQueryMs) {
305
+ logger.warn(`[updateBatch] 慢操作警告`, {
306
+ ns: `${databaseName}.${collectionName}`,
307
+ duration,
308
+ threshold: slowQueryMs,
309
+ totalCount,
310
+ matchedCount: result.matchedCount,
311
+ modifiedCount: result.modifiedCount,
312
+ batchCount: result.batchCount,
313
+ errorCount: result.errors.length,
314
+ retryCount: result.retries.length,
315
+ comment
316
+ });
317
+ } else {
318
+ logger.debug(`[updateBatch] 操作完成`, {
319
+ ns: `${databaseName}.${collectionName}`,
320
+ duration,
321
+ matchedCount: result.matchedCount,
322
+ modifiedCount: result.modifiedCount,
323
+ batchCount: result.batchCount
324
+ });
325
+ }
326
+
327
+ resolve(result);
328
+ });
329
+
330
+ // 9. 监听流错误事件
331
+ stream.on('error', (error) => {
332
+ logger.error(`[updateBatch] 流式查询错误`, {
333
+ ns: `${databaseName}.${collectionName}`,
334
+ error: error.message,
335
+ code: error.code
336
+ });
337
+
338
+ result.errors.push({
339
+ batchIndex: result.batchCount,
340
+ error: error,
341
+ message: `流式查询错误: ${error.message}`
342
+ });
343
+
344
+ reject(createError(
345
+ ErrorCodes.WRITE_ERROR,
346
+ `updateBatch 流式查询失败: ${error.message}`,
347
+ null,
348
+ error
349
+ ));
350
+ });
351
+ });
352
+ };
353
+
354
+ return { updateBatch };
355
+ }
356
+
357
+ module.exports = { createUpdateBatchOps };
358
+
@@ -3,9 +3,10 @@
3
3
  * 更新所有匹配的文档
4
4
  */
5
5
 
6
- const { createError, ErrorCodes } = require("../../errors");
7
- const CacheFactory = require("../../cache");
8
- const { isInTransaction, getTransactionFromSession } = require("../common/transaction-aware");
6
+ const { createError, ErrorCodes } = require('../../errors');
7
+ const CacheFactory = require('../../cache');
8
+ const { isInTransaction, getTransactionFromSession } = require('../common/transaction-aware');
9
+ const { convertObjectIdStrings, convertUpdateDocument } = require('../../utils/objectid-converter');
9
10
 
10
11
  /**
11
12
  * 创建 updateMany 操作
@@ -60,39 +61,54 @@ function createUpdateManyOps(context) {
60
61
  const startTime = Date.now();
61
62
 
62
63
  // 1. 参数验证
63
- if (!filter || typeof filter !== "object" || Array.isArray(filter)) {
64
+ if (!filter || typeof filter !== 'object' || Array.isArray(filter)) {
64
65
  throw createError(
65
66
  ErrorCodes.INVALID_ARGUMENT,
66
- "filter 必须是对象类型",
67
- [{ field: "filter", type: "object.required", message: "filter 是必需参数且必须是对象" }]
67
+ 'filter 必须是对象类型',
68
+ [{ field: 'filter', type: 'object.required', message: 'filter 是必需参数且必须是对象' }]
68
69
  );
69
70
  }
70
71
 
71
- if (!update || typeof update !== "object" || Array.isArray(update)) {
72
+ if (!update || typeof update !== 'object' || Array.isArray(update)) {
72
73
  throw createError(
73
74
  ErrorCodes.INVALID_ARGUMENT,
74
- "update 必须是对象类型",
75
- [{ field: "update", type: "object.required", message: "update 是必需参数且必须是对象" }]
75
+ 'update 必须是对象类型',
76
+ [{ field: 'update', type: 'object.required', message: 'update 是必需参数且必须是对象' }]
76
77
  );
77
78
  }
78
79
 
79
80
  // 验证 update 包含更新操作符(防止整体替换)
80
81
  const updateKeys = Object.keys(update);
81
- if (updateKeys.length > 0 && !updateKeys.some(key => key.startsWith("$"))) {
82
+ if (updateKeys.length > 0 && !updateKeys.some(key => key.startsWith('$'))) {
82
83
  throw createError(
83
84
  ErrorCodes.INVALID_ARGUMENT,
84
- "update 必须使用更新操作符(如 $set, $inc 等)",
85
- [{ field: "update", type: "object.invalid", message: "请使用 $set, $inc, $push 等更新操作符" }]
85
+ 'update 必须使用更新操作符(如 $set, $inc 等)',
86
+ [{ field: 'update', type: 'object.invalid', message: '请使用 $set, $inc, $push 等更新操作符' }]
86
87
  );
87
88
  }
88
89
 
90
+ // ✅ v1.3.0: 自动转换 ObjectId 字符串
91
+ const convertedFilter = convertObjectIdStrings(filter, 'filter', 0, new WeakSet(), {
92
+ logger: context.logger,
93
+ excludeFields: context.autoConvertConfig?.excludeFields,
94
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
95
+ maxDepth: context.autoConvertConfig?.maxDepth
96
+ });
97
+
98
+ const convertedUpdate = convertUpdateDocument(update, {
99
+ logger: context.logger,
100
+ excludeFields: context.autoConvertConfig?.excludeFields,
101
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
102
+ maxDepth: context.autoConvertConfig?.maxDepth
103
+ });
104
+
89
105
  // 2. 构建操作上下文
90
- const operation = "updateMany";
106
+ const operation = 'updateMany';
91
107
  const ns = `${databaseName}.${collectionName}`;
92
108
 
93
109
  try {
94
- // 3. 执行更新操作
95
- const result = await nativeCollection.updateMany(filter, update, options);
110
+ // 3. 执行批量更新操作
111
+ const result = await nativeCollection.updateMany(convertedFilter, convertedUpdate, options);
96
112
 
97
113
  // 4. 自动失效缓存(只要有匹配,就失效缓存)
98
114
  if (cache && result.matchedCount > 0) {
@@ -100,7 +116,7 @@ function createUpdateManyOps(context) {
100
116
  // 使用标准命名空间模式删除该集合的所有缓存
101
117
  const ns = {
102
118
  iid: instanceId,
103
- type: "mongodb",
119
+ type: 'mongodb',
104
120
  db: databaseName,
105
121
  collection: collectionName
106
122
  };
@@ -182,8 +198,8 @@ function createUpdateManyOps(context) {
182
198
  // MongoDB 重复键错误(可能在 upsert 时发生)
183
199
  throw createError(
184
200
  ErrorCodes.DUPLICATE_KEY,
185
- "批量更新失败:违反唯一性约束",
186
- [{ field: "_id", message: error.message }],
201
+ '批量更新失败:违反唯一性约束',
202
+ [{ field: '_id', message: error.message }],
187
203
  error
188
204
  );
189
205
  }
@@ -3,9 +3,10 @@
3
3
  * 更新单个匹配的文档
4
4
  */
5
5
 
6
- const { createError, ErrorCodes } = require("../../errors");
7
- const CacheFactory = require("../../cache");
8
- const { isInTransaction, getTransactionFromSession } = require("../common/transaction-aware");
6
+ const { createError, ErrorCodes } = require('../../errors');
7
+ const CacheFactory = require('../../cache');
8
+ const { isInTransaction, getTransactionFromSession } = require('../common/transaction-aware');
9
+ const { convertObjectIdStrings, convertUpdateDocument } = require('../../utils/objectid-converter');
9
10
 
10
11
  /**
11
12
  * 创建 updateOne 操作
@@ -60,39 +61,54 @@ function createUpdateOneOps(context) {
60
61
  const startTime = Date.now();
61
62
 
62
63
  // 1. 参数验证
63
- if (!filter || typeof filter !== "object" || Array.isArray(filter)) {
64
+ if (!filter || typeof filter !== 'object' || Array.isArray(filter)) {
64
65
  throw createError(
65
66
  ErrorCodes.INVALID_ARGUMENT,
66
- "filter 必须是对象类型",
67
- [{ field: "filter", type: "object.required", message: "filter 是必需参数且必须是对象" }]
67
+ 'filter 必须是对象类型',
68
+ [{ field: 'filter', type: 'object.required', message: 'filter 是必需参数且必须是对象' }]
68
69
  );
69
70
  }
70
71
 
71
- if (!update || typeof update !== "object" || Array.isArray(update)) {
72
+ if (!update || typeof update !== 'object' || Array.isArray(update)) {
72
73
  throw createError(
73
74
  ErrorCodes.INVALID_ARGUMENT,
74
- "update 必须是对象类型",
75
- [{ field: "update", type: "object.required", message: "update 是必需参数且必须是对象" }]
75
+ 'update 必须是对象类型',
76
+ [{ field: 'update', type: 'object.required', message: 'update 是必需参数且必须是对象' }]
76
77
  );
77
78
  }
78
79
 
79
80
  // 验证 update 包含更新操作符(防止整体替换)
80
81
  const updateKeys = Object.keys(update);
81
- if (updateKeys.length > 0 && !updateKeys.some(key => key.startsWith("$"))) {
82
+ if (updateKeys.length > 0 && !updateKeys.some(key => key.startsWith('$'))) {
82
83
  throw createError(
83
84
  ErrorCodes.INVALID_ARGUMENT,
84
- "update 必须使用更新操作符(如 $set, $inc 等)",
85
- [{ field: "update", type: "object.invalid", message: "请使用 $set, $inc, $push 等更新操作符,或使用 replaceOne 进行整体替换" }]
85
+ 'update 必须使用更新操作符(如 $set, $inc 等)',
86
+ [{ field: 'update', type: 'object.invalid', message: '请使用 $set, $inc, $push 等更新操作符,或使用 replaceOne 进行整体替换' }]
86
87
  );
87
88
  }
88
89
 
90
+ // ✅ v1.3.0: 自动转换 ObjectId 字符串
91
+ const convertedFilter = convertObjectIdStrings(filter, 'filter', 0, new WeakSet(), {
92
+ logger: context.logger,
93
+ excludeFields: context.autoConvertConfig?.excludeFields,
94
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
95
+ maxDepth: context.autoConvertConfig?.maxDepth
96
+ });
97
+
98
+ const convertedUpdate = convertUpdateDocument(update, {
99
+ logger: context.logger,
100
+ excludeFields: context.autoConvertConfig?.excludeFields,
101
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
102
+ maxDepth: context.autoConvertConfig?.maxDepth
103
+ });
104
+
89
105
  // 2. 构建操作上下文
90
- const operation = "updateOne";
106
+ const operation = 'updateOne';
91
107
  const ns = `${databaseName}.${collectionName}`;
92
108
 
93
109
  try {
94
110
  // 3. 执行更新操作
95
- const result = await nativeCollection.updateOne(filter, update, options);
111
+ const result = await nativeCollection.updateOne(convertedFilter, convertedUpdate, options);
96
112
 
97
113
  // 4. 自动失效缓存
98
114
  if (cache && result.modifiedCount > 0) {
@@ -100,7 +116,7 @@ function createUpdateOneOps(context) {
100
116
  // 使用标准命名空间模式删除该集合的所有缓存
101
117
  const ns = {
102
118
  iid: instanceId,
103
- type: "mongodb",
119
+ type: 'mongodb',
104
120
  db: databaseName,
105
121
  collection: collectionName
106
122
  };
@@ -184,8 +200,8 @@ function createUpdateOneOps(context) {
184
200
  // MongoDB 重复键错误(可能在 upsert 时发生)
185
201
  throw createError(
186
202
  ErrorCodes.DUPLICATE_KEY,
187
- "更新失败:违反唯一性约束",
188
- [{ field: "_id", message: error.message }],
203
+ '更新失败:违反唯一性约束',
204
+ [{ field: '_id', message: error.message }],
189
205
  error
190
206
  );
191
207
  }
@@ -4,7 +4,8 @@
4
4
  */
5
5
 
6
6
  const { createError, ErrorCodes } = require('../../errors');
7
- const { isInTransaction, getTransactionFromSession } = require("../common/transaction-aware");
7
+ const { isInTransaction, getTransactionFromSession } = require('../common/transaction-aware');
8
+ const { convertObjectIdStrings } = require('../../utils/objectid-converter');
8
9
 
9
10
  /**
10
11
  * 创建 upsertOne 操作
@@ -68,11 +69,26 @@ function createUpsertOneOps(context) {
68
69
  );
69
70
  }
70
71
 
72
+ // ✅ v1.3.0: 自动转换 ObjectId 字符串
73
+ const convertedFilter = convertObjectIdStrings(filter, 'filter', 0, new WeakSet(), {
74
+ logger: context.logger,
75
+ excludeFields: context.autoConvertConfig?.excludeFields,
76
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
77
+ maxDepth: context.autoConvertConfig?.maxDepth
78
+ });
79
+
80
+ const convertedUpdate = convertObjectIdStrings(update, 'document', 0, new WeakSet(), {
81
+ logger: context.logger,
82
+ excludeFields: context.autoConvertConfig?.excludeFields,
83
+ customFieldPatterns: context.autoConvertConfig?.customFieldPatterns,
84
+ maxDepth: context.autoConvertConfig?.maxDepth
85
+ });
86
+
71
87
  // 2. 检查 update 是否包含更新操作符
72
- const hasOperator = Object.keys(update).some(key => key.startsWith('$'));
88
+ const hasOperator = Object.keys(convertedUpdate).some(key => key.startsWith('$'));
73
89
 
74
90
  // 如果没有操作符,自动包装为 $set
75
- const updateDoc = hasOperator ? update : { $set: update };
91
+ const updateDoc = hasOperator ? convertedUpdate : { $set: convertedUpdate };
76
92
 
77
93
  // 3. 构建选项
78
94
  const maxTimeMS = options.maxTimeMS !== undefined ? options.maxTimeMS : defaults.maxTimeMS;
@@ -84,7 +100,7 @@ function createUpsertOneOps(context) {
84
100
  // 4. 执行 updateOne 操作
85
101
  let result;
86
102
  try {
87
- result = await collection.updateOne(filter, updateDoc, driverOpts);
103
+ result = await collection.updateOne(convertedFilter, updateDoc, driverOpts);
88
104
  } catch (error) {
89
105
  // 统一错误处理
90
106
  if (error.code === 11000) {
@@ -146,7 +162,7 @@ function createUpsertOneOps(context) {
146
162
  operation: 'upsertOne',
147
163
  durationMs: duration,
148
164
  iid: instanceId,
149
- type: type,
165
+ type,
150
166
  db: effectiveDbName,
151
167
  collection: collection.collectionName,
152
168
  matchedCount: result.matchedCount,
@@ -154,7 +170,7 @@ function createUpsertOneOps(context) {
154
170
  upsertedId: result.upsertedId,
155
171
  filter: mongoSlowLogShaper?.sanitize ? mongoSlowLogShaper.sanitize(filter) : filter,
156
172
  update: mongoSlowLogShaper?.sanitize ? mongoSlowLogShaper.sanitize(updateDoc) : updateDoc,
157
- comment: comment
173
+ comment
158
174
  };
159
175
  logger?.warn?.('🐌 Slow query: upsertOne', meta);
160
176
  emit?.('slow-query', meta);
@@ -166,11 +182,11 @@ function createUpsertOneOps(context) {
166
182
  // 7. 日志记录
167
183
  logger?.debug?.('[upsertOne] 操作完成', {
168
184
  ns: `${effectiveDbName}.${collection.collectionName}`,
169
- duration: duration,
185
+ duration,
170
186
  matchedCount: result.matchedCount,
171
187
  modifiedCount: result.modifiedCount,
172
188
  upsertedId: result.upsertedId === null ? undefined : result.upsertedId,
173
- upsertedCount: upsertedCount
189
+ upsertedCount
174
190
  });
175
191
 
176
192
  // 8. 返回结果
@@ -179,7 +195,7 @@ function createUpsertOneOps(context) {
179
195
  matchedCount: result.matchedCount,
180
196
  modifiedCount: result.modifiedCount,
181
197
  upsertedId: result.upsertedId === null ? undefined : result.upsertedId,
182
- upsertedCount: upsertedCount
198
+ upsertedCount
183
199
  };
184
200
  };
185
201
 
package/lib/operators.js CHANGED
@@ -327,4 +327,4 @@ module.exports = {
327
327
  '$setReadConcern': { mysql: '' },
328
328
  '$setReadPref': { mysql: '' }
329
329
  }
330
- }
330
+ };
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Redis 缓存适配器:将 Redis 封装为 CacheLike 接口
3
3
  * 支持直接传入 Redis URL 字符串或 ioredis 实例
4
- *
4
+ *
5
5
  * @example
6
6
  * const { createRedisCacheAdapter } = require('monsqlize/lib/redis-cache-adapter');
7
- *
7
+ *
8
8
  * // 方式 1:传入 URL 字符串(推荐)
9
9
  * const cache = createRedisCacheAdapter('redis://localhost:6379/0');
10
- *
10
+ *
11
11
  * // 方式 2:传入 ioredis 实例
12
12
  * const Redis = require('ioredis');
13
13
  * const redis = new Redis('redis://localhost:6379/0');