fix(mongodb): preserve document types when copying data

This commit is contained in:
zipg authored and GitHub committed 2026-07-13 16:15:02 +08:00
1 parent cc9b802b49
commit c2c4860a28
7 files changed
+159 -11

No files matched your search

@@ -55,6 +55,34 @@ function row(data: unknown[]) {
};
}
function createMongoExportState(options: { columns: string[]; item: ReturnType<typeof row> & { sourceIndex: number }; mongoDocuments: unknown[] }) {
const state: UseDataGridExportOptions = {
columns: computed(() => options.columns),
displayItems: computed(() => [options.item]),
sql: computed(() => undefined),
tableMeta: computed(() => undefined),
copyInsertTargetLabel: computed(() => "documents"),
databaseType: computed(() => "mongodb"),
connectionId: computed(() => "connection-1"),
database: computed(() => "dbx"),
context: computed(() => "results"),
sourceColumns: computed(() => options.columns),
mongoDocuments: computed(() => options.mongoDocuments),
columnTypes: computed(() => undefined),
whereInput: computed(() => undefined),
orderBy: computed(() => undefined),
exportBatchSize: computed(() => 1000),
hasCellSelection: computed(() => false),
selectedCells: computed(() => ({ columns: [], rows: [] })),
selectedRange: computed(() => null),
contextCell: ref({ rowId: options.item.id, rowIndex: 0, col: -1 }),
getRowItem: (rowId) => (rowId === options.item.id ? options.item : undefined),
selectedRowIds: ref(new Set<number>()),
hasRowSelection: computed(() => false),
};
return useDataGridExport(state);
}
function createExportState(tableMeta: DataGridTableMeta, columns = tableMeta.columns?.map((column) => column.name) ?? ["id", "name"]) {
const item = row(columns.map((column, index) => (column === "id" ? 1 : `value-${index}`)));
const options: UseDataGridExportOptions = {
@@ -166,4 +194,42 @@ describe("useDataGridExport prepared row statements", () => {
expect(toast).toHaveBeenCalledWith("grid.copyFailed: update builder unavailable", 5000);
expect(copyToClipboard).not.toHaveBeenCalled();
});
it("copies Mongo JSON from the original document using the sorted source index and visible columns", async () => {
const item = { ...row(["true", '{"role":"admin"}']), sourceIndex: 1 };
const state = createMongoExportState({
columns: ["booleanText", "profile"],
item,
mongoDocuments: [
{ booleanText: "wrong row", profile: { role: "viewer" } },
{ booleanText: "true", profile: { role: "admin" }, hidden: "not selected" },
],
});
await state.copyRow();
expect(copyToClipboard).toHaveBeenCalledWith(JSON.stringify({ booleanText: "true", profile: { role: "admin" } }, null, 2));
});
it("preserves original Mongo string types in INSERT and applies explicit edits", async () => {
const item = { ...row(["123", "true", '{"kind":"literal"}', "2024-01-01 00:00:00", '{"role":"maintainer"}']), sourceIndex: 0 };
item.isDirtyCol = [false, false, false, false, true];
const state = createMongoExportState({
columns: ["numericText", "booleanText", "jsonText", "dateText", "profile"],
item,
mongoDocuments: [
{
numericText: "123",
booleanText: "true",
jsonText: '{"kind":"literal"}',
dateText: "2024-01-01 00:00:00",
profile: { role: "admin" },
},
],
});
await state.copyRowAsInsert();
expect(copyToClipboard).toHaveBeenCalledWith('db.getCollection("documents").insert({"numericText":"123","booleanText":"true","jsonText":"{\\"kind\\":\\"literal\\"}","dateText":"2024-01-01 00:00:00","profile":{"role":"maintainer"}});');
});
});
@@ -12,7 +12,7 @@ import { formatSqlInsert } from "@/lib/export/exportFormats";
import { uuid } from "@/lib/common/utils";
import { useSettingsStore } from "@/stores/settingsStore";
import { expandNestedJsonStringsForCopy } from "@/lib/common/jsonCopyValue";
import { buildMongoCopyInsertDocument, formatMongoShellLiteral, type MongoInputValue } from "@/lib/mongo/mongoDocumentValues";
import { buildMongoCopyDocumentFromOriginal, buildMongoCopyInsertDocument, formatMongoShellLiteral, type MongoInputValue } from "@/lib/mongo/mongoDocumentValues";
import type { DatabaseType, QueryResult } from "@/types/database";
import type { QueryResultExportRequest } from "@/lib/backend/api";
import { DBX_ROWID_COLUMN } from "@/lib/table/tableEditing";
@@ -40,6 +40,7 @@ export interface UseDataGridExportOptions {
database: ComputedRef<string | undefined>;
context: ComputedRef<"results" | "table-data" | undefined>;
sourceColumns: ComputedRef<Array<string | undefined> | undefined>;
mongoDocuments?: ComputedRef<unknown[] | undefined>;
columnTypes: ComputedRef<Array<string | undefined> | undefined>;
whereInput: ComputedRef<string | undefined>;
orderBy: ComputedRef<string | undefined>;
@@ -233,7 +234,9 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
}
function insertCopyKey(excludePrimaryKeys: boolean, insertMode: DataGridCopyInsertMode): string {
const rows = copyStatementRowsKey(insertEligibleRows());
const eligibleRows = insertEligibleRows();
const rows = copyStatementRowsKey(eligibleRows);
const originalMongoDocuments = eligibleRows.map((item) => (item.sourceIndex === undefined ? undefined : options.mongoDocuments?.value?.[item.sourceIndex]));
return JSON.stringify({
databaseType: databaseType.value ?? null,
schema: tableMeta.value?.schema ?? null,
@@ -245,12 +248,13 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
excludePrimaryKeys,
insertMode,
rows,
originalMongoDocuments,
});
}
function copyStatementRowsKey(rows: RowItem[]): Array<{ id: number; data: CellValue[] }> {
function copyStatementRowsKey(rows: RowItem[]): Array<{ id: number; sourceIndex?: number; data: CellValue[]; isDirtyCol: boolean[] }> {
// Prepared copy SQL depends on current cell values; edited rows keep the same id while their data changes.
return rows.map((item) => ({ id: item.id, data: item.data }));
return rows.map((item) => ({ id: item.id, sourceIndex: item.sourceIndex, data: item.data, isDirtyCol: item.isDirtyCol }));
}
function insertCopyCache(excludePrimaryKeys: boolean, insertMode: DataGridCopyInsertMode): CopyStatementCache {
@@ -309,7 +313,8 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
collection: copyInsertTargetLabel?.value || tableMeta.value?.tableName || "collection",
columns: columns.value,
sourceColumns: sourceColumns.value,
rows: rows.map((item) => item.data),
rows,
mongoDocuments: options.mongoDocuments?.value,
excludePrimaryKeys,
insertMode,
})
@@ -516,6 +521,11 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
}
function rowToJsonObject(item: RowItem): Record<string, unknown> {
if (options.databaseType.value === "mongodb" && item.sourceIndex !== undefined) {
const original = options.mongoDocuments?.value?.[item.sourceIndex];
const document = buildMongoCopyDocumentFromOriginal(original, item.data as MongoInputValue[], columns.value, item.isDirtyCol);
if (document) return document;
}
const obj: Record<string, unknown> = {};
columns.value.forEach((col, i) => {
obj[col] = item.data[i];
@@ -526,7 +536,8 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
async function copyRowsAsJson(items: RowItem[]) {
if (items.length === 0) return;
const value = items.length === 1 ? rowToJsonObject(items[0]) : items.map(rowToJsonObject);
const copyValue = options.databaseType.value === "mongodb" ? expandNestedJsonStringsForCopy(value) : value;
const hasOriginalMongoDocuments = options.databaseType.value === "mongodb" && items.every((item) => item.sourceIndex !== undefined && options.mongoDocuments?.value?.[item.sourceIndex] !== undefined);
const copyValue = options.databaseType.value === "mongodb" && !hasOriginalMongoDocuments ? expandNestedJsonStringsForCopy(value) : value;
await copyText(JSON.stringify(copyValue, null, 2));
}
@@ -1241,12 +1252,17 @@ function replaceControlCharacters(value: string, replacement: string): string {
.join("");
}
function buildMongoCopyInsertStatement(options: { collection: string; columns: string[]; sourceColumns?: Array<string | undefined>; rows: CellValue[][]; excludePrimaryKeys?: boolean; insertMode?: DataGridCopyInsertMode }): string | undefined {
function buildMongoCopyInsertStatement(options: { collection: string; columns: string[]; sourceColumns?: Array<string | undefined>; rows: RowItem[]; mongoDocuments?: unknown[]; excludePrimaryKeys?: boolean; insertMode?: DataGridCopyInsertMode }): string | undefined {
const saveColumns = effectiveColumns(options.sourceColumns, options.columns);
const columnIndexes = saveColumns.map((column, index) => ({ column, index })).filter((item): item is { column: string; index: number } => !!item.column);
if (columnIndexes.length === 0 || options.rows.length === 0) return undefined;
const documentColumns = columnIndexes.map((item) => item.column);
const documents = options.rows.map((row) => buildMongoCopyInsertDocument(columnIndexes.map((item) => row[item.index]) as MongoInputValue[], documentColumns, { excludePrimaryKeys: options.excludePrimaryKeys }));
const documents = options.rows.map((item) => {
const row = columnIndexes.map(({ index }) => item.data[index]) as MongoInputValue[];
const dirtyColumns = columnIndexes.map(({ index }) => item.isDirtyCol[index] ?? false);
const original = item.sourceIndex === undefined ? undefined : options.mongoDocuments?.[item.sourceIndex];
return buildMongoCopyDocumentFromOriginal(original, row, documentColumns, dirtyColumns, { excludePrimaryKeys: options.excludePrimaryKeys }) ?? buildMongoCopyInsertDocument(row, documentColumns, { excludePrimaryKeys: options.excludePrimaryKeys });
});
const collection = `db.getCollection(${JSON.stringify(options.collection)})`;
if (documents.length === 1) return `${collection}.insert(${formatMongoShellLiteral(documents[0])});`;
if (options.insertMode === "row-by-row") {