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。

fix(mongodb): accept trailing commas in shell arguments

Guoyu Sud341581ff01b
diff --git a/packages/app-tests/mongoShellCommand.test.ts b/packages/app-tests/mongoShellCommand.test.ts
index 4444f95904..e8c4ce459c 100644
--- a/packages/app-tests/mongoShellCommand.test.ts
+++ b/packages/app-tests/mongoShellCommand.test.ts
@@ -534,6 +534,69 @@ test("parseMongoAggregateCommand accepts an empty pipeline", () => {
});
});
+test("parseMongoAggregateCommand accepts Mongo Shell trailing commas", () => {
+ const command = parseMongoAggregateCommand(`db.AdressInfo.aggregate([
+ {
+ $match: {
+ IsDelete: 0,
+ DataSource: { $ne: 'XC' },
+ TypeName: 1,
+ },
+ },
+ {
+ $project: {
+ MainId: '$_id',
+ labels: ['primary', 'backup',],
+ },
+ },
+ { $out: 'IBMBiititle' },
+ ], {
+ allowDiskUse: true,
+ })`);
+
+ assert.ok(command);
+ assert.deepEqual(JSON.parse(command.pipeline), [
+ {
+ $match: {
+ IsDelete: 0,
+ DataSource: { $ne: "XC" },
+ TypeName: 1,
+ },
+ },
+ {
+ $project: {
+ MainId: "$_id",
+ labels: ["primary", "backup"],
+ },
+ },
+ { $out: "IBMBiititle" },
+ ]);
+ assert.deepEqual(JSON.parse(command.options ?? "null"), { allowDiskUse: true });
+});
+
+test("parseMongoAggregateCommand preserves trailing-comma text inside strings", () => {
+ const command = parseMongoAggregateCommand(`db.logs.aggregate([
+ {
+ $project: {
+ objectText: "literal,}",
+ arrayText: "literal,]",
+ escapedQuote: "literal\\",]",
+ },
+ },
+ ])`);
+
+ assert.ok(command);
+ assert.deepEqual(JSON.parse(command.pipeline), [
+ {
+ $project: {
+ objectText: "literal,}",
+ arrayText: "literal,]",
+ escapedQuote: 'literal",]',
+ },
+ },
+ ]);
+});
+
test("parseMongoAggregateCommand ignores comments inside aggregate pipelines", () => {
const command = parseMongoAggregateCommand(`
db.cash.aggregate([
diff --git a/packages/mongo-shell/src/json.ts b/packages/mongo-shell/src/json.ts
index 87a634aded..19573ec571 100644
--- a/packages/mongo-shell/src/json.ts
+++ b/packages/mongo-shell/src/json.ts
@@ -241,6 +241,39 @@ function mongoLineCommentEnd(source: string, start: number): number {
return source.length;
}
+function removeTrailingCommas(source: string): string {
+ let result = "";
+ let inString = false;
+ let escaped = false;
+
+ for (let index = 0; index < source.length; index += 1) {
+ const char = source[index] ?? "";
+ if (inString) {
+ result += char;
+ if (escaped) escaped = false;
+ else if (char === "\\") escaped = true;
+ else if (char === '"') inString = false;
+ continue;
+ }
+
+ if (char === '"') {
+ inString = true;
+ result += char;
+ continue;
+ }
+
+ if (char === ",") {
+ let next = index + 1;
+ while (/\s/.test(source[next] ?? "")) next += 1;
+ if (source[next] === "}" || source[next] === "]") continue;
+ }
+
+ result += char;
+ }
+
+ return result;
+}
+
export function quoteUnquotedObjectKeys(source: string): string {
let result = "";
let quote: string | null = null;
@@ -407,5 +440,6 @@ function convertSingleQuotedStrings(source: string): string {
}
}
- return quote === "'" ? source : result + source.slice(copiedUntil);
+ const converted = quote === "'" ? source : result + source.slice(copiedUntil);
+ return removeTrailingCommas(converted);
}