diff --git a/package.json b/package.json index e8a6b4c..d9e94bc 100644 --- a/package.json +++ b/package.json @@ -243,6 +243,11 @@ "command": "cosmosDB.deleteDocDBDocument", "title": "Delete Document" }, + { + "category": "Cosmos DB", + "command": "cosmosDB.deleteStoredProcedure", + "title": "Delete Stored Procedure" + }, { "category": "Cosmos DB", "command": "cosmosDB.newMongoScrapbook", @@ -475,6 +480,10 @@ "command": "cosmosDB.deleteDocDBDocument", "when": "view == cosmosDBExplorer && viewItem == cosmosDBDocument" }, + { + "command": "cosmosDB.deleteStoredProcedure", + "when": "view == cosmosDBExplorer && viewItem == cosmosDBStoredProcedure" + }, { "command": "cosmosDB.deleteDocDBDatabase", "when": "view == cosmosDBExplorer && viewItem == cosmosDBDocumentDatabase" diff --git a/src/docdb/registerDocDBCommands.ts b/src/docdb/registerDocDBCommands.ts index c07b9cd..80f7f4a 100644 --- a/src/docdb/registerDocDBCommands.ts +++ b/src/docdb/registerDocDBCommands.ts @@ -9,6 +9,7 @@ import { DocDBAccountTreeItem } from "./tree/DocDBAccountTreeItem"; import { DocDBCollectionTreeItem } from "./tree/DocDBCollectionTreeItem"; import { DocDBDocumentTreeItem } from "./tree/DocDBDocumentTreeItem"; import { DocDBDocumentsTreeItem } from "./tree/DocDBDocumentsTreeItem"; +import { DocDBStoredProcedureTreeItem } from "./tree/DocDBStoredProcedureTreeItem"; export function registerDocDBCommands(actionHandler: AzureActionHandler, tree: AzureTreeDataProvider): void { actionHandler.registerCommand('cosmosDB.createDocDBDatabase', async (node?: IAzureParentNode) => { @@ -48,4 +49,10 @@ export function registerDocDBCommands(actionHandler: AzureActionHandler, tree: A } await node.deleteNode(); }); + actionHandler.registerCommand('cosmosDB.deleteStoredProcedure', async (node?: IAzureNode) => { + if (!node) { + node = await tree.showNodePicker(DocDBStoredProcedureTreeItem.contextValue); + } + await node.deleteNode(); + }); } diff --git a/src/docdb/tree/DocDBCollectionTreeItem.ts b/src/docdb/tree/DocDBCollectionTreeItem.ts index 700f5ce..3d5c815 100644 --- a/src/docdb/tree/DocDBCollectionTreeItem.ts +++ b/src/docdb/tree/DocDBCollectionTreeItem.ts @@ -11,6 +11,8 @@ import { getDocumentClient } from "../getDocumentClient"; import { DocDBDocumentsTreeItem } from './DocDBDocumentsTreeItem'; import * as path from "path"; import { DialogBoxResponses } from '../../constants'; +import { DocDBStoredProcedureTreeItem } from './DocDBStoredProcedureTreeItem'; +import { DocDBDocumentTreeItem } from './DocDBDocumentTreeItem'; /** * Represents a DocumentDB collection @@ -19,7 +21,8 @@ export class DocDBCollectionTreeItem implements IAzureParentTreeItem { public static contextValue: string = "cosmosDBDocumentCollection"; public readonly contextValue: string = DocDBCollectionTreeItem.contextValue; - private readonly _children: IAzureTreeItem[]; + private readonly _documentsTreeItem: DocDBDocumentsTreeItem; + private readonly _storedProceduresTreeItem: DocDBStoredProceduresTreeItem; constructor( private _documentEndpoint: string, @@ -27,10 +30,8 @@ export class DocDBCollectionTreeItem implements IAzureParentTreeItem { private _collection: CollectionMeta, private _isEmulator: boolean) { - this._children = [ - new DocDBDocumentsTreeItem(this._documentEndpoint, this._masterKey, this, this._isEmulator), - new DocDBStoredProceduresTreeItem(this._documentEndpoint, this._masterKey, this._collection, this._isEmulator) - ]; + this._documentsTreeItem = new DocDBDocumentsTreeItem(this._documentEndpoint, this._masterKey, this, this._isEmulator); + this._storedProceduresTreeItem = new DocDBStoredProceduresTreeItem(this._documentEndpoint, this._masterKey, this._collection, this._isEmulator); } public get id(): string { @@ -49,7 +50,7 @@ export class DocDBCollectionTreeItem implements IAzureParentTreeItem { } public async loadMoreChildren(node: IAzureNode, clearCache: boolean): Promise { - return this._children; + return [this._documentsTreeItem, this._storedProceduresTreeItem]; } public hasMoreChildren(): boolean { @@ -84,6 +85,17 @@ export class DocDBCollectionTreeItem implements IAzureParentTreeItem { } public pickTreeItem?(expectedContextValue: string): IAzureTreeItem | undefined { - return this._children.find(node => node.contextValue === expectedContextValue); + switch (expectedContextValue) { + case DocDBDocumentsTreeItem.contextValue: + case DocDBDocumentTreeItem.contextValue: + return this._documentsTreeItem; + + case DocDBStoredProceduresTreeItem.contextValue: + case DocDBStoredProcedureTreeItem.contextValue: + return this._storedProceduresTreeItem; + + default: + return undefined; + } } } diff --git a/src/docdb/tree/DocDBStoredProcedureTreeItem.ts b/src/docdb/tree/DocDBStoredProcedureTreeItem.ts index c954837..1e0b057 100644 --- a/src/docdb/tree/DocDBStoredProcedureTreeItem.ts +++ b/src/docdb/tree/DocDBStoredProcedureTreeItem.ts @@ -5,8 +5,10 @@ import * as path from 'path'; import * as vscode from "vscode"; -import { IAzureTreeItem } from 'vscode-azureextensionui'; +import { IAzureTreeItem, IAzureNode, UserCancelledError } from 'vscode-azureextensionui'; import { ProcedureMeta } from 'documentdb'; +import { DialogBoxResponses } from '../../constants'; +import { getDocumentClient } from '../getDocumentClient'; /** * Represents a Cosmos DB DocumentDB (SQL) stored procedure @@ -15,7 +17,7 @@ export class DocDBStoredProcedureTreeItem implements IAzureTreeItem { public static contextValue: string = "cosmosDBStoredProcedure"; public readonly contextValue: string = DocDBStoredProcedureTreeItem.contextValue; - constructor(private _procedure: ProcedureMeta) { + constructor(private _documentEndpoint: string, private _masterKey: string, private _isEmulator: boolean, private _procedure: ProcedureMeta) { } public get id(): string { @@ -36,4 +38,19 @@ export class DocDBStoredProcedureTreeItem implements IAzureTreeItem { dark: path.join(__filename, '..', '..', '..', '..', '..', 'resources', 'icons', 'dark', 'Process_16x.svg') }; } + + public async deleteTreeItem(_node: IAzureNode): Promise { + const message: string = `Are you sure you want to delete stored procedure '${this.label}'?`; + const result = await vscode.window.showWarningMessage(message, DialogBoxResponses.Yes, DialogBoxResponses.Cancel); + if (result === DialogBoxResponses.Yes) { + const client = getDocumentClient(this._documentEndpoint, this._masterKey, this._isEmulator); + await new Promise((resolve, reject) => { + client.deleteStoredProcedure(this.link, function (err) { + err ? reject(err) : resolve(); + }); + }); + } else { + throw new UserCancelledError(); + } + } } diff --git a/src/docdb/tree/DocDBStoredProceduresTreeItem.ts b/src/docdb/tree/DocDBStoredProceduresTreeItem.ts index 38936b5..bdab435 100644 --- a/src/docdb/tree/DocDBStoredProceduresTreeItem.ts +++ b/src/docdb/tree/DocDBStoredProceduresTreeItem.ts @@ -23,7 +23,7 @@ export class DocDBStoredProceduresTreeItem extends DocDBTreeItemBase