@@ -3,10 +3,11 @@ import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './
3
3
import { ChangeStream , type ChangeStreamDocument , type ChangeStreamOptions } from './change_stream' ;
4
4
import { Collection , type CollectionOptions } from './collection' ;
5
5
import * as CONSTANTS from './constants' ;
6
+ import { CursorTimeoutContext } from './cursor/abstract_cursor' ;
6
7
import { AggregationCursor } from './cursor/aggregation_cursor' ;
7
8
import { ListCollectionsCursor } from './cursor/list_collections_cursor' ;
8
9
import { RunCommandCursor , type RunCursorCommandOptions } from './cursor/run_command_cursor' ;
9
- import { MongoInvalidArgumentError } from './error' ;
10
+ import { MONGODB_ERROR_CODES , MongoInvalidArgumentError , MongoServerError } from './error' ;
10
11
import type { MongoClient , PkFactory } from './mongo_client' ;
11
12
import type { Abortable , TODO_NODE_3286 } from './mongo_types' ;
12
13
import type { AggregateOptions } from './operations/aggregate' ;
@@ -42,6 +43,7 @@ import {
42
43
import { DbStatsOperation , type DbStatsOptions } from './operations/stats' ;
43
44
import { ReadConcern } from './read_concern' ;
44
45
import { ReadPreference , type ReadPreferenceLike } from './read_preference' ;
46
+ import { TimeoutContext } from './timeout' ;
45
47
import { DEFAULT_PK_FACTORY , filterOptions , MongoDBNamespace , resolveOptions } from './utils' ;
46
48
import { WriteConcern , type WriteConcernOptions } from './write_concern' ;
47
49
@@ -410,9 +412,60 @@ export class Db {
410
412
* @param options - Optional settings for the command
411
413
*/
412
414
async dropCollection ( name : string , options ?: DropCollectionOptions ) : Promise < boolean > {
415
+ options = resolveOptions ( this , options ) ;
416
+ options . session ??= this . client . startSession ( { owner : Symbol ( ) , explicit : false } ) ;
417
+
418
+ const timeoutContext = TimeoutContext . create ( {
419
+ session : options . session ,
420
+ serverSelectionTimeoutMS : this . client . s . options . serverSelectionTimeoutMS ,
421
+ waitQueueTimeoutMS : this . client . s . options . waitQueueTimeoutMS ,
422
+ timeoutMS : options . timeoutMS
423
+ } ) ;
424
+
425
+ const encryptedFieldsMap = this . client . s . options . autoEncryption ?. encryptedFieldsMap ;
426
+ let encryptedFields : Document | undefined =
427
+ options . encryptedFields ?? encryptedFieldsMap ?. [ `${ this . databaseName } .${ name } ` ] ;
428
+
429
+ if ( ! encryptedFields && encryptedFieldsMap ) {
430
+ // If the MongoClient was configured with an encryptedFieldsMap,
431
+ // and no encryptedFields config was available in it or explicitly
432
+ // passed as an argument, the spec tells us to look one up using
433
+ // listCollections().
434
+ const listCollectionsResult = await this . listCollections (
435
+ { name } ,
436
+ {
437
+ nameOnly : false ,
438
+ session : options . session ,
439
+ timeoutContext : new CursorTimeoutContext ( timeoutContext , Symbol ( ) )
440
+ }
441
+ ) . toArray ( ) ;
442
+ encryptedFields = listCollectionsResult ?. [ 0 ] ?. options ?. encryptedFields ;
443
+ }
444
+
445
+ if ( encryptedFields ) {
446
+ const escCollection = encryptedFields . escCollection || `enxcol_.${ name } .esc` ;
447
+ const ecocCollection = encryptedFields . ecocCollection || `enxcol_.${ name } .ecoc` ;
448
+
449
+ for ( const collectionName of [ escCollection , ecocCollection ] ) {
450
+ // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
451
+ const dropOp = new DropCollectionOperation ( this , collectionName , options ) ;
452
+ try {
453
+ await executeOperation ( this . client , dropOp , timeoutContext ) ;
454
+ } catch ( err ) {
455
+ if (
456
+ ! ( err instanceof MongoServerError ) ||
457
+ err . code !== MONGODB_ERROR_CODES . NamespaceNotFound
458
+ ) {
459
+ throw err ;
460
+ }
461
+ }
462
+ }
463
+ }
464
+
413
465
return await executeOperation (
414
466
this . client ,
415
- new DropCollectionOperation ( this , name , resolveOptions ( this , options ) )
467
+ new DropCollectionOperation ( this , name , options ) ,
468
+ timeoutContext
416
469
) ;
417
470
}
418
471
0 commit comments