30998/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-25 17:04:23 +00:00

341 lines
7.5 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Comments = db.comments;
const Courses = db.courses;
const Groups = db.groups;
const Posts = db.posts;
const CommentsData = [
{
content: 'Amazing shot!',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'Love the colors in this photo.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'This is so serene.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const CoursesData = [
{
title: 'Basics of Photography',
description: 'Learn the fundamentals of photography.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
title: 'Advanced Landscape Techniques',
description: 'Master the art of landscape photography.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
title: 'Portrait Photography',
description: 'Capture stunning portraits.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
];
const GroupsData = [
{
name: 'Nature Photographers',
description: 'A group for those who love capturing nature.',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
name: 'Urban Explorers',
description: 'For photographers who explore cityscapes.',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
name: 'Wildlife Enthusiasts',
description: 'Share your wildlife photography here.',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
];
const PostsData = [
{
title: 'Sunset Over the Hills',
content: 'Captured this beautiful sunset during my evening hike.',
// type code here for "images" field
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
title: 'City Lights',
content: 'The city looks magical at night with all the lights.',
// type code here for "images" field
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
title: 'Forest Trail',
content: 'A peaceful walk through the forest trail.',
// type code here for "images" field
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_many" field
},
];
// Similar logic for "relation_many"
async function associateCommentWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Comment0 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Comment0?.setAuthor) {
await Comment0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Comment1 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Comment1?.setAuthor) {
await Comment1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Comment2 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Comment2?.setAuthor) {
await Comment2.setAuthor(relatedAuthor2);
}
}
async function associateCommentWithPost() {
const relatedPost0 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const Comment0 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Comment0?.setPost) {
await Comment0.setPost(relatedPost0);
}
const relatedPost1 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const Comment1 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Comment1?.setPost) {
await Comment1.setPost(relatedPost1);
}
const relatedPost2 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const Comment2 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Comment2?.setPost) {
await Comment2.setPost(relatedPost2);
}
}
async function associateCourseWithInstructor() {
const relatedInstructor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Course0 = await Courses.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Course0?.setInstructor) {
await Course0.setInstructor(relatedInstructor0);
}
const relatedInstructor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Course1 = await Courses.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Course1?.setInstructor) {
await Course1.setInstructor(relatedInstructor1);
}
const relatedInstructor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Course2 = await Courses.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Course2?.setInstructor) {
await Course2.setInstructor(relatedInstructor2);
}
}
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associatePostWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Post0 = await Posts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Post0?.setAuthor) {
await Post0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Post1 = await Posts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Post1?.setAuthor) {
await Post1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Post2 = await Posts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Post2?.setAuthor) {
await Post2.setAuthor(relatedAuthor2);
}
}
// Similar logic for "relation_many"
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Comments.bulkCreate(CommentsData);
await Courses.bulkCreate(CoursesData);
await Groups.bulkCreate(GroupsData);
await Posts.bulkCreate(PostsData);
await Promise.all([
// Similar logic for "relation_many"
await associateCommentWithAuthor(),
await associateCommentWithPost(),
await associateCourseWithInstructor(),
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associatePostWithAuthor(),
// Similar logic for "relation_many"
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('comments', null, {});
await queryInterface.bulkDelete('courses', null, {});
await queryInterface.bulkDelete('groups', null, {});
await queryInterface.bulkDelete('posts', null, {});
},
};