feat(mongodb): add Compass-style collection import/export

This commit is contained in:
onenewcode authored and GitHub committed 2026-09-12 21:59:29 +08:00
1 parent bf4874906c
commit 8ff56b4837
47 files changed
+5626 -52

No files matched your search

@@ -0,0 +1,64 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, reactive, type App } from "vue";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
const store = reactive({
connections: [],
mongoImportSource: null as { connectionId: string; database: string; collection: string } | null,
transferSource: null,
schemaDiffSource: null,
dataCompareSource: null,
sqlFileSource: null,
diagramSource: null,
docsSource: null,
tableImportSource: null,
tableDataGenerateSource: null,
fieldLineageSource: null,
databaseSearchSource: null,
databaseExportSource: null,
});
vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => store }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
import { useDialogSources } from "@/composables/useDialogSources";
const mountedApps: App[] = [];
let dialogs: ReturnType<typeof useDialogSources>;
async function mountDialogs() {
const container = document.createElement("div");
document.body.append(container);
const app = createApp(
defineComponent({
setup() {
dialogs = useDialogSources();
return () => h("div");
},
}),
);
mountedApps.push(app);
app.use(i18n);
app.mount(container);
await nextTick();
}
describe("useDialogSources mongo import/export", () => {
beforeAll(async () => {
await mountDialogs();
});
afterAll(() => {
for (const app of mountedApps.splice(0)) app.unmount();
});
it("opens the mongo import dialog from a collection source", async () => {
store.mongoImportSource = { connectionId: "c1", database: "shop", collection: "orders" };
await nextTick();
expect(dialogs.showMongoImportDialog.value).toBe(true);
expect(dialogs.mongoImportPrefillCollection.value).toBe("orders");
expect(store.mongoImportSource).toBeNull();
});
});
@@ -16,6 +16,7 @@ const apiMock = vi.hoisted(() => ({
getColumns: vi.fn(),
getTableDdl: vi.fn(),
startTableExport: vi.fn(),
exportMongodbQuery: vi.fn(),
}));
vi.mock("@/lib/backend/api", () => apiMock);
@@ -122,6 +123,41 @@ describe("useSidebarTreeExportRuntime", () => {
expect(toastMock).toHaveBeenCalledWith("导出失败:上一个 DuckDB 查询仍在停止中,请稍后重试。", 5000);
});
it("exports a mongo collection through the save-file path without a setup dialog", async () => {
apiMock.exportMongodbQuery.mockImplementation(async (_request, onProgress) => {
onProgress({ exportId: "export-1", status: "done", documentsRead: 3, bytesWritten: 12, elapsedMs: 4 });
return { exportId: "export-1", documentsExported: 3, filePath: "orders.ndjson", elapsedMs: 4 };
});
const activeNode = shallowRef({ id: "col-1", type: "mongo-collection", label: "orders", connectionId: "conn-1", database: "shop", children: [] } as TreeNode);
const connectionStore = {
ensureConnected: vi.fn(),
getConfig: vi.fn(() => ({ db_type: "mongodb" })),
treeNodes: [],
selectedTreeNodeIds: [],
};
const runtime = useSidebarTreeExportRuntime({
activeNode,
connectionStore: connectionStore as never,
settingsStore: exportSettings() as never,
acceptedSelectionIds: () => null,
});
await runtime.exportMongoCollection("ndjson");
expect(connectionStore.ensureConnected).toHaveBeenCalledWith("conn-1");
expect(apiMock.exportMongodbQuery).toHaveBeenCalledWith(
expect.objectContaining({
connectionId: "conn-1",
database: "shop",
collection: "orders",
format: "ndjson",
filePath: "orders.ndjson",
}),
expect.any(Function),
);
expect(toastMock).toHaveBeenCalledWith("grid.exported");
});
it("loads and joins every selected DDL in tree order", async () => {
apiMock.getTableDdl.mockResolvedValueOnce("CREATE TABLE one (id INT)").mockResolvedValueOnce("CREATE VIEW two AS SELECT 1;");
const first = { id: "table-1", type: "table", label: "one", connectionId: "conn-1", database: "db", schema: "main" } as TreeNode;
@@ -10,6 +10,7 @@ function setup(node: Partial<TreeNode>, options: { treeNodes?: TreeNode[]; selec
docsSource: null as unknown,
diagramSource: null as unknown,
databaseExportSource: null as unknown,
mongoImportSource: undefined as unknown,
schemaDiffSource: null as unknown,
treeNodes: options.treeNodes ?? [],
selectedTreeNodeIds: options.selectedTreeNodeIds ?? [],
@@ -109,6 +110,25 @@ describe("useSidebarTreeToolRuntime diagram and database export", () => {
});
});
describe("useSidebarTreeToolRuntime mongo import", () => {
it("opens collection import from a mongo-collection node", () => {
const { connectionStore, runtime } = setup({
type: "mongo-collection",
label: "orders",
connectionId: "conn-1",
database: "shop",
});
runtime.openMongoImport();
expect(connectionStore.mongoImportSource).toEqual({
connectionId: "conn-1",
database: "shop",
collection: "orders",
});
});
});
describe("useSidebarTreeToolRuntime openSchemaDiffForRoutine", () => {
it("prefills schema diff with a signature-aware routine key", () => {
const { connectionStore, runtime } = setup({