fix(mongodb): display array structures as shell-style bson literals

This commit is contained in:
onenewcode authored and GitHub committed 2026-09-29 12:22:42 +08:00
1 parent 27078b8eed
commit cf78eebbc9
8 files changed
+223 -34

No files matched your search

@@ -24,7 +24,7 @@ describe("MongoDB document date display", () => {
it("formats nested dates without changing non-date BSON values", () => {
const value = mongoDocumentGridValue({ event: { at: { $date: "2026-09-24T02:00:00.000Z" }, sequence: { $numberLong: "9007199254740993" } }, tags: ["a"] });
expect(mongoDocumentGridDisplayText(value, dateFormatter("Asia/Shanghai"))).toBe('{"event":{"at":"2026-09-24 10:00:00","sequence":{"$numberLong":"9007199254740993"}},"tags":["a"]}');
expect(mongoDocumentGridDisplayText(value, dateFormatter("Asia/Shanghai"))).toBe('{"event": {"at": "2026-09-24 10:00:00", "sequence": NumberLong("9007199254740993")}, "tags": ["a"]}');
});
it("uses local time when no timezone override is configured", () => {
@@ -32,9 +32,9 @@ describe("MongoDB document date display", () => {
expect(mongoDocumentGridDisplayText(value, dateFormatter(undefined))).toMatch(/^2026-09-2[34] \d{2}:00:00$/);
});
it("falls back to the original Extended JSON for an invalid timezone", () => {
it("falls back to the shell-style display for an invalid timezone", () => {
const value = mongoDocumentGridValue({ nested: { $date: "2026-09-24T02:00:00.000Z" } });
expect(mongoDocumentGridDisplayText(value, dateFormatter("Invalid/Timezone"))).toBe('{"nested":{"$date":"2026-09-24T02:00:00.000Z"}}');
expect(mongoDocumentGridDisplayText(value, dateFormatter("Invalid/Timezone"))).toBe('{"nested": ISODate("2026-09-24T02:00:00.000Z")}');
});
it("keeps edit, copy, and external values independent from display formatting", () => {
@@ -52,3 +52,38 @@ describe("MongoDB document date display", () => {
expect(mongoDocumentGridDisplayText(value, dateFormatter("Asia/Shanghai"))).toBeUndefined();
});
});
describe("MongoDB array structure display", () => {
it("renders arrays with shell-style BSON literals instead of the internal JSON encoding", () => {
const value = mongoDocumentGridValue({
ids: [{ $oid: "6743e4bfa3f6f84bc3fff6c8" }],
history: [{ at: 'ISODate("2026-06-10T13:59:31.287Z")', by: { $oid: "507f1f77bcf86cd799439011" } }],
counts: [{ $numberLong: "2326645729978441729" }, 2],
});
expect(mongoDocumentGridDisplayText(value)).toBe('{"ids": [ObjectId("6743e4bfa3f6f84bc3fff6c8")], "history": [{"at": ISODate("2026-06-10T13:59:31.287Z"), "by": ObjectId("507f1f77bcf86cd799439011")}], "counts": [NumberLong("2326645729978441729"), 2]}');
});
it("keeps the structure separators readable for scalar arrays and nested containers", () => {
expect(mongoDocumentGridDisplayText(mongoDocumentGridValue(["a", "b"]))).toBe('["a", "b"]');
expect(mongoDocumentGridDisplayText(mongoDocumentGridValue({ tags: [], matrix: [[1, 2], [3]] }))).toBe('{"tags": [], "matrix": [[1, 2], [3]]}');
});
it("still treats JSON-shaped BSON strings as data, not as structure", () => {
expect(mongoDocumentGridDisplayText(mongoDocumentGridValue({ note: "[1,2]" }))).toBe('{"note": "[1,2]"}');
});
it("keeps edit, copy, and external JSON untouched by the shell-style display", () => {
const original = { items: [{ at: { $date: "2026-06-10T13:59:31.287Z" } }] };
const value = mongoDocumentGridValue(original) as string;
const rawJson = JSON.stringify(original);
expect(mongoDocumentGridDisplayText(value)).toBe('{"items": [{"at": ISODate("2026-06-10T13:59:31.287Z")}]}');
expect(mongoDocumentGridEditorText(value)).toBe(rawJson);
expect(mongoDocumentGridClipboardText(value)).toBe(rawJson);
expect(mongoDocumentGridExternalValue(value)).toBe(rawJson);
});
it("renders out-of-range canonical dates as the raw wrapper instead of throwing", () => {
const value = mongoDocumentGridValue({ d: { $date: { $numberLong: "9223372036854775807" } } });
expect(mongoDocumentGridDisplayText(value)).toBe('{"d": {"$date": NumberLong("9223372036854775807")}}');
});
});
@@ -147,6 +147,93 @@ export function mongoDocumentDisplayValue(value: unknown): unknown {
return value;
}
/**
* Strings that are already shell-style BSON display literals. The driver ships
* dates as `ISODate("…")` text and the grid renders other typed scalars the same
* way, so inside arrays and subdocuments they must stay verbatim instead of
* being re-quoted (which produced the escaped `ISODate(\"…\")` mess).
*/
const MONGO_SHELL_DISPLAY_LITERAL_PATTERN = /^(?:ISODate|new Date|ObjectId|NumberLong|NumberInt|NumberDouble|NumberDecimal)\(\s*(["']).+\1\s*\)$/;
/**
* Renders a browser/extended-JSON BSON value the way mongosh prints it, so an
* array cell reads `[ObjectId("…"), ISODate("…")]` instead of the internal
* `[{"$oid":"…"},"ISODate(\\\"…\\\")"]` encoding. Display-only: edit, copy and
* save paths keep the original JSON representation untouched.
*/
export function mongoDocumentDisplayText(value: unknown): string {
if (value === null || value === undefined) return "null";
if (typeof value === "number" || typeof value === "boolean") return String(value);
if (typeof value === "string") return MONGO_SHELL_DISPLAY_LITERAL_PATTERN.test(value) ? value : JSON.stringify(value);
if (Array.isArray(value)) return `[${value.map((item) => mongoDocumentDisplayText(item)).join(", ")}]`;
if (typeof value === "object") {
const object = value as Record<string, unknown>;
const wrapper = mongoShellWrapperDisplayText(object);
if (wrapper !== undefined) return wrapper;
return `{${Object.entries(object)
.map(([key, item]) => `${JSON.stringify(key)}: ${mongoDocumentDisplayText(item)}`)
.join(", ")}}`;
}
return JSON.stringify(String(value));
}
function mongoShellWrapperDisplayText(object: Record<string, unknown>): string | undefined {
const keys = Object.keys(object);
if (keys.length !== 1) return undefined;
const inner = object[keys[0] ?? ""];
switch (keys[0]) {
case "$oid":
return typeof inner === "string" ? `ObjectId(${JSON.stringify(inner)})` : undefined;
case "$numberLong":
return typeof inner === "string" ? `NumberLong(${JSON.stringify(inner)})` : undefined;
case "$numberInt":
return typeof inner === "string" ? `NumberInt(${JSON.stringify(inner)})` : undefined;
case "$numberDouble":
return typeof inner === "string" ? `NumberDouble(${JSON.stringify(inner)})` : undefined;
case "$numberDecimal":
return typeof inner === "string" ? `NumberDecimal(${JSON.stringify(inner)})` : undefined;
case "$date":
return mongoShellDateWrapperDisplayText(inner);
case "$minKey":
return "MinKey()";
case "$maxKey":
return "MaxKey()";
default:
return undefined;
}
}
function mongoShellDateWrapperDisplayText(inner: unknown): string | undefined {
if (typeof inner === "string") return `ISODate(${JSON.stringify(inner)})`;
if (inner && typeof inner === "object" && !Array.isArray(inner)) {
const long = (inner as Record<string, unknown>).$numberLong;
if (typeof long === "string" && /^-?\d+$/.test(long)) {
const millis = Number(long);
// Dates beyond the JS Date range cannot round-trip through toISOString();
// fall back to the raw wrapper instead of throwing past the grid formatter.
if (Number.isFinite(millis) && Math.abs(millis) <= 8.64e15) {
return `ISODate(${JSON.stringify(new Date(millis).toISOString())})`;
}
}
}
return undefined;
}
/**
* Relaxed Extended JSON for read-only document previews: the browser form keeps
* dates as `ISODate("…")` strings, and the preview must show them as
* `{"$date": "…"}` like MongoDB Compass instead of escaped shell text.
*/
export function mongoDocumentRelaxedExtendedJson(value: unknown): unknown {
const date = mongoShellDateToExtendedJson(value);
if (date !== value) return date;
if (Array.isArray(value)) return value.map((item) => mongoDocumentRelaxedExtendedJson(item));
if (value && typeof value === "object") {
return Object.fromEntries(Object.entries(value as Record<string, unknown>).map(([key, item]) => [key, mongoDocumentRelaxedExtendedJson(item)]));
}
return value;
}
/**
* Maps BSON values into the flat collection-grid representation. The grid
* needs an internal sentinel for an explicit BSON null because an empty cell
@@ -195,14 +282,19 @@ export function mongoDocumentGridDisplayText(value: unknown, formatter?: ColumnF
}
function formatMongoDocumentGridJson(json: string, formatter: ColumnFormatterConfig | undefined): string {
if (formatter?.kind !== "datetime" || !validMongoDisplayTimeZone(formatter.timezone)) return json;
let value: unknown;
try {
const transformed = formatMongoDocumentDates(JSON.parse(json), formatter);
if (!transformed.changed) return json;
return typeof transformed.value === "string" ? transformed.value : JSON.stringify(transformed.value);
value = JSON.parse(json);
} catch {
return json;
}
if (formatter?.kind === "datetime" && validMongoDisplayTimeZone(formatter.timezone)) {
const transformed = formatMongoDocumentDates(value, formatter);
if (transformed.changed) return typeof transformed.value === "string" ? transformed.value : mongoDocumentDisplayText(transformed.value);
}
// Shell-style structure text keeps BSON types inside arrays/subdocuments
// readable (`[ObjectId("…"), ISODate("…")]`) instead of raw JSON wrappers.
return mongoDocumentDisplayText(value);
}
function validMongoDisplayTimeZone(timeZone: string | undefined): boolean {
@@ -454,7 +546,8 @@ export function mongoDocumentIdForGrid(value: unknown): MongoInputValue {
return JSON.stringify(value);
}
function isMongoExtendedJsonId(value: unknown): value is Record<string, unknown> {
/** A single typed scalar wrapper (`{$oid}` / `{$numberLong}`) the grid shows compactly. */
export function isMongoExtendedJsonId(value: unknown): value is Record<string, unknown> {
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
const object = value as Record<string, unknown>;
const keys = Object.keys(object);
@@ -1,5 +1,5 @@
import type { QueryResult } from "@/types/database";
import { mongoDocumentIdForGrid } from "@/lib/mongo/mongoDocumentValues";
import { isMongoExtendedJsonId, mongoDocumentGridValue, mongoDocumentIdForGrid } from "@/lib/mongo/mongoDocumentValues";
import {
chainedMethodCallPattern,
describeMongoCommandParseFailure as describeMongoCommandParseFailureBasic,
@@ -1136,7 +1136,17 @@ export function evaluateMongoAggregateSafety(command: MongoAggregateCommand, opt
return { allowed: true };
}
export function mongoDocumentsToQueryResult(documents: unknown[], executionTimeMs: number, total: number, copyDocuments?: unknown[], totalIsExact = true): QueryResult {
export interface MongoDocumentsToQueryResultOptions {
/**
* Encode arrays/subdocuments with the collection grid's BSON-faithful values so
* display, editing and clipboard share one representation. Export paths that
* need source text pass `false` and keep plain JSON cells.
*/
documentGridValues?: boolean;
}
export function mongoDocumentsToQueryResult(documents: unknown[], executionTimeMs: number, total: number, copyDocuments?: unknown[], totalIsExact = true, options: MongoDocumentsToQueryResultOptions = {}): QueryResult {
const documentGridValues = options.documentGridValues !== false;
const columns: string[] = [];
for (const doc of documents) {
@@ -1150,8 +1160,8 @@ export function mongoDocumentsToQueryResult(documents: unknown[], executionTimeM
}
const rows = documents.map((doc) => {
if (isRecord(doc)) return columns.map((column) => toCellValue(doc[column]));
return columns.map((column) => (column === "value" ? toCellValue(doc) : null));
if (isRecord(doc)) return columns.map((column) => toCellValue(doc[column], documentGridValues));
return columns.map((column) => (column === "value" ? toCellValue(doc, documentGridValues) : null));
});
return {
@@ -2030,8 +2040,12 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
function toCellValue(value: unknown): string | number | boolean | null {
function toCellValue(value: unknown, documentGridValues = false): string | number | boolean | null {
if (value === undefined || value === null) return null;
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
return mongoDocumentIdForGrid(value);
// Typed scalar ids keep their compact grid display; arrays and subdocuments take the
// document-grid encoding so display, editing and clipboard share the collection grid's
// representation instead of dumping the internal JSON encoding into the cell.
if (isMongoExtendedJsonId(value)) return mongoDocumentIdForGrid(value);
return documentGridValues ? (mongoDocumentGridValue(value) as string) : mongoDocumentIdForGrid(value);
}