31420/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-11 15:25:48 +00:00

252 lines
5.2 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Courses = db.courses;
const Documents = db.documents;
const Statistics = db.statistics;
const CoursesData = [
{
title: 'Introduction to Quantum Mechanics',
description: 'A comprehensive course on quantum mechanics.',
// type code here for "relation_many" field
},
{
title: 'Advanced Calculus',
description: 'An in-depth study of calculus and its applications.',
// type code here for "relation_many" field
},
{
title: 'Modern European History',
description:
'Exploring the history of Europe from the Renaissance to the present.',
// type code here for "relation_many" field
},
{
title: 'Organic Chemistry',
description: 'Detailed exploration of organic compounds and reactions.',
// type code here for "relation_many" field
},
];
const DocumentsData = [
{
title: 'Quantum Mechanics Exam 2023',
format: 'tex',
// type code here for "files" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Calculus Exercises',
format: 'pdf',
// type code here for "files" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'European History Midterm',
format: 'pdf',
// type code here for "files" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Organic Chemistry Lab Report',
format: 'pdf',
// type code here for "files" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const StatisticsData = [
{
total_users: 1000,
total_courses: 50,
total_documents: 200,
},
{
total_users: 1500,
total_courses: 75,
total_documents: 300,
},
{
total_users: 2000,
total_courses: 100,
total_documents: 400,
},
{
total_users: 2500,
total_courses: 125,
total_documents: 500,
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateDocumentWithCourse() {
const relatedCourse0 = await Courses.findOne({
offset: Math.floor(Math.random() * (await Courses.count())),
});
const Document0 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Document0?.setCourse) {
await Document0.setCourse(relatedCourse0);
}
const relatedCourse1 = await Courses.findOne({
offset: Math.floor(Math.random() * (await Courses.count())),
});
const Document1 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Document1?.setCourse) {
await Document1.setCourse(relatedCourse1);
}
const relatedCourse2 = await Courses.findOne({
offset: Math.floor(Math.random() * (await Courses.count())),
});
const Document2 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Document2?.setCourse) {
await Document2.setCourse(relatedCourse2);
}
const relatedCourse3 = await Courses.findOne({
offset: Math.floor(Math.random() * (await Courses.count())),
});
const Document3 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Document3?.setCourse) {
await Document3.setCourse(relatedCourse3);
}
}
async function associateDocumentWithUploader() {
const relatedUploader0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document0 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Document0?.setUploader) {
await Document0.setUploader(relatedUploader0);
}
const relatedUploader1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document1 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Document1?.setUploader) {
await Document1.setUploader(relatedUploader1);
}
const relatedUploader2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document2 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Document2?.setUploader) {
await Document2.setUploader(relatedUploader2);
}
const relatedUploader3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document3 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Document3?.setUploader) {
await Document3.setUploader(relatedUploader3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Courses.bulkCreate(CoursesData);
await Documents.bulkCreate(DocumentsData);
await Statistics.bulkCreate(StatisticsData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateDocumentWithCourse(),
await associateDocumentWithUploader(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('courses', null, {});
await queryInterface.bulkDelete('documents', null, {});
await queryInterface.bulkDelete('statistics', null, {});
},
};