in src/mongo/MongoScrapbook.ts [92:153]
async function executeCommand(context: IActionContext, command: MongoCommand, readOnlyContent?: ReadOnlyContent): Promise<void> {
if (command) {
try {
context.telemetry.properties.command = command.name;
context.telemetry.properties.argsCount = String(command.arguments ? command.arguments.length : 0);
} catch (error) {
// Ignore
}
const database = ext.connectedMongoDB;
if (!database) {
throw new Error('Please select a MongoDB database to run against by selecting it in the explorer and selecting the "Connect" context menu item');
}
if (command.errors && command.errors.length > 0) {
//Currently, we take the first error pushed. Tests correlate that the parser visits errors in left-to-right, top-to-bottom.
const err = command.errors[0];
throw new Error(localize('unableToParseSyntax', `Unable to parse syntax. Error near line ${err.range.start.line + 1}, column ${err.range.start.character + 1}: "${err.message}"`));
}
// we don't handle chained commands so we can only handle "find" if isn't chained
if (command.name === 'find' && !command.chained) {
const db = await database.connectToDb();
const collectionName: string = nonNullProp(command, 'collection');
const collection: Collection = db.collection(collectionName);
// NOTE: Intentionally creating a _new_ tree item rather than searching for a cached node in the tree because
// the executed 'find' command could have a filter or projection that is not handled by a cached tree node
const node = new MongoCollectionTreeItem(database, collection, command.argumentObjects);
await ext.fileSystem.showTextDocument(node, { viewColumn: vscode.ViewColumn.Beside });
} else {
const result = await database.executeCommand(command, context);
if (command.name === 'findOne') {
if (result === "null") {
throw new Error(`Could not find any documents`);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const document: IMongoDocument = EJSON.parse(result);
const collectionName: string = nonNullProp(command, 'collection');
const collectionId: string = `${database.fullId}/${collectionName}`;
const colNode: MongoCollectionTreeItem | undefined = await ext.tree.findTreeItem(collectionId, context);
if (!colNode) {
throw new Error(localize('failedToFind', 'Failed to find collection "{0}".', collectionName));
}
const docNode = new MongoDocumentTreeItem(colNode, document);
await ext.fileSystem.showTextDocument(docNode, { viewColumn: vscode.ViewColumn.Beside });
} else {
if (readOnlyContent) {
await readOnlyContent.append(`${result}${EOL}${EOL}`);
} else {
const label: string = 'Scrapbook-results';
const fullId: string = `${database.fullId}/${label}`;
await openReadOnlyContent({ label, fullId }, result, '.json', { viewColumn: vscode.ViewColumn.Beside });
}
await refreshTreeAfterCommand(database, command, context);
}
}
} else {
throw new Error('No MongoDB command found at the current cursor location.');
}
}