refactor(db): switch records layer to Prisma
- Replace hand-written node:sqlite queries with a Prisma client using the better-sqlite3 driver adapter - Version the schema with migrations: baseline table, timestamp normalisation, and WAL journal mode - Store timestamps with an explicit UTC offset so they stop being parsed as local time - Show created/updated times on the edit page, formatted with dayjs - Move the root redirect into next.config to avoid a prerender error - Run migrations from the container entrypoint before starting the server BREAKING CHANGE: DNSMASQ_ADMIN_DB_PATH is replaced by DATABASE_URL, which must be a Prisma connection URL (file:./data/dnsmasq-admin.db). Existing database files are migrated in place and need no conversion.
This commit is contained in:
25 files changed
+1796
-141
No files matched your search
@@ -1,5 +0,0 @@
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
export default function Home() {
|
||||
redirect("/records")
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import { notFound } from "next/navigation"
|
||||
import { Suspense } from "react"
|
||||
import { formatDateTime } from "@/lib/datetime"
|
||||
import { getRecord } from "@/lib/records"
|
||||
import { RecordForm } from "../../_components/RecordForm"
|
||||
import { updateRecordAction } from "../../actions"
|
||||
import { RecordForm } from "../_components/RecordForm"
|
||||
import { updateRecordAction } from "../actions"
|
||||
|
||||
export const metadata = { title: "编辑记录" }
|
||||
|
||||
@@ -31,11 +32,24 @@ async function EditRecordForm({ params }: Pick<PageProps<"/records/[id]">, "para
|
||||
if (!record) notFound()
|
||||
|
||||
return (
|
||||
<RecordForm
|
||||
action={updateRecordAction}
|
||||
record={record}
|
||||
submitLabel="保存修改"
|
||||
cancelHref="/records"
|
||||
/>
|
||||
<>
|
||||
<dl className="flex flex-wrap gap-x-6 gap-y-1 text-xs text-zinc-500 dark:text-zinc-400">
|
||||
<div className="flex gap-1">
|
||||
<dt>创建于</dt>
|
||||
<dd className="text-zinc-700 dark:text-zinc-300">{formatDateTime(record.createdAt)}</dd>
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
<dt>更新于</dt>
|
||||
<dd className="text-zinc-700 dark:text-zinc-300">{formatDateTime(record.updatedAt)}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
<RecordForm
|
||||
action={updateRecordAction}
|
||||
record={record}
|
||||
submitLabel="保存修改"
|
||||
cancelHref="/records"
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -38,7 +38,7 @@ export async function createRecordAction(
|
||||
if (!parsed.ok) return { ok: false, fieldErrors: parsed.fieldErrors }
|
||||
|
||||
try {
|
||||
createRecord(parsed.input)
|
||||
await createRecord(parsed.input)
|
||||
} catch (error) {
|
||||
const duplicate = asDuplicateState(error)
|
||||
if (duplicate) return duplicate
|
||||
@@ -60,7 +60,7 @@ export async function updateRecordAction(
|
||||
if (!parsed.ok) return { ok: false, fieldErrors: parsed.fieldErrors }
|
||||
|
||||
try {
|
||||
updateRecord(id, parsed.input)
|
||||
await updateRecord(id, parsed.input)
|
||||
} catch (error) {
|
||||
const duplicate = asDuplicateState(error)
|
||||
if (duplicate) return duplicate
|
||||
@@ -74,7 +74,7 @@ export async function updateRecordAction(
|
||||
export async function deleteRecordAction(formData: FormData): Promise<void> {
|
||||
const id = readId(formData)
|
||||
if (id === null) return
|
||||
removeRecord(id)
|
||||
await removeRecord(id)
|
||||
revalidatePath(RECORDS_PATH)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ export async function toggleRecordAction(formData: FormData): Promise<void> {
|
||||
const id = readId(formData)
|
||||
if (id === null) return
|
||||
// The hidden field carries the state this record should move *to*.
|
||||
setRecordEnabled(id, formData.get("enabled") === "1")
|
||||
await setRecordEnabled(id, formData.get("enabled") === "1")
|
||||
revalidatePath(RECORDS_PATH)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user