feat(mongodb): distinguish missing fields from BSON null in grid

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-10 11:59:28 +08:00
1 parent 464f2f33c5
commit 0f07d337c3
16 files changed
+542 -69

No files matched your search

@@ -0,0 +1,62 @@
// @vitest-environment happy-dom
import { computed, ref } from "vue";
import { describe, expect, it, vi } from "vitest";
import { useDataGridCellDetailEdit } from "@/composables/useDataGridCellDetailEdit";
import { MONGO_DOCUMENT_GRID_NULL } from "@/lib/mongo/mongoDocumentValues";
import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
function detail(value: string): DataGridCellDetail {
return {
rowNumber: 1,
rowId: 3,
colIndex: 1,
column: "value",
type: "",
comment: "",
value,
rawValue: value,
rawValuePreview: value,
displayValue: "NULL",
displayValuePreview: "NULL",
isValuePreviewTruncated: false,
imagePreviewUrl: null,
length: value.length,
formattedJson: "",
isEditable: true,
};
}
describe("useDataGridCellDetailEdit", () => {
it("preserves the Mongo collection null marker in detail editing and set-null", async () => {
const activeDetail = ref<DataGridCellDetail | null>(detail(MONGO_DOCUMENT_GRID_NULL));
const applyCellValue = vi.fn();
const editorText = (value: string | number | boolean | null) => (value === MONGO_DOCUMENT_GRID_NULL ? "NULL" : String(value ?? ""));
const editor = useDataGridCellDetailEdit({
activeDetail: computed(() => activeDetail.value),
activeTab: ref("details"),
jsonFormatted: computed(() => false),
databaseType: computed(() => "mongodb"),
resultRows: computed(() => [["id", MONGO_DOCUMENT_GRID_NULL]]),
getColumnInfo: () => undefined,
cellEditorText: editorText,
nullValue: () => MONGO_DOCUMENT_GRID_NULL,
getRowItem: () => ({ sourceIndex: 0, isNew: false, isDeleted: false }),
hydrateLargeValueCell: async () => true,
applyCellValue,
restoreCellValue: vi.fn(),
syncEditor: vi.fn(),
refreshDetail: vi.fn(),
warnFormattedJsonEdit: vi.fn(),
});
await editor.startDetailEdit();
expect(editor.detailEditValue.value).toBe("NULL");
editor.commitDetailEdit();
expect(applyCellValue).toHaveBeenLastCalledWith(3, 1, MONGO_DOCUMENT_GRID_NULL);
editor.setDetailNull();
expect(applyCellValue).toHaveBeenLastCalledWith(3, 1, MONGO_DOCUMENT_GRID_NULL);
});
});
@@ -2,6 +2,8 @@
import { computed, nextTick, ref, type Ref } from "vue";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { clearDataGridPendingSnapshot, DATA_GRID_QUICK_ENTRY_DRAFT_ROW_ID, useDataGridEditor } from "@/composables/useDataGridEditor";
import { clearDataGridClipboardCopy, parseDataGridClipboard, rememberDataGridClipboardCopy } from "@/lib/dataGrid/dataGridClipboard";
import { buildMongoUpdateDocument, MONGO_DOCUMENT_GRID_NULL, mongoDocumentGridInputValue, mongoDocumentGridValue } from "@/lib/mongo/mongoDocumentValues";
import type { CellValue } from "@/lib/dataGrid/cellValue";
const mocks = vi.hoisted(() => ({
@@ -46,6 +48,7 @@ function createEditorWithResult(
existingRows: CellValue[][] = [],
onCellValueChanged?: (rowId: number, columnIndex: number) => void,
tableColumns?: Array<{ name: string; data_type: string; extra?: string; column_default?: string }>,
mongoCollectionGrid = false,
) {
let editor: ReturnType<typeof useDataGridEditor>;
const result = ref<{ columns: string[]; rows: CellValue[][] }>({
@@ -56,7 +59,8 @@ function createEditorWithResult(
editor = useDataGridEditor({
result: computed(() => result.value),
editable: computed(() => true),
databaseType: computed(() => "postgres"),
databaseType: computed(() => (mongoCollectionGrid ? "mongodb" : "postgres")),
normalizeEditorInput: mongoCollectionGrid ? mongoDocumentGridInputValue : undefined,
connectionId: computed(() => "connection-1"),
database: computed(() => "app"),
tableMeta: computed(() => ({
@@ -1188,3 +1192,21 @@ describe("useDataGridEditor cell edit focus", () => {
vi.unstubAllGlobals();
});
});
describe("Mongo collection-grid clipboard round-trip", () => {
it.each([null, MONGO_DOCUMENT_GRID_NULL, "\u0000dbx:mongo-document-grid:string:literal", "NULL"])("preserves BSON value %j through paste and save", (bsonValue) => {
const encoded = mongoDocumentGridValue(bsonValue) as string;
const editor = createEditor(undefined, true, undefined, undefined, [["before", "", ""]], undefined, undefined, true);
try {
// The OS clipboard is display text; the internal matrix retains encoding.
rememberDataGridClipboardCopy("copied text", [[encoded]]);
const pasted = parseDataGridClipboard("copied text")[0]![0]!;
editor.applyCellValue(0, 0, pasted);
const changes = editor.dirtyRows.value.get(0)!;
expect(changes.get(0)).toBe(encoded);
expect(buildMongoUpdateDocument(changes, ["value"], { value: "before" })).toEqual({ $set: { value: bsonValue } });
} finally {
clearDataGridClipboardCopy();
}
});
});
@@ -1,4 +1,5 @@
import { computed, ref } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useDataGridExport, type UseDataGridExportOptions } from "@/composables/useDataGridExport";
import type { DatabaseType } from "@/types/database";
@@ -7,9 +8,10 @@ import { copyToClipboard } from "@/lib/common/clipboard";
import type { DataGridTableMeta } from "@/lib/dataGrid/dataGridSql";
import type { CellSelectionMatrix, SelectionData } from "@/lib/dataGrid/gridSelection";
import type { CellValue } from "@/lib/dataGrid/cellValue";
import { extractDataGridSelection, exportQueryResultJson } from "@/lib/backend/api";
import { extractDataGridSelection, exportQueryResultCsv, exportQueryResultJson } from "@/lib/backend/api";
import { DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS } from "@/lib/dataGrid/dataGridCopyExtractor";
import { clearDataGridClipboardCopy, parseDataGridClipboard } from "@/lib/dataGrid/dataGridClipboard";
import { MONGO_DOCUMENT_GRID_NULL, mongoDocumentGridExternalValue } from "@/lib/mongo/mongoDocumentValues";
const toast = vi.fn();
@@ -46,6 +48,7 @@ vi.mock("@/lib/backend/api", async (importOriginal) => {
return {
...original,
extractDataGridSelection: vi.fn(),
exportQueryResultCsv: vi.fn(),
exportQueryResultJson: vi.fn(),
};
});
@@ -72,6 +75,7 @@ function createMongoExportState(options: {
contextColumn?: number;
syntheticContext?: boolean;
fullExportResult?: UseDataGridExportOptions["fullExportResult"];
externalCellValue?: UseDataGridExportOptions["externalCellValue"];
}) {
const items = options.items ?? [options.item];
const selectedRowIds = options.selectedRowIds ?? new Set<number>();
@@ -102,6 +106,7 @@ function createMongoExportState(options: {
selectedRowIds: ref(selectedRowIds),
hasRowSelection: computed(() => selectedRowIds.size > 0),
fullExportResult: options.fullExportResult,
externalCellValue: options.externalCellValue,
};
return useDataGridExport(state);
}
@@ -1513,6 +1518,74 @@ describe("useDataGridExport prepared row statements", () => {
expect(copyToClipboard).not.toHaveBeenCalled();
});
it("restores Mongo collection-grid values before bulk copy, extractors, and CSV export", async () => {
const item = { ...row(["1", MONGO_DOCUMENT_GRID_NULL]), sourceIndex: 0 };
const state = createMongoExportState({
columns: ["_id", "nullable"],
item,
mongoDocuments: [{ _id: "1", nullable: null }],
externalCellValue: (value) => mongoDocumentGridExternalValue(value) as CellValue,
selectedCellMatrix: {
rowIndexes: [0],
columnIndexes: [1],
columns: ["nullable"],
rows: [[MONGO_DOCUMENT_GRID_NULL]],
},
});
await state.copyAll();
expect(copyToClipboard).toHaveBeenCalledWith("_id\tnullable\n1\t");
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: "", mimeType: "text/tab-separated-values", fileExtension: "tsv", rowCount: 1, columnCount: 1 });
await expect(state.copyWithExtractor("tsv")).resolves.toBe(true);
expect(extractDataGridSelection).toHaveBeenCalledWith(expect.objectContaining({ rows: [[null]] }));
setActivePinia(createPinia());
await state.exportCurrentPageCsv();
expect(exportQueryResultCsv).toHaveBeenCalledWith(expect.any(String), ["_id", "nullable"], [["1", null]], expect.anything());
const reservedString = MONGO_DOCUMENT_GRID_NULL;
const fullExportState = createMongoExportState({
columns: ["_id", "value"],
item: { ...row(["1", mongoDocumentGridExternalValue(reservedString)]), sourceIndex: 0 },
mongoDocuments: [{ _id: "1", value: reservedString }],
externalCellValue: (value) => mongoDocumentGridExternalValue(value) as CellValue,
fullExportResult: async () => ({
columns: ["_id", "value"],
column_types: ["", ""],
rows: [["1", reservedString]],
mongo_documents: [{ _id: "1", value: reservedString }],
affected_rows: 1,
execution_time_ms: 1,
}),
});
await fullExportState.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["_id", "value"], [["1", reservedString]], expect.anything());
});
it("exports missing Mongo fields as null while retaining explicit empty strings", async () => {
const columns = ["_id", "missing", "nullable", "empty"];
const document = { _id: "1", nullable: null, empty: "" };
const item = { ...row(["1", "", MONGO_DOCUMENT_GRID_NULL, ""]), sourceIndex: 0 };
const state = createMongoExportState({
columns,
item,
mongoDocuments: [document],
externalCellValue: mongoDocumentGridExternalValue,
fullExportResult: async () => ({
columns,
column_types: ["", "", "", ""],
rows: [item.data],
mongo_copy_documents: [document],
affected_rows: 1,
execution_time_ms: 1,
}),
});
await state.exportJson();
expect(exportQueryResultJson).toHaveBeenCalledWith(expect.any(String), columns, [["1", null, null, ""]]);
});
it("preserves Mongo Extended JSON objects and dates in JSON exports", async () => {
const columns = ["_id", "valueMap", "createdTime"];
const mongoCopyDocument = {