Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/data-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"test-connectivity": "mocha ./src/connect.spec.ts",
"posttest": "mongodb-runner stop --port=27018",
"pretest-csfle": "mongodb-runner start --enterprise --mongodb-version \">=6.0.0-rc1\" --topology=replicaset --secondaries=0 --port=27018",
"test-csfle": "mocha ./src/csfle-collection-tracker.spec.ts",
"test-csfle": "mocha ./src/csfle-collection-tracker.spec.ts ./src/data-service.spec.ts",
"posttest-csfle": "mongodb-runner stop --enterprise --mongodb-version \">=6.0.0-rc1\" --topology=replicaset --secondaries=0 --port=27018",
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
"test-watch": "npm run test -- --watch",
Expand Down
71 changes: 68 additions & 3 deletions packages/data-service/src/data-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert from 'assert';
import type { Document } from 'bson';
import { ObjectId } from 'bson';
import { expect } from 'chai';
import type { Sort } from 'mongodb';
import { MongoClient } from 'mongodb';
import { MongoClient } from 'mongodb-fle';
import sinon from 'sinon';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -213,6 +214,14 @@ describe('DataService', function () {
await mongoClient.db(testDatabaseName).createCollection('bar');
});

afterEach(async function () {
try {
await mongoClient.db(testDatabaseName).collection('bar').drop();
} catch {
/* ignore ns not found */
}
});

it('drops a collection', function (done) {
dataService.dropCollection(`${testDatabaseName}.bar`, function (error) {
assert.equal(null, error);
Expand All @@ -225,6 +234,61 @@ describe('DataService', function () {
.catch(done);
});
});

it('drops a collection with fle2 options', async function () {
const buildInfo = await new Promise<Document>((resolve, reject) => {
dataService.command('admin', { buildInfo: 1 }, (error, result) => {
error ? reject(error) : resolve(result);
});
});
if (
(buildInfo.versionArray?.[0] ?? 0) <= 5 ||
dataService.currentTopologyType() === 'Single' ||
process.env.COMPASS_CSFLE_SUPPORT !== 'true'
) {
return this.skip(); // FLE2 requires 6.0+ replset
}

await mongoClient.db(testDatabaseName).createCollection('fle2', {
encryptedFields: {
escCollection: 'enxcol_.fle2.esc',
eccCollection: 'enxcol_.fle2.ecc',
ecocCollection: 'enxcol_.fle2.ecoc',
fields: [],
},
});

let items = (
await mongoClient
.db(testDatabaseName)
.listCollections({}, { nameOnly: true })
.toArray()
).map(({ name }) => name);
expect(items).to.include('fle2');
expect(items).to.include('enxcol_.fle2.esc');
expect(items).to.include('enxcol_.fle2.ecc');
expect(items).to.include('enxcol_.fle2.ecoc');

await new Promise<void>((resolve, reject) => {
dataService.dropCollection(
`${testDatabaseName}.fle2`,
function (error) {
error ? reject(error) : resolve();
}
);
});

items = (
await mongoClient
.db(testDatabaseName)
.listCollections({}, { nameOnly: true })
.toArray()
).map(({ name }) => name);
expect(items).to.not.include('fle2');
expect(items).to.not.include('enxcol_.fle2.esc');
expect(items).to.not.include('enxcol_.fle2.ecc');
expect(items).to.not.include('enxcol_.fle2.ecoc');
});
});

describe('#dropDatabase', function () {
Expand Down Expand Up @@ -1005,15 +1069,16 @@ describe('DataService', function () {
const topology = dataService.getLastSeenTopology();

expect(topology).to.not.be.null;
expect(topology!.servers.has('127.0.0.1:27018')).to.equal(true);
expect(topology!.servers.values().next().value.address).to.be.a(
'string'
);

expect(topology).to.deep.include({
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: 30,
stale: false,
type: 'Single',
});
});

Expand Down
31 changes: 24 additions & 7 deletions packages/data-service/src/data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
DeleteOptions,
DeleteResult,
Document,
DropCollectionOptions,
EstimatedDocumentCountOptions,
Filter,
FindCursor,
Expand Down Expand Up @@ -1382,14 +1383,30 @@ export class DataServiceImpl extends EventEmitter implements DataService {
'Running dropCollection',
{ ns }
);
this._collection(ns, 'CRUD').drop((error, result) => {
logop(error, result);
if (error) {
// @ts-expect-error Callback without result...
return callback(this._translateMessage(error));

const client = this._initializedClient('CRUD');
const db = client.db(this._databaseName(ns));
const collName = this._collectionName(ns);
const coll = db.collection(collName);

db.listCollections({ name: collName }, { nameOnly: false }).toArray(
(_errIgnore, result) => {
const options: DropCollectionOptions = {};
const encryptedFieldsInfo = result?.[0]?.options?.encryptedFields;
if (encryptedFieldsInfo) {
// @ts-expect-error next driver release has types
options.encryptedFields = encryptedFieldsInfo;
}
coll.drop(options, (error, result) => {
logop(error, result);
if (error) {
// @ts-expect-error Callback without result...
return callback(this._translateMessage(error));
}
callback(null, result!);
});
}
callback(null, result!);
});
);
}

dropDatabase(name: string, callback: Callback<boolean>): void {
Expand Down