Skip to content

Commit d818fed

Browse files
drop collections fix
1 parent 9174a2f commit d818fed

File tree

2 files changed

+78
-62
lines changed

2 files changed

+78
-62
lines changed

src/db.ts

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ 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';
76
import { AggregationCursor } from './cursor/aggregation_cursor';
87
import { ListCollectionsCursor } from './cursor/list_collections_cursor';
98
import { RunCommandCursor, type RunCursorCommandOptions } from './cursor/run_command_cursor';
10-
import { MONGODB_ERROR_CODES, MongoInvalidArgumentError, MongoServerError } from './error';
9+
import { MongoInvalidArgumentError } from './error';
1110
import type { MongoClient, PkFactory } from './mongo_client';
1211
import type { Abortable, TODO_NODE_3286 } from './mongo_types';
1312
import type { AggregateOptions } from './operations/aggregate';
@@ -16,8 +15,8 @@ import {
1615
type CreateCollectionOptions
1716
} from './operations/create_collection';
1817
import {
19-
DropCollectionOperation,
2018
type DropCollectionOptions,
19+
dropCollections,
2120
DropDatabaseOperation,
2221
type DropDatabaseOptions
2322
} from './operations/drop';
@@ -43,7 +42,6 @@ import {
4342
import { DbStatsOperation, type DbStatsOptions } from './operations/stats';
4443
import { ReadConcern } from './read_concern';
4544
import { ReadPreference, type ReadPreferenceLike } from './read_preference';
46-
import { TimeoutContext } from './timeout';
4745
import { DEFAULT_PK_FACTORY, filterOptions, MongoDBNamespace, resolveOptions } from './utils';
4846
import { WriteConcern, type WriteConcernOptions } from './write_concern';
4947

@@ -361,13 +359,13 @@ export class Db {
361359
): ListCollectionsCursor<CollectionInfo>;
362360
listCollections<
363361
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
364-
| Pick<CollectionInfo, 'name' | 'type'>
365-
| CollectionInfo
362+
| Pick<CollectionInfo, 'name' | 'type'>
363+
| CollectionInfo
366364
>(filter?: Document, options?: ListCollectionsOptions & Abortable): ListCollectionsCursor<T>;
367365
listCollections<
368366
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
369-
| Pick<CollectionInfo, 'name' | 'type'>
370-
| CollectionInfo
367+
| Pick<CollectionInfo, 'name' | 'type'>
368+
| CollectionInfo
371369
>(
372370
filter: Document = {},
373371
options: ListCollectionsOptions & Abortable = {}
@@ -412,61 +410,15 @@ export class Db {
412410
* @param options - Optional settings for the command
413411
*/
414412
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-
}
413+
options = resolveOptions(this, { ...options });
414+
if (options.session) {
415+
return await dropCollections(this, name, options);
463416
}
464417

465-
return await executeOperation(
466-
this.client,
467-
new DropCollectionOperation(this, name, options),
468-
timeoutContext
469-
);
418+
return await this.client.withSession({ explicit: false }, async session => {
419+
options.session = session;
420+
return await dropCollections(this, name, options);
421+
});
470422
}
471423

472424
/**

src/operations/drop.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { Document } from '../bson';
2+
import { CursorTimeoutContext } from '../cursor/abstract_cursor';
23
import type { Db } from '../db';
4+
import { MONGODB_ERROR_CODES, MongoServerError } from '../error';
35
import type { Server } from '../sdam/server';
46
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
7+
import { TimeoutContext } from '../timeout';
68
import { CommandOperation, type CommandOperationOptions } from './command';
9+
import { executeOperation } from './execute_operation';
710
import { Aspect, defineAspects } from './operation';
811

912
/** @public */
@@ -37,6 +40,67 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
3740
}
3841
}
3942

43+
export async function dropCollections(
44+
db: Db,
45+
name: string,
46+
options: DropCollectionOptions
47+
): Promise<boolean> {
48+
const timeoutContext = TimeoutContext.create({
49+
session: options.session,
50+
serverSelectionTimeoutMS: db.client.s.options.serverSelectionTimeoutMS,
51+
waitQueueTimeoutMS: db.client.s.options.waitQueueTimeoutMS,
52+
timeoutMS: options.timeoutMS
53+
});
54+
55+
const encryptedFieldsMap = db.client.s.options.autoEncryption?.encryptedFieldsMap;
56+
let encryptedFields: Document | undefined =
57+
options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
58+
59+
if (!encryptedFields && encryptedFieldsMap) {
60+
// If the MongoClient was configured with an encryptedFieldsMap,
61+
// and no encryptedFields config was available in it or explicitly
62+
// passed as an argument, the spec tells us to look one up using
63+
// listCollections().
64+
const listCollectionsResult = await db
65+
.listCollections(
66+
{ name },
67+
{
68+
nameOnly: false,
69+
session: options.session,
70+
timeoutContext: new CursorTimeoutContext(timeoutContext, Symbol())
71+
}
72+
)
73+
.toArray();
74+
encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
75+
}
76+
77+
if (encryptedFields) {
78+
const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
79+
const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
80+
81+
for (const collectionName of [escCollection, ecocCollection]) {
82+
// Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
83+
const dropOp = new DropCollectionOperation(db, collectionName, options);
84+
try {
85+
await executeOperation(db.client, dropOp, timeoutContext);
86+
} catch (err) {
87+
if (
88+
!(err instanceof MongoServerError) ||
89+
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound
90+
) {
91+
throw err;
92+
}
93+
}
94+
}
95+
}
96+
97+
return await executeOperation(
98+
db.client,
99+
new DropCollectionOperation(db, name, options),
100+
timeoutContext
101+
);
102+
}
103+
40104
/** @public */
41105
export type DropDatabaseOptions = CommandOperationOptions;
42106

0 commit comments

Comments
 (0)