@orion-js/mongodb 4.2.3 → 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 +94 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +93 -66
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
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
|
|
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
|
-
|
|
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,67 +1287,8 @@ async function deleteUnusedIndexes(collection) {
|
|
|
1272
1287
|
return result;
|
|
1273
1288
|
}
|
|
1274
1289
|
|
|
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
1290
|
// src/createCollection/collectionsRegistry.ts
|
|
1335
|
-
var
|
|
1291
|
+
var import_logger3 = require("@orion-js/logger");
|
|
1336
1292
|
var collectionsRegistry = /* @__PURE__ */ new Map();
|
|
1337
1293
|
function indexesAreEqual(indexA, indexB) {
|
|
1338
1294
|
const nameA = getIndexName(indexA);
|
|
@@ -1374,17 +1330,28 @@ function getRegisteredCollections(connectionName = "main") {
|
|
|
1374
1330
|
}
|
|
1375
1331
|
return Array.from(connectionCollections.values());
|
|
1376
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
|
+
}
|
|
1377
1344
|
async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
1378
1345
|
const collections = getRegisteredCollections(connectionName);
|
|
1379
1346
|
if (collections.length === 0) {
|
|
1380
|
-
|
|
1347
|
+
import_logger3.logger.warn(`No collections registered for connection "${connectionName}"`);
|
|
1381
1348
|
return [];
|
|
1382
1349
|
}
|
|
1383
1350
|
if (createIndexesPromises.length > 0) {
|
|
1384
|
-
|
|
1351
|
+
import_logger3.logger.info("Waiting for pending index creation to complete before deleting unused indexes...");
|
|
1385
1352
|
await Promise.all(createIndexesPromises);
|
|
1386
1353
|
}
|
|
1387
|
-
|
|
1354
|
+
import_logger3.logger.info(
|
|
1388
1355
|
`Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
|
|
1389
1356
|
);
|
|
1390
1357
|
const results = [];
|
|
@@ -1395,7 +1362,67 @@ async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
|
1395
1362
|
}
|
|
1396
1363
|
}
|
|
1397
1364
|
const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
|
|
1398
|
-
|
|
1365
|
+
import_logger3.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
|
|
1366
|
+
return results;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// src/createCollection/createIndexes.ts
|
|
1370
|
+
async function checkIndexes(collection) {
|
|
1371
|
+
await collection.connectionPromise;
|
|
1372
|
+
let currentIndexes = [];
|
|
1373
|
+
try {
|
|
1374
|
+
currentIndexes = await collection.rawCollection.indexes();
|
|
1375
|
+
} catch (error) {
|
|
1376
|
+
if (error.codeName !== "NamespaceNotFound") throw error;
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1379
|
+
const mergedIndexes = getMergedIndexes(collection.connectionName, collection.name);
|
|
1380
|
+
if (mergedIndexes.length === 0) {
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
const unexpectedIndexes = currentIndexes.filter(
|
|
1384
|
+
(index) => index.name !== "_id_" && !isIndexDefined(mergedIndexes, index)
|
|
1385
|
+
);
|
|
1386
|
+
if (unexpectedIndexes.length > 0) {
|
|
1387
|
+
import_logger4.logger.warn(
|
|
1388
|
+
`${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
|
|
1389
|
+
);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
async function loadIndexes(collection) {
|
|
1393
|
+
if (!collection.indexes) return;
|
|
1394
|
+
if (!collection.indexes.length) return;
|
|
1395
|
+
await collection.connectionPromise;
|
|
1396
|
+
const results = Promise.all(
|
|
1397
|
+
collection.indexes.map(async (indexDef) => {
|
|
1398
|
+
const { keys } = indexDef;
|
|
1399
|
+
const options = getIndexOptions(indexDef);
|
|
1400
|
+
try {
|
|
1401
|
+
return await collection.rawCollection.createIndex(keys, options);
|
|
1402
|
+
} catch (error) {
|
|
1403
|
+
if (error.code === 85 || error.code === 86) {
|
|
1404
|
+
console.info("Will delete index to create the new version");
|
|
1405
|
+
const indexName = (() => {
|
|
1406
|
+
const message = error.errorResponse.errmsg;
|
|
1407
|
+
const indexName2 = message.split('name: "')[1].split('"')[0];
|
|
1408
|
+
return indexName2;
|
|
1409
|
+
})();
|
|
1410
|
+
await collection.rawCollection.dropIndex(indexName);
|
|
1411
|
+
console.info("Index was deleted, creating new index");
|
|
1412
|
+
const result = await collection.rawCollection.createIndex(keys, options);
|
|
1413
|
+
console.info("Index updated correctly");
|
|
1414
|
+
return result;
|
|
1415
|
+
}
|
|
1416
|
+
if (error instanceof import_mongodb2.MongoExpiredSessionError || error instanceof import_mongodb2.MongoNotConnectedError) {
|
|
1417
|
+
} else {
|
|
1418
|
+
console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
|
|
1419
|
+
console.error(error);
|
|
1420
|
+
return error.message;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
})
|
|
1424
|
+
);
|
|
1425
|
+
await checkIndexes(collection);
|
|
1399
1426
|
return results;
|
|
1400
1427
|
}
|
|
1401
1428
|
|
|
@@ -1680,6 +1707,7 @@ var ENCRYPTION_ALGORITHMS = {
|
|
|
1680
1707
|
createIndexesPromises,
|
|
1681
1708
|
deleteAllUnusedIndexes,
|
|
1682
1709
|
getDBName,
|
|
1710
|
+
getMergedIndexes,
|
|
1683
1711
|
getMongoConnection,
|
|
1684
1712
|
getOrCreateEncryptionKey,
|
|
1685
1713
|
getRegisteredCollections,
|