Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): format Mongo shell date values

kiritocd51e247cbb8
diff --git a/apps/desktop/src/lib/dataGrid/columnFormatter.ts b/apps/desktop/src/lib/dataGrid/columnFormatter.ts
index 8653428b35..58a4f78402 100644
--- a/apps/desktop/src/lib/dataGrid/columnFormatter.ts
+++ b/apps/desktop/src/lib/dataGrid/columnFormatter.ts
@@ -51,6 +51,7 @@ const STRICT_LOCAL_DATETIME_INPUT_PATTERNS = [
];
const ISO_OFFSET_DATETIME_PATTERN = /^(\d{4})([-/])(\d{2})\2(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/;
const FRACTIONAL_LOCAL_DATETIME_PATTERN = /^(\d{4})([-/])(\d{1,2})\2(\d{1,2})([ T])(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9})$/;
+const MONGO_SHELL_DATE_PATTERN = /^(?:ISODate|new Date)\(\s*(["'])(.+)\1\s*\)$/;
export interface CustomColumnFormatterConfig {
id: string;
@@ -262,6 +263,11 @@ function resolveDateTimeValue(value: string | number, unit: DateTimeFormatterUni
if (!trimmed) {
return undefined;
}
+ const mongoShellDate = unwrapMongoShellDate(trimmed);
+ if (mongoShellDate !== undefined) {
+ return parseStrictDateTimeString(mongoShellDate);
+ }
+
const timestamp = parseTimestampMilliseconds(trimmed, unit);
if (timestamp !== undefined) {
const parsedTimestamp = dayjs(timestamp);
@@ -279,6 +285,10 @@ function resolveDateTimeValue(value: string | number, unit: DateTimeFormatterUni
return parsedTimestamp.isValid() ? parsedTimestamp : undefined;
}
+function unwrapMongoShellDate(value: string): string | undefined {
+ return value.match(MONGO_SHELL_DATE_PATTERN)?.[2];
+}
+
function normalizeDateTimePattern(pattern: unknown): string {
return typeof pattern === "string" && pattern.trim() ? pattern : DEFAULT_DATETIME_PATTERN;
}
diff --git a/packages/app-tests/columnFormatter.test.ts b/packages/app-tests/columnFormatter.test.ts
index a99524be37..5871fc75be 100644
--- a/packages/app-tests/columnFormatter.test.ts
+++ b/packages/app-tests/columnFormatter.test.ts
@@ -85,6 +85,22 @@ test("formats Oracle timestamp fractional precision in the data grid", () => {
assert.equal(applyColumnFormatter("2024-02-25 13:02:15.123456", { kind: "datetime", unit: "auto", pattern: "YYYY/MM/DD HH:mm:ss.SSS", timezone: undefined }), "2024/02/25 13:02:15.123");
});
+test("formats only strict Mongo shell date wrappers", () => {
+ for (const unit of ["auto", "seconds", "milliseconds"] as const) {
+ const formatter: ColumnFormatterConfig = { kind: "datetime", unit, pattern: "YYYY-MM-DD HH:mm:ss.SSSZ", timezone: "Asia/Shanghai" };
+
+ assert.equal(applyColumnFormatter('ISODate("2024-06-28T09:15:36.439Z")', formatter), "2024-06-28 17:15:36.439+08:00");
+ assert.equal(applyColumnFormatter(" new Date( '2024-06-28T09:15:36.439Z' ) ", formatter), "2024-06-28 17:15:36.439+08:00");
+
+ for (const value of ['ISODate("not-a-date")', "new Date('2024-02-30T09:15:36.439Z')", 'ISODate("1715758200")', "new Date('1715758200001')"]) {
+ assert.equal(applyColumnFormatter(value, formatter), value);
+ }
+ }
+
+ assert.equal(applyColumnFormatter("1715758200", { kind: "datetime", unit: "seconds", pattern: "YYYY-MM-DD HH:mm:ssZ", timezone: "Asia/Shanghai" }), "2024-05-15 15:30:00+08:00");
+ assert.equal(applyColumnFormatter("1715758200001", { kind: "datetime", unit: "milliseconds", pattern: "YYYY-MM-DD HH:mm:ss.SSSZ", timezone: "Asia/Shanghai" }), "2024-05-15 15:30:00.001+08:00");
+});
+
test("does not treat compact date strings as unix timestamps", () => {
dayjs.extend(utc);
dayjs.extend(timezone);