1
1
import type { Document } from '../bson' ;
2
2
import type { Db } from '../db' ;
3
+ import { MONGODB_ERROR_CODES , MongoServerError } from '../error' ;
3
4
import type { Server } from '../sdam/server' ;
4
5
import type { ClientSession } from '../sessions' ;
5
6
import type { Callback } from '../utils' ;
@@ -14,10 +15,14 @@ export interface DropCollectionOptions extends CommandOperationOptions {
14
15
15
16
/** @internal */
16
17
export class DropCollectionOperation extends CommandOperation < boolean > {
18
+ override options : DropCollectionOptions ;
19
+ db : Db ;
17
20
name : string ;
18
21
19
22
constructor ( db : Db , name : string , options : DropCollectionOptions = { } ) {
20
23
super ( db , options ) ;
24
+ this . db = db ;
25
+ this . options = options ;
21
26
this . name = name ;
22
27
}
23
28
@@ -27,6 +32,45 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
27
32
callback : Callback < boolean >
28
33
) : void {
29
34
( async ( ) => {
35
+ const db = this . db ;
36
+ const options = this . options ;
37
+ const name = this . name ;
38
+
39
+ const encryptedFieldsMap = db . s . client . options . autoEncryption ?. encryptedFieldsMap ;
40
+ let encryptedFields : Document | undefined =
41
+ options . encryptedFields ?? encryptedFieldsMap ?. [ `${ db . databaseName } .${ name } ` ] ;
42
+
43
+ if ( ! encryptedFields && encryptedFieldsMap ) {
44
+ // If the MongoClient was configured with an encryptedFieldsMap,
45
+ // and no encryptedFields config was available in it or explicitly
46
+ // passed as an argument, the spec tells us to look one up using
47
+ // listCollections().
48
+ const listCollectionsResult = await db
49
+ . listCollections ( { name } , { nameOnly : false } )
50
+ . toArray ( ) ;
51
+ encryptedFields = listCollectionsResult ?. [ 0 ] ?. options ?. encryptedFields ;
52
+ }
53
+
54
+ if ( encryptedFields ) {
55
+ const escCollection = encryptedFields . escCollection || `enxcol_.${ name } .esc` ;
56
+ const ecocCollection = encryptedFields . ecocCollection || `enxcol_.${ name } .ecoc` ;
57
+
58
+ for ( const collectionName of [ escCollection , ecocCollection ] ) {
59
+ // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
60
+ const dropOp = new DropCollectionOperation ( db , collectionName ) ;
61
+ try {
62
+ await dropOp . executeWithoutEncryptedFieldsCheck ( server , session ) ;
63
+ } catch ( err ) {
64
+ if (
65
+ ! ( err instanceof MongoServerError ) ||
66
+ err . code !== MONGODB_ERROR_CODES . NamespaceNotFound
67
+ ) {
68
+ throw err ;
69
+ }
70
+ }
71
+ }
72
+ }
73
+
30
74
return this . executeWithoutEncryptedFieldsCheck ( server , session ) ;
31
75
} ) ( ) . then (
32
76
result => callback ( undefined , result ) ,
0 commit comments