From 288d62bf647b6319c12a48c99f7dbc5f326c0e1a Mon Sep 17 00:00:00 2001 From: Freedom <48708512+xddcode@users.noreply.github.com> Date: Tue, 21 Jul 2026 17:08:38 +0800 Subject: [PATCH] fix(mongo): preserve date-shaped string values --- .../src/lib/mongo/mongoDocumentValues.ts | 11 ---- crates/dbx-core/src/db/mongo_driver.rs | 62 ++++++------------- .../app-tests/mongoDocumentValues.test.ts | 22 +++++-- 3 files changed, 36 insertions(+), 59 deletions(-) diff --git a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts index 4e0bb893f..2b6289414 100644 --- a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts +++ b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts @@ -2,7 +2,6 @@ export type MongoInputValue = string | number | boolean | null; const MONGO_SHELL_DATE_PATTERN = /^(?:ISODate|new Date)\(\s*(["'])(.+)\1\s*\)$/; const MONGO_SHELL_NUMBER_LONG_PATTERN = /^NumberLong\(\s*(["'])(-?\d+)\1\s*\)$/; -const LEGACY_MONGO_DATE_DISPLAY_PATTERN = /^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2})(?:\.(\d{1,3}))?$/; const MONGO_OBJECT_ID_PATTERN = /^[a-fA-F0-9]{24}$/; const MONGO_INTEGER_PATTERN = /^-?\d+$/; const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER); @@ -28,9 +27,6 @@ export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown { const shellNumberLong = mongoShellNumberLongToExtendedJson(trimmed); if (shellNumberLong !== trimmed) return shellNumberLong; - const legacyDate = legacyMongoDateDisplayToExtendedJson(trimmed); - if (legacyDate) return legacyDate; - if (MONGO_INTEGER_PATTERN.test(trimmed)) { const integer = BigInt(trimmed); if (integer > MAX_SAFE_BIGINT || integer < -MAX_SAFE_BIGINT) { @@ -67,13 +63,6 @@ function mongoDocumentFieldValue(document: unknown, field: string): unknown { return (document as Record)[field]; } -function legacyMongoDateDisplayToExtendedJson(value: string): { $date: string } | null { - const match = value.match(LEGACY_MONGO_DATE_DISPLAY_PATTERN); - if (!match) return null; - const [, date, time, millis = "000"] = match; - return { $date: `${date}T${time}.${millis.padEnd(3, "0")}Z` }; -} - function mongoShellNumberLongToExtendedJson(value: string): unknown { const match = value.match(MONGO_SHELL_NUMBER_LONG_PATTERN); return match ? { $numberLong: match[2] } : value; diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rs index 01e1fdf29..10cfdbab6 100644 --- a/crates/dbx-core/src/db/mongo_driver.rs +++ b/crates/dbx-core/src/db/mongo_driver.rs @@ -1736,44 +1736,13 @@ fn parse_extended_json_value(obj: &serde_json::Map) - fn parse_mongo_shell_date(value: &str) -> Option { let trimmed = value.trim(); - if let Some(inner) = trimmed.strip_prefix("ISODate(").or_else(|| trimmed.strip_prefix("new Date(")) { - let inner = inner.strip_suffix(')')?.trim(); - let quoted = inner - .strip_prefix('"') - .and_then(|value| value.strip_suffix('"')) - .or_else(|| inner.strip_prefix('\'').and_then(|value| value.strip_suffix('\'')))?; - return DateTime::parse_rfc3339_str(quoted).ok(); - } - parse_legacy_mongo_date_display(trimmed) -} - -fn parse_legacy_mongo_date_display(value: &str) -> Option { - let (date, time) = value.split_once(' ').or_else(|| value.split_once('T'))?; - if date.len() != 10 || time.len() < 8 || time.len() > 12 { - return None; - } - if !date - .chars() - .enumerate() - .all(|(index, ch)| matches!(index, 4 | 7) && ch == '-' || !matches!(index, 4 | 7) && ch.is_ascii_digit()) - { - return None; - } - let (seconds, millis) = time.split_once('.').unwrap_or((time, "000")); - if seconds.len() != 8 || millis.is_empty() || millis.len() > 3 { - return None; - } - if !seconds - .chars() - .enumerate() - .all(|(index, ch)| matches!(index, 2 | 5) && ch == ':' || !matches!(index, 2 | 5) && ch.is_ascii_digit()) - { - return None; - } - if !millis.chars().all(|ch| ch.is_ascii_digit()) { - return None; - } - DateTime::parse_rfc3339_str(format!("{date}T{seconds}.{millis:0<3}Z")).ok() + let inner = trimmed.strip_prefix("ISODate(").or_else(|| trimmed.strip_prefix("new Date("))?; + let inner = inner.strip_suffix(')')?.trim(); + let quoted = inner + .strip_prefix('"') + .and_then(|value| value.strip_suffix('"')) + .or_else(|| inner.strip_prefix('\'').and_then(|value| value.strip_suffix('\'')))?; + DateTime::parse_rfc3339_str(quoted).ok() } fn parse_extended_json_date(obj: &serde_json::Map) -> Option { @@ -2780,16 +2749,21 @@ mod tests { } #[test] - fn json_object_to_document_parses_legacy_date_display_strings() { + fn json_object_to_document_preserves_date_shaped_strings() { let value = serde_json::json!({ - "created_at": "2025-08-14 02:25:43.718", + "$set": { + "create_time": "2025-04-01 19:46:03", + "nested": { "updated": "2025-08-14T02:25:43" }, + "items": ["2025-08-14 02:25:43"], + } }); let doc = json_object_to_document(&value).unwrap(); + let set = doc.get_document("$set").unwrap(); - assert!(matches!( - doc.get("created_at"), - Some(Bson::DateTime(value)) if value.timestamp_millis() == 1_755_138_343_718 - )); + assert_eq!(set.get_str("create_time").unwrap(), "2025-04-01 19:46:03"); + assert_eq!(set.get_document("nested").unwrap().get_str("updated").unwrap(), "2025-08-14T02:25:43"); + assert_eq!(set.get_array("items").unwrap()[0].as_str().unwrap(), "2025-08-14 02:25:43"); + assert!(is_update_operator_document(&doc)); } #[test] diff --git a/packages/app-tests/mongoDocumentValues.test.ts b/packages/app-tests/mongoDocumentValues.test.ts index f3f242f4f..474e0b25d 100644 --- a/packages/app-tests/mongoDocumentValues.test.ts +++ b/packages/app-tests/mongoDocumentValues.test.ts @@ -22,13 +22,27 @@ test("parses Mongo shell ISODate literals as extended JSON dates", () => { }); }); -test("parses legacy Mongo date display values as UTC dates", () => { - assert.deepEqual(parseMongoDocumentInputValue("2025-08-14 02:25:43.718"), { - $date: "2025-08-14T02:25:43.718Z", - }); +test("preserves date-shaped Mongo strings instead of guessing Date", () => { + assert.equal(parseMongoDocumentInputValue("2025-08-14 02:25:43.718"), "2025-08-14 02:25:43.718"); + assert.equal(parseMongoDocumentInputValue("2025-04-01 19:46:03"), "2025-04-01 19:46:03"); assert.equal(parseMongoDocumentInputValue('"2025-08-14 02:25:43.718"'), "2025-08-14 02:25:43.718"); }); +test("preserves existing date-shaped Mongo string fields on grid update", () => { + const original = { + _id: "1", + create_time: "2025-04-01 19:46:03", + last_updated_time: "2025-04-01 19:54:12", + }; + const changes = new Map([[1, "2025-04-01 19:41:03"]]); + + assert.deepEqual(buildMongoUpdateDocument(changes, ["_id", "create_time", "last_updated_time"], original), { + $set: { + create_time: "2025-04-01 19:41:03", + }, + }); +}); + test("preserves unsafe Mongo int64 input values without JavaScript rounding", () => { assert.deepEqual(parseMongoDocumentInputValue("2048938405781032962"), { $numberLong: "2048938405781032962" }); assert.equal(parseMongoDocumentInputValue("9007199254740991"), 9007199254740991);