fix(mongodb): read zone-less filter dates as local time

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-30 15:00:53 +08:00
1 parent 3dbaed310d
commit 71068cef16
6 files changed
+72 -7

No files matched your search

@@ -2,7 +2,7 @@ import type { ComposerTranslation } from "vue-i18n";
import { normalizeJsonArgument } from "@dbx-app/mongo-shell";
import { isElasticsearchCompatibleDatabaseType, isMeilisearchDatabaseType, isSolrDatabaseType, type DatabaseType } from "@/types/database";
import { quoteUnquotedObjectKeys } from "@/lib/mongo/mongoShellCommand";
import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues";
import { formatMongoShellLiteral, normalizeMongoDateInput } from "@/lib/mongo/mongoDocumentValues";
export type DocumentStoreKind = "mongodb" | "dynamodb" | "elasticsearch" | "meilisearch" | "solr";
export type DocumentFilterMode = "equals" | "not-equals" | "like" | "not-like" | "begins-with" | "ends-with" | "greater-than" | "greater-than-or-equal" | "less-than" | "less-than-or-equal" | "in" | "not-in" | "between" | "not-between" | "is-null" | "is-not-null";
@@ -910,10 +910,12 @@ function parseMongoFilterValueAs(raw: string, valueType: Exclude<DocumentFilterV
case "object-id":
if (!/^[0-9a-f]{24}$/i.test(text)) throw invalidMongoFilterValue(valueType, raw);
return { $oid: text };
case "date":
case "date": {
if (/^-?\d+$/.test(text)) return { $date: { $numberLong: text } };
if (Number.isNaN(Date.parse(text))) throw invalidMongoFilterValue(valueType, raw);
return { $date: text };
const normalized = normalizeMongoDateInput(text);
if (!normalized) throw invalidMongoFilterValue(valueType, raw);
return { $date: normalized };
}
case "int32": {
if (!/^-?\d+$/.test(text)) throw invalidMongoFilterValue(valueType, raw);
const value = BigInt(text);
@@ -103,7 +103,41 @@ export function mongoShellDateToExtendedJson(value: unknown): unknown {
if (typeof value !== "string") return value;
const match = value.trim().match(MONGO_SHELL_DATE_PATTERN);
if (!match) return value;
return { $date: match[2] };
return { $date: normalizeMongoDateInput(match[2] ?? "") ?? match[2] };
}
/** `2025-04-01 19:46:03`, `2025/04/01`, `2025-04-01T19:46` … : a date with no zone, as people read one off a screen. */
const MONGO_LOCAL_DATE_PATTERN = /^(\d{4})[-/](\d{1,2})[-/](\d{1,2})(?:[T ](\d{1,2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?)?$/;
/** The one spelling the server's `$date` parser accepts, kept as written. */
const MONGO_RFC3339_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
/**
* Turns the date text a person types into the RFC 3339 string `$date` requires.
*
* The server accepts nothing else, but people paste what they see: a grid cell
* shows `2025-04-01 19:46:03`, a log line `2025-04-01`. Those name no zone, so
* they are read as local time, the way the person reads them, and sent as UTC.
* Text that already spells RFC 3339 is kept as written; any other spelling the
* platform can parse is canonicalised. Returns null when the text is no date.
*/
export function normalizeMongoDateInput(text: string): string | null {
const trimmed = text.trim();
if (!trimmed) return null;
if (MONGO_RFC3339_DATE_PATTERN.test(trimmed) && !Number.isNaN(Date.parse(trimmed))) return trimmed;
const local = MONGO_LOCAL_DATE_PATTERN.exec(trimmed);
if (local) {
const [, year = "", month = "", day = "", hour = "0", minute = "0", second = "0", fraction = "0"] = local;
const parts = [Number(year), Number(month), Number(day), Number(hour), Number(minute), Number(second), Number(fraction.padEnd(3, "0"))] as const;
const date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5], parts[6]);
// `new Date(2025, 1, 30)` rolls over into March; a day that does not exist is a typo, not a date.
const exists = date.getFullYear() === parts[0] && date.getMonth() === parts[1] - 1 && date.getDate() === parts[2];
if (!exists || parts[3] > 23 || parts[4] > 59 || parts[5] > 59) return null;
return date.toISOString();
}
const parsed = Date.parse(trimmed);
return Number.isNaN(parsed) ? null : new Date(parsed).toISOString();
}
export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown {