Skip to content

Commit b74b53f

Browse files
committed
fix: only ecc collection affected
1 parent 497ff95 commit b74b53f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/operations/create_collection.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
137137
if (server.description.maxWireVersion < MIN_SUPPORTED_QE_WIRE_VERSION) {
138138
throw new MongoCompatibilityError(INVALID_QE_VERSION);
139139
}
140+
// Create auxilliary collections for queryable encryption support.
141+
const escCollection = encryptedFields.escCollection ?? `enxcol_.${name}.esc`;
142+
const ecocCollection = encryptedFields.ecocCollection ?? `enxcol_.${name}.ecoc`;
143+
144+
for (const collectionName of [escCollection, ecocCollection]) {
145+
const createOp = new CreateCollectionOperation(db, collectionName, {
146+
clusteredIndex: {
147+
key: { _id: 1 },
148+
unique: true
149+
}
150+
});
151+
await createOp.executeWithoutEncryptedFieldsCheck(server, session);
152+
}
153+
140154
if (!options.encryptedFields) {
141155
this.options = { ...this.options, encryptedFields };
142156
}

src/operations/drop.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Document } from '../bson';
22
import type { Db } from '../db';
3+
import { MONGODB_ERROR_CODES, MongoServerError } from '../error';
34
import type { Server } from '../sdam/server';
45
import type { ClientSession } from '../sessions';
56
import type { Callback } from '../utils';
@@ -14,10 +15,14 @@ export interface DropCollectionOptions extends CommandOperationOptions {
1415

1516
/** @internal */
1617
export class DropCollectionOperation extends CommandOperation<boolean> {
18+
override options: DropCollectionOptions;
19+
db: Db;
1720
name: string;
1821

1922
constructor(db: Db, name: string, options: DropCollectionOptions = {}) {
2023
super(db, options);
24+
this.db = db;
25+
this.options = options;
2126
this.name = name;
2227
}
2328

@@ -27,6 +32,45 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
2732
callback: Callback<boolean>
2833
): void {
2934
(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+
3074
return this.executeWithoutEncryptedFieldsCheck(server, session);
3175
})().then(
3276
result => callback(undefined, result),

0 commit comments

Comments
 (0)