- 桌面端:解析 db.<集合>.aggregate([...]) shell 语法,新增 mongoAggregateDocuments
接口(Tauri + HTTP);mongo 入口统一补 ensureConnected;ObjectId(...) 自动
转换为 {"\$oid":"..."} 扩展 JSON 表示。
- dbx-core:新增 aggregate_documents、insert/update/delete_documents bulk
操作;扩展 JSON 解析识别 \$oid;schema list_tables_core 补 MongoDB/Elasticsearch
分支返回 COLLECTION/INDEX。
- dbx-web / Tauri 命令 / MCP bridge:注册 4 个新 mongo 路由
(aggregate/insert/update/delete documents);MCP bridge 新增 mongo HTTP 入口
和 lazy 连接复用。
- node-core (MCP 后端):mongodb 走专属 executeQuery / listTables /
describeTable;read-only 默认 + DBX_MCP_ALLOW_WRITES /
DBX_MCP_ALLOW_DANGEROUS_SQL 两级开关;aggregate 含 \$out/\$merge 走同样开关;
describeTable 抽样 20 条文档推断字段类型,_id 自动标记主键。
- mcp-server:mongodb 跳过 SQL safety evaluator(mongo shell 与 SQL 语法
不兼容),改由 backend 做命令级 gating;附注释。
- AiAssistant 同时 emit replaceSql + executeSql 让编辑器和结果保持一致;
App.vue ensureQueryTab 在无上下文时回退到连接默认库;aiMessageRender
识别 mongodb / mongo 代码块。
- Review 修复:合并两份重复的 evaluateMongo*Safety 实现(同时修复 web-backend
用字符串比较导致空 filter 检查可被空格绕过的 bug);ObjectId 正则兼容
单/双引号;mongo_ops 错误文案 "MCP" 改为 "bulk";补 aggregate 解析测试用例。
测试:pnpm test 911 通过;vue-tsc 通过;cargo check -p dbx-core 通过。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import { strict as assert } from "node:assert";
|
|
import test from "node:test";
|
|
import {
|
|
mongoCountToQueryResult,
|
|
mongoDocumentsToQueryResult,
|
|
parseMongoAggregateCommand,
|
|
parseMongoCountDocumentsCommand,
|
|
parseMongoFindCommand,
|
|
} from "../../apps/desktop/src/lib/mongoShellCommand.ts";
|
|
|
|
test("parseMongoFindCommand parses db collection find with an empty JSON filter", () => {
|
|
assert.deepEqual(parseMongoFindCommand("db.users.find({})"), {
|
|
collection: "users",
|
|
filter: "{}",
|
|
skip: 0,
|
|
limit: 100,
|
|
sort: undefined,
|
|
});
|
|
});
|
|
|
|
test("parseMongoFindCommand parses getCollection find with chained sort skip and limit", () => {
|
|
assert.deepEqual(
|
|
parseMongoFindCommand(
|
|
'db.getCollection("audit.logs").find({"level":"warn"}).sort({"createdAt":-1}).skip(20).limit(10)',
|
|
),
|
|
{
|
|
collection: "audit.logs",
|
|
filter: '{"level":"warn"}',
|
|
skip: 20,
|
|
limit: 10,
|
|
sort: '{"createdAt":-1}',
|
|
},
|
|
);
|
|
});
|
|
|
|
test("parseMongoFindCommand rejects unsupported mongo shell commands", () => {
|
|
assert.equal(parseMongoFindCommand("db.users.insertOne({})"), null);
|
|
});
|
|
|
|
test("parseMongoCountDocumentsCommand parses db collection countDocuments", () => {
|
|
assert.deepEqual(parseMongoCountDocumentsCommand("db.products.countDocuments({})"), {
|
|
collection: "products",
|
|
filter: "{}",
|
|
});
|
|
});
|
|
|
|
test("parseMongoAggregateCommand parses db collection aggregate", () => {
|
|
assert.deepEqual(parseMongoAggregateCommand('db.products.aggregate([{"$match":{"active":true}},{"$count":"total"}])'), {
|
|
collection: "products",
|
|
pipeline: '[{"$match":{"active":true}},{"$count":"total"}]',
|
|
});
|
|
});
|
|
|
|
test("parseMongoAggregateCommand accepts an empty pipeline", () => {
|
|
assert.deepEqual(parseMongoAggregateCommand("db.products.aggregate([])"), {
|
|
collection: "products",
|
|
pipeline: "[]",
|
|
});
|
|
});
|
|
|
|
test("parseMongoAggregateCommand rejects non-array pipelines and extra arguments", () => {
|
|
assert.equal(parseMongoAggregateCommand('db.products.aggregate({"$match":{}})'), null);
|
|
assert.equal(parseMongoAggregateCommand("db.products.aggregate([], {})"), null);
|
|
assert.equal(parseMongoAggregateCommand("db.products.aggregate([]).limit(10)"), null);
|
|
});
|
|
|
|
test("parseMongoAggregateCommand normalises ObjectId arguments with either quote style", () => {
|
|
const oid = "507f1f77bcf86cd799439011";
|
|
for (const quote of ["\"", "'"]) {
|
|
const command = parseMongoAggregateCommand(
|
|
`db.orders.aggregate([{"$match":{"_id":ObjectId(${quote}${oid}${quote})}}])`,
|
|
);
|
|
assert.ok(command, `quote=${quote} should parse`);
|
|
assert.equal(command.collection, "orders");
|
|
assert.deepEqual(JSON.parse(command.pipeline), [{ "$match": { "_id": { "$oid": oid } } }]);
|
|
}
|
|
});
|
|
|
|
test("mongoCountToQueryResult returns a single count row", () => {
|
|
assert.deepEqual(mongoCountToQueryResult(42, 5), {
|
|
columns: ["count"],
|
|
rows: [[42]],
|
|
affected_rows: 42,
|
|
execution_time_ms: 5,
|
|
});
|
|
});
|
|
|
|
test("mongoDocumentsToQueryResult turns mongo documents into grid rows", () => {
|
|
const result = mongoDocumentsToQueryResult(
|
|
[
|
|
{ _id: "1", name: "Ada", profile: { role: "admin" } },
|
|
{ _id: "2", active: true, name: "Lin" },
|
|
],
|
|
5,
|
|
12,
|
|
);
|
|
|
|
assert.deepEqual(result.columns, ["_id", "name", "profile", "active"]);
|
|
assert.deepEqual(result.rows, [
|
|
["1", "Ada", '{"role":"admin"}', null],
|
|
["2", "Lin", null, true],
|
|
]);
|
|
assert.equal(result.affected_rows, 12);
|
|
assert.equal(result.execution_time_ms, 5);
|
|
assert.equal(result.truncated, true);
|
|
});
|