From 0542f386ed1608873c00fe805c002919ee7459ff Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 18 Aug 2026 03:36:18 +0000 Subject: [PATCH] fix: preserve datetime timezone on row updates Datetime inputs are displayed as timezone-less local ISO strings. Without converting back to UTC on submit, unchanged datetime fields drift by the browser offset on every update (console#2870). Mirror the filters path and canonicalize datetime scalars/arrays in buildPayload. Co-authored-by: chiragaggarwal5k --- .../table-[table]/rows/store.test.ts | 49 +++++++++++++++++++ .../table-[table]/rows/store.ts | 30 ++++++++++-- 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.test.ts diff --git a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.test.ts b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.test.ts new file mode 100644 index 000000000..0e20d9966 --- /dev/null +++ b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest'; +import { toLocalDateTimeISO } from '$lib/helpers/date'; +import { buildPayload } from './store'; +import type { Field } from '$database/(entity)'; + +describe('buildPayload', () => { + it('converts local datetime-local strings to UTC ISO for datetime fields', () => { + const utc = '2026-01-01T00:00:00.000Z'; + const local = toLocalDateTimeISO(utc); + const fields = [{ key: 'startAt', type: 'datetime' }] as Field[]; + + const payload = buildPayload(fields, { startAt: local, name: 'unchanged' }); + + expect(payload.startAt).toBe(new Date(local).toISOString()); + expect(payload.name).toBe('unchanged'); + }); + + it('round-trips an unchanged datetime through local display encoding', () => { + const utc = '2026-01-01T00:00:00.000Z'; + const fields = [{ key: 'startAt', type: 'datetime' }] as Field[]; + + const payload = buildPayload(fields, { startAt: toLocalDateTimeISO(utc) }); + + expect(payload.startAt).toBe(new Date(utc).toISOString()); + }); + + it('converts datetime arrays element-wise', () => { + const utc = '2026-01-01T00:00:00.000Z'; + const local = toLocalDateTimeISO(utc); + const fields = [{ key: 'times', type: 'datetime', array: true }] as Field[]; + + const payload = buildPayload(fields, { times: [local, null] }); + + expect(payload.times).toEqual([new Date(local).toISOString(), null]); + }); + + it('leaves null and empty datetime values alone', () => { + const fields = [{ key: 'startAt', type: 'datetime' }] as Field[]; + + expect(buildPayload(fields, { startAt: null }).startAt).toBeNull(); + expect(buildPayload(fields, { startAt: '' }).startAt).toBe(''); + }); + + it('still stringifies bigint fields', () => { + const fields = [{ key: 'count', type: 'bigint' }] as Field[]; + + expect(buildPayload(fields, { count: 42n }).count).toBe('42'); + }); +}); diff --git a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.ts b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.ts index ee1686c11..707359464 100644 --- a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.ts +++ b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/store.ts @@ -65,6 +65,29 @@ function castBigIntValue(value: RowValue): RowValue { return String(value); } +/** + * Datetime inputs are shown as timezone-less local ISO strings (see + * `toLocalDateTimeISO`). Convert them back to UTC before the API call so + * unchanged fields do not drift by the browser's UTC offset on every update. + */ +function castDatetimeValue(value: RowValue): RowValue { + if (value === null || value === undefined || value === '') { + return value; + } + + if (typeof value !== 'string') { + return value; + } + + const date = new Date(value); + + if (isNaN(date.getTime())) { + return value; + } + + return date.toISOString(); +} + export function buildPayload>( fields: Field[] | undefined, row: T @@ -72,18 +95,19 @@ export function buildPayload>( const payload = structuredClone(row) as Record; for (const field of fields ?? []) { - if (field.type !== 'bigint') { + if (field.type !== 'bigint' && field.type !== 'datetime') { continue; } const value = payload[field.key]; + const cast = field.type === 'bigint' ? castBigIntValue : castDatetimeValue; if (field.array && Array.isArray(value)) { - payload[field.key] = value.map((item) => castBigIntValue(item)); + payload[field.key] = value.map((item) => cast(item)); continue; } - payload[field.key] = castBigIntValue(value); + payload[field.key] = cast(value); } return payload as T;