65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
// xuchao: 数学内容种子——src/content/math/*.json 灌入 math_point;表空全量,否则增量(按 pointId 跳过,seq 续接)
|
|
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 { mathPoints } from "../src/db/schema";
|
|
|
|
type MathPoint = { id: string; title: string; grade?: string; difficulty: number; summary?: string; md: string };
|
|
type MathModuleFile = { module: string; moduleName: string; points: MathPoint[] };
|
|
|
|
// 模块顺序即学习顺序,与 src/lib/math.ts 的 MODULES 保持一致
|
|
const MODULE_ORDER = ["calc", "apply", "geo", "num", "travel", "eq", "combo"];
|
|
|
|
async function main() {
|
|
const dir = path.join(process.cwd(), "src/content/math");
|
|
if (!fs.existsSync(dir)) {
|
|
console.log("src/content/math 不存在,跳过数学种子");
|
|
return;
|
|
}
|
|
const files: MathModuleFile[] = fs
|
|
.readdirSync(dir)
|
|
.filter((f) => f.endsWith(".json"))
|
|
.sort()
|
|
.map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")));
|
|
|
|
const conn = await mysql.createConnection(process.env.DATABASE_URL!);
|
|
const db = drizzle(conn);
|
|
|
|
const [cnt] = await db.select({ n: sql<number>`COUNT(*)` }).from(mathPoints);
|
|
if (cnt.n > 0) console.log("math_point 已有", cnt.n, "条,增量模式(跳过已存在 pointId)");
|
|
const [mx] = await db.select({ m: sql<number>`COALESCE(MAX(seq),0)` }).from(mathPoints);
|
|
let seq = mx.m;
|
|
const existing = new Set(
|
|
(await db.select({ pointId: mathPoints.pointId }).from(mathPoints)).map((r) => r.pointId),
|
|
);
|
|
|
|
let added = 0;
|
|
for (const mod of MODULE_ORDER) {
|
|
for (const file of files.filter((f) => f.module === mod)) {
|
|
for (const p of file.points) {
|
|
if (existing.has(p.id)) continue;
|
|
seq += 1;
|
|
await db.insert(mathPoints).values({
|
|
pointId: p.id,
|
|
module: file.module,
|
|
moduleName: file.moduleName,
|
|
title: p.title,
|
|
grade: p.grade ?? null,
|
|
difficulty: p.difficulty || 1,
|
|
summary: p.summary ?? null,
|
|
md: p.md,
|
|
seq,
|
|
});
|
|
added++;
|
|
}
|
|
}
|
|
}
|
|
const [tot] = await db.select({ n: sql<number>`COUNT(*)` }).from(mathPoints);
|
|
console.log("新增数学课:", added, "| 数学知识点总数:", tot.n);
|
|
await conn.end();
|
|
}
|
|
|
|
main();
|