// 将参考语法体系(ref-data.json)灌入 grammar_point 表;仅当表为空时执行 import fs from "node:fs"; import path from "node:path"; import { sql } from "drizzle-orm"; import { drizzle } from "drizzle-orm/mysql2"; import mysql from "mysql2/promise"; import { grammarPoints } from "../src/db/schema"; type RefPoint = { id: string; title: string; difficulty: number; prereq: string[]; summary: string; md: string; }; type RefLevel = { id: string; name: string; points: RefPoint[] }; async function main() { const file = path.join(process.cwd(), "src/content/grammar/ref-data.json"); const data = JSON.parse(fs.readFileSync(file, "utf8")) as { levels: RefLevel[] }; const conn = await mysql.createConnection(process.env.DATABASE_URL!); const db = drizzle(conn); const [cnt] = await db.select({ n: sql`COUNT(*)` }).from(grammarPoints); if (cnt.n > 0) { console.log("grammar_point 已有", cnt.n, "条,跳过种子"); await conn.end(); return; } let seq = 0; const rows = []; for (const lvl of data.levels) { for (const p of lvl.points) { seq += 1; rows.push({ pointId: p.id, level: lvl.id, levelName: lvl.name, title: p.title, difficulty: p.difficulty, summary: p.summary, prereq: JSON.stringify(p.prereq ?? []), md: p.md, seq, }); } } await db.insert(grammarPoints).values(rows); console.log("已写入 grammar_point:", rows.length, "条"); await conn.end(); } main();