@orion-js/mongodb 4.2.1 → 4.2.3

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/dist/index.d.cts CHANGED
@@ -317,11 +317,14 @@ interface DeleteUnusedIndexesResult {
317
317
  /**
318
318
  * Registry that tracks all collections created via createCollection().
319
319
  * Maps connection name to a map of collection name to collection instance.
320
+ * When the same collection is registered multiple times, indexes are merged.
320
321
  */
321
322
  declare const collectionsRegistry: Map<string, Map<string, Collection<ModelClassBase>>>;
322
323
  /**
323
324
  * Registers a collection in the registry.
324
325
  * Called automatically when a collection is created via createCollection().
326
+ * If the same collection name is registered multiple times, indexes are merged
327
+ * to support multiple createCollection() calls for the same collection.
325
328
  * @param connectionName - The name of the MongoDB connection
326
329
  * @param collection - The collection instance to register
327
330
  */
@@ -337,6 +340,10 @@ declare function getRegisteredCollections(connectionName?: string): Array<Collec
337
340
  * Iterates over all collections registered via createCollection() and
338
341
  * removes indexes that exist in MongoDB but are not defined in the collection configuration.
339
342
  *
343
+ * **Important**: This function waits for all pending index creation to complete before
344
+ * deleting any indexes, to avoid race conditions where an index being created might
345
+ * be incorrectly identified as unused.
346
+ *
340
347
  * @param connectionName - The name of the MongoDB connection (defaults to 'main')
341
348
  * @returns Array of results for each collection that had indexes deleted
342
349
  *
package/dist/index.d.ts CHANGED
@@ -317,11 +317,14 @@ interface DeleteUnusedIndexesResult {
317
317
  /**
318
318
  * Registry that tracks all collections created via createCollection().
319
319
  * Maps connection name to a map of collection name to collection instance.
320
+ * When the same collection is registered multiple times, indexes are merged.
320
321
  */
321
322
  declare const collectionsRegistry: Map<string, Map<string, Collection<ModelClassBase>>>;
322
323
  /**
323
324
  * Registers a collection in the registry.
324
325
  * Called automatically when a collection is created via createCollection().
326
+ * If the same collection name is registered multiple times, indexes are merged
327
+ * to support multiple createCollection() calls for the same collection.
325
328
  * @param connectionName - The name of the MongoDB connection
326
329
  * @param collection - The collection instance to register
327
330
  */
@@ -337,6 +340,10 @@ declare function getRegisteredCollections(connectionName?: string): Array<Collec
337
340
  * Iterates over all collections registered via createCollection() and
338
341
  * removes indexes that exist in MongoDB but are not defined in the collection configuration.
339
342
  *
343
+ * **Important**: This function waits for all pending index creation to complete before
344
+ * deleting any indexes, to avoid race conditions where an index being created might
345
+ * be incorrectly identified as unused.
346
+ *
340
347
  * @param connectionName - The name of the MongoDB connection (defaults to 'main')
341
348
  * @returns Array of results for each collection that had indexes deleted
342
349
  *
package/dist/index.js CHANGED
@@ -1283,11 +1283,38 @@ async function loadIndexes(collection) {
1283
1283
  // src/createCollection/collectionsRegistry.ts
1284
1284
  import { logger as logger4 } from "@orion-js/logger";
1285
1285
  var collectionsRegistry = /* @__PURE__ */ new Map();
1286
+ function indexesAreEqual(indexA, indexB) {
1287
+ const nameA = getIndexName(indexA);
1288
+ const nameB = getIndexName(indexB);
1289
+ if (nameA && nameB) {
1290
+ return nameA === nameB;
1291
+ }
1292
+ return keysMatch(
1293
+ indexA.keys,
1294
+ indexB.keys
1295
+ );
1296
+ }
1297
+ function mergeIndexes(existingIndexes, newIndexes) {
1298
+ const merged = [...existingIndexes];
1299
+ for (const newIndex of newIndexes) {
1300
+ const isDuplicate = existingIndexes.some((existing) => indexesAreEqual(existing, newIndex));
1301
+ if (!isDuplicate) {
1302
+ merged.push(newIndex);
1303
+ }
1304
+ }
1305
+ return merged;
1306
+ }
1286
1307
  function registerCollection(connectionName, collection) {
1287
1308
  if (!collectionsRegistry.has(connectionName)) {
1288
1309
  collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
1289
1310
  }
1290
- collectionsRegistry.get(connectionName).set(collection.name, collection);
1311
+ const connectionMap = collectionsRegistry.get(connectionName);
1312
+ const existingCollection = connectionMap.get(collection.name);
1313
+ if (existingCollection) {
1314
+ existingCollection.indexes = mergeIndexes(existingCollection.indexes, collection.indexes);
1315
+ } else {
1316
+ connectionMap.set(collection.name, collection);
1317
+ }
1291
1318
  }
1292
1319
  function getRegisteredCollections(connectionName = "main") {
1293
1320
  const connectionCollections = collectionsRegistry.get(connectionName);
@@ -1302,6 +1329,10 @@ async function deleteAllUnusedIndexes(connectionName = "main") {
1302
1329
  logger4.warn(`No collections registered for connection "${connectionName}"`);
1303
1330
  return [];
1304
1331
  }
1332
+ if (createIndexesPromises.length > 0) {
1333
+ logger4.info("Waiting for pending index creation to complete before deleting unused indexes...");
1334
+ await Promise.all(createIndexesPromises);
1335
+ }
1305
1336
  logger4.info(
1306
1337
  `Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
1307
1338
  );