Skip to content

Commit 8c54b4e

Browse files
move drop logic into Db class
1 parent acd8625 commit 8c54b4e

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
@@ -33,7 +33,7 @@ import {
3333
type DeleteResult
3434
} from './operations/delete';
3535
import { DistinctOperation, type DistinctOptions } from './operations/distinct';
36-
import { DropCollectionOperation, type DropCollectionOptions } from './operations/drop';
36+
import { type DropCollectionOptions } from './operations/drop';
3737
import {
3838
EstimatedDocumentCountOperation,
3939
type EstimatedDocumentCountOptions
@@ -491,10 +491,7 @@ export class Collection<TSchema extends Document = Document> {
491491
* @param options - Optional settings for the command
492492
*/
493493
async drop(options?: DropCollectionOptions): Promise<boolean> {
494-
return await executeOperation(
495-
this.client,
496-
new DropCollectionOperation(this.s.db, this.collectionName, options)
497-
);
494+
return await this.s.db.dropCollection(this.collectionName, options);
498495
}
499496

500497
/**

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';
@@ -43,6 +44,7 @@ import {
4344
import { DbStatsOperation, type DbStatsOptions } from './operations/stats';
4445
import { ReadConcern } from './read_concern';
4546
import { ReadPreference, type ReadPreferenceLike } from './read_preference';
47+
import { TimeoutContext } from './timeout';
4648
import { DEFAULT_PK_FACTORY, filterOptions, MongoDBNamespace, resolveOptions } from './utils';
4749
import { WriteConcern, type WriteConcernOptions } from './write_concern';
4850

@@ -411,9 +413,60 @@ export class Db {
411413
* @param options - Optional settings for the command
412414
*/
413415
async dropCollection(name: string, options?: DropCollectionOptions): Promise<boolean> {
416+
options = resolveOptions(this, options);
417+
options.session ??= this.client.startSession({ owner: Symbol(), explicit: false });
418+
419+
const timeoutContext = TimeoutContext.create({
420+
session: options.session,
421+
serverSelectionTimeoutMS: this.client.s.options.serverSelectionTimeoutMS,
422+
waitQueueTimeoutMS: this.client.s.options.waitQueueTimeoutMS,
423+
timeoutMS: options.timeoutMS
424+
});
425+
426+
const encryptedFieldsMap = this.client.s.options.autoEncryption?.encryptedFieldsMap;
427+
let encryptedFields: Document | undefined =
428+
options.encryptedFields ?? encryptedFieldsMap?.[`${this.databaseName}.${name}`];
429+
430+
if (!encryptedFields && encryptedFieldsMap) {
431+
// If the MongoClient was configured with an encryptedFieldsMap,
432+
// and no encryptedFields config was available in it or explicitly
433+
// passed as an argument, the spec tells us to look one up using
434+
// listCollections().
435+
const listCollectionsResult = await this.listCollections(
436+
{ name },
437+
{
438+
nameOnly: false,
439+
session: options.session,
440+
timeoutContext: new CursorTimeoutContext(timeoutContext, Symbol())
441+
}
442+
).toArray();
443+
encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
444+
}
445+
446+
if (encryptedFields) {
447+
const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
448+
const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
449+
450+
for (const collectionName of [escCollection, ecocCollection]) {
451+
// Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
452+
const dropOp = new DropCollectionOperation(this, collectionName, options);
453+
try {
454+
await executeOperation(this.client, dropOp, timeoutContext);
455+
} catch (err) {
456+
if (
457+
!(err instanceof MongoServerError) ||
458+
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound
459+
) {
460+
throw err;
461+
}
462+
}
463+
}
464+
}
465+
414466
return await executeOperation(
415467
this.client,
416-
new DropCollectionOperation(this, name, resolveOptions(this, options))
468+
new DropCollectionOperation(this, name, options),
469+
timeoutContext
417470
);
418471
}
419472

0 commit comments

Comments
 (0)