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
View File
@@ -10,6 +10,7 @@ import {
formatMongoShellLiteral,
MONGO_DOCUMENT_GRID_NULL,
mongoDocumentDisplayValue,
mongoDocumentDisplayText,
mongoDocumentGridDisplayText,
mongoDocumentGridClipboardText,
mongoDocumentGridEditorText,
@@ -18,6 +19,7 @@ import {
mongoDocumentGridValue,
mongoDocumentGridColumnTypes,
mongoDocumentIdForGrid,
mongoDocumentRelaxedExtendedJson,
parseMongoDocumentInputValue,
serializeMongoDocumentId,
} from "../../apps/desktop/src/lib/mongo/mongoDocumentValues.ts";
@@ -344,6 +346,28 @@ test("keeps normal Mongo values readable and unsafe Int64 editable", () => {
assert.deepEqual(parseMongoDocumentInputValue('NumberLong("9007199254740993")'), { $numberLong: "9007199254740993" });
});
test("renders Mongo structures as shell-style literals like mongosh", () => {
assert.equal(mongoDocumentDisplayText({ $oid: "6743e4bfa3f6f84bc3fff6c8" }), 'ObjectId("6743e4bfa3f6f84bc3fff6c8")');
assert.equal(mongoDocumentDisplayText({ $numberLong: "2326645729978441729" }), 'NumberLong("2326645729978441729")');
assert.equal(mongoDocumentDisplayText({ $numberInt: "42" }), 'NumberInt("42")');
assert.equal(mongoDocumentDisplayText({ $date: "2026-06-10T13:59:31.287Z" }), 'ISODate("2026-06-10T13:59:31.287Z")');
assert.equal(mongoDocumentDisplayText({ $date: { $numberLong: "1781099971287" } }), 'ISODate("2026-06-10T13:59:31.287Z")');
assert.equal(mongoDocumentDisplayText({ $minKey: 1 }), "MinKey()");
assert.equal(mongoDocumentDisplayText(['ISODate("2026-06-10T13:59:31.287Z")', { $oid: "507f1f77bcf86cd799439011" }, 2]), '[ISODate("2026-06-10T13:59:31.287Z"), ObjectId("507f1f77bcf86cd799439011"), 2]');
assert.equal(mongoDocumentDisplayText({ note: "[1,2]", nested: [[1], []] }), '{"note": "[1,2]", "nested": [[1], []]}');
assert.equal(mongoDocumentDisplayText({ $binary: { base64: "AQI=", subType: "00" } }), '{"$binary": {"base64": "AQI=", "subType": "00"}}');
});
test("converts browser-form dates to relaxed Extended JSON for document previews", () => {
assert.deepEqual(mongoDocumentRelaxedExtendedJson({ at: 'ISODate("2026-06-10T13:59:31.287Z")', keep: "ISODate-ish text" }), {
at: { $date: "2026-06-10T13:59:31.287Z" },
keep: "ISODate-ish text",
});
assert.deepEqual(mongoDocumentRelaxedExtendedJson({ items: [{ by: { $oid: "507f1f77bcf86cd799439011" }, at: 'new Date("2026-06-10T13:59:31.287Z")' }] }), {
items: [{ by: { $oid: "507f1f77bcf86cd799439011" }, at: { $date: "2026-06-10T13:59:31.287Z" } }],
});
});
test("builds edits for Int32, Double, Date, and unsafe Int64", () => {
const changes = new Map<number, string | number | boolean | null>([
[1, 42],
+15 -2
View File
@@ -38,7 +38,7 @@ import {
splitMongoCommandRanges,
} from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts";
import type { MongoWriteCommand } from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts";
import { buildMongoUpdateDocument as buildMongoDocumentUpdate, formatMongoShellLiteral as formatMongoDocumentShellLiteral } from "../../apps/desktop/src/lib/mongo/mongoDocumentValues.ts";
import { buildMongoUpdateDocument as buildMongoDocumentUpdate, formatMongoShellLiteral as formatMongoDocumentShellLiteral, mongoDocumentGridDisplayText, mongoDocumentGridEditorText, mongoDocumentGridValue } from "../../apps/desktop/src/lib/mongo/mongoDocumentValues.ts";
import { normalizeJsonArgument } from "../mongo-shell/src/json.ts";
test("parseMongoFindCommand parses db collection find with an empty JSON filter", () => {
@@ -1751,7 +1751,7 @@ test("mongoDocumentsToQueryResult turns mongo documents into grid rows", () => {
assert.deepEqual(result.columns, ["_id", "name", "profile", "active"]);
assert.deepEqual(result.rows, [
["1", "Ada", '{"role":"admin"}', null],
["1", "Ada", mongoDocumentGridValue({ role: "admin" }), null],
["2", "Lin", null, true],
]);
assert.deepEqual(result.mongo_documents, [
@@ -1763,6 +1763,19 @@ test("mongoDocumentsToQueryResult turns mongo documents into grid rows", () => {
assert.equal(result.truncated, true);
});
test("mongoDocumentsToQueryResult displays array structures with shell-style BSON literals", () => {
const document = {
_id: "1",
history: [{ at: 'ISODate("2026-06-10T13:59:31.287Z")', by: { $oid: "507f1f77bcf86cd799439011" } }],
};
const result = mongoDocumentsToQueryResult([document], 5, 1);
const historyCell = result.rows[0]?.[1];
assert.equal(typeof historyCell, "string");
assert.equal(mongoDocumentGridDisplayText(historyCell), '[{"at": ISODate("2026-06-10T13:59:31.287Z"), "by": ObjectId("507f1f77bcf86cd799439011")}]');
assert.equal(mongoDocumentGridEditorText(historyCell), JSON.stringify(document.history));
});
test("mongoDocumentsToQueryResult keeps aligned extended documents for copying", () => {
const documents = [{ _id: { $oid: "6743e4bfa3f6f84bc3fff6c8" }, createdAt: 'ISODate("2026-07-24T00:00:00Z")' }];
const copyDocuments = [{ _id: { $oid: "6743e4bfa3f6f84bc3fff6c8" }, createdAt: { $date: "2026-07-24T00:00:00Z" } }];