@orion-js/mongodb 4.1.10 → 4.2.1
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 +150 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +81 -2
- package/dist/index.d.ts +81 -2
- package/dist/index.js +146 -18
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.d.cts
CHANGED
|
@@ -73,8 +73,26 @@ type DocumentWithId<TSchema> = EnhancedOmit<TSchema, '_id'> & {
|
|
|
73
73
|
_id: InferIdType<TSchema>;
|
|
74
74
|
};
|
|
75
75
|
type ModelClassBase = DocumentWithId<MongoDB.Document>;
|
|
76
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Index definition for a MongoDB collection.
|
|
78
|
+
* Supports flat options (recommended) or nested options object (deprecated).
|
|
79
|
+
*
|
|
80
|
+
* @example New format (recommended):
|
|
81
|
+
* ```ts
|
|
82
|
+
* { keys: { email: 1 }, unique: true, sparse: true }
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example Old format (deprecated):
|
|
86
|
+
* ```ts
|
|
87
|
+
* { keys: { email: 1 }, options: { unique: true, sparse: true } }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
interface CollectionIndex extends Partial<MongoDB.CreateIndexesOptions> {
|
|
77
91
|
keys: MongoDB.IndexSpecification;
|
|
92
|
+
/**
|
|
93
|
+
* @deprecated Use flat options instead. Example: `{ keys: { email: 1 }, unique: true }`
|
|
94
|
+
* instead of `{ keys: { email: 1 }, options: { unique: true } }`
|
|
95
|
+
*/
|
|
78
96
|
options?: MongoDB.CreateIndexesOptions;
|
|
79
97
|
}
|
|
80
98
|
declare namespace DataLoader {
|
|
@@ -229,6 +247,15 @@ declare class BaseCollection<ModelClass extends ModelClassBase = ModelClassBase>
|
|
|
229
247
|
*/
|
|
230
248
|
createIndexes: () => Promise<string[]>;
|
|
231
249
|
createIndexesPromise: Promise<string[]>;
|
|
250
|
+
/**
|
|
251
|
+
* Deletes indexes that exist in MongoDB but are not defined in the collection configuration.
|
|
252
|
+
* This helps clean up stale indexes that are no longer needed.
|
|
253
|
+
* Always preserves the _id_ index.
|
|
254
|
+
*/
|
|
255
|
+
deleteUnusedIndexes: () => Promise<{
|
|
256
|
+
deletedIndexes: string[];
|
|
257
|
+
collectionName: string;
|
|
258
|
+
}>;
|
|
232
259
|
/**
|
|
233
260
|
* @deprecated Use startConnection() instead. This property is not guaranteed to be resolved if the connection is not started.
|
|
234
261
|
* When using async calls startConnection or connectionPromise is no longer needed. Orion will automatically start the connection if it is not already started.
|
|
@@ -277,6 +304,58 @@ declare function createCollection<T extends TypedSchemaOnSchema & {
|
|
|
277
304
|
}>(options: CreateCollectionOptionsWithTypedSchema<T>): Collection<InferSchemaType<T>>;
|
|
278
305
|
declare function createCollection<T extends ModelClassBase>(options: CreateCollectionOptions<T>): Collection<T>;
|
|
279
306
|
|
|
307
|
+
/**
|
|
308
|
+
* Result of the deleteUnusedIndexes operation
|
|
309
|
+
*/
|
|
310
|
+
interface DeleteUnusedIndexesResult {
|
|
311
|
+
/** Names of the indexes that were deleted */
|
|
312
|
+
deletedIndexes: string[];
|
|
313
|
+
/** Name of the collection that was cleaned */
|
|
314
|
+
collectionName: string;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Registry that tracks all collections created via createCollection().
|
|
319
|
+
* Maps connection name to a map of collection name to collection instance.
|
|
320
|
+
*/
|
|
321
|
+
declare const collectionsRegistry: Map<string, Map<string, Collection<ModelClassBase>>>;
|
|
322
|
+
/**
|
|
323
|
+
* Registers a collection in the registry.
|
|
324
|
+
* Called automatically when a collection is created via createCollection().
|
|
325
|
+
* @param connectionName - The name of the MongoDB connection
|
|
326
|
+
* @param collection - The collection instance to register
|
|
327
|
+
*/
|
|
328
|
+
declare function registerCollection(connectionName: string, collection: Collection<ModelClassBase>): void;
|
|
329
|
+
/**
|
|
330
|
+
* Gets all registered collections for a specific connection.
|
|
331
|
+
* @param connectionName - The name of the MongoDB connection (defaults to 'main')
|
|
332
|
+
* @returns Array of registered collections for the connection
|
|
333
|
+
*/
|
|
334
|
+
declare function getRegisteredCollections(connectionName?: string): Array<Collection<ModelClassBase>>;
|
|
335
|
+
/**
|
|
336
|
+
* Deletes unused indexes from all registered collections for a connection.
|
|
337
|
+
* Iterates over all collections registered via createCollection() and
|
|
338
|
+
* removes indexes that exist in MongoDB but are not defined in the collection configuration.
|
|
339
|
+
*
|
|
340
|
+
* @param connectionName - The name of the MongoDB connection (defaults to 'main')
|
|
341
|
+
* @returns Array of results for each collection that had indexes deleted
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* // Delete unused indexes from all collections on the main connection
|
|
346
|
+
* const results = await deleteAllUnusedIndexes()
|
|
347
|
+
*
|
|
348
|
+
* // Delete unused indexes from all collections on a specific connection
|
|
349
|
+
* const results = await deleteAllUnusedIndexes('secondary')
|
|
350
|
+
*
|
|
351
|
+
* // Log results
|
|
352
|
+
* for (const result of results) {
|
|
353
|
+
* console.log(`${result.collectionName}: deleted ${result.deletedIndexes.length} indexes`)
|
|
354
|
+
* }
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
declare function deleteAllUnusedIndexes(connectionName?: string): Promise<DeleteUnusedIndexesResult[]>;
|
|
358
|
+
|
|
280
359
|
declare function getOrCreateEncryptionKey({ keyAltName, kmsProvider, masterKey, connectionName, keyVaultDatabase, keyVaultCollection, kmsProviders, }: {
|
|
281
360
|
keyAltName: string;
|
|
282
361
|
kmsProvider: keyof KMSProviders;
|
|
@@ -294,4 +373,4 @@ declare const ENCRYPTION_ALGORITHMS: {
|
|
|
294
373
|
RANDOM: string;
|
|
295
374
|
};
|
|
296
375
|
|
|
297
|
-
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, configureConnection, connections, createCollection, createIndexesPromises, getDBName, getMongoConnection, getOrCreateEncryptionKey, typedId };
|
|
376
|
+
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -73,8 +73,26 @@ type DocumentWithId<TSchema> = EnhancedOmit<TSchema, '_id'> & {
|
|
|
73
73
|
_id: InferIdType<TSchema>;
|
|
74
74
|
};
|
|
75
75
|
type ModelClassBase = DocumentWithId<MongoDB.Document>;
|
|
76
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Index definition for a MongoDB collection.
|
|
78
|
+
* Supports flat options (recommended) or nested options object (deprecated).
|
|
79
|
+
*
|
|
80
|
+
* @example New format (recommended):
|
|
81
|
+
* ```ts
|
|
82
|
+
* { keys: { email: 1 }, unique: true, sparse: true }
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example Old format (deprecated):
|
|
86
|
+
* ```ts
|
|
87
|
+
* { keys: { email: 1 }, options: { unique: true, sparse: true } }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
interface CollectionIndex extends Partial<MongoDB.CreateIndexesOptions> {
|
|
77
91
|
keys: MongoDB.IndexSpecification;
|
|
92
|
+
/**
|
|
93
|
+
* @deprecated Use flat options instead. Example: `{ keys: { email: 1 }, unique: true }`
|
|
94
|
+
* instead of `{ keys: { email: 1 }, options: { unique: true } }`
|
|
95
|
+
*/
|
|
78
96
|
options?: MongoDB.CreateIndexesOptions;
|
|
79
97
|
}
|
|
80
98
|
declare namespace DataLoader {
|
|
@@ -229,6 +247,15 @@ declare class BaseCollection<ModelClass extends ModelClassBase = ModelClassBase>
|
|
|
229
247
|
*/
|
|
230
248
|
createIndexes: () => Promise<string[]>;
|
|
231
249
|
createIndexesPromise: Promise<string[]>;
|
|
250
|
+
/**
|
|
251
|
+
* Deletes indexes that exist in MongoDB but are not defined in the collection configuration.
|
|
252
|
+
* This helps clean up stale indexes that are no longer needed.
|
|
253
|
+
* Always preserves the _id_ index.
|
|
254
|
+
*/
|
|
255
|
+
deleteUnusedIndexes: () => Promise<{
|
|
256
|
+
deletedIndexes: string[];
|
|
257
|
+
collectionName: string;
|
|
258
|
+
}>;
|
|
232
259
|
/**
|
|
233
260
|
* @deprecated Use startConnection() instead. This property is not guaranteed to be resolved if the connection is not started.
|
|
234
261
|
* When using async calls startConnection or connectionPromise is no longer needed. Orion will automatically start the connection if it is not already started.
|
|
@@ -277,6 +304,58 @@ declare function createCollection<T extends TypedSchemaOnSchema & {
|
|
|
277
304
|
}>(options: CreateCollectionOptionsWithTypedSchema<T>): Collection<InferSchemaType<T>>;
|
|
278
305
|
declare function createCollection<T extends ModelClassBase>(options: CreateCollectionOptions<T>): Collection<T>;
|
|
279
306
|
|
|
307
|
+
/**
|
|
308
|
+
* Result of the deleteUnusedIndexes operation
|
|
309
|
+
*/
|
|
310
|
+
interface DeleteUnusedIndexesResult {
|
|
311
|
+
/** Names of the indexes that were deleted */
|
|
312
|
+
deletedIndexes: string[];
|
|
313
|
+
/** Name of the collection that was cleaned */
|
|
314
|
+
collectionName: string;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Registry that tracks all collections created via createCollection().
|
|
319
|
+
* Maps connection name to a map of collection name to collection instance.
|
|
320
|
+
*/
|
|
321
|
+
declare const collectionsRegistry: Map<string, Map<string, Collection<ModelClassBase>>>;
|
|
322
|
+
/**
|
|
323
|
+
* Registers a collection in the registry.
|
|
324
|
+
* Called automatically when a collection is created via createCollection().
|
|
325
|
+
* @param connectionName - The name of the MongoDB connection
|
|
326
|
+
* @param collection - The collection instance to register
|
|
327
|
+
*/
|
|
328
|
+
declare function registerCollection(connectionName: string, collection: Collection<ModelClassBase>): void;
|
|
329
|
+
/**
|
|
330
|
+
* Gets all registered collections for a specific connection.
|
|
331
|
+
* @param connectionName - The name of the MongoDB connection (defaults to 'main')
|
|
332
|
+
* @returns Array of registered collections for the connection
|
|
333
|
+
*/
|
|
334
|
+
declare function getRegisteredCollections(connectionName?: string): Array<Collection<ModelClassBase>>;
|
|
335
|
+
/**
|
|
336
|
+
* Deletes unused indexes from all registered collections for a connection.
|
|
337
|
+
* Iterates over all collections registered via createCollection() and
|
|
338
|
+
* removes indexes that exist in MongoDB but are not defined in the collection configuration.
|
|
339
|
+
*
|
|
340
|
+
* @param connectionName - The name of the MongoDB connection (defaults to 'main')
|
|
341
|
+
* @returns Array of results for each collection that had indexes deleted
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* // Delete unused indexes from all collections on the main connection
|
|
346
|
+
* const results = await deleteAllUnusedIndexes()
|
|
347
|
+
*
|
|
348
|
+
* // Delete unused indexes from all collections on a specific connection
|
|
349
|
+
* const results = await deleteAllUnusedIndexes('secondary')
|
|
350
|
+
*
|
|
351
|
+
* // Log results
|
|
352
|
+
* for (const result of results) {
|
|
353
|
+
* console.log(`${result.collectionName}: deleted ${result.deletedIndexes.length} indexes`)
|
|
354
|
+
* }
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
declare function deleteAllUnusedIndexes(connectionName?: string): Promise<DeleteUnusedIndexesResult[]>;
|
|
358
|
+
|
|
280
359
|
declare function getOrCreateEncryptionKey({ keyAltName, kmsProvider, masterKey, connectionName, keyVaultDatabase, keyVaultCollection, kmsProviders, }: {
|
|
281
360
|
keyAltName: string;
|
|
282
361
|
kmsProvider: keyof KMSProviders;
|
|
@@ -294,4 +373,4 @@ declare const ENCRYPTION_ALGORITHMS: {
|
|
|
294
373
|
RANDOM: string;
|
|
295
374
|
};
|
|
296
375
|
|
|
297
|
-
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, configureConnection, connections, createCollection, createIndexesPromises, getDBName, getMongoConnection, getOrCreateEncryptionKey, typedId };
|
|
376
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -248,6 +248,12 @@ var BaseCollection = class {
|
|
|
248
248
|
*/
|
|
249
249
|
createIndexes;
|
|
250
250
|
createIndexesPromise;
|
|
251
|
+
/**
|
|
252
|
+
* Deletes indexes that exist in MongoDB but are not defined in the collection configuration.
|
|
253
|
+
* This helps clean up stale indexes that are no longer needed.
|
|
254
|
+
* Always preserves the _id_ index.
|
|
255
|
+
*/
|
|
256
|
+
deleteUnusedIndexes;
|
|
251
257
|
/**
|
|
252
258
|
* @deprecated Use startConnection() instead. This property is not guaranteed to be resolved if the connection is not started.
|
|
253
259
|
* When using async calls startConnection or connectionPromise is no longer needed. Orion will automatically start the connection if it is not already started.
|
|
@@ -1140,12 +1146,82 @@ var generateId_default = getIdGenerator;
|
|
|
1140
1146
|
|
|
1141
1147
|
// src/createCollection/createIndexes.ts
|
|
1142
1148
|
import { MongoExpiredSessionError, MongoNotConnectedError } from "mongodb";
|
|
1149
|
+
import { logger as logger3 } from "@orion-js/logger";
|
|
1150
|
+
|
|
1151
|
+
// src/createCollection/deleteUnusedIndexes.ts
|
|
1143
1152
|
import { logger as logger2 } from "@orion-js/logger";
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1153
|
+
|
|
1154
|
+
// src/createCollection/getIndexOptions.ts
|
|
1155
|
+
function getIndexOptions(indexDef) {
|
|
1156
|
+
const { keys: _keys, options: deprecatedOptions, ...flatOptions } = indexDef;
|
|
1157
|
+
if (deprecatedOptions) {
|
|
1158
|
+
return { ...deprecatedOptions, ...flatOptions };
|
|
1159
|
+
}
|
|
1160
|
+
return Object.keys(flatOptions).length > 0 ? flatOptions : void 0;
|
|
1161
|
+
}
|
|
1162
|
+
function getIndexName(indexDef) {
|
|
1163
|
+
var _a;
|
|
1164
|
+
if (indexDef.name) {
|
|
1165
|
+
return indexDef.name;
|
|
1166
|
+
}
|
|
1167
|
+
return (_a = indexDef.options) == null ? void 0 : _a.name;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
// src/createCollection/deleteUnusedIndexes.ts
|
|
1171
|
+
function keysMatch(definitionKeys, currentIndexKey) {
|
|
1172
|
+
const defEntries = Object.entries(definitionKeys);
|
|
1173
|
+
const curEntries = Object.entries(currentIndexKey);
|
|
1174
|
+
if (defEntries.length !== curEntries.length) return false;
|
|
1175
|
+
for (let i = 0; i < defEntries.length; i++) {
|
|
1176
|
+
const [defKey, defValue] = defEntries[i];
|
|
1177
|
+
const [curKey, curValue] = curEntries[i];
|
|
1178
|
+
if (defKey !== curKey || defValue !== curValue) return false;
|
|
1179
|
+
}
|
|
1180
|
+
return true;
|
|
1181
|
+
}
|
|
1182
|
+
function isIndexDefined(definedIndexes, currentIndex) {
|
|
1183
|
+
return definedIndexes.some((defIndex) => {
|
|
1184
|
+
const customName = getIndexName(defIndex);
|
|
1185
|
+
if (customName && customName === currentIndex.name) return true;
|
|
1186
|
+
return keysMatch(defIndex.keys, currentIndex.key);
|
|
1187
|
+
});
|
|
1148
1188
|
}
|
|
1189
|
+
async function deleteUnusedIndexes(collection) {
|
|
1190
|
+
await collection.connectionPromise;
|
|
1191
|
+
const result = {
|
|
1192
|
+
deletedIndexes: [],
|
|
1193
|
+
collectionName: collection.name
|
|
1194
|
+
};
|
|
1195
|
+
let currentIndexes = [];
|
|
1196
|
+
try {
|
|
1197
|
+
currentIndexes = await collection.rawCollection.indexes();
|
|
1198
|
+
} catch (error) {
|
|
1199
|
+
if (error.codeName === "NamespaceNotFound") {
|
|
1200
|
+
return result;
|
|
1201
|
+
}
|
|
1202
|
+
throw error;
|
|
1203
|
+
}
|
|
1204
|
+
if (!collection.indexes || collection.indexes.length === 0) {
|
|
1205
|
+
return result;
|
|
1206
|
+
}
|
|
1207
|
+
const unusedIndexes = currentIndexes.filter(
|
|
1208
|
+
(index) => index.name !== "_id_" && !isIndexDefined(collection.indexes, index)
|
|
1209
|
+
);
|
|
1210
|
+
for (const index of unusedIndexes) {
|
|
1211
|
+
try {
|
|
1212
|
+
await collection.rawCollection.dropIndex(index.name);
|
|
1213
|
+
result.deletedIndexes.push(index.name);
|
|
1214
|
+
logger2.info(`Deleted unused index "${index.name}" from collection "${collection.name}"`);
|
|
1215
|
+
} catch (error) {
|
|
1216
|
+
logger2.error(`Failed to delete index "${index.name}" from collection "${collection.name}"`, {
|
|
1217
|
+
error
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return result;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// src/createCollection/createIndexes.ts
|
|
1149
1225
|
async function checkIndexes(collection) {
|
|
1150
1226
|
await collection.connectionPromise;
|
|
1151
1227
|
let currentIndexes = [];
|
|
@@ -1153,13 +1229,17 @@ async function checkIndexes(collection) {
|
|
|
1153
1229
|
currentIndexes = await collection.rawCollection.indexes();
|
|
1154
1230
|
} catch (error) {
|
|
1155
1231
|
if (error.codeName !== "NamespaceNotFound") throw error;
|
|
1232
|
+
return;
|
|
1156
1233
|
}
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
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`
|
|
1163
1243
|
);
|
|
1164
1244
|
}
|
|
1165
1245
|
}
|
|
@@ -1168,7 +1248,9 @@ async function loadIndexes(collection) {
|
|
|
1168
1248
|
if (!collection.indexes.length) return;
|
|
1169
1249
|
await collection.connectionPromise;
|
|
1170
1250
|
const results = Promise.all(
|
|
1171
|
-
collection.indexes.map(async (
|
|
1251
|
+
collection.indexes.map(async (indexDef) => {
|
|
1252
|
+
const { keys } = indexDef;
|
|
1253
|
+
const options = getIndexOptions(indexDef);
|
|
1172
1254
|
try {
|
|
1173
1255
|
return await collection.rawCollection.createIndex(keys, options);
|
|
1174
1256
|
} catch (error) {
|
|
@@ -1198,6 +1280,43 @@ async function loadIndexes(collection) {
|
|
|
1198
1280
|
return results;
|
|
1199
1281
|
}
|
|
1200
1282
|
|
|
1283
|
+
// src/createCollection/collectionsRegistry.ts
|
|
1284
|
+
import { logger as logger4 } from "@orion-js/logger";
|
|
1285
|
+
var collectionsRegistry = /* @__PURE__ */ new Map();
|
|
1286
|
+
function registerCollection(connectionName, collection) {
|
|
1287
|
+
if (!collectionsRegistry.has(connectionName)) {
|
|
1288
|
+
collectionsRegistry.set(connectionName, /* @__PURE__ */ new Map());
|
|
1289
|
+
}
|
|
1290
|
+
collectionsRegistry.get(connectionName).set(collection.name, collection);
|
|
1291
|
+
}
|
|
1292
|
+
function getRegisteredCollections(connectionName = "main") {
|
|
1293
|
+
const connectionCollections = collectionsRegistry.get(connectionName);
|
|
1294
|
+
if (!connectionCollections) {
|
|
1295
|
+
return [];
|
|
1296
|
+
}
|
|
1297
|
+
return Array.from(connectionCollections.values());
|
|
1298
|
+
}
|
|
1299
|
+
async function deleteAllUnusedIndexes(connectionName = "main") {
|
|
1300
|
+
const collections = getRegisteredCollections(connectionName);
|
|
1301
|
+
if (collections.length === 0) {
|
|
1302
|
+
logger4.warn(`No collections registered for connection "${connectionName}"`);
|
|
1303
|
+
return [];
|
|
1304
|
+
}
|
|
1305
|
+
logger4.info(
|
|
1306
|
+
`Deleting unused indexes from ${collections.length} collections on connection "${connectionName}"`
|
|
1307
|
+
);
|
|
1308
|
+
const results = [];
|
|
1309
|
+
for (const collection of collections) {
|
|
1310
|
+
const result = await collection.deleteUnusedIndexes();
|
|
1311
|
+
if (result.deletedIndexes.length > 0) {
|
|
1312
|
+
results.push(result);
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
const totalDeleted = results.reduce((sum, r) => sum + r.deletedIndexes.length, 0);
|
|
1316
|
+
logger4.info(`Deleted ${totalDeleted} unused indexes from ${results.length} collections`);
|
|
1317
|
+
return results;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1201
1320
|
// src/createCollection/getSchemaAndModel.ts
|
|
1202
1321
|
import { clone as clone3 } from "@orion-js/helpers";
|
|
1203
1322
|
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
@@ -1230,7 +1349,7 @@ function getSchema(options) {
|
|
|
1230
1349
|
}
|
|
1231
1350
|
|
|
1232
1351
|
// src/createCollection/wrapMethods.ts
|
|
1233
|
-
import { logger as
|
|
1352
|
+
import { logger as logger5 } from "@orion-js/logger";
|
|
1234
1353
|
function wrapMethods(collection) {
|
|
1235
1354
|
const methodsWithPromises = [
|
|
1236
1355
|
"findOne",
|
|
@@ -1266,7 +1385,7 @@ function wrapMethods(collection) {
|
|
|
1266
1385
|
collection[methodName2] = (...args) => {
|
|
1267
1386
|
collection.startConnection();
|
|
1268
1387
|
if (!collection.rawCollection) {
|
|
1269
|
-
|
|
1388
|
+
logger5.error("Method called before connection was initialized", {
|
|
1270
1389
|
collectionName: collection.name,
|
|
1271
1390
|
connectionName: collection.connectionName,
|
|
1272
1391
|
methodName: methodName2
|
|
@@ -1367,11 +1486,16 @@ function createCollection(options) {
|
|
|
1367
1486
|
return createIndexPromise;
|
|
1368
1487
|
};
|
|
1369
1488
|
mainCollection.createIndexes = createIndexes;
|
|
1489
|
+
mainCollection.deleteUnusedIndexes = async () => {
|
|
1490
|
+
await orionConnection.startConnection();
|
|
1491
|
+
return deleteUnusedIndexes(mainCollection);
|
|
1492
|
+
};
|
|
1370
1493
|
if (!process.env.DONT_CREATE_INDEXES_AUTOMATICALLY) {
|
|
1371
1494
|
createIndexes();
|
|
1372
1495
|
}
|
|
1373
1496
|
wrapMethods(mainCollection);
|
|
1374
1497
|
wrapMethods(encryptedCollection);
|
|
1498
|
+
registerCollection(connectionName, mainCollection);
|
|
1375
1499
|
return mainCollection;
|
|
1376
1500
|
}
|
|
1377
1501
|
|
|
@@ -1402,7 +1526,7 @@ function MongoCollection(options) {
|
|
|
1402
1526
|
}
|
|
1403
1527
|
|
|
1404
1528
|
// src/encrypted/getOrCreateEncryptionKey.ts
|
|
1405
|
-
import { logger as
|
|
1529
|
+
import { logger as logger6 } from "@orion-js/logger";
|
|
1406
1530
|
import { ClientEncryption, MongoClient as MongoClient2 } from "mongodb";
|
|
1407
1531
|
async function getOrCreateEncryptionKey({
|
|
1408
1532
|
keyAltName,
|
|
@@ -1414,7 +1538,7 @@ async function getOrCreateEncryptionKey({
|
|
|
1414
1538
|
kmsProviders
|
|
1415
1539
|
}) {
|
|
1416
1540
|
const keyVaultNamespace = `${keyVaultDatabase}.${keyVaultCollection}`;
|
|
1417
|
-
|
|
1541
|
+
logger6.info("Connecting to database to get or create the encryption key", {
|
|
1418
1542
|
keyVaultNamespace,
|
|
1419
1543
|
keyAltName
|
|
1420
1544
|
});
|
|
@@ -1432,14 +1556,14 @@ async function getOrCreateEncryptionKey({
|
|
|
1432
1556
|
});
|
|
1433
1557
|
const key = await clientEncryption.getKeyByAltName(keyAltName);
|
|
1434
1558
|
if (key) {
|
|
1435
|
-
|
|
1559
|
+
logger6.info("Key found on the key vault", {
|
|
1436
1560
|
keyVaultNamespace,
|
|
1437
1561
|
keyAltName,
|
|
1438
1562
|
UUID: key._id
|
|
1439
1563
|
});
|
|
1440
1564
|
return { key: key._id, keyVaultNamespace };
|
|
1441
1565
|
}
|
|
1442
|
-
|
|
1566
|
+
logger6.info("Key not found on the key vault, creating a new one", {
|
|
1443
1567
|
keyVaultNamespace,
|
|
1444
1568
|
keyAltName
|
|
1445
1569
|
});
|
|
@@ -1447,7 +1571,7 @@ async function getOrCreateEncryptionKey({
|
|
|
1447
1571
|
keyAltNames: [keyAltName],
|
|
1448
1572
|
...masterKey ? { masterKey } : {}
|
|
1449
1573
|
});
|
|
1450
|
-
|
|
1574
|
+
logger6.info("New encryption key created", {
|
|
1451
1575
|
keyVaultNamespace,
|
|
1452
1576
|
keyAltName,
|
|
1453
1577
|
UUID: newKey
|
|
@@ -1466,13 +1590,17 @@ export {
|
|
|
1466
1590
|
MongoDB,
|
|
1467
1591
|
Repository,
|
|
1468
1592
|
allConnectionPromises,
|
|
1593
|
+
collectionsRegistry,
|
|
1469
1594
|
configureConnection,
|
|
1470
1595
|
connections,
|
|
1471
1596
|
createCollection,
|
|
1472
1597
|
createIndexesPromises,
|
|
1598
|
+
deleteAllUnusedIndexes,
|
|
1473
1599
|
getDBName,
|
|
1474
1600
|
getMongoConnection,
|
|
1475
1601
|
getOrCreateEncryptionKey,
|
|
1602
|
+
getRegisteredCollections,
|
|
1603
|
+
registerCollection,
|
|
1476
1604
|
typedId
|
|
1477
1605
|
};
|
|
1478
1606
|
//# sourceMappingURL=index.js.map
|