@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.cjs CHANGED
@@ -1334,11 +1334,38 @@ async function loadIndexes(collection) {
1334
1334
  // src/createCollection/collectionsRegistry.ts
1335
1335
  var import_logger4 = require("@orion-js/logger");
1336
1336
  var collectionsRegistry = /* @__PURE__ */ new Map();
1337
+ function indexesAreEqual(indexA, indexB) {
1338
+ const nameA = getIndexName(indexA);
1339
+ const nameB = getIndexName(indexB);
1340
+ if (nameA && nameB) {
1341
+ return nameA === nameB;
1342
+ }
1343
+ return keysMatch(
1344
+ indexA.keys,
1345
+ indexB.keys
1346
+ );
1347
+ }
1348
+ function mergeIndexes(existingIndexes, newIndexes) {
1349
+ const merged = [...existingIndexes];
1350
+ for (const newIndex of newIndexes) {
1351
+ const isDuplicate = existingIndexes.some((existing) => indexesAreEqual(existing, newIndex));
1352
+ if (!isDuplicate) {
1353
+ merged.push(newIndex);
1354
+ }
1355
+ }
1356
+ return merged;
1357
+ }
1337
1358
  function registerCollection(connectionName, collection) {
1338
1359
  if (!collectionsRegistry.has(connectionName)) {
1339
1360
  collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
1340
1361
  }
1341
- collectionsRegistry.get(connectionName).set(collection.name, collection);
1362
+ const connectionMap = collectionsRegistry.get(connectionName);
1363
+ const existingCollection = connectionMap.get(collection.name);
1364
+ if (existingCollection) {
1365
+ existingCollection.indexes = mergeIndexes(existingCollection.indexes, collection.indexes);
1366
+ } else {
1367
+ connectionMap.set(collection.name, collection);
1368
+ }
1342
1369
  }
1343
1370
  function getRegisteredCollections(connectionName = "main") {
1344
1371
  const connectionCollections = collectionsRegistry.get(connectionName);
@@ -1353,6 +1380,10 @@ async function deleteAllUnusedIndexes(connectionName = "main") {
1353
1380
  import_logger4.logger.warn(`No collections registered for connection "${connectionName}"`);
1354
1381
  return [];
1355
1382
  }
1383
+ if (createIndexesPromises.length > 0) {
1384
+ import_logger4.logger.info("Waiting for pending index creation to complete before deleting unused indexes...");
1385
+ await Promise.all(createIndexesPromises);
1386
+ }
1356
1387
  import_logger4.logger.info(
1357
1388
  `Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
1358
1389
  );