Skip to content

Commit a49c127

Browse files
move drop logic into Db class
1 parent c536534 commit a49c127

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/collection.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
type DeleteResult
3838
} from './operations/delete';
3939
import { DistinctOperation, type DistinctOptions } from './operations/distinct';
40-
import { DropCollectionOperation, type DropCollectionOptions } from './operations/drop';
40+
import { type DropCollectionOptions } from './operations/drop';
4141
import {
4242
EstimatedDocumentCountOperation,
4343
type EstimatedDocumentCountOptions
@@ -523,10 +523,7 @@ export class Collection<TSchema extends Document = Document> {
523523
* @param options - Optional settings for the command
524524
*/
525525
async drop(options?: DropCollectionOptions): Promise<boolean> {
526-
return await executeOperation(
527-
this.client,
528-
new DropCollectionOperation(this.s.db, this.collectionName, options)
529-
);
526+
return await this.s.db.dropCollection(this.collectionName, options);
530527
}
531528

532529
/**

src/db.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './
33
import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream';
44
import { Collection, type CollectionOptions } from './collection';
55
import * as CONSTANTS from './constants';
6+
import { CursorTimeoutContext } from './cursor/abstract_cursor';
67
import { AggregationCursor } from './cursor/aggregation_cursor';
78
import { ListCollectionsCursor } from './cursor/list_collections_cursor';
89
import { RunCommandCursor, type RunCursorCommandOptions } from './cursor/run_command_cursor';
9-
import { MongoInvalidArgumentError } from './error';
10+
import { MONGODB_ERROR_CODES, MongoInvalidArgumentError, MongoServerError } from './error';
1011
import type { MongoClient, PkFactory } from './mongo_client';
1112
import type { Abortable, TODO_NODE_3286 } from './mongo_types';
1213
import type { AggregateOptions } from './operations/aggregate';
@@ -42,6 +43,7 @@ import {
4243
import { DbStatsOperation, type DbStatsOptions } from './operations/stats';
4344
import { ReadConcern } from './read_concern';
4445
import { ReadPreference, type ReadPreferenceLike } from './read_preference';
46+
import { TimeoutContext } from './timeout';
4547
import { DEFAULT_PK_FACTORY, filterOptions, MongoDBNamespace, resolveOptions } from './utils';
4648
import { WriteConcern, type WriteConcernOptions } from './write_concern';
4749

@@ -410,9 +412,60 @@ export class Db {
410412
* @param options - Optional settings for the command
411413
*/
412414
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+
413465
return await executeOperation(
414466
this.client,
415-
new DropCollectionOperation(this, name, resolveOptions(this, options))
467+
new DropCollectionOperation(this, name, options),
468+
timeoutContext
416469
);
417470
}
418471

0 commit comments

Comments
 (0)