Skip to content

Commit ed24e88

Browse files
authored
feat(playground): Add clusteredIndex option to createCollection playground template VSCODE-330 (#409)
1 parent ed67120 commit ed24e88

4 files changed

+14
-210
lines changed

src/editors/playgroundController.ts

-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode';
22
import vm from 'vm';
3-
import semver from 'semver';
43

54
import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
65
import CodeActionProvider from './codeActionProvider';
@@ -15,7 +14,6 @@ import { LanguageServerController } from '../language';
1514
import { OutputChannel, ProgressLocation, TextEditor } from 'vscode';
1615
import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate';
1716
import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate';
18-
import playgroundCreateCollectionWithTSTemplate from '../templates/playgroundCreateCollectionWithTSTemplate';
1917
import {
2018
PlaygroundResult,
2119
ShellExecuteAllResult,
@@ -35,16 +33,6 @@ import TelemetryService from '../telemetry/telemetryService';
3533
const log = createLogger('playground controller');
3634
const transpiler = require('bson-transpilers');
3735

38-
const MIN_TIME_SERIES_SERVER_VERSION = '5.0.0-alpha0';
39-
40-
const hasTimeSeriesSupport = (serverVersion) => {
41-
try {
42-
return semver.gte(serverVersion, MIN_TIME_SERIES_SERVER_VERSION);
43-
} catch (e) {
44-
return true;
45-
}
46-
};
47-
4836
interface ToCompile {
4937
filter?: string;
5038
aggregation?: string;
@@ -241,17 +229,8 @@ export default class PlaygroundController {
241229
async createPlaygroundForCreateCollection(
242230
element: ConnectionTreeItem | DatabaseTreeItem
243231
): Promise<boolean> {
244-
const dataService = this._connectionController.getActiveDataService();
245232
let content = playgroundCreateCollectionTemplate;
246233

247-
if (dataService) {
248-
const instance = await dataService.instance();
249-
250-
if (hasTimeSeriesSupport(instance.build.version)) {
251-
content = playgroundCreateCollectionWithTSTemplate;
252-
}
253-
}
254-
255234
element.cacheIsUpToDate = false;
256235

257236
if (element instanceof DatabaseTreeItem) {

src/templates/playgroundCreateCollectionTemplate.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ db.createCollection(collection);
2525
viewOn: <string>,
2626
pipeline: <pipeline>,
2727
collation: <document>,
28-
writeConcern: <document>
28+
writeConcern: <document>,
29+
timeseries: { // Added in MongoDB 5.0
30+
timeField: <string>, // required for time series collections
31+
metaField: <string>,
32+
granularity: <string>
33+
},
34+
expireAfterSeconds: <number>,
35+
clusteredIndex: <document>, // Added in MongoDB 5.3
2936
}
3037
)*/
38+
39+
// More information on the \`createCollection\` command can be found at:
40+
// https://www.mongodb.com/docs/manual/reference/method/db.createCollection/
3141
`;
3242

3343
export default template;

src/templates/playgroundCreateCollectionWithTSTemplate.ts

-45
This file was deleted.

src/test/suite/mdbExtensionController.test.ts

+3-143
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ suite('MDBExtensionController Test Suite', function () {
582582
.then(done, done);
583583
});
584584

585-
test('mdb.addDatabase should create a MongoDB playground with create collection template without time-series', async () => {
585+
test('mdb.addDatabase should create a MongoDB playground with create collection template', async () => {
586586
const mockTreeItem = new ConnectionTreeItem(
587587
'tasty_sandwhich',
588588
vscode.TreeItemCollapsibleState.None,
@@ -592,74 +592,6 @@ suite('MDBExtensionController Test Suite', function () {
592592
{}
593593
);
594594

595-
const mockGetActiveDataService: any = sinon.fake.returns({
596-
instance: () => Promise.resolve({
597-
dataLake: {},
598-
build: { version: '4.9.7' },
599-
genuineMongoDB: {},
600-
host: {}
601-
}),
602-
createCollection: (namespace, options, callback) => {
603-
callback(null);
604-
}
605-
});
606-
sinon.replace(
607-
mdbTestExtension.testExtensionController._connectionController,
608-
'getActiveDataService',
609-
mockGetActiveDataService
610-
);
611-
const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich');
612-
sinon.replace(
613-
mdbTestExtension.testExtensionController._connectionController,
614-
'getActiveConnectionId',
615-
mockActiveConnectionId
616-
);
617-
618-
const mockOpenTextDocument: any = sinon.fake.resolves('untitled');
619-
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);
620-
621-
const mockShowTextDocument: any = sinon.fake();
622-
sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument);
623-
624-
await vscode.commands.executeCommand('mdb.addDatabase', mockTreeItem);
625-
626-
assert(mockOpenTextDocument.firstArg.language === 'mongodb');
627-
assert(
628-
mockOpenTextDocument.firstArg.content.includes(
629-
'// Create a new database.'
630-
)
631-
);
632-
assert(mockOpenTextDocument.firstArg.content.includes('NEW_DATABASE_NAME'));
633-
assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME'));
634-
assert(!mockOpenTextDocument.firstArg.content.includes('time-series'));
635-
});
636-
637-
test('mdb.addDatabase should create a MongoDB playground with create collection template with time-series', async () => {
638-
const mockTreeItem = new ConnectionTreeItem(
639-
'tasty_sandwhich',
640-
vscode.TreeItemCollapsibleState.None,
641-
false,
642-
mdbTestExtension.testExtensionController._connectionController,
643-
false,
644-
{}
645-
);
646-
647-
const mockGetActiveDataService: any = sinon.fake.returns({
648-
instance: () => Promise.resolve({
649-
dataLake: {},
650-
build: { version: '5.0.1' },
651-
genuineMongoDB: {},
652-
host: {}
653-
}),
654-
createCollection: (namespace, options, callback) => {
655-
callback(null);
656-
}
657-
});
658-
sinon.replace(
659-
mdbTestExtension.testExtensionController._connectionController,
660-
'getActiveDataService',
661-
mockGetActiveDataService
662-
);
663595
const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich');
664596
sinon.replace(
665597
mdbTestExtension.testExtensionController._connectionController,
@@ -683,7 +615,6 @@ suite('MDBExtensionController Test Suite', function () {
683615
);
684616
assert(mockOpenTextDocument.firstArg.content.includes('NEW_DATABASE_NAME'));
685617
assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME'));
686-
assert(mockOpenTextDocument.firstArg.content.includes('time-series'));
687618
});
688619

689620
test('mdb.addDatabase command fails when disconnecting', (done) => {
@@ -782,31 +713,15 @@ suite('MDBExtensionController Test Suite', function () {
782713
.then(done, done);
783714
});
784715

785-
test('mdb.addCollection should create a MongoDB playground with create collection template without time-series', async () => {
786-
const mockGetActiveDataService: any = sinon.fake.returns({
787-
instance: () => Promise.resolve({
788-
dataLake: {},
789-
build: { version: '4.9.7' },
790-
genuineMongoDB: {},
791-
host: {}
792-
}),
793-
createCollection: (namespace, options, callback) => {
794-
callback(null);
795-
}
796-
});
716+
test('mdb.addCollection should create a MongoDB playground with create collection template', async () => {
797717
const mockTreeItem = new DatabaseTreeItem(
798718
'iceCreamDB',
799-
mockGetActiveDataService,
719+
{},
800720
false,
801721
false,
802722
{}
803723
);
804724

805-
sinon.replace(
806-
mdbTestExtension.testExtensionController._connectionController,
807-
'getActiveDataService',
808-
mockGetActiveDataService
809-
);
810725
const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich');
811726
sinon.replace(
812727
mdbTestExtension.testExtensionController._connectionController,
@@ -833,61 +748,6 @@ suite('MDBExtensionController Test Suite', function () {
833748
assert(!mockOpenTextDocument.firstArg.content.includes('time-series'));
834749
});
835750

836-
test('mdb.addCollection should create a MongoDB playground with create collection template with time-series', async () => {
837-
const mockTreeItem = new DatabaseTreeItem(
838-
'iceCreamDB',
839-
{
840-
createCollection: (namespace, options, callback): void => {
841-
callback(null);
842-
}
843-
},
844-
false,
845-
false,
846-
{}
847-
);
848-
849-
const mockGetActiveDataService: any = sinon.fake.returns({
850-
instance: () => Promise.resolve({
851-
dataLake: {},
852-
build: { version: '5.0.1' },
853-
genuineMongoDB: {},
854-
host: {}
855-
}),
856-
createCollection: (namespace, options, callback) => {
857-
callback(null);
858-
}
859-
});
860-
sinon.replace(
861-
mdbTestExtension.testExtensionController._connectionController,
862-
'getActiveDataService',
863-
mockGetActiveDataService
864-
);
865-
const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich');
866-
sinon.replace(
867-
mdbTestExtension.testExtensionController._connectionController,
868-
'getActiveConnectionId',
869-
mockActiveConnectionId
870-
);
871-
872-
const mockOpenTextDocument: any = sinon.fake.resolves('untitled');
873-
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);
874-
875-
const mockShowTextDocument: any = sinon.fake();
876-
sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument);
877-
878-
await vscode.commands.executeCommand('mdb.addCollection', mockTreeItem);
879-
880-
assert(mockOpenTextDocument.firstArg.language === 'mongodb');
881-
assert(
882-
mockOpenTextDocument.firstArg.content.includes(
883-
'// The current database to use.'
884-
)
885-
);
886-
assert(mockOpenTextDocument.firstArg.content.includes('iceCreamDB'));
887-
assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME'));
888-
assert(mockOpenTextDocument.firstArg.content.includes('time-series'));
889-
});
890-
891751
test('mdb.addCollection command fails when disconnecting', (done) => {
892752
const mockTreeItem = new DatabaseTreeItem(
893753
'iceCreamDB',

0 commit comments

Comments
 (0)