Seed ไฟล์ตัวอย่างแบบ idempotent sync กับ public/samples/
- scripts/seed-samples.mjs: INSERT ไฟล์ที่ยังไม่มี row + DELETE row ที่ไฟล์หาย (CASCADE ลบ annotation ตาม) — แทน SQL seed มือเดิมใน README - รันอัตโนมัติตอน container start ต่อจาก migrate + npm run seed-samples บนเครื่อง dev - สลับไฟล์ sample เป็นคำสั่งศาล 2 ฉบับ Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
ec26d79f0e
commit
79c9e2cc41
7 changed files with 47 additions and 3 deletions
|
|
@ -59,12 +59,14 @@ npx drizzle-kit push
|
|||
|
||||
### 5. Seed ไฟล์ตัวอย่าง
|
||||
|
||||
ไฟล์ตัวอย่างอยู่ที่ `public/samples/` แล้ว — ลงทะเบียนในตาราง `files` (รันเฉพาะเมื่อตารางว่าง ไม่งั้นข้อมูลจะซ้ำ):
|
||||
ไฟล์ตัวอย่างอยู่ที่ `public/samples/` แล้ว — sync ลงตาราง `files` (idempotent รันซ้ำได้):
|
||||
|
||||
```bash
|
||||
docker exec judes-note-db psql -U postgres -c "insert into files (title, pdf_url) values ('01012-672427-4f-690609-0000840227.pdf','/samples/01012-672427-4f-690609-0000840227.pdf'), ('01012-672427-1f-690609-0000840223.pdf','/samples/01012-672427-1f-690609-0000840223.pdf'), ('dashboard-report-2026-09-03.pdf','/samples/dashboard-report-2026-09-03.pdf');"
|
||||
npm run seed-samples
|
||||
```
|
||||
|
||||
> บน production ไม่ต้องรันมือ — script เดียวกัน (`scripts/seed-samples.mjs`) รันอัตโนมัติตอน container start ต่อจาก migration ไฟล์ที่หายจาก `public/samples/` จะถูกลบ row พร้อม annotation ที่ผูกอยู่ (CASCADE)
|
||||
|
||||
### 6. รัน dev server
|
||||
|
||||
```bash
|
||||
|
|
|
|||
|
|
@ -41,4 +41,5 @@ USER nextjs
|
|||
EXPOSE 3000
|
||||
# DATABASE_URL ฉีดตอนรันผ่าน env ของ container (อย่าฝังใน image)
|
||||
# migrate สร้าง database ถ้ายังไม่มี + apply ไฟล์ที่ยังไม่ได้ apply (idempotent)
|
||||
CMD ["sh", "-c", "node scripts/migrate.mjs && exec node server.js"]
|
||||
# seed-samples sync ตาราง files กับ public/samples/ ที่ bake อยู่ใน image (idempotent)
|
||||
CMD ["sh", "-c", "node scripts/migrate.mjs && node scripts/seed-samples.mjs && exec node server.js"]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
"build": "next build",
|
||||
"test": "vitest run",
|
||||
"start": "next start",
|
||||
"seed-samples": "node --env-file=.env scripts/seed-samples.mjs",
|
||||
"postinstall": "node -e \"require('fs').copyFileSync('node_modules/pdfjs-dist/build/pdf.worker.min.mjs','public/pdf.worker.min.mjs')\""
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
BIN
public/samples/court_command_11_123 1.pdf
Normal file
BIN
public/samples/court_command_11_123 1.pdf
Normal file
Binary file not shown.
BIN
public/samples/court_command_1_2547 1.pdf
Normal file
BIN
public/samples/court_command_1_2547 1.pdf
Normal file
Binary file not shown.
Binary file not shown.
40
scripts/seed-samples.mjs
Normal file
40
scripts/seed-samples.mjs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Sync ตาราง files กับไฟล์จริงใน public/samples/ (idempotent — รันซ้ำได้ทุกครั้ง)
|
||||
// - INSERT ไฟล์ .pdf ที่ยังไม่มี row (pdf_url = /samples/<ชื่อไฟล์>)
|
||||
// - DELETE row ที่ชี้ /samples/* แต่ไฟล์หายจาก disk (CASCADE ลบ annotation/bookmark ตาม)
|
||||
// รันตอน container start ต่อจาก migrate.mjs และรันมือบนเครื่อง dev ได้ผ่าน npm run seed-samples
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import postgres from "postgres";
|
||||
|
||||
const SAMPLES_DIR = path.join(process.cwd(), "public", "samples");
|
||||
|
||||
const dbUrl = process.env.DATABASE_URL;
|
||||
if (!dbUrl) throw new Error("ต้องตั้ง DATABASE_URL ก่อนรัน seed-samples");
|
||||
|
||||
const sql = postgres(dbUrl, { prepare: false, max: 1 });
|
||||
try {
|
||||
const onDisk = fs
|
||||
.readdirSync(SAMPLES_DIR)
|
||||
.filter((f) => f.toLowerCase().endsWith(".pdf"))
|
||||
.map((f) => ({ title: f, pdfUrl: `/samples/${f}` }));
|
||||
const diskUrls = new Set(onDisk.map((f) => f.pdfUrl));
|
||||
|
||||
const rows = await sql`select id, title, pdf_url from files where pdf_url like '/samples/%'`;
|
||||
const dbUrls = new Set(rows.map((r) => r.pdf_url));
|
||||
|
||||
for (const file of onDisk) {
|
||||
if (dbUrls.has(file.pdfUrl)) continue;
|
||||
console.log(`[seed] เพิ่ม ${file.pdfUrl}`);
|
||||
await sql`insert into files (title, pdf_url) values (${file.title}, ${file.pdfUrl})`;
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
if (diskUrls.has(row.pdf_url)) continue;
|
||||
console.log(`[seed] ลบ ${row.pdf_url} (ไฟล์หายจาก image — annotation ที่ผูกอยู่ CASCADE ตามไปด้วย)`);
|
||||
await sql`delete from files where id = ${row.id}`;
|
||||
}
|
||||
|
||||
console.log(`[seed] sync เสร็จ: ${onDisk.length} ไฟล์ใน samples`);
|
||||
} finally {
|
||||
await sql.end();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue