From 8a1a9a8fb8ca6148548e85e4d465fff5f624ee9c Mon Sep 17 00:00:00 2001 From: Syngnat Date: Fri, 17 Apr 2026 18:56:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(mongodb):=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Mongo=20shell=20=E5=BF=AB=E6=8D=B7=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 show dbs 和 show databases 转换 listDatabases JSON 命令 - 为 show collections 和 show tables 转换 listCollections JSON 命令 - 补充 Mongo shell 快捷命令回归测试并验证前端构建 Fixes #390 --- frontend/src/utils/mongodb.test.ts | 19 ++++++++++++++++++ frontend/src/utils/mongodb.ts | 32 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 frontend/src/utils/mongodb.test.ts diff --git a/frontend/src/utils/mongodb.test.ts b/frontend/src/utils/mongodb.test.ts new file mode 100644 index 0000000..2e8ef64 --- /dev/null +++ b/frontend/src/utils/mongodb.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; + +import { convertMongoShellToJsonCommand } from './mongodb'; + +describe('convertMongoShellToJsonCommand', () => { + it('converts show dbs shell shortcut to listDatabases command', () => { + expect(convertMongoShellToJsonCommand('show dbs;')).toEqual({ + recognized: true, + command: JSON.stringify({ listDatabases: 1, nameOnly: true }), + }); + }); + + it('converts show collections shell shortcut to listCollections command', () => { + expect(convertMongoShellToJsonCommand(' show collections ')).toEqual({ + recognized: true, + command: JSON.stringify({ listCollections: 1, filter: {}, nameOnly: true }), + }); + }); +}); diff --git a/frontend/src/utils/mongodb.ts b/frontend/src/utils/mongodb.ts index 646d51e..d2b399d 100644 --- a/frontend/src/utils/mongodb.ts +++ b/frontend/src/utils/mongodb.ts @@ -752,10 +752,42 @@ const buildMongoDeleteCommand = ( return JSON.stringify(command); }; +const convertMongoShellShortcutCommand = (raw: string): ShellConvertResult | null => { + const normalized = String(raw || '') + .replace(/[;;]+\s*$/, '') + .trim() + .replace(/\s+/g, ' ') + .toLowerCase(); + + if (!normalized) { + return null; + } + + if (normalized === 'show dbs' || normalized === 'show databases') { + return { + recognized: true, + command: JSON.stringify({ listDatabases: 1, nameOnly: true }), + }; + } + + if (normalized === 'show collections' || normalized === 'show tables') { + return { + recognized: true, + command: JSON.stringify({ listCollections: 1, filter: {}, nameOnly: true }), + }; + } + + return null; +}; + export const convertMongoShellToJsonCommand = (raw: string): ShellConvertResult => { let input = String(raw || '').trim(); input = input.replace(/^[\s]*(\/\/[^\n]*\n)+/g, '').trim(); input = input.replace(/[;;]+\s*$/, ''); + const shortcut = convertMongoShellShortcutCommand(input); + if (shortcut) { + return shortcut; + } if (!/^db\./i.test(input)) { return { recognized: false }; }