Skip to content

Commit

Permalink
Force refresh a document's tree item post-update. Fixes #145
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashanth committed May 21, 2018
1 parent 5a06dcb commit ae7e176
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/docdb/editors/DocDBDocumentNodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export class DocDBDocumentNodeEditor implements ICosmosEditor<RetrievedDocument>
}

public async update(document: RetrievedDocument): Promise<RetrievedDocument> {
return await this._documentNode.treeItem.update(document);
const updatedDoc = await this._documentNode.treeItem.update(document);
this._documentNode.refresh();
return updatedDoc;
}

public get id(): string {
Expand Down
4 changes: 3 additions & 1 deletion src/mongo/editors/MongoCollectionNodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IAzureParentNode, IAzureNode } from "vscode-azureextensionui";
import { IMongoDocument, MongoDocumentTreeItem } from "../tree/MongoDocumentTreeItem";
import { ICosmosEditor } from "../../CosmosEditorManager";
import { MongoCollectionTreeItem } from "../tree/MongoCollectionTreeItem";
import { getNodeEditorLabel } from '../../utils/vscodeUtils';
import { getNodeEditorLabel, getDocumentTreeItemLabel } from '../../utils/vscodeUtils';
// tslint:disable:no-var-requires
const EJSON = require("mongodb-extended-json");

Expand Down Expand Up @@ -38,6 +38,8 @@ export class MongoCollectionNodeEditor implements ICosmosEditor<IMongoDocument[]
const documentNode = documentNodes.find((node) => node.treeItem.document._id.toString() === updatedDoc._id.toString());
if (documentNode) {
documentNode.treeItem.document = updatedDoc;
documentNode.treeItem.label = getDocumentTreeItemLabel(documentNode.treeItem.document);
documentNode.refresh();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/mongo/editors/MongoDocumentNodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export class MongoDocumentNodeEditor implements ICosmosEditor<IMongoDocument> {
}

public async update(document: IMongoDocument): Promise<IMongoDocument> {
return await this._documentNode.treeItem.update(document);
const updatedDoc = await this._documentNode.treeItem.update(document);
this._documentNode.refresh();
return updatedDoc;
}

public get id(): string {
Expand Down
7 changes: 5 additions & 2 deletions src/mongo/editors/MongoFindOneResultEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ export class MongoFindOneResultEditor implements ICosmosEditor<IMongoDocument> {

public async update(newDocument: IMongoDocument): Promise<IMongoDocument> {
const node = await this._tree.findNode(this.id);
let result: IMongoDocument;
if (node) {
return (<IAzureNode<MongoDocumentTreeItem>>node).treeItem.update(newDocument);
result = await (<IAzureNode<MongoDocumentTreeItem>>node).treeItem.update(newDocument);
node.refresh();
}
// If the node isn't cached already, just update it to Mongo directly (without worrying about updating the tree)
const db = await this._databaseNode.treeItem.getDb();
return await MongoDocumentTreeItem.update(db.collection(this._collectionName), newDocument);
result = await MongoDocumentTreeItem.update(db.collection(this._collectionName), newDocument);
return result;
}

public get id(): string {
Expand Down
11 changes: 7 additions & 4 deletions src/utils/vscodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ function isAccountTreeItem(treeItem: IAzureTreeItem): boolean {
export function getDocumentTreeItemLabel(document: IMongoDocument | RetrievedDocument): string {
for (let field of documentDefaultFields) {
if (document.hasOwnProperty(field)) {
if (document[field]) { //ignore if false-y value: null, undefined, "".
if (typeof document[field] === "string") {
return document[field];
let value = document[field];
if (value) { //ignore if false-y value: null, undefined, "".
if (typeof value === "string") {
return value;
} else if (typeof value === "number") {
return value.toString();
} else { // A simple instanceOf check doesn't seem to work. I run into this issue detailed here :
//https://libertyseeds.ca/2015/01/03/Mongo-ObjectID-fails-instanceof-type-test-in-nodeunit/
const result = document[field].toString();
const result = value.toString();
if (!result.startsWith("[object ")) {
return result;
}
Expand Down

0 comments on commit ae7e176

Please sign in to comment.