course_web01 / backend /src /seed-categories.ts
trae-bot
Update project
426f2a4
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Course } from './entities/course.entity';
const CATEGORIES = [
'AI新资讯',
'Comfyui资讯',
'满血整合包',
'闭源API接口',
'应用广场',
];
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
const courseRepository = app.get(getRepositoryToken(Course));
const courses = await courseRepository.find();
console.log(`Found ${courses.length} courses. Updating categories...`);
for (const course of courses) {
const randomCategory =
CATEGORIES[Math.floor(Math.random() * CATEGORIES.length)];
course.category = randomCategory;
await courseRepository.save(course);
console.log(
`Updated course ${course.id} "${course.title}" to category: ${randomCategory}`,
);
}
console.log('Finished updating categories.');
await app.close();
}
bootstrap();