@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.d.cts
CHANGED
|
@@ -335,6 +335,14 @@ declare function registerCollection(connectionName: string, collection: Collecti
|
|
|
335
335
|
* @returns Array of registered collections for the connection
|
|
336
336
|
*/
|
|
337
337
|
declare function getRegisteredCollections(connectionName?: string): Array<Collection<ModelClassBase>>;
|
|
338
|
+
/**
|
|
339
|
+
* Gets the merged indexes for a specific collection from the registry.
|
|
340
|
+
* This includes all indexes from all createCollection() calls for the same collection name.
|
|
341
|
+
* @param connectionName - The name of the MongoDB connection
|
|
342
|
+
* @param collectionName - The name of the collection
|
|
343
|
+
* @returns Array of merged index definitions, or empty array if collection not registered
|
|
344
|
+
*/
|
|
345
|
+
declare function getMergedIndexes(connectionName: string, collectionName: string): CollectionIndex[];
|
|
338
346
|
/**
|
|
339
347
|
* Deletes unused indexes from all registered collections for a connection.
|
|
340
348
|
* Iterates over all collections registered via createCollection() and
|
|
@@ -380,4 +388,4 @@ declare const ENCRYPTION_ALGORITHMS: {
|
|
|
380
388
|
RANDOM: string;
|
|
381
389
|
};
|
|
382
390
|
|
|
383
|
-
export { BaseCollection, Collection, type CollectionIndex, type CountDocuments, type CreateCollectionOptions, type CreateCollectionOptionsWithSchemaType, type CreateCollectionOptionsWithTypedSchema, DataLoader, type DeleteMany, type DeleteOne, type DistinctDocumentId, type DocumentWithId, ENCRYPTION_ALGORITHMS, type EstimatedDocumentCount, type Find, type FindCursor, type FindOne, type FindOneAndUpdate, type FindOneAndUpdateUpdateOptions, type InferIdType, type InferSchemaTypeWithId, type InitItem, type InsertAndFind, type InsertMany, type InsertManyOptions, type InsertOne, type InsertOptions, type ModelClassBase, type ModelToMongoSelector, MongoCollection, type MongoFilter, type MongoSelector, type OptionalId, Repository, type SchemaWithRequiredId, type TypedId, type UpdateAndFind, type UpdateItem, type UpdateMany, type UpdateOne, type UpdateOptions, type Upsert, allConnectionPromises, collectionsRegistry, configureConnection, connections, createCollection, createIndexesPromises, deleteAllUnusedIndexes, getDBName, getMongoConnection, getOrCreateEncryptionKey, getRegisteredCollections, registerCollection, typedId };
|
|
391
|
+
export { BaseCollection, Collection, type CollectionIndex, type CountDocuments, type CreateCollectionOptions, type CreateCollectionOptionsWithSchemaType, type CreateCollectionOptionsWithTypedSchema, DataLoader, type DeleteMany, type DeleteOne, type DistinctDocumentId, type DocumentWithId, ENCRYPTION_ALGORITHMS, type EstimatedDocumentCount, type Find, type FindCursor, type FindOne, type FindOneAndUpdate, type FindOneAndUpdateUpdateOptions, type InferIdType, type InferSchemaTypeWithId, type InitItem, type InsertAndFind, type InsertMany, type InsertManyOptions, type InsertOne, type InsertOptions, type ModelClassBase, type ModelToMongoSelector, MongoCollection, type MongoFilter, type MongoSelector, type OptionalId, Repository, type SchemaWithRequiredId, type TypedId, type UpdateAndFind, type UpdateItem, type UpdateMany, type UpdateOne, type UpdateOptions, type Upsert, allConnectionPromises, collectionsRegistry, configureConnection, connections, createCollection, createIndexesPromises, deleteAllUnusedIndexes, getDBName, getMergedIndexes, getMongoConnection, getOrCreateEncryptionKey, getRegisteredCollections, registerCollection, typedId };
|
package/dist/index.d.ts
CHANGED
|
@@ -335,6 +335,14 @@ declare function registerCollection(connectionName: string, collection: Collecti
|
|
|
335
335
|
* @returns Array of registered collections for the connection
|
|
336
336
|
*/
|
|
337
337
|
declare function getRegisteredCollections(connectionName?: string): Array<Collection<ModelClassBase>>;
|
|
338
|
+
/**
|
|
339
|
+
* Gets the merged indexes for a specific collection from the registry.
|
|
340
|
+
* This includes all indexes from all createCollection() calls for the same collection name.
|
|
341
|
+
* @param connectionName - The name of the MongoDB connection
|
|
342
|
+
* @param collectionName - The name of the collection
|
|
343
|
+
* @returns Array of merged index definitions, or empty array if collection not registered
|
|
344
|
+
*/
|
|
345
|
+
declare function getMergedIndexes(connectionName: string, collectionName: string): CollectionIndex[];
|
|
338
346
|
/**
|
|
339
347
|
* Deletes unused indexes from all registered collections for a connection.
|
|
340
348
|
* Iterates over all collections registered via createCollection() and
|
|
@@ -380,4 +388,4 @@ declare const ENCRYPTION_ALGORITHMS: {
|
|
|
380
388
|
RANDOM: string;
|
|
381
389
|
};
|
|
382
390
|
|
|
383
|
-
export { BaseCollection, Collection, type CollectionIndex, type CountDocuments, type CreateCollectionOptions, type CreateCollectionOptionsWithSchemaType, type CreateCollectionOptionsWithTypedSchema, DataLoader, type DeleteMany, type DeleteOne, type DistinctDocumentId, type DocumentWithId, ENCRYPTION_ALGORITHMS, type EstimatedDocumentCount, type Find, type FindCursor, type FindOne, type FindOneAndUpdate, type FindOneAndUpdateUpdateOptions, type InferIdType, type InferSchemaTypeWithId, type InitItem, type InsertAndFind, type InsertMany, type InsertManyOptions, type InsertOne, type InsertOptions, type ModelClassBase, type ModelToMongoSelector, MongoCollection, type MongoFilter, type MongoSelector, type OptionalId, Repository, type SchemaWithRequiredId, type TypedId, type UpdateAndFind, type UpdateItem, type UpdateMany, type UpdateOne, type UpdateOptions, type Upsert, allConnectionPromises, collectionsRegistry, configureConnection, connections, createCollection, createIndexesPromises, deleteAllUnusedIndexes, getDBName, getMongoConnection, getOrCreateEncryptionKey, getRegisteredCollections, registerCollection, typedId };
|
|
391
|
+
export { BaseCollection, Collection, type CollectionIndex, type CountDocuments, type CreateCollectionOptions, type CreateCollectionOptionsWithSchemaType, type CreateCollectionOptionsWithTypedSchema, DataLoader, type DeleteMany, type DeleteOne, type DistinctDocumentId, type DocumentWithId, ENCRYPTION_ALGORITHMS, type EstimatedDocumentCount, type Find, type FindCursor, type FindOne, type FindOneAndUpdate, type FindOneAndUpdateUpdateOptions, type InferIdType, type InferSchemaTypeWithId, type InitItem, type InsertAndFind, type InsertMany, type InsertManyOptions, type InsertOne, type InsertOptions, type ModelClassBase, type ModelToMongoSelector, MongoCollection, type MongoFilter, type MongoSelector, type OptionalId, Repository, type SchemaWithRequiredId, type TypedId, type UpdateAndFind, type UpdateItem, type UpdateMany, type UpdateOne, type UpdateOptions, type Upsert, allConnectionPromises, collectionsRegistry, configureConnection, connections, createCollection, createIndexesPromises, deleteAllUnusedIndexes, getDBName, getMergedIndexes, getMongoConnection, getOrCreateEncryptionKey, getRegisteredCollections, registerCollection, typedId };
|
package/dist/index.js
CHANGED
|
@@ -1146,7 +1146,7 @@ var generateId_default = getIdGenerator;
|
|
|
1146
1146
|
|
|
1147
1147
|
// src/createCollection/createIndexes.ts
|
|
1148
1148
|
import { MongoExpiredSessionError, MongoNotConnectedError } from "mongodb";
|
|
1149
|
-
import { logger as
|
|
1149
|
+
import { logger as logger4 } from "@orion-js/logger";
|
|
1150
1150
|
|
|
1151
1151
|
// src/createCollection/deleteUnusedIndexes.ts
|
|
1152
1152
|
import { logger as logger2 } from "@orion-js/logger";
|
|
@@ -1168,6 +1168,15 @@ function getIndexName(indexDef) {
|
|
|
1168
1168
|
}
|
|
1169
1169
|
|
|
1170
1170
|
// src/createCollection/deleteUnusedIndexes.ts
|
|
1171
|
+
function isTextIndexDefinition(keys) {
|
|
1172
|
+
return Object.values(keys).some((value) => value === "text");
|
|
1173
|
+
}
|
|
1174
|
+
function isMongoDBTextIndex(key) {
|
|
1175
|
+
return "_fts" in key && "_ftsx" in key;
|
|
1176
|
+
}
|
|
1177
|
+
function generateIndexName(keys) {
|
|
1178
|
+
return Object.entries(keys).map(([field, value]) => `${field}_${value}`).join("_");
|
|
1179
|
+
}
|
|
1171
1180
|
function keysMatch(definitionKeys, currentIndexKey) {
|
|
1172
1181
|
const defEntries = Object.entries(definitionKeys);
|
|
1173
1182
|
const curEntries = Object.entries(currentIndexKey);
|
|
@@ -1183,7 +1192,12 @@ function isIndexDefined(definedIndexes, currentIndex) {
|
|
|
1183
1192
|
return definedIndexes.some((defIndex) => {
|
|
1184
1193
|
const customName = getIndexName(defIndex);
|
|
1185
1194
|
if (customName && customName === currentIndex.name) return true;
|
|
1186
|
-
|
|
1195
|
+
const defKeys = defIndex.keys;
|
|
1196
|
+
if (isTextIndexDefinition(defKeys) && isMongoDBTextIndex(currentIndex.key)) {
|
|
1197
|
+
const expectedName = generateIndexName(defKeys);
|
|
1198
|
+
return currentIndex.name === expectedName;
|
|
1199
|
+
}
|
|
1200
|
+
return keysMatch(defKeys, currentIndex.key);
|
|
1187
1201
|
});
|
|
1188
1202
|
}
|
|
1189
1203
|
async function deleteUnusedIndexes(collection) {
|
|
@@ -1221,67 +1235,8 @@ async function deleteUnusedIndexes(collection) {
|
|
|
1221
1235
|
return result;
|
|
1222
1236
|
}
|
|
1223
1237
|
|
|
1224
|
-
// src/createCollection/createIndexes.ts
|
|
1225
|
-
async function checkIndexes(collection) {
|
|
1226
|
-
await collection.connectionPromise;
|
|
1227
|
-
let currentIndexes = [];
|
|
1228
|
-
try {
|
|
1229
|
-
currentIndexes = await collection.rawCollection.indexes();
|
|
1230
|
-
} catch (error) {
|
|
1231
|
-
if (error.codeName !== "NamespaceNotFound") throw error;
|
|
1232
|
-
return;
|
|
1233
|
-
}
|
|
1234
|
-
if (!collection.indexes || collection.indexes.length === 0) {
|
|
1235
|
-
return;
|
|
1236
|
-
}
|
|
1237
|
-
const unexpectedIndexes = currentIndexes.filter(
|
|
1238
|
-
(index) => index.name !== "_id_" && !isIndexDefined(collection.indexes, index)
|
|
1239
|
-
);
|
|
1240
|
-
if (unexpectedIndexes.length > 0) {
|
|
1241
|
-
logger3.warn(
|
|
1242
|
-
`${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
|
|
1243
|
-
);
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
async function loadIndexes(collection) {
|
|
1247
|
-
if (!collection.indexes) return;
|
|
1248
|
-
if (!collection.indexes.length) return;
|
|
1249
|
-
await collection.connectionPromise;
|
|
1250
|
-
const results = Promise.all(
|
|
1251
|
-
collection.indexes.map(async (indexDef) => {
|
|
1252
|
-
const { keys } = indexDef;
|
|
1253
|
-
const options = getIndexOptions(indexDef);
|
|
1254
|
-
try {
|
|
1255
|
-
return await collection.rawCollection.createIndex(keys, options);
|
|
1256
|
-
} catch (error) {
|
|
1257
|
-
if (error.code === 85 || error.code === 86) {
|
|
1258
|
-
console.info("Will delete index to create the new version");
|
|
1259
|
-
const indexName = (() => {
|
|
1260
|
-
const message = error.errorResponse.errmsg;
|
|
1261
|
-
const indexName2 = message.split('name: "')[1].split('"')[0];
|
|
1262
|
-
return indexName2;
|
|
1263
|
-
})();
|
|
1264
|
-
await collection.rawCollection.dropIndex(indexName);
|
|
1265
|
-
console.info("Index was deleted, creating new index");
|
|
1266
|
-
const result = await collection.rawCollection.createIndex(keys, options);
|
|
1267
|
-
console.info("Index updated correctly");
|
|
1268
|
-
return result;
|
|
1269
|
-
}
|
|
1270
|
-
if (error instanceof MongoExpiredSessionError || error instanceof MongoNotConnectedError) {
|
|
1271
|
-
} else {
|
|
1272
|
-
console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
|
|
1273
|
-
console.error(error);
|
|
1274
|
-
return error.message;
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1277
|
-
})
|
|
1278
|
-
);
|
|
1279
|
-
await checkIndexes(collection);
|
|
1280
|
-
return results;
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
1238
|
// src/createCollection/collectionsRegistry.ts
|
|
1284
|
-
import { logger as
|
|
1239
|
+
import { logger as logger3 } from "@orion-js/logger";
|
|
1285
1240
|
var collectionsRegistry = /* @__PURE__ */ new Map();
|
|
1286
1241
|
function indexesAreEqual(indexA, indexB) {
|
|
1287
1242
|
const nameA = getIndexName(indexA);
|
|
@@ -1323,17 +1278,28 @@ function getRegisteredCollections(connectionName = "main") {
|
|
|
1323
1278
|
}
|
|
1324
1279
|
return Array.from(connectionCollections.values());
|
|
1325
1280
|
}
|
|
1281
|
+
function getMergedIndexes(connectionName, collectionName) {
|
|
1282
|
+
const connectionCollections = collectionsRegistry.get(connectionName);
|
|
1283
|
+
if (!connectionCollections) {
|
|
1284
|
+
return [];
|
|
1285
|
+
}
|
|
1286
|
+
const collection = connectionCollections.get(collectionName);
|
|
1287
|
+
if (!collection) {
|
|
1288
|
+
return [];
|
|
1289
|
+
}
|
|
1290
|
+
return collection.indexes || [];
|
|
1291
|
+
}
|
|
1326
1292
|
async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
1327
1293
|
const collections = getRegisteredCollections(connectionName);
|
|
1328
1294
|
if (collections.length === 0) {
|
|
1329
|
-
|
|
1295
|
+
logger3.warn(`No collections registered for connection "${connectionName}"`);
|
|
1330
1296
|
return [];
|
|
1331
1297
|
}
|
|
1332
1298
|
if (createIndexesPromises.length > 0) {
|
|
1333
|
-
|
|
1299
|
+
logger3.info("Waiting for pending index creation to complete before deleting unused indexes...");
|
|
1334
1300
|
await Promise.all(createIndexesPromises);
|
|
1335
1301
|
}
|
|
1336
|
-
|
|
1302
|
+
logger3.info(
|
|
1337
1303
|
`Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
|
|
1338
1304
|
);
|
|
1339
1305
|
const results = [];
|
|
@@ -1344,7 +1310,67 @@ async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
|
1344
1310
|
}
|
|
1345
1311
|
}
|
|
1346
1312
|
const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
|
|
1347
|
-
|
|
1313
|
+
logger3.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
|
|
1314
|
+
return results;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
// src/createCollection/createIndexes.ts
|
|
1318
|
+
async function checkIndexes(collection) {
|
|
1319
|
+
await collection.connectionPromise;
|
|
1320
|
+
let currentIndexes = [];
|
|
1321
|
+
try {
|
|
1322
|
+
currentIndexes = await collection.rawCollection.indexes();
|
|
1323
|
+
} catch (error) {
|
|
1324
|
+
if (error.codeName !== "NamespaceNotFound") throw error;
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
const mergedIndexes = getMergedIndexes(collection.connectionName, collection.name);
|
|
1328
|
+
if (mergedIndexes.length === 0) {
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const unexpectedIndexes = currentIndexes.filter(
|
|
1332
|
+
(index) => index.name !== "_id_" && !isIndexDefined(mergedIndexes, index)
|
|
1333
|
+
);
|
|
1334
|
+
if (unexpectedIndexes.length > 0) {
|
|
1335
|
+
logger4.warn(
|
|
1336
|
+
`${unexpectedIndexes.length} unexpected indexes found in collection "${collection.name}": ${unexpectedIndexes.map((i) => i.name).join(", ")} | Delete the index or fix the collection definition`
|
|
1337
|
+
);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
async function loadIndexes(collection) {
|
|
1341
|
+
if (!collection.indexes) return;
|
|
1342
|
+
if (!collection.indexes.length) return;
|
|
1343
|
+
await collection.connectionPromise;
|
|
1344
|
+
const results = Promise.all(
|
|
1345
|
+
collection.indexes.map(async (indexDef) => {
|
|
1346
|
+
const { keys } = indexDef;
|
|
1347
|
+
const options = getIndexOptions(indexDef);
|
|
1348
|
+
try {
|
|
1349
|
+
return await collection.rawCollection.createIndex(keys, options);
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
if (error.code === 85 || error.code === 86) {
|
|
1352
|
+
console.info("Will delete index to create the new version");
|
|
1353
|
+
const indexName = (() => {
|
|
1354
|
+
const message = error.errorResponse.errmsg;
|
|
1355
|
+
const indexName2 = message.split('name: "')[1].split('"')[0];
|
|
1356
|
+
return indexName2;
|
|
1357
|
+
})();
|
|
1358
|
+
await collection.rawCollection.dropIndex(indexName);
|
|
1359
|
+
console.info("Index was deleted, creating new index");
|
|
1360
|
+
const result = await collection.rawCollection.createIndex(keys, options);
|
|
1361
|
+
console.info("Index updated correctly");
|
|
1362
|
+
return result;
|
|
1363
|
+
}
|
|
1364
|
+
if (error instanceof MongoExpiredSessionError || error instanceof MongoNotConnectedError) {
|
|
1365
|
+
} else {
|
|
1366
|
+
console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
|
|
1367
|
+
console.error(error);
|
|
1368
|
+
return error.message;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
})
|
|
1372
|
+
);
|
|
1373
|
+
await checkIndexes(collection);
|
|
1348
1374
|
return results;
|
|
1349
1375
|
}
|
|
1350
1376
|
|
|
@@ -1628,6 +1654,7 @@ export {
|
|
|
1628
1654
|
createIndexesPromises,
|
|
1629
1655
|
deleteAllUnusedIndexes,
|
|
1630
1656
|
getDBName,
|
|
1657
|
+
getMergedIndexes,
|
|
1631
1658
|
getMongoConnection,
|
|
1632
1659
|
getOrCreateEncryptionKey,
|
|
1633
1660
|
getRegisteredCollections,
|