Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree-explorer): add insert document context menu action VSCODE-367 #469

Merged
merged 2 commits into from
Feb 15, 2023
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
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@
"command": "mdb.refreshCollection",
"title": "Refresh"
},
{
"command": "mdb.insertDocumentFromTreeView",
"title": "Insert Document..."
},
{
"command": "mdb.refreshSchema",
"title": "Refresh"
Expand Down Expand Up @@ -559,10 +563,15 @@
"group": "2@1"
},
{
"command": "mdb.dropCollection",
"command": "mdb.insertDocumentFromTreeView",
"when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem",
"group": "3@1"
},
{
"command": "mdb.dropCollection",
"when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem",
"group": "4@1"
},
{
"command": "mdb.searchForDocuments",
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem",
Expand All @@ -575,17 +584,24 @@
},
{
"command": "mdb.viewCollectionDocuments",
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem"
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem",
"group": "1@1"
},
{
"command": "mdb.refreshDocumentList",
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem"
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem",
"group": "1@2"
},
{
"command": "mdb.searchForDocuments",
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem",
"group": "2@1"
},
{
"command": "mdb.insertDocumentFromTreeView",
"when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem",
"group": "3@1"
},
{
"command": "mdb.refreshSchema",
"when": "view == mongoDBConnectionExplorer && viewItem == schemaTreeItem"
Expand Down Expand Up @@ -760,6 +776,10 @@
"command": "mdb.refreshDocumentList",
"when": "false"
},
{
"command": "mdb.insertDocumentFromTreeView",
"when": "false"
},
{
"command": "mdb.copyCollectionName",
"when": "false"
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum EXTENSION_COMMANDS {
MDB_VIEW_COLLECTION_DOCUMENTS = 'mdb.viewCollectionDocuments',
MDB_REFRESH_COLLECTION = 'mdb.refreshCollection',
MDB_REFRESH_DOCUMENT_LIST = 'mdb.refreshDocumentList',
MDB_INSERT_DOCUMENT_FROM_TREE_VIEW = 'mdb.insertDocumentFromTreeView',
MDB_REFRESH_SCHEMA = 'mdb.refreshSchema',
MDB_COPY_SCHEMA_FIELD_NAME = 'mdb.copySchemaFieldName',
MDB_REFRESH_INDEXES = 'mdb.refreshIndexes',
Expand Down
12 changes: 12 additions & 0 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { LanguageServerController } from '../language';
import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate';
import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate';
import playgroundCloneDocumentTemplate from '../templates/playgroundCloneDocumentTemplate';
import playgroundInsertDocumentTemplate from '../templates/playgroundInsertDocumentTemplate';
import {
PlaygroundResult,
ShellExecuteAllResult,
Expand Down Expand Up @@ -298,6 +299,17 @@ export default class PlaygroundController {
return this._createPlaygroundFileWithContent(content);
}

createPlaygroundForInsertDocument(
databaseName: string,
collectionName: string
): Promise<boolean> {
const content = playgroundInsertDocumentTemplate
.replace('CURRENT_DATABASE', databaseName)
.replace('CURRENT_COLLECTION', collectionName);

return this._createPlaygroundFileWithContent(content);
}

async createPlayground(): Promise<boolean> {
const useDefaultTemplate = !!vscode.workspace
.getConfiguration('mdb')
Expand Down
11 changes: 11 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ export default class MDBExtensionController implements vscode.Disposable {
return Promise.resolve(true);
}
);
this.registerCommand(
EXTENSION_COMMANDS.MDB_INSERT_DOCUMENT_FROM_TREE_VIEW,
async (
documentsListTreeItem: DocumentListTreeItem | CollectionTreeItem
): Promise<boolean> => {
return this._playgroundController.createPlaygroundForInsertDocument(
documentsListTreeItem.databaseName,
documentsListTreeItem.collectionName
);
}
);
this.registerCommand(
EXTENSION_COMMANDS.MDB_REFRESH_SCHEMA,
(schemaTreeItem: SchemaTreeItem): Promise<boolean> => {
Expand Down
13 changes: 13 additions & 0 deletions src/templates/playgroundInsertDocumentTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const template = `// MongoDB Playground
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// The current database to use.
use('CURRENT_DATABASE');

// Create a new document in the collection.
db.getCollection('CURRENT_COLLECTION').insertOne({

});
`;

export default template;
35 changes: 35 additions & 0 deletions src/test/suite/mdbExtensionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,41 @@ suite('MDBExtensionController Test Suite', function () {
assert.strictEqual(namespaceUsed, 'waffle.house');
});

test('mdb.insertDocumentFromTreeView event should open a playground with an insert document template', async () => {
const collectionTreeItem = new CollectionTreeItem(
{
name: 'pineapple',
type: CollectionTypes.collection,
},
'plants',
{},
false,
false,
null
);

const mockCreatePlaygroundForInsertDocument = sinon.fake();
sinon.replace(
mdbTestExtension.testExtensionController._playgroundController,
'createPlaygroundForInsertDocument',
mockCreatePlaygroundForInsertDocument
);

await vscode.commands.executeCommand(
'mdb.insertDocumentFromTreeView',
collectionTreeItem
);
assert.strictEqual(mockCreatePlaygroundForInsertDocument.calledOnce, true);
assert.strictEqual(
mockCreatePlaygroundForInsertDocument.firstCall.args[0],
'plants'
);
assert.strictEqual(
mockCreatePlaygroundForInsertDocument.firstCall.args[1],
'pineapple'
);
});

test('mdb.deleteDocumentFromTreeView should not delete a document when the confirmation is cancelled', async () => {
const mockDocument = {
_id: 'pancakes',
Expand Down