monsqlize 1.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.
- package/CHANGELOG.md +2474 -0
- package/LICENSE +21 -0
- package/README.md +1368 -0
- package/index.d.ts +1052 -0
- package/lib/cache.js +491 -0
- package/lib/common/cursor.js +58 -0
- package/lib/common/docs-urls.js +72 -0
- package/lib/common/index-options.js +222 -0
- package/lib/common/log.js +60 -0
- package/lib/common/namespace.js +21 -0
- package/lib/common/normalize.js +33 -0
- package/lib/common/page-result.js +42 -0
- package/lib/common/runner.js +56 -0
- package/lib/common/server-features.js +231 -0
- package/lib/common/shape-builders.js +26 -0
- package/lib/common/validation.js +49 -0
- package/lib/connect.js +76 -0
- package/lib/constants.js +54 -0
- package/lib/count-queue.js +187 -0
- package/lib/distributed-cache-invalidator.js +259 -0
- package/lib/errors.js +157 -0
- package/lib/index.js +352 -0
- package/lib/logger.js +224 -0
- package/lib/model/examples/test.js +114 -0
- package/lib/mongodb/common/accessor-helpers.js +44 -0
- package/lib/mongodb/common/agg-pipeline.js +32 -0
- package/lib/mongodb/common/iid.js +27 -0
- package/lib/mongodb/common/lexicographic-expr.js +52 -0
- package/lib/mongodb/common/shape.js +31 -0
- package/lib/mongodb/common/sort.js +38 -0
- package/lib/mongodb/common/transaction-aware.js +24 -0
- package/lib/mongodb/connect.js +178 -0
- package/lib/mongodb/index.js +458 -0
- package/lib/mongodb/management/admin-ops.js +199 -0
- package/lib/mongodb/management/bookmark-ops.js +166 -0
- package/lib/mongodb/management/cache-ops.js +49 -0
- package/lib/mongodb/management/collection-ops.js +386 -0
- package/lib/mongodb/management/database-ops.js +201 -0
- package/lib/mongodb/management/index-ops.js +474 -0
- package/lib/mongodb/management/index.js +16 -0
- package/lib/mongodb/management/namespace.js +30 -0
- package/lib/mongodb/management/validation-ops.js +267 -0
- package/lib/mongodb/queries/aggregate.js +133 -0
- package/lib/mongodb/queries/chain.js +623 -0
- package/lib/mongodb/queries/count.js +88 -0
- package/lib/mongodb/queries/distinct.js +68 -0
- package/lib/mongodb/queries/find-and-count.js +183 -0
- package/lib/mongodb/queries/find-by-ids.js +235 -0
- package/lib/mongodb/queries/find-one-by-id.js +170 -0
- package/lib/mongodb/queries/find-one.js +61 -0
- package/lib/mongodb/queries/find-page.js +565 -0
- package/lib/mongodb/queries/find.js +161 -0
- package/lib/mongodb/queries/index.js +49 -0
- package/lib/mongodb/writes/delete-many.js +181 -0
- package/lib/mongodb/writes/delete-one.js +173 -0
- package/lib/mongodb/writes/find-one-and-delete.js +193 -0
- package/lib/mongodb/writes/find-one-and-replace.js +222 -0
- package/lib/mongodb/writes/find-one-and-update.js +223 -0
- package/lib/mongodb/writes/increment-one.js +243 -0
- package/lib/mongodb/writes/index.js +41 -0
- package/lib/mongodb/writes/insert-batch.js +498 -0
- package/lib/mongodb/writes/insert-many.js +218 -0
- package/lib/mongodb/writes/insert-one.js +171 -0
- package/lib/mongodb/writes/replace-one.js +199 -0
- package/lib/mongodb/writes/result-handler.js +236 -0
- package/lib/mongodb/writes/update-many.js +205 -0
- package/lib/mongodb/writes/update-one.js +207 -0
- package/lib/mongodb/writes/upsert-one.js +190 -0
- package/lib/multi-level-cache.js +189 -0
- package/lib/operators.js +330 -0
- package/lib/redis-cache-adapter.js +237 -0
- package/lib/transaction/CacheLockManager.js +161 -0
- package/lib/transaction/DistributedCacheLockManager.js +239 -0
- package/lib/transaction/Transaction.js +314 -0
- package/lib/transaction/TransactionManager.js +266 -0
- package/lib/transaction/index.js +10 -0
- package/package.json +111 -0
|
@@ -0,0 +1,474 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
};
|