001project_wildgrowth/backend/prisma/insert-single-courses.ts

272 lines
7.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient } from '@prisma/client';
import dotenv from 'dotenv';
// 加载环境变量
dotenv.config();
const prisma = new PrismaClient();
async function main() {
console.log('🌱 开始插入小节课测试数据...');
try {
// ============================================================
// 小节课 15分钟时间管理
// ============================================================
const course1 = await prisma.course.upsert({
where: { id: 'course_single_001' },
update: {
title: '5分钟时间管理',
type: 'single',
totalNodes: 1,
},
create: {
id: 'course_single_001',
title: '5分钟时间管理',
type: 'single',
totalNodes: 1,
},
});
const node1 = await prisma.courseNode.upsert({
where: { id: 'node_single_001' },
update: {
courseId: course1.id,
title: '时间管理的核心原则',
orderIndex: 0,
},
create: {
id: 'node_single_001',
courseId: course1.id,
title: '时间管理的核心原则',
orderIndex: 0,
},
});
// 删除现有幻灯片(如果存在)
await prisma.nodeSlide.deleteMany({
where: { nodeId: node1.id },
});
// 创建幻灯片
const slides1 = [
{
id: 'slide_single_001_01',
nodeId: node1.id,
slideType: 'text',
orderIndex: 1,
content: {
title: '5分钟时间管理',
paragraphs: [
'欢迎学习时间管理核心原则',
'让我们快速掌握高效的时间管理方法',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_001_02',
nodeId: node1.id,
slideType: 'text',
orderIndex: 2,
content: {
title: '核心原则',
paragraphs: [
'1. 优先级排序:重要且紧急的事情优先',
'2. 番茄工作法25分钟专注5分钟休息',
'3. 时间块:为每个任务分配固定时间',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_001_03',
nodeId: node1.id,
slideType: 'text',
orderIndex: 3,
content: {
title: '实践要点',
paragraphs: [
'每天早上列出今日最重要的3件事',
'使用番茄钟保持专注',
'每天晚上回顾完成情况',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_001_04',
nodeId: node1.id,
slideType: 'text',
orderIndex: 4,
content: {
title: '本节小结',
paragraphs: [
'你已经完成了「时间管理的核心原则」的学习',
'记住:高效的时间管理需要持续练习',
'每天进步一点点,最终会带来巨大的改变',
],
},
effect: 'fade_in',
},
];
for (const slide of slides1) {
await prisma.nodeSlide.upsert({
where: { id: slide.id },
update: slide,
create: slide,
});
}
console.log('✅ 小节课 1 创建成功5分钟时间管理');
// ============================================================
// 小节课 23分钟学会专注
// ============================================================
const course2 = await prisma.course.upsert({
where: { id: 'course_single_002' },
update: {
title: '3分钟学会专注',
type: 'single',
totalNodes: 1,
},
create: {
id: 'course_single_002',
title: '3分钟学会专注',
type: 'single',
totalNodes: 1,
},
});
const node2 = await prisma.courseNode.upsert({
where: { id: 'node_single_002' },
update: {
courseId: course2.id,
title: '专注力的训练方法',
orderIndex: 0,
},
create: {
id: 'node_single_002',
courseId: course2.id,
title: '专注力的训练方法',
orderIndex: 0,
},
});
// 删除现有幻灯片(如果存在)
await prisma.nodeSlide.deleteMany({
where: { nodeId: node2.id },
});
// 创建幻灯片
const slides2 = [
{
id: 'slide_single_002_01',
nodeId: node2.id,
slideType: 'text',
orderIndex: 1,
content: {
title: '3分钟学会专注',
paragraphs: [
'欢迎学习专注力的训练方法',
'让我们快速掌握提升专注力的技巧',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_002_02',
nodeId: node2.id,
slideType: 'text',
orderIndex: 2,
content: {
title: '专注的原理',
paragraphs: [
'专注力是一种可以训练的能力',
'大脑需要时间进入专注状态约15分钟',
'减少干扰是提升专注的关键',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_002_03',
nodeId: node2.id,
slideType: 'text',
orderIndex: 3,
content: {
title: '实用技巧',
paragraphs: [
'关闭所有通知和干扰源',
'设置专门的专注时间和空间',
'使用深呼吸帮助快速进入专注状态',
],
},
effect: 'fade_in',
},
{
id: 'slide_single_002_04',
nodeId: node2.id,
slideType: 'text',
orderIndex: 4,
content: {
title: '本节小结',
paragraphs: [
'你已经完成了「专注力的训练方法」的学习',
'记住:专注力需要持续练习',
'从每天15分钟开始逐步提升专注时长',
],
},
effect: 'fade_in',
},
];
for (const slide of slides2) {
await prisma.nodeSlide.upsert({
where: { id: slide.id },
update: slide,
create: slide,
});
}
console.log('✅ 小节课 2 创建成功3分钟学会专注');
// 验证数据
const courses = await prisma.course.findMany({
where: { type: 'single' },
include: {
nodes: {
include: {
slides: true,
},
},
},
});
console.log('\n📊 验证数据:');
for (const course of courses) {
console.log(`\n课程${course.title} (${course.id})`);
for (const node of course.nodes) {
console.log(` - 节点:${node.title} (${node.id})`);
console.log(` - 幻灯片数量:${node.slides.length}`);
}
}
console.log('\n🎉 小节课测试数据插入完成!');
} catch (error) {
console.error('❌ 插入数据失败:', error);
throw error;
}
}
main()
.catch((e) => {
console.error('❌ 执行失败:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});