MongoCollectionTreeItem: fix findOne's result to recognize ObjectIDs (#610)

* MongoCollectionTreeItem: fix findOne's result to recognize ObjectIDs

* Add a comment

* parseJSContent: use EJSON.parse instead of sandbox

* Remove unused import
This commit is contained in:
Prashanth 2018-05-04 19:44:13 -07:00 коммит произвёл Stephen Weatherford (MSFT)
Родитель 7e52723548
Коммит a26836a5b2
2 изменённых файлов: 7 добавлений и 9 удалений

Просмотреть файл

@ -19,7 +19,7 @@ export class MongoFindOneResultEditor implements ICosmosEditor<IMongoDocument> {
constructor(databaseNode: IAzureParentNode<MongoDatabaseTreeItem>, collectionName: string, data: string, tree: AzureTreeDataProvider) {
this._databaseNode = databaseNode;
this._collectionName = collectionName;
this._originalDocument = JSON.parse(data);
this._originalDocument = EJSON.parse(data);
this._tree = tree;
}

Просмотреть файл

@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as vm from 'vm';
import * as path from 'path';
import * as _ from 'underscore';
import * as util from '../../utils/vscodeUtils';
@ -12,6 +11,8 @@ import { Collection, Cursor, ObjectID, InsertOneWriteOpResult, BulkWriteOpResult
import { IAzureParentTreeItem, IAzureTreeItem, IAzureNode, UserCancelledError, DialogResponses } from 'vscode-azureextensionui';
import { DefaultBatchSize } from '../../constants';
import { IMongoDocument, MongoDocumentTreeItem } from './MongoDocumentTreeItem';
// tslint:disable:no-var-requires
const EJSON = require("mongodb-extended-json");
export class MongoCollectionTreeItem implements IAzureParentTreeItem {
public static contextValue: string = "MongoCollection";
@ -181,7 +182,9 @@ export class MongoCollectionTreeItem implements IAzureParentTreeItem {
} else {
return Promise.reject(new Error("Too many arguments passed to findOne."));
}
return this.stringify(result);
// findOne is the only command in this file whose output requires EJSON support.
// Hence that's the only function which uses EJSON.stringify rather than this.stringify.
return EJSON.stringify(result, null, '\t');
}
private insert(document: Object): Thenable<string> {
@ -269,12 +272,7 @@ function reportProgress<T>(promise: Thenable<T>, title: string): Thenable<T> {
// tslint:disable-next-line:no-any
function parseJSContent(content: string): any {
try {
const sandbox = {};
// tslint:disable-next-line:insecure-random
const key = 'parse' + Math.floor(Math.random() * 1000000);
sandbox[key] = {};
vm.runInNewContext(key + '=' + content, sandbox);
return sandbox[key];
return EJSON.parse(content);
} catch (error) {
throw error.message;
}