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,475 +0,0 @@
1
- /**
2
- * MongoDB 索引管理操作
3
- * 提供统一的索引创建、删除、列出等功能
4
- *
5
- * @module mongodb/management/index-ops
6
- * @since 2025-11-17
7
- */
8
-
9
- const { createError } = require('../../errors');
10
- const { validateIndexKeys, normalizeIndexOptions } = require('../../common/index-options');
11
-
12
- /**
13
- * 创建索引操作工厂函数
14
- *
15
- * @param {Object} context - 上下文对象
16
- * @param {Object} context.logger - 日志实例
17
- * @param {Object} context.cache - 缓存实例
18
- * @param {Object} context.defaults - 默认配置
19
- * @param {string} databaseName - 数据库名称
20
- * @param {string} collectionName - 集合名称
21
- * @param {Object} nativeCollection - MongoDB 原生集合对象
22
- * @returns {Object} 索引操作方法集合
23
- */
24
- function createIndexOps(context, databaseName, collectionName, nativeCollection) {
25
- const { logger, defaults } = context;
26
- const operation = 'indexOps';
27
-
28
- /**
29
- * 创建单个索引
30
- *
31
- * @param {Object} keys - 索引键定义,如 { email: 1, age: -1 }
32
- * @param {Object} options - 索引选项
33
- * @param {string} [options.name] - 索引名称
34
- * @param {boolean} [options.unique] - 是否唯一索引
35
- * @param {boolean} [options.sparse] - 是否稀疏索引
36
- * @param {number} [options.expireAfterSeconds] - TTL 索引过期时间(秒)
37
- * @param {Object} [options.partialFilterExpression] - 部分索引过滤表达式
38
- * @param {Object} [options.collation] - 排序规则
39
- * @param {boolean} [options.hidden] - 是否隐藏索引(MongoDB 4.4+)
40
- * @param {boolean} [options.background] - 是否后台创建(已废弃但保留兼容)
41
- * @param {Object} [options.wildcardProjection] - 通配符投影(用于通配符索引)
42
- * @param {Object} [options.weights] - 文本索引权重
43
- * @param {string} [options.default_language] - 文本索引默认语言
44
- * @param {string} [options.language_override] - 文本索引语言覆盖字段
45
- * @returns {Promise<Object>} 索引创建结果 { name: string }
46
- *
47
- * @example
48
- * // 基本索引
49
- * await collection("users").createIndex({ email: 1 });
50
- *
51
- * @example
52
- * // 唯一索引
53
- * await collection("users").createIndex({ email: 1 }, { unique: true, name: "email_unique" });
54
- *
55
- * @example
56
- * // 复合索引
57
- * await collection("orders").createIndex({ userId: 1, status: 1 });
58
- *
59
- * @example
60
- * // TTL 索引(自动过期)
61
- * await collection("sessions").createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 });
62
- *
63
- * @example
64
- * // 文本索引
65
- * await collection("articles").createIndex({ title: "text", content: "text" });
66
- */
67
- async function createIndex(keys, options = {}) {
68
- const startTime = Date.now();
69
- const ns = `${databaseName}.${collectionName}`;
70
-
71
- try {
72
- // 1. 参数验证
73
- if (!keys || typeof keys !== 'object' || Object.keys(keys).length === 0) {
74
- throw createError(
75
- 'INVALID_ARGUMENT',
76
- 'createIndex: keys 参数必须是非空对象',
77
- { keys, ns }
78
- );
79
- }
80
-
81
- // 验证索引键
82
- validateIndexKeys(keys);
83
-
84
- // 2. 归一化选项
85
- const normalizedOptions = normalizeIndexOptions(options);
86
-
87
- // 3. 记录开始日志
88
- logger.debug('[createIndex] 开始创建索引', {
89
- ns,
90
- keys,
91
- options: normalizedOptions
92
- });
93
-
94
- // 4. 执行创建
95
- const result = await nativeCollection.createIndex(keys, normalizedOptions);
96
-
97
- // 5. 记录成功日志
98
- const duration = Date.now() - startTime;
99
- logger.info('[createIndex] 索引创建成功', {
100
- ns,
101
- indexName: typeof result === 'string' ? result : result.name || 'unknown',
102
- keys,
103
- options: normalizedOptions,
104
- duration
105
- });
106
-
107
- // 6. 返回结果(统一格式)
108
- return typeof result === 'string' ? { name: result } : result;
109
-
110
- } catch (error) {
111
- // 7. 错误处理
112
- const duration = Date.now() - startTime;
113
-
114
- logger.error('[createIndex] 索引创建失败', {
115
- ns,
116
- keys,
117
- options,
118
- error: error.message,
119
- code: error.code,
120
- duration
121
- });
122
-
123
- // 识别特定错误类型(灵活处理错误码和消息)
124
- const errorMsg = error.message || '';
125
-
126
- // 索引已存在或名称冲突 (code 85, 86 或消息包含相关关键词)
127
- if (error.code === 85 || error.code === 86 ||
128
- errorMsg.includes('already exists') ||
129
- errorMsg.includes('same name') ||
130
- errorMsg.includes('index name')) {
131
- throw createError(
132
- 'MONGODB_ERROR',
133
- `索引已存在或名称冲突: ${error.message}`,
134
- { ns, keys, options, cause: error }
135
- );
136
- } else if (error.code === 67 || errorMsg.includes('CannotCreateIndex')) {
137
- // 不支持的索引类型
138
- throw createError(
139
- 'MONGODB_ERROR',
140
- `不支持的索引类型: ${error.message}`,
141
- { ns, keys, cause: error }
142
- );
143
- }
144
-
145
- // 抛出通用错误
146
- throw createError(
147
- 'MONGODB_ERROR',
148
- `创建索引失败: ${error.message}`,
149
- { ns, keys, options, cause: error }
150
- );
151
- }
152
- }
153
-
154
- /**
155
- * 批量创建多个索引
156
- *
157
- * @param {Array<Object>} indexSpecs - 索引规范数组
158
- * @param {Object} indexSpecs[].key - 索引键定义
159
- * @param {string} [indexSpecs[].name] - 索引名称
160
- * @param {boolean} [indexSpecs[].unique] - 是否唯一索引
161
- * @param {boolean} [indexSpecs[].sparse] - 是否稀疏索引
162
- * @param {number} [indexSpecs[].expireAfterSeconds] - TTL 索引过期时间
163
- * @param {Object} [indexSpecs[].partialFilterExpression] - 部分索引过滤表达式
164
- * @param {Object} [indexSpecs[].collation] - 排序规则
165
- * @param {boolean} [indexSpecs[].hidden] - 是否隐藏索引
166
- * @returns {Promise<Array<string>>} 创建的索引名称数组
167
- *
168
- * @example
169
- * // 批量创建索引
170
- * await collection("users").createIndexes([
171
- * { key: { email: 1 }, unique: true },
172
- * { key: { age: 1 } },
173
- * { key: { createdAt: -1 } }
174
- * ]);
175
- */
176
- async function createIndexes(indexSpecs) {
177
- const startTime = Date.now();
178
- const ns = `${databaseName}.${collectionName}`;
179
-
180
- try {
181
- // 1. 参数验证
182
- if (!Array.isArray(indexSpecs) || indexSpecs.length === 0) {
183
- throw createError(
184
- 'INVALID_ARGUMENT',
185
- 'createIndexes: indexSpecs 参数必须是非空数组',
186
- { indexSpecs, ns }
187
- );
188
- }
189
-
190
- // 验证每个索引规范
191
- for (let i = 0; i < indexSpecs.length; i++) {
192
- const spec = indexSpecs[i];
193
- if (!spec.key || typeof spec.key !== 'object') {
194
- throw createError(
195
- 'INVALID_ARGUMENT',
196
- `createIndexes: indexSpecs[${i}] 缺少有效的 key 属性`,
197
- { spec, ns }
198
- );
199
- }
200
- validateIndexKeys(spec.key);
201
- }
202
-
203
- // 2. 归一化选项
204
- const normalizedSpecs = indexSpecs.map(spec => ({
205
- key: spec.key,
206
- ...normalizeIndexOptions(spec)
207
- }));
208
-
209
- // 3. 记录开始日志
210
- logger.debug('[createIndexes] 开始批量创建索引', {
211
- ns,
212
- count: normalizedSpecs.length,
213
- specs: normalizedSpecs
214
- });
215
-
216
- // 4. 执行批量创建
217
- const result = await nativeCollection.createIndexes(normalizedSpecs);
218
-
219
- // 5. 记录成功日志
220
- const duration = Date.now() - startTime;
221
- logger.info('[createIndexes] 批量创建索引成功', {
222
- ns,
223
- count: normalizedSpecs.length,
224
- indexNames: result,
225
- duration
226
- });
227
-
228
- // 6. 返回结果
229
- return result;
230
-
231
- } catch (error) {
232
- // 7. 错误处理
233
- const duration = Date.now() - startTime;
234
-
235
- logger.error('[createIndexes] 批量创建索引失败', {
236
- ns,
237
- count: indexSpecs?.length || 0,
238
- error: error.message,
239
- code: error.code,
240
- duration
241
- });
242
-
243
- // 抛出错误
244
- throw createError(
245
- 'MONGODB_ERROR',
246
- `批量创建索引失败: ${error.message}`,
247
- { ns, indexSpecs, cause: error }
248
- );
249
- }
250
- }
251
-
252
- /**
253
- * 列出集合的所有索引
254
- *
255
- * @returns {Promise<Array<Object>>} 索引列表
256
- *
257
- * @example
258
- * const indexes = await collection("users").listIndexes();
259
- * console.log(indexes);
260
- * // [
261
- * // { v: 2, key: { _id: 1 }, name: "_id_" },
262
- * // { v: 2, key: { email: 1 }, name: "email_1", unique: true },
263
- * // { v: 2, key: { age: 1 }, name: "age_1" }
264
- * // ]
265
- */
266
- async function listIndexes() {
267
- const startTime = Date.now();
268
- const ns = `${databaseName}.${collectionName}`;
269
-
270
- try {
271
- // 1. 记录开始日志
272
- logger.debug('[listIndexes] 开始列出索引', { ns });
273
-
274
- // 2. 执行查询
275
- const cursor = nativeCollection.listIndexes();
276
- const indexes = await cursor.toArray();
277
-
278
- // 3. 记录成功日志
279
- const duration = Date.now() - startTime;
280
- logger.debug('[listIndexes] 列出索引成功', {
281
- ns,
282
- count: indexes.length,
283
- indexNames: indexes.map(idx => idx.name),
284
- duration
285
- });
286
-
287
- // 4. 返回结果
288
- return indexes;
289
-
290
- } catch (error) {
291
- // 5. 错误处理
292
- const duration = Date.now() - startTime;
293
-
294
- logger.error('[listIndexes] 列出索引失败', {
295
- ns,
296
- error: error.message,
297
- code: error.code,
298
- duration
299
- });
300
-
301
- // MongoDB 命名空间不存在错误(集合不存在)
302
- if (error.code === 26) {
303
- // 返回空数组(集合不存在时没有索引)
304
- logger.debug('[listIndexes] 集合不存在,返回空数组', { ns });
305
- return [];
306
- }
307
-
308
- // 抛出其他错误
309
- throw createError(
310
- 'MONGODB_ERROR',
311
- `列出索引失败: ${error.message}`,
312
- { ns, cause: error }
313
- );
314
- }
315
- }
316
-
317
- /**
318
- * 删除指定索引
319
- *
320
- * @param {string} indexName - 索引名称
321
- * @returns {Promise<Object>} 删除结果 { ok: 1, nIndexesWas: number }
322
- *
323
- * @example
324
- * await collection("users").dropIndex("email_1");
325
- */
326
- async function dropIndex(indexName) {
327
- const startTime = Date.now();
328
- const ns = `${databaseName}.${collectionName}`;
329
-
330
- try {
331
- // 1. 参数验证
332
- if (!indexName || typeof indexName !== 'string') {
333
- throw createError(
334
- 'INVALID_ARGUMENT',
335
- 'dropIndex: indexName 参数必须是非空字符串',
336
- { indexName, ns }
337
- );
338
- }
339
-
340
- // 不允许删除 _id 索引
341
- if (indexName === '_id_') {
342
- throw createError(
343
- 'INVALID_ARGUMENT',
344
- 'dropIndex: 不允许删除 _id 索引',
345
- { indexName, ns }
346
- );
347
- }
348
-
349
- // 2. 记录开始日志
350
- logger.debug('[dropIndex] 开始删除索引', { ns, indexName });
351
-
352
- // 3. 执行删除
353
- const result = await nativeCollection.dropIndex(indexName);
354
-
355
- // 4. 记录成功日志
356
- const duration = Date.now() - startTime;
357
- logger.info('[dropIndex] 删除索引成功', {
358
- ns,
359
- indexName,
360
- result,
361
- duration
362
- });
363
-
364
- // 5. 返回结果
365
- return result;
366
-
367
- } catch (error) {
368
- // 6. 错误处理
369
- const duration = Date.now() - startTime;
370
-
371
- logger.error('[dropIndex] 删除索引失败', {
372
- ns,
373
- indexName,
374
- error: error.message,
375
- code: error.code,
376
- duration
377
- });
378
-
379
- // 检查是否是尝试删除 _id 索引导致的错误
380
- const errorMsg = error.message || '';
381
- if (errorMsg.includes('_id_') || errorMsg.includes('cannot drop _id index')) {
382
- throw createError(
383
- 'INVALID_ARGUMENT',
384
- '不允许删除 _id 索引',
385
- { ns, indexName, cause: error }
386
- );
387
- }
388
-
389
- // 索引不存在
390
- if (error.code === 27 || errorMsg.includes('index not found') || errorMsg.includes('IndexNotFound')) {
391
- throw createError(
392
- 'MONGODB_ERROR',
393
- `索引不存在: ${indexName}`,
394
- { ns, indexName, cause: error }
395
- );
396
- }
397
-
398
- // 抛出其他错误
399
- throw createError(
400
- 'MONGODB_ERROR',
401
- `删除索引失败: ${error.message}`,
402
- { ns, indexName, cause: error }
403
- );
404
- }
405
- }
406
-
407
- /**
408
- * 删除集合的所有索引(_id 索引除外)
409
- *
410
- * @returns {Promise<Object>} 删除结果 { ok: 1, nIndexesWas: number }
411
- *
412
- * @example
413
- * await collection("users").dropIndexes();
414
- */
415
- async function dropIndexes() {
416
- const startTime = Date.now();
417
- const ns = `${databaseName}.${collectionName}`;
418
-
419
- try {
420
- // 1. 记录开始日志
421
- logger.debug('[dropIndexes] 开始删除所有索引', { ns });
422
-
423
- // 2. 执行删除(MongoDB 会自动保留 _id 索引)
424
- const result = await nativeCollection.dropIndexes();
425
-
426
- // 3. 记录成功日志
427
- const duration = Date.now() - startTime;
428
- logger.info('[dropIndexes] 删除所有索引成功', {
429
- ns,
430
- result,
431
- duration
432
- });
433
-
434
- // 4. 返回结果
435
- return result;
436
-
437
- } catch (error) {
438
- // 5. 错误处理
439
- const duration = Date.now() - startTime;
440
-
441
- logger.error('[dropIndexes] 删除所有索引失败', {
442
- ns,
443
- error: error.message,
444
- code: error.code,
445
- duration
446
- });
447
-
448
- // 命名空间不存在(集合不存在)
449
- if (error.code === 26) {
450
- logger.debug('[dropIndexes] 集合不存在,无需删除索引', { ns });
451
- return { ok: 1, msg: '集合不存在,无索引可删除', nIndexesWas: 0 };
452
- }
453
-
454
- // 抛出其他错误
455
- throw createError(
456
- 'MONGODB_ERROR',
457
- `删除所有索引失败: ${error.message}`,
458
- { ns, cause: error }
459
- );
460
- }
461
- }
462
-
463
- // 返回索引操作方法
464
- return {
465
- createIndex,
466
- createIndexes,
467
- listIndexes,
468
- dropIndex,
469
- dropIndexes
470
- };
471
- }
472
-
473
- module.exports = createIndexOps;
474
-
475
-
@@ -1,17 +0,0 @@
1
- /**
2
- * 管理模块统一导出
3
- * @module management
4
- */
5
-
6
- module.exports = {
7
- createNamespaceOps: require('./namespace'),
8
- createCollectionOps: require('./collection-ops'),
9
- createCacheOps: require('./cache-ops'),
10
- createBookmarkOps: require('./bookmark-ops'),
11
- createIndexOps: require('./index-ops'),
12
- // 新增管理方法 (v0.3.0+)
13
- createAdminOps: require('./admin-ops').createAdminOps,
14
- createDatabaseOps: require('./database-ops').createDatabaseOps,
15
- createValidationOps: require('./validation-ops').createValidationOps
16
- };
17
-
@@ -1,31 +0,0 @@
1
- /**
2
- * 命名空间管理模块
3
- * @module management/namespace
4
- */
5
-
6
- /**
7
- * 创建命名空间操作方法
8
- * @param {Object} context - 上下文对象
9
- * @param {string} context.instanceId - 实例ID
10
- * @param {string} context.type - 数据库类型
11
- * @param {string} context.effectiveDbName - 数据库名称
12
- * @param {Object} context.collection - MongoDB集合实例
13
- * @returns {Object} 命名空间操作方法
14
- */
15
- module.exports = function createNamespaceOps(context) {
16
- const { instanceId, type, effectiveDbName, collection } = context;
17
-
18
- return {
19
- /**
20
- * 返回当前访问器的命名空间信息
21
- * @returns {Object} 命名空间信息
22
- */
23
- getNamespace: () => ({
24
- iid: instanceId,
25
- type,
26
- db: effectiveDbName,
27
- collection: collection.collectionName
28
- })
29
- };
30
- };
31
-