@orion-js/mongodb 4.2.3 → 4.2.5

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,
@@ -127,9 +128,6 @@ var OrionMongoDatabaseWrapper = class {
127
128
  configTimeout;
128
129
  constructor(connectionName) {
129
130
  this.connectionName = connectionName;
130
- import_logger.logger.info("New connection requested", {
131
- connectionName
132
- });
133
131
  this.connectionEvent.setMaxListeners(Number.POSITIVE_INFINITY);
134
132
  this.connectionPromise = new Promise((resolve, reject) => {
135
133
  if (this.state === "connected") {
@@ -175,11 +173,8 @@ var OrionMongoDatabaseWrapper = class {
175
173
  return this;
176
174
  }
177
175
  this.state = "connecting";
178
- const censoredURI = this.uri.replace(/\/\/.*:.*@/, "//");
179
- import_logger.logger.info("Connecting to mongo", {
180
- uri: censoredURI,
181
- connectionName: this.connectionName
182
- });
176
+ const censoredURI = this.uri.replace(/\/\/.*:.*@/, "//").replace(/^mongodb(\+srv)?:\/\//, "");
177
+ import_logger.logger.info(`Starting MongoDB connection "${this.connectionName}" [${censoredURI}]`);
183
178
  if (this.encrypted.client) {
184
179
  await this.connectWithRetry(this.encrypted.client);
185
180
  import_logger.logger.info("Successfully connected to encrypted mongo", {
@@ -190,10 +185,6 @@ var OrionMongoDatabaseWrapper = class {
190
185
  await this.connectWithRetry(this.client);
191
186
  this.state = "connected";
192
187
  this.connectionEvent.emit("connected", this.client);
193
- import_logger.logger.info("Successfully connected to mongo", {
194
- uri: censoredURI,
195
- connectionName: this.connectionName
196
- });
197
188
  (0, import_node_process.nextTick)(() => {
198
189
  this.connectionEvent.removeAllListeners();
199
190
  this.connectionEvent = null;
@@ -1197,7 +1188,7 @@ var generateId_default = getIdGenerator;
1197
1188
 
1198
1189
  // src/createCollection/createIndexes.ts
1199
1190
  var import_mongodb2 = require("mongodb");
1200
- var import_logger3 = require("@orion-js/logger");
1191
+ var import_logger4 = require("@orion-js/logger");
1201
1192
 
1202
1193
  // src/createCollection/deleteUnusedIndexes.ts
1203
1194
  var import_logger2 = require("@orion-js/logger");
@@ -1219,6 +1210,15 @@ function getIndexName(indexDef) {
1219
1210
  }
1220
1211
 
1221
1212
  // src/createCollection/deleteUnusedIndexes.ts
1213
+ function isTextIndexDefinition(keys) {
1214
+ return Object.values(keys).some((value) => value === "text");
1215
+ }
1216
+ function isMongoDBTextIndex(key) {
1217
+ return "_fts" in key && "_ftsx" in key;
1218
+ }
1219
+ function generateIndexName(keys) {
1220
+ return Object.entries(keys).map(([field, value]) => `${field}_${value}`).join("_");
1221
+ }
1222
1222
  function keysMatch(definitionKeys, currentIndexKey) {
1223
1223
  const defEntries = Object.entries(definitionKeys);
1224
1224
  const curEntries = Object.entries(currentIndexKey);
@@ -1234,7 +1234,12 @@ function isIndexDefined(definedIndexes, currentIndex) {
1234
1234
  return definedIndexes.some((defIndex) => {
1235
1235
  const customName = getIndexName(defIndex);
1236
1236
  if (customName && customName === currentIndex.name) return true;
1237
- return keysMatch(defIndex.keys, currentIndex.key);
1237
+ const defKeys = defIndex.keys;
1238
+ if (isTextIndexDefinition(defKeys) && isMongoDBTextIndex(currentIndex.key)) {
1239
+ const expectedName = generateIndexName(defKeys);
1240
+ return currentIndex.name === expectedName;
1241
+ }
1242
+ return keysMatch(defKeys, currentIndex.key);
1238
1243
  });
1239
1244
  }
1240
1245
  async function deleteUnusedIndexes(collection) {
@@ -1272,67 +1277,8 @@ async function deleteUnusedIndexes(collection) {
1272
1277
  return result;
1273
1278
  }
1274
1279
 
1275
- // src/createCollection/createIndexes.ts
1276
- async function checkIndexes(collection) {
1277
- await collection.connectionPromise;
1278
- let currentIndexes = [];
1279
- try {
1280
- currentIndexes = await collection.rawCollection.indexes();
1281
- } catch (error) {
1282
- if (error.codeName !== "NamespaceNotFound") throw error;
1283
- return;
1284
- }
1285
- if (!collection.indexes || collection.indexes.length === 0) {
1286
- return;
1287
- }
1288
- const unexpectedIndexes = currentIndexes.filter(
1289
- (index) => index.name !== "_id_" && !isIndexDefined(collection.indexes, index)
1290
- );
1291
- if (unexpectedIndexes.length > 0) {
1292
- import_logger3.logger.warn(
1293
- `${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
1294
- );
1295
- }
1296
- }
1297
- async function loadIndexes(collection) {
1298
- if (!collection.indexes) return;
1299
- if (!collection.indexes.length) return;
1300
- await collection.connectionPromise;
1301
- const results = Promise.all(
1302
- collection.indexes.map(async (indexDef) => {
1303
- const { keys } = indexDef;
1304
- const options = getIndexOptions(indexDef);
1305
- try {
1306
- return await collection.rawCollection.createIndex(keys, options);
1307
- } catch (error) {
1308
- if (error.code === 85 || error.code === 86) {
1309
- console.info("Will delete index to create the new version");
1310
- const indexName = (() => {
1311
- const message = error.errorResponse.errmsg;
1312
- const indexName2 = message.split('name: "')[1].split('"')[0];
1313
- return indexName2;
1314
- })();
1315
- await collection.rawCollection.dropIndex(indexName);
1316
- console.info("Index was deleted, creating new index");
1317
- const result = await collection.rawCollection.createIndex(keys, options);
1318
- console.info("Index updated correctly");
1319
- return result;
1320
- }
1321
- if (error instanceof import_mongodb2.MongoExpiredSessionError || error instanceof import_mongodb2.MongoNotConnectedError) {
1322
- } else {
1323
- console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
1324
- console.error(error);
1325
- return error.message;
1326
- }
1327
- }
1328
- })
1329
- );
1330
- await checkIndexes(collection);
1331
- return results;
1332
- }
1333
-
1334
1280
  // src/createCollection/collectionsRegistry.ts
1335
- var import_logger4 = require("@orion-js/logger");
1281
+ var import_logger3 = require("@orion-js/logger");
1336
1282
  var collectionsRegistry = /* @__PURE__ */ new Map();
1337
1283
  function indexesAreEqual(indexA, indexB) {
1338
1284
  const nameA = getIndexName(indexA);
@@ -1374,17 +1320,28 @@ function getRegisteredCollections(connectionName = "main") {
1374
1320
  }
1375
1321
  return Array.from(connectionCollections.values());
1376
1322
  }
1323
+ function getMergedIndexes(connectionName, collectionName) {
1324
+ const connectionCollections = collectionsRegistry.get(connectionName);
1325
+ if (!connectionCollections) {
1326
+ return [];
1327
+ }
1328
+ const collection = connectionCollections.get(collectionName);
1329
+ if (!collection) {
1330
+ return [];
1331
+ }
1332
+ return collection.indexes || [];
1333
+ }
1377
1334
  async function deleteAllUnusedIndexes(connectionName = "main") {
1378
1335
  const collections = getRegisteredCollections(connectionName);
1379
1336
  if (collections.length === 0) {
1380
- import_logger4.logger.warn(`No collections registered for connection "${connectionName}"`);
1337
+ import_logger3.logger.warn(`No collections registered for connection "${connectionName}"`);
1381
1338
  return [];
1382
1339
  }
1383
1340
  if (createIndexesPromises.length > 0) {
1384
- import_logger4.logger.info("Waiting for pending index creation to complete before deleting unused indexes...");
1341
+ import_logger3.logger.info("Waiting for pending index creation to complete before deleting unused indexes...");
1385
1342
  await Promise.all(createIndexesPromises);
1386
1343
  }
1387
- import_logger4.logger.info(
1344
+ import_logger3.logger.info(
1388
1345
  `Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
1389
1346
  );
1390
1347
  const results = [];
@@ -1395,7 +1352,67 @@ async function deleteAllUnusedIndexes(connectionName = "main") {
1395
1352
  }
1396
1353
  }
1397
1354
  const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
1398
- import_logger4.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
1355
+ import_logger3.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
1356
+ return results;
1357
+ }
1358
+
1359
+ // src/createCollection/createIndexes.ts
1360
+ async function checkIndexes(collection) {
1361
+ await collection.connectionPromise;
1362
+ let currentIndexes = [];
1363
+ try {
1364
+ currentIndexes = await collection.rawCollection.indexes();
1365
+ } catch (error) {
1366
+ if (error.codeName !== "NamespaceNotFound") throw error;
1367
+ return;
1368
+ }
1369
+ const mergedIndexes = getMergedIndexes(collection.connectionName, collection.name);
1370
+ if (mergedIndexes.length === 0) {
1371
+ return;
1372
+ }
1373
+ const unexpectedIndexes = currentIndexes.filter(
1374
+ (index) => index.name !== "_id_" && !isIndexDefined(mergedIndexes, index)
1375
+ );
1376
+ if (unexpectedIndexes.length > 0) {
1377
+ import_logger4.logger.warn(
1378
+ `${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
1379
+ );
1380
+ }
1381
+ }
1382
+ async function loadIndexes(collection) {
1383
+ if (!collection.indexes) return;
1384
+ if (!collection.indexes.length) return;
1385
+ await collection.connectionPromise;
1386
+ const results = Promise.all(
1387
+ collection.indexes.map(async (indexDef) => {
1388
+ const { keys } = indexDef;
1389
+ const options = getIndexOptions(indexDef);
1390
+ try {
1391
+ return await collection.rawCollection.createIndex(keys, options);
1392
+ } catch (error) {
1393
+ if (error.code === 85 || error.code === 86) {
1394
+ console.info("Will delete index to create the new version");
1395
+ const indexName = (() => {
1396
+ const message = error.errorResponse.errmsg;
1397
+ const indexName2 = message.split('name: "')[1].split('"')[0];
1398
+ return indexName2;
1399
+ })();
1400
+ await collection.rawCollection.dropIndex(indexName);
1401
+ console.info("Index was deleted, creating new index");
1402
+ const result = await collection.rawCollection.createIndex(keys, options);
1403
+ console.info("Index updated correctly");
1404
+ return result;
1405
+ }
1406
+ if (error instanceof import_mongodb2.MongoExpiredSessionError || error instanceof import_mongodb2.MongoNotConnectedError) {
1407
+ } else {
1408
+ console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
1409
+ console.error(error);
1410
+ return error.message;
1411
+ }
1412
+ }
1413
+ })
1414
+ );
1415
+ await checkIndexes(collection);
1399
1416
  return results;
1400
1417
  }
1401
1418
 
@@ -1680,6 +1697,7 @@ var ENCRYPTION_ALGORITHMS = {
1680
1697
  createIndexesPromises,
1681
1698
  deleteAllUnusedIndexes,
1682
1699
  getDBName,
1700
+ getMergedIndexes,
1683
1701
  getMongoConnection,
1684
1702
  getOrCreateEncryptionKey,
1685
1703
  getRegisteredCollections,