125 lines
3.6 KiB
JavaScript
125 lines
3.6 KiB
JavaScript
import { PrismaClient } from "@prisma/client";
|
|
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
|
import "dotenv/config";
|
|
|
|
const base = process.env.DATABASE_URL;
|
|
const sep = base.includes("?") ? "&" : "?";
|
|
const connectionString = `${base}${sep}connection_limit=5&pool_timeout=10`;
|
|
const adapter = new PrismaMariaDb(connectionString);
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
const CATEGORIES = [
|
|
{
|
|
name: "综合讨论区", slug: "general",
|
|
children: [
|
|
{ name: "新手入门", slug: "newbie" },
|
|
{ name: "AI资讯", slug: "ai-news" },
|
|
{ name: "观点讨论", slug: "discuss" },
|
|
],
|
|
},
|
|
{
|
|
name: "AI工具", slug: "ai-tools",
|
|
children: [
|
|
{ name: "聊天写作", slug: "chat-writing" },
|
|
{ name: "图像创作", slug: "image-gen" },
|
|
{ name: "视频音频", slug: "video-audio" },
|
|
{ name: "代码编程", slug: "code-tools" },
|
|
{ name: "效率办公", slug: "productivity" },
|
|
{ name: "数据分析", slug: "data-analysis" },
|
|
{ name: "教育学习", slug: "edu-tools" },
|
|
],
|
|
},
|
|
{
|
|
name: "开源技能", slug: "skills",
|
|
children: [
|
|
{ name: "LLM框架", slug: "llm-framework" },
|
|
{ name: "Agent框架", slug: "agent-framework" },
|
|
{ name: "RAG技术", slug: "rag" },
|
|
{ name: "模型训练", slug: "model-training" },
|
|
{ name: "部署推理", slug: "deploy-infer" },
|
|
{ name: "多模态", slug: "multimodal" },
|
|
{ name: "数据工程", slug: "data-eng" },
|
|
],
|
|
},
|
|
{
|
|
name: "技术深度", slug: "tech",
|
|
children: [
|
|
{ name: "算法原理", slug: "algorithms" },
|
|
{ name: "模型评测", slug: "model-review" },
|
|
{ name: "安全对齐", slug: "safety" },
|
|
{ name: "硬件加速", slug: "hardware" },
|
|
],
|
|
},
|
|
{
|
|
name: "教程与分享", slug: "tutorials",
|
|
children: [
|
|
{ name: "入门教程", slug: "beginner-guide" },
|
|
{ name: "实战项目", slug: "projects" },
|
|
{ name: "最佳实践", slug: "best-practice" },
|
|
],
|
|
},
|
|
{
|
|
name: "社区互动", slug: "social",
|
|
children: [
|
|
{ name: "求助问答", slug: "qa" },
|
|
{ name: "求职招聘", slug: "jobs" },
|
|
{ name: "闲聊灌水", slug: "random" },
|
|
{ name: "反馈建议", slug: "feedback" },
|
|
],
|
|
},
|
|
];
|
|
|
|
async function main() {
|
|
console.log("Fixing forum category parent-child relationships...\n");
|
|
|
|
for (const cat of CATEGORIES) {
|
|
const parent = await prisma.forumCategory.findUnique({
|
|
where: { slug: cat.slug },
|
|
});
|
|
|
|
if (!parent) {
|
|
console.log(` ✗ Parent "${cat.name}" not found, skipping`);
|
|
continue;
|
|
}
|
|
|
|
console.log(` Parent: ${cat.name} (id=${parent.id})`);
|
|
|
|
for (const child of cat.children) {
|
|
const existing = await prisma.forumCategory.findUnique({
|
|
where: { slug: child.slug },
|
|
});
|
|
|
|
if (!existing) {
|
|
await prisma.forumCategory.create({
|
|
data: {
|
|
name: child.name,
|
|
slug: child.slug,
|
|
parentId: parent.id,
|
|
},
|
|
});
|
|
console.log(` + Created: ${child.name}`);
|
|
} else if (existing.parentId !== parent.id) {
|
|
await prisma.forumCategory.update({
|
|
where: { id: existing.id },
|
|
data: { parentId: parent.id },
|
|
});
|
|
console.log(` ✓ Fixed: ${child.name} (parentId: ${existing.parentId} → ${parent.id})`);
|
|
} else {
|
|
console.log(` ✓ OK: ${child.name}`);
|
|
}
|
|
}
|
|
console.log("");
|
|
}
|
|
|
|
console.log("Done! All category relationships are correct.");
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|