Skip to content

Commit

Permalink
refactor: replace temp variables with query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Nov 17, 2021
1 parent 6e7adba commit 34425ad
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions src/language/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@ import {
MongoClientOptions
} from '@mongosh/service-provider-server';
import { CompletionItemKind } from 'vscode-languageserver/node';
import { EJSON } from 'bson';
import { EJSON, Document } from 'bson';
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import parseSchema = require('mongodb-schema');
import { parentPort, workerData } from 'worker_threads';

import type { PlaygroundResult, PlaygroundDebug, ShellExecuteAllResult } from '../types/playgroundType';
import { PlaygroundResult, PlaygroundDebug, ShellExecuteAllResult } from '../types/playgroundType';
import { ServerCommands } from './serverCommands';

type EvaluationResult = {
interface EvaluationResult {
printable: any;
type: string | null;
};
}

type WorkerResult = ShellExecuteAllResult;
type WorkerError = any | null;

const content = ({ type, printable }: EvaluationResult) => {
if (type === 'Cursor' || type === 'AggregationCursor') {
return JSON.parse(EJSON.stringify(printable.documents));
}

return typeof printable === 'string'
? printable
: JSON.parse(EJSON.stringify(printable));
};

const language = (evaluationResult: EvaluationResult) => {
if (typeof content(evaluationResult) === 'object' && content(evaluationResult) !== null) {
return 'json';
}

return 'plaintext';
};

const executeAll = async (
codeToEvaluate: string,
connectionString: string,
Expand All @@ -32,10 +50,11 @@ const executeAll = async (
connectionString,
connectionOptions
);

const outputLines: PlaygroundDebug = [];

// Create a new instance of the runtime and evaluate code from a playground.
const runtime: ElectronRuntime = new ElectronRuntime(serviceProvider);

runtime.setEvaluationListener({
onPrint(values: EvaluationResult[]) {
for (const { type, printable } of values) {
Expand All @@ -49,25 +68,14 @@ const executeAll = async (
}
});
const { source, type, printable } = await runtime.evaluate(codeToEvaluate);
const namespace =
source && source.namespace
? `${source.namespace.db}.${source.namespace.collection}`
: null;
let content = '';

if (type === 'Cursor' || type === 'AggregationCursor') {
content = JSON.parse(EJSON.stringify(printable.documents));
} else {
content = typeof printable === 'string'
? printable
: JSON.parse(EJSON.stringify(printable));
}

const namespace = (source && source.namespace)
? `${source.namespace.db}.${source.namespace.collection}`
: null;
const result: PlaygroundResult = {
namespace,
type: type ? type : typeof printable,
content,
language: (typeof content === 'object' && content !== null) ? 'json' : 'plaintext'
content: content({ type, printable }),
language: language({ type, printable })
};

return [null, { outputLines, result }];
Expand Down Expand Up @@ -133,6 +141,17 @@ const getFieldsFromSchema = async (
}
};

const prepareCompletionItems = (result: Document) => {
if (!result) {
return [];
}

return result.databases.map((item) => ({
label: item.name,
kind: CompletionItemKind.Value
}));
};

const getListDatabases = async (
connectionString: string,
connectionOptions: any
Expand All @@ -145,14 +164,9 @@ const getListDatabases = async (

// TODO: There is a mistake in the service provider interface
// Use `admin` as arguments to get list of dbs
// and remove it later when `mongosh` will merge a fix
// and remove it later when `mongosh` will merge a fix.
const result = await serviceProvider.listDatabases('admin');
const databases = result
? result.databases.map((item) => ({
label: item.name,
kind: CompletionItemKind.Value
}))
: [];
const databases = prepareCompletionItems(result);

return [null, databases];
} catch (error) {
Expand Down

0 comments on commit 34425ad

Please sign in to comment.