diff --git a/.changeset/tiny-gifts-fry.md b/.changeset/tiny-gifts-fry.md new file mode 100644 index 0000000000..b798e6c6a5 --- /dev/null +++ b/.changeset/tiny-gifts-fry.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Convert non-ISO date to UTC time diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index ba157f01da..93c034cf56 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -19,6 +19,11 @@ export function hasPrimaryKey(column: DBColumn) { return 'primaryKey' in column.schema && !!column.schema.primaryKey; } +// Taken from: +// https://stackoverflow.com/questions/52869695/check-if-a-date-string-is-in-iso-and-utc-format +const isISODateString = (str: string) => + /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str); + const dateType = customType<{ data: Date; driverData: string }>({ dataType() { return 'text'; @@ -27,6 +32,11 @@ const dateType = customType<{ data: Date; driverData: string }>({ return value.toISOString(); }, fromDriver(value) { + if(!isISODateString(value)) { + // values saved using CURRENT_TIMESTAMP are not valid ISO strings + // but *are* in UTC, so append the UTC zone. + value += 'Z'; + } return new Date(value); }, });