(fwiw this was one of the main things I had to do for compatibility on d1/drizzle, then replace in m

(fwiw this was one of the main things I had to do for compatibility on d1/drizzle, then replace in my better auth drizzle schema all the date [e.g. createdAt] column types)
import { customType } from "drizzle-orm/sqlite-core";

// Custom date type for D1 compatibility - converts Date objects to/from Unix timestamps
// Required for better-auth to work with Cloudflare D1 which doesn't support Date objects
export const D1DateTime = customType<{
  data: Date | null;
  driverData: number | null;
}>({
  dataType() {
    return "integer";
  },
  fromDriver(value: number | null): Date | null {
    return value ? new Date(value * 1000) : null; // Convert from Unix seconds to Date
  },
  toDriver(value: Date | null): number | null {
    return value ? Math.floor(value.getTime() / 1000) : null; // Convert Date to Unix seconds
  },
});
Was this page helpful?