30263/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-28 15:55:37 +00:00

186 lines
4.3 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Answers = db.answers;
const Questions = db.questions;
const AnswersData = [
{
content: 'The capital of France is Paris.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content:
'Machine learning is a method of data analysis that automates analytical model building.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content:
'AI can improve efficiency, accuracy, and decision-making processes.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const QuestionsData = [
{
content: 'What is the capital of France?',
// type code here for "relation_one" field
},
{
content: 'How does machine learning work?',
// type code here for "relation_one" field
},
{
content: 'What are the benefits of AI?',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateAnswerWithQuestion() {
const relatedQuestion0 = await Questions.findOne({
offset: Math.floor(Math.random() * (await Questions.count())),
});
const Answer0 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Answer0?.setQuestion) {
await Answer0.setQuestion(relatedQuestion0);
}
const relatedQuestion1 = await Questions.findOne({
offset: Math.floor(Math.random() * (await Questions.count())),
});
const Answer1 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Answer1?.setQuestion) {
await Answer1.setQuestion(relatedQuestion1);
}
const relatedQuestion2 = await Questions.findOne({
offset: Math.floor(Math.random() * (await Questions.count())),
});
const Answer2 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Answer2?.setQuestion) {
await Answer2.setQuestion(relatedQuestion2);
}
}
async function associateAnswerWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Answer0 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Answer0?.setUser) {
await Answer0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Answer1 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Answer1?.setUser) {
await Answer1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Answer2 = await Answers.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Answer2?.setUser) {
await Answer2.setUser(relatedUser2);
}
}
async function associateQuestionWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Question0 = await Questions.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Question0?.setUser) {
await Question0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Question1 = await Questions.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Question1?.setUser) {
await Question1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Question2 = await Questions.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Question2?.setUser) {
await Question2.setUser(relatedUser2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Answers.bulkCreate(AnswersData);
await Questions.bulkCreate(QuestionsData);
await Promise.all([
// Similar logic for "relation_many"
await associateAnswerWithQuestion(),
await associateAnswerWithUser(),
await associateQuestionWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('answers', null, {});
await queryInterface.bulkDelete('questions', null, {});
},
};