@orion-js/mongodb 4.2.0 → 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 +84 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +80 -7
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -36,13 +36,17 @@ __export(index_exports, {
|
|
|
36
36
|
MongoDB: () => MongoDB,
|
|
37
37
|
Repository: () => Repository,
|
|
38
38
|
allConnectionPromises: () => allConnectionPromises,
|
|
39
|
+
collectionsRegistry: () => collectionsRegistry,
|
|
39
40
|
configureConnection: () => configureConnection,
|
|
40
41
|
connections: () => connections,
|
|
41
42
|
createCollection: () => createCollection,
|
|
42
43
|
createIndexesPromises: () => createIndexesPromises,
|
|
44
|
+
deleteAllUnusedIndexes: () => deleteAllUnusedIndexes,
|
|
43
45
|
getDBName: () => getDBName,
|
|
44
46
|
getMongoConnection: () => getMongoConnection,
|
|
45
47
|
getOrCreateEncryptionKey: () => getOrCreateEncryptionKey,
|
|
48
|
+
getRegisteredCollections: () => getRegisteredCollections,
|
|
49
|
+
registerCollection: () => registerCollection,
|
|
46
50
|
typedId: () => typedId
|
|
47
51
|
});
|
|
48
52
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -1327,6 +1331,74 @@ async function loadIndexes(collection) {
|
|
|
1327
1331
|
return results;
|
|
1328
1332
|
}
|
|
1329
1333
|
|
|
1334
|
+
// src/createCollection/collectionsRegistry.ts
|
|
1335
|
+
var import_logger4 = require("@orion-js/logger");
|
|
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
|
+
}
|
|
1358
|
+
function registerCollection(connectionName, collection) {
|
|
1359
|
+
if (!collectionsRegistry.has(connectionName)) {
|
|
1360
|
+
collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
|
|
1361
|
+
}
|
|
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
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
function getRegisteredCollections(connectionName = "main") {
|
|
1371
|
+
const connectionCollections = collectionsRegistry.get(connectionName);
|
|
1372
|
+
if (!connectionCollections) {
|
|
1373
|
+
return [];
|
|
1374
|
+
}
|
|
1375
|
+
return Array.from(connectionCollections.values());
|
|
1376
|
+
}
|
|
1377
|
+
async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
1378
|
+
const collections = getRegisteredCollections(connectionName);
|
|
1379
|
+
if (collections.length === 0) {
|
|
1380
|
+
import_logger4.logger.warn(`No collections registered for connection "${connectionName}"`);
|
|
1381
|
+
return [];
|
|
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
|
+
}
|
|
1387
|
+
import_logger4.logger.info(
|
|
1388
|
+
`Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
|
|
1389
|
+
);
|
|
1390
|
+
const results = [];
|
|
1391
|
+
for (const collection of collections) {
|
|
1392
|
+
const result = await collection.deleteUnusedIndexes();
|
|
1393
|
+
if (result.deletedIndexes.length > 0) {
|
|
1394
|
+
results.push(result);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
|
|
1398
|
+
import_logger4.logger.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
|
|
1399
|
+
return results;
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1330
1402
|
// src/createCollection/getSchemaAndModel.ts
|
|
1331
1403
|
var import_helpers7 = require("@orion-js/helpers");
|
|
1332
1404
|
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
@@ -1359,7 +1431,7 @@ function getSchema(options) {
|
|
|
1359
1431
|
}
|
|
1360
1432
|
|
|
1361
1433
|
// src/createCollection/wrapMethods.ts
|
|
1362
|
-
var
|
|
1434
|
+
var import_logger5 = require("@orion-js/logger");
|
|
1363
1435
|
function wrapMethods(collection) {
|
|
1364
1436
|
const methodsWithPromises = [
|
|
1365
1437
|
"findOne",
|
|
@@ -1395,7 +1467,7 @@ function wrapMethods(collection) {
|
|
|
1395
1467
|
collection[methodName2] = (...args) => {
|
|
1396
1468
|
collection.startConnection();
|
|
1397
1469
|
if (!collection.rawCollection) {
|
|
1398
|
-
|
|
1470
|
+
import_logger5.logger.error("Method called before connection was initialized", {
|
|
1399
1471
|
collectionName: collection.name,
|
|
1400
1472
|
connectionName: collection.connectionName,
|
|
1401
1473
|
methodName: methodName2
|
|
@@ -1505,6 +1577,7 @@ function createCollection(options) {
|
|
|
1505
1577
|
}
|
|
1506
1578
|
wrapMethods(mainCollection);
|
|
1507
1579
|
wrapMethods(encryptedCollection);
|
|
1580
|
+
registerCollection(connectionName, mainCollection);
|
|
1508
1581
|
return mainCollection;
|
|
1509
1582
|
}
|
|
1510
1583
|
|
|
@@ -1535,7 +1608,7 @@ function MongoCollection(options) {
|
|
|
1535
1608
|
}
|
|
1536
1609
|
|
|
1537
1610
|
// src/encrypted/getOrCreateEncryptionKey.ts
|
|
1538
|
-
var
|
|
1611
|
+
var import_logger6 = require("@orion-js/logger");
|
|
1539
1612
|
var import_mongodb3 = require("mongodb");
|
|
1540
1613
|
async function getOrCreateEncryptionKey({
|
|
1541
1614
|
keyAltName,
|
|
@@ -1547,7 +1620,7 @@ async function getOrCreateEncryptionKey({
|
|
|
1547
1620
|
kmsProviders
|
|
1548
1621
|
}) {
|
|
1549
1622
|
const keyVaultNamespace = `${keyVaultDatabase}.${keyVaultCollection}`;
|
|
1550
|
-
|
|
1623
|
+
import_logger6.logger.info("Connecting to database to get or create the encryption key", {
|
|
1551
1624
|
keyVaultNamespace,
|
|
1552
1625
|
keyAltName
|
|
1553
1626
|
});
|
|
@@ -1565,14 +1638,14 @@ async function getOrCreateEncryptionKey({
|
|
|
1565
1638
|
});
|
|
1566
1639
|
const key = await clientEncryption.getKeyByAltName(keyAltName);
|
|
1567
1640
|
if (key) {
|
|
1568
|
-
|
|
1641
|
+
import_logger6.logger.info("Key found on the key vault", {
|
|
1569
1642
|
keyVaultNamespace,
|
|
1570
1643
|
keyAltName,
|
|
1571
1644
|
UUID: key._id
|
|
1572
1645
|
});
|
|
1573
1646
|
return { key: key._id, keyVaultNamespace };
|
|
1574
1647
|
}
|
|
1575
|
-
|
|
1648
|
+
import_logger6.logger.info("Key not found on the key vault, creating a new one", {
|
|
1576
1649
|
keyVaultNamespace,
|
|
1577
1650
|
keyAltName
|
|
1578
1651
|
});
|
|
@@ -1580,7 +1653,7 @@ async function getOrCreateEncryptionKey({
|
|
|
1580
1653
|
keyAltNames: [keyAltName],
|
|
1581
1654
|
...masterKey ? { masterKey } : {}
|
|
1582
1655
|
});
|
|
1583
|
-
|
|
1656
|
+
import_logger6.logger.info("New encryption key created", {
|
|
1584
1657
|
keyVaultNamespace,
|
|
1585
1658
|
keyAltName,
|
|
1586
1659
|
UUID: newKey
|
|
@@ -1600,13 +1673,17 @@ var ENCRYPTION_ALGORITHMS = {
|
|
|
1600
1673
|
MongoDB,
|
|
1601
1674
|
Repository,
|
|
1602
1675
|
allConnectionPromises,
|
|
1676
|
+
collectionsRegistry,
|
|
1603
1677
|
configureConnection,
|
|
1604
1678
|
connections,
|
|
1605
1679
|
createCollection,
|
|
1606
1680
|
createIndexesPromises,
|
|
1681
|
+
deleteAllUnusedIndexes,
|
|
1607
1682
|
getDBName,
|
|
1608
1683
|
getMongoConnection,
|
|
1609
1684
|
getOrCreateEncryptionKey,
|
|
1685
|
+
getRegisteredCollections,
|
|
1686
|
+
registerCollection,
|
|
1610
1687
|
typedId
|
|
1611
1688
|
});
|
|
1612
1689
|
//# sourceMappingURL=index.cjs.map
|