Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
582 changes: 475 additions & 107 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@
{
"command": "mdb.stopMCPServer",
"title": "MongoDB: Stop MCP Server"
},
{
"command": "mdb.getMCPServerConfig",
"title": "MongoDB: Get MCP Server Config"
}
],
"menus": {
Expand Down Expand Up @@ -1414,6 +1418,7 @@
"mongodb-connection-string-url": "^3.0.2",
"mongodb-data-service": "^22.30.1",
"mongodb-log-writer": "^2.4.1",
"mongodb-mcp-server": "^0.3.0",
"mongodb-query-parser": "^4.4.2",
"mongodb-schema": "^12.6.2",
"node-machine-id": "1.1.12",
Expand Down Expand Up @@ -1445,7 +1450,7 @@
"@types/lodash": "^4.17.14",
"@types/micromatch": "^4.0.9",
"@types/mocha": "^8.2.3",
"@types/node": "^14.18.63",
"@types/node": "^20.19.0",
"@types/prettier": "^2.7.3",
"@types/react": "^17.0.83",
"@types/react-dom": "^17.0.25",
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum EXTENSION_COMMANDS {
// MCP Server commands.
START_MCP_SERVER = 'mdb.startMCPServer',
STOP_MCP_SERVER = 'mdb.stopMCPServer',
GET_MCP_SERVER_CONFIG = 'mdb.getMCPServerConfig',
}

export type ExtensionCommand = EXTENSION_COMMANDS;
Expand Down
31 changes: 16 additions & 15 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ const log = createLogger('connection controller');

const MAX_CONNECTION_NAME_LENGTH = 512;

export enum DataServiceEventTypes {
CONNECTIONS_DID_CHANGE = 'CONNECTIONS_DID_CHANGE',
ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED',
interface DataServiceEventTypes {
CONNECTIONS_DID_CHANGE: any;
ACTIVE_CONNECTION_CHANGED: any;
}

export enum ConnectionTypes {
Expand Down Expand Up @@ -156,7 +156,8 @@ export default class ConnectionController {
private _statusView: StatusView;

// Used by other parts of the extension that respond to changes in the connections.
private eventEmitter: EventEmitter = new EventEmitter();
private eventEmitter: EventEmitter<DataServiceEventTypes> =
new EventEmitter();

constructor({
statusView,
Expand Down Expand Up @@ -231,7 +232,7 @@ export default class ConnectionController {
}

if (loadedConnections.length) {
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
}

// TODO: re-enable with fewer 'Saved Connections Loaded' events
Expand Down Expand Up @@ -439,7 +440,7 @@ export default class ConnectionController {
});
this._connectionAttempt = connectionAttempt;
this._connectingConnectionId = connectionId;
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');

if (this._activeDataService) {
log.info('Disconnecting from the previous connection...', {
Expand Down Expand Up @@ -531,7 +532,7 @@ export default class ConnectionController {
this._connectingConnectionId = null;
}

this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
}

log.info('Successfully connected', { connectionId });
Expand All @@ -547,7 +548,7 @@ export default class ConnectionController {
this._connectingConnectionId = null;

this._connections[connectionId].lastUsed = new Date();
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');
await this._connectionStorage.saveConnection(
this._connections[connectionId],
);
Expand Down Expand Up @@ -721,8 +722,8 @@ export default class ConnectionController {
this._disconnecting = true;
this._statusView.showMessage('Disconnecting from current connection...');

this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');

if (!this._activeDataService) {
log.error('Unable to disconnect: no active connection');
Expand Down Expand Up @@ -766,7 +767,7 @@ export default class ConnectionController {

delete this._connections[connectionId];
await this._connectionStorage.removeConnection(connectionId);
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
}

// Prompts the user to remove the connection then removes it on affirmation.
Expand Down Expand Up @@ -957,8 +958,8 @@ export default class ConnectionController {
}

this._connections[connectionId].name = inputtedConnectionName;
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');

await this._connectionStorage.saveConnection(
this._connections[connectionId],
Expand All @@ -969,14 +970,14 @@ export default class ConnectionController {
}

addEventListener(
eventType: DataServiceEventTypes,
eventType: keyof DataServiceEventTypes,
listener: () => void,
): void {
this.eventEmitter.addListener(eventType, listener);
}

removeEventListener(
eventType: DataServiceEventTypes,
eventType: keyof DataServiceEventTypes,
listener: () => void,
): void {
this.eventEmitter.removeListener(eventType, listener);
Expand Down
5 changes: 2 additions & 3 deletions src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import EXTENSION_COMMANDS from '../commands';
import type ConnectionController from '../connectionController';
import { isPlayground } from '../utils/playground';
import { getDBFromConnectionString } from '../utils/connection-string-db';
import { DataServiceEventTypes } from '../connectionController';

export default class ActiveConnectionCodeLensProvider
implements vscode.CodeLensProvider
Expand All @@ -28,7 +27,7 @@ export default class ActiveConnectionCodeLensProvider
this._onDidChangeCodeLenses.fire();
};
this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
'ACTIVE_CONNECTION_CHANGED',
this._activeConnectionChangedHandler,
);
}
Expand Down Expand Up @@ -67,7 +66,7 @@ export default class ActiveConnectionCodeLensProvider

deactivate(): void {
this._connectionController.removeEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
'ACTIVE_CONNECTION_CHANGED',
this._activeConnectionChangedHandler,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/editors/memoryFileSystemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class MemoryFileSystemProvider implements vscode.FileSystemProvider {

_emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
_bufferedEvents: vscode.FileChangeEvent[] = [];
_fireSoonHandle?: NodeJS.Timer;
_fireSoonHandle?: NodeJS.Timeout;

readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> =
this._emitter.event;
Expand Down
5 changes: 2 additions & 3 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import os from 'os';

import type PlaygroundSelectionCodeActionProvider from './playgroundSelectionCodeActionProvider';
import type ConnectionController from '../connectionController';
import { DataServiceEventTypes } from '../connectionController';
import { createLogger } from '../logging';
import type { ConnectionTreeItem } from '../explorer';
import { CollectionTreeItem } from '../explorer';
Expand Down Expand Up @@ -103,7 +102,7 @@ export default class PlaygroundController {
void this._activeConnectionChanged();
};
this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
'ACTIVE_CONNECTION_CHANGED',
this._activeConnectionChangedHandler,
);

Expand Down Expand Up @@ -684,7 +683,7 @@ export default class PlaygroundController {

deactivate(): void {
this._connectionController.removeEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
'ACTIVE_CONNECTION_CHANGED',
this._activeConnectionChangedHandler,
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/explorer/explorerController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as vscode from 'vscode';

import type ConnectionController from '../connectionController';
import { DataServiceEventTypes } from '../connectionController';
import ExplorerTreeController from './explorerTreeController';
import { createTrackedTreeView } from '../utils/treeViewHelper';
import type { TelemetryService } from '../telemetry';
Expand All @@ -23,7 +22,7 @@ export default class ExplorerController {
private createTreeView = (): void => {
// Remove the listener that called this function.
this._connectionController.removeEventListener(
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
'CONNECTIONS_DID_CHANGE',
this.createTreeView,
);

Expand All @@ -41,7 +40,7 @@ export default class ExplorerController {
// Listen for a change in connections to occur before we create the tree
// so that we show the `viewsWelcome` before any connections are added.
this._connectionController.addEventListener(
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
'CONNECTIONS_DID_CHANGE',
this.createTreeView,
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/explorer/explorerTreeController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from 'vscode';

import type ConnectionController from '../connectionController';
import { DataServiceEventTypes } from '../connectionController';
import ConnectionTreeItem from './connectionTreeItem';
import { createLogger } from '../logging';
import { DOCUMENT_ITEM } from './documentTreeItem';
Expand Down Expand Up @@ -31,7 +30,7 @@ export default class ExplorerTreeController

// Subscribe to changes in the connections.
this._connectionController.addEventListener(
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
'CONNECTIONS_DID_CHANGE',
() => {
this.refresh();
},
Expand All @@ -42,7 +41,7 @@ export default class ExplorerTreeController

removeListeners(): void {
this._connectionController.removeEventListener(
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
'CONNECTIONS_DID_CHANGE',
() => {
this.refresh();
},
Expand Down
42 changes: 11 additions & 31 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,33 @@ class Logger implements ILogger {
this.name = name;
}

private formatMessage(message?: any, ...optionalParams: any[]): string {
return `[${this.name}] ${message} ${optionalParams.length ? util.inspect(optionalParams) : ''}`;
}

public trace(message?: any, ...optionalParams: any[]): void {
this.append(
'TRACE',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
Logger.channel.trace(this.formatMessage(message, ...optionalParams));
}

public debug(message?: any, ...optionalParams: any[]): void {
this.append(
'DEBUG',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
Logger.channel.debug(this.formatMessage(message, ...optionalParams));
}

public info(message?: any, ...optionalParams: any[]): void {
this.append(
'INFO ',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
Logger.channel.info(this.formatMessage(message, ...optionalParams));
}

public warn(message?: any, ...optionalParams: any[]): void {
this.append(
'WARN ',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
Logger.channel.warn(this.formatMessage(message, ...optionalParams));
}

public error(message?: any, ...optionalParams: any[]): void {
this.append(
'ERROR',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
Logger.channel.error(this.formatMessage(message, ...optionalParams));
}

public fatal(message?: any, ...optionalParams: any[]): void {
this.append(
'FATAL',
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
);
}

private append(type: string, message: string): void {
// https://code.visualstudio.com/api/references/vscode-api#window.createOutputChannel

Logger.channel.appendLine(
`${new Date().toISOString()} ${this.name} ${type} ${message}\n`,
Logger.channel.error(
`FATAL: ${this.formatMessage(message, ...optionalParams)}`,
);
}
}
Expand Down
Loading
Loading