@orion-js/mongodb 4.2.1 → 4.2.4

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
@@ -43,6 +43,7 @@ __export(index_exports, {
43
43
  createIndexesPromises: () => createIndexesPromises,
44
44
  deleteAllUnusedIndexes: () => deleteAllUnusedIndexes,
45
45
  getDBName: () => getDBName,
46
+ getMergedIndexes: () => getMergedIndexes,
46
47
  getMongoConnection: () => getMongoConnection,
47
48
  getOrCreateEncryptionKey: () => getOrCreateEncryptionKey,
48
49
  getRegisteredCollections: () => getRegisteredCollections,
@@ -1197,7 +1198,7 @@ var generateId_default = getIdGenerator;
1197
1198
 
1198
1199
  // src/createCollection/createIndexes.ts
1199
1200
  var import_mongodb2 = require("mongodb");
1200
- var import_logger3 = require("@orion-js/logger");
1201
+ var import_logger4 = require("@orion-js/logger");
1201
1202
 
1202
1203
  // src/createCollection/deleteUnusedIndexes.ts
1203
1204
  var import_logger2 = require("@orion-js/logger");
@@ -1219,6 +1220,15 @@ function getIndexName(indexDef) {
1219
1220
  }
1220
1221
 
1221
1222
  // src/createCollection/deleteUnusedIndexes.ts
1223
+ function isTextIndexDefinition(keys) {
1224
+ return Object.values(keys).some((value) => value === "text");
1225
+ }
1226
+ function isMongoDBTextIndex(key) {
1227
+ return "_fts" in key && "_ftsx" in key;
1228
+ }
1229
+ function generateIndexName(keys) {
1230
+ return Object.entries(keys).map(([field, value]) => `${field}_${value}`).join("_");
1231
+ }
1222
1232
  function keysMatch(definitionKeys, currentIndexKey) {
1223
1233
  const defEntries = Object.entries(definitionKeys);
1224
1234
  const curEntries = Object.entries(currentIndexKey);
@@ -1234,7 +1244,12 @@ function isIndexDefined(definedIndexes, currentIndex) {
1234
1244
  return definedIndexes.some((defIndex) => {
1235
1245
  const customName = getIndexName(defIndex);
1236
1246
  if (customName && customName === currentIndex.name) return true;
1237
- return keysMatch(defIndex.keys, currentIndex.key);
1247
+ const defKeys = defIndex.keys;
1248
+ if (isTextIndexDefinition(defKeys) && isMongoDBTextIndex(currentIndex.key)) {
1249
+ const expectedName = generateIndexName(defKeys);
1250
+ return currentIndex.name === expectedName;
1251
+ }
1252
+ return keysMatch(defKeys, currentIndex.key);
1238
1253
  });
1239
1254
  }
1240
1255
  async function deleteUnusedIndexes(collection) {
@@ -1272,6 +1287,85 @@ async function deleteUnusedIndexes(collection) {
1272
1287
  return result;
1273
1288
  }
1274
1289
 
1290
+ // src/createCollection/collectionsRegistry.ts
1291
+ var import_logger3 = require("@orion-js/logger");
1292
+ var collectionsRegistry = /* @__PURE__ */ new Map();
1293
+ function indexesAreEqual(indexA, indexB) {
1294
+ const nameA = getIndexName(indexA);
1295
+ const nameB = getIndexName(indexB);
1296
+ if (nameA && nameB) {
1297
+ return nameA === nameB;
1298
+ }
1299
+ return keysMatch(
1300
+ indexA.keys,
1301
+ indexB.keys
1302
+ );
1303
+ }
1304
+ function mergeIndexes(existingIndexes, newIndexes) {
1305
+ const merged = [...existingIndexes];
1306
+ for (const newIndex of newIndexes) {
1307
+ const isDuplicate = existingIndexes.some((existing) => indexesAreEqual(existing, newIndex));
1308
+ if (!isDuplicate) {
1309
+ merged.push(newIndex);
1310
+ }
1311
+ }
1312
+ return merged;
1313
+ }
1314
+ function registerCollection(connectionName, collection) {
1315
+ if (!collectionsRegistry.has(connectionName)) {
1316
+ collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
1317
+ }
1318
+ const connectionMap = collectionsRegistry.get(connectionName);
1319
+ const existingCollection = connectionMap.get(collection.name);
1320
+ if (existingCollection) {
1321
+ existingCollection.indexes = mergeIndexes(existingCollection.indexes, collection.indexes);
1322
+ } else {
1323
+ connectionMap.set(collection.name, collection);
1324
+ }
1325
+ }
1326
+ function getRegisteredCollections(connectionName = "main") {
1327
+ const connectionCollections = collectionsRegistry.get(connectionName);
1328
+ if (!connectionCollections) {
1329
+ return [];
1330
+ }
1331
+ return Array.from(connectionCollections.values());
1332
+ }
1333
+ function getMergedIndexes(connectionName, collectionName) {
1334
+ const connectionCollections = collectionsRegistry.get(connectionName);
1335
+ if (!connectionCollections) {
1336
+ return [];
1337
+ }
1338
+ const collection = connectionCollections.get(collectionName);
1339
+ if (!collection) {
1340
+ return [];
1341
+ }
1342
+ return collection.indexes || [];
1343
+ }
1344
+ async function deleteAllUnusedIndexes(connectionName = "main") {
1345
+ const collections = getRegisteredCollections(connectionName);
1346
+ if (collections.length === 0) {
1347
+ import_logger3.logger.warn(`No collections registered for connection "${connectionName}"`);
1348
+ return [];
1349
+ }
1350
+ if (createIndexesPromises.length > 0) {
1351
+ import_logger3.logger.info("Waiting for pending index creation to complete before deleting unused indexes...");
1352
+ await Promise.all(createIndexesPromises);
1353
+ }
1354
+ import_logger3.logger.info(
1355
+ `Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
1356
+ );
1357
+ const results = [];
1358
+ for (const collection of collections) {
1359
+ const result = await collection.deleteUnusedIndexes();
1360
+ if (result.deletedIndexes.length > 0) {
1361
+ results.push(result);
1362
+ }
1363
+ }
1364
+ const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
1365
+ import_logger3.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
1366
+ return results;
1367
+ }
1368
+
1275
1369
  // src/createCollection/createIndexes.ts
1276
1370
  async function checkIndexes(collection) {
1277
1371
  await collection.connectionPromise;
@@ -1282,14 +1376,15 @@ async function checkIndexes(collection) {
1282
1376
  if (error.codeName !== "NamespaceNotFound") throw error;
1283
1377
  return;
1284
1378
  }
1285
- if (!collection.indexes || collection.indexes.length === 0) {
1379
+ const mergedIndexes = getMergedIndexes(collection.connectionName, collection.name);
1380
+ if (mergedIndexes.length === 0) {
1286
1381
  return;
1287
1382
  }
1288
1383
  const unexpectedIndexes = currentIndexes.filter(
1289
- (index) => index.name !== "_id_" && !isIndexDefined(collection.indexes, index)
1384
+ (index) => index.name !== "_id_" && !isIndexDefined(mergedIndexes, index)
1290
1385
  );
1291
1386
  if (unexpectedIndexes.length > 0) {
1292
- import_logger3.logger.warn(
1387
+ import_logger4.logger.warn(
1293
1388
  `${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
1294
1389
  );
1295
1390
  }
@@ -1331,43 +1426,6 @@ async function loadIndexes(collection) {
1331
1426
  return results;
1332
1427
  }
1333
1428
 
1334
- // src/createCollection/collectionsRegistry.ts
1335
- var import_logger4 = require("@orion-js/logger");
1336
- var collectionsRegistry = /* @__PURE__ */ new Map();
1337
- function registerCollection(connectionName, collection) {
1338
- if (!collectionsRegistry.has(connectionName)) {
1339
- collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
1340
- }
1341
- collectionsRegistry.get(connectionName).set(collection.name, collection);
1342
- }
1343
- function getRegisteredCollections(connectionName = "main") {
1344
- const connectionCollections = collectionsRegistry.get(connectionName);
1345
- if (!connectionCollections) {
1346
- return [];
1347
- }
1348
- return Array.from(connectionCollections.values());
1349
- }
1350
- async function deleteAllUnusedIndexes(connectionName = "main") {
1351
- const collections = getRegisteredCollections(connectionName);
1352
- if (collections.length === 0) {
1353
- import_logger4.logger.warn(`No collections registered for connection "${connectionName}"`);
1354
- return [];
1355
- }
1356
- import_logger4.logger.info(
1357
- `Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
1358
- );
1359
- const results = [];
1360
- for (const collection of collections) {
1361
- const result = await collection.deleteUnusedIndexes();
1362
- if (result.deletedIndexes.length > 0) {
1363
- results.push(result);
1364
- }
1365
- }
1366
- const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
1367
- import_logger4.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
1368
- return results;
1369
- }
1370
-
1371
1429
  // src/createCollection/getSchemaAndModel.ts
1372
1430
  var import_helpers7 = require("@orion-js/helpers");
1373
1431
  Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
@@ -1649,6 +1707,7 @@ var ENCRYPTION_ALGORITHMS = {
1649
1707
  createIndexesPromises,
1650
1708
  deleteAllUnusedIndexes,
1651
1709
  getDBName,
1710
+ getMergedIndexes,
1652
1711
  getMongoConnection,
1653
1712
  getOrCreateEncryptionKey,
1654
1713
  getRegisteredCollections,