Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mongodb): support use command to switch database

t8y209d54d09bc0b
diff --git a/apps/desktop/src/lib/mongoShellCommand.ts b/apps/desktop/src/lib/mongoShellCommand.ts
index 6b491e98b8..7d6bc34974 100644
--- a/apps/desktop/src/lib/mongoShellCommand.ts
+++ b/apps/desktop/src/lib/mongoShellCommand.ts
@@ -22,6 +22,10 @@ export interface MongoGetIndexesCommand {
collection: string;
}
+export interface MongoUseCommand {
+ database: string;
+}
+
export type MongoWriteCommand = { kind: "insert"; collection: string; docsJson: string } | { kind: "update"; collection: string; filter: string; update: string; many: boolean } | { kind: "delete"; collection: string; filter: string; many: boolean };
export interface MongoAggregateSafetyOptions {
@@ -130,6 +134,15 @@ export function parseMongoGetIndexesCommand(input: string): MongoGetIndexesComma
};
}
+export function parseMongoUseCommand(input: string): MongoUseCommand | null {
+ const source = input.trim().replace(/;$/, "").trim();
+ const match = /^use\s+([a-zA-Z0-9_-]+)$/i.exec(source);
+ if (!match) return null;
+ return {
+ database: match[1],
+ };
+}
+
export function parseMongoWriteCommand(input: string): MongoWriteCommand | null {
const source = input.trim().replace(/;$/, "").trim();
const insertOne = parseCollectionMethodTarget(source, "insertOne");
@@ -251,6 +264,15 @@ export function mongoWriteToQueryResult(affectedRows: number, executionTimeMs: n
};
}
+export function mongoUseToQueryResult(database: string, executionTimeMs: number): QueryResult {
+ return {
+ columns: ["message"],
+ rows: [[`switched to db ${database}`]],
+ affected_rows: 0,
+ execution_time_ms: Math.max(0, Math.round(executionTimeMs)),
+ };
+}
+
export function mongoIndexesToQueryResult(
indexes: {
name: string;
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index 8da38dcd25..225b20b1f4 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -15,11 +15,13 @@ import {
mongoDocumentsToQueryResult,
mongoIndexesToQueryResult,
mongoWriteToQueryResult,
+ mongoUseToQueryResult,
parseMongoAggregateCommand,
parseMongoCountDocumentsCommand,
parseMongoFindCommand,
parseMongoGetIndexesCommand,
parseMongoWriteCommand,
+ parseMongoUseCommand,
type MongoAggregateSafetyOptions,
} from "@/lib/mongoShellCommand";
import { redisCommandResultToQueryResult } from "@/lib/redisQueryResult";
@@ -1538,6 +1540,32 @@ export const useQueryStore = defineStore("query", () => {
return;
}
+ const mongoUse = conn?.db_type === "mongodb" ? parseMongoUseCommand(sql) : null;
+ if (mongoUse) {
+ console.info("[DBX][executeTabSql:mongo-use:start]", { traceId, database: mongoUse.database });
+ const current = tabs.value.find((t) => t.id === id);
+ if (current?.executionId === executionId) {
+ current.database = mongoUse.database;
+ current.results = undefined;
+ current.activeResultIndex = undefined;
+ current.result = markQueryResultRowsRaw(mongoUseToQueryResult(mongoUse.database, performance.now() - startedAt));
+ touchResult(current);
+ current.queryAnalysis = undefined;
+ current.querySourceColumns = undefined;
+ current.queryEditabilityReason = undefined;
+ current.tableMeta = undefined;
+ current.resultBaseSql = options?.resultBaseSql ?? sql;
+ current.resultSortedSql = options?.resultSortedSql;
+ syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
+ }
+ console.info("[DBX][executeTabSql:mongo-use:done]", {
+ traceId,
+ database: mongoUse.database,
+ elapsed: elapsed(),
+ });
+ return;
+ }
+
console.info("[DBX][executeTabSql:execute-multi:start]", { traceId, elapsed: elapsed() });
const clientSessionId = tab.mode === "query" ? tab.id : undefined;
const executionOptions = {