30436/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-03 08:43:39 +00:00

203 lines
4.8 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Articles = db.articles;
const Comments = db.comments;
const ArticlesData = [
{
title: 'Top 10 Upcoming Games of 2024',
content: 'A detailed look at the most anticipated games of the next year.',
published_date: new Date('2023-11-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
title: 'The Evolution of Esports',
content:
'Exploring the rise of competitive gaming and its impact on the industry.',
published_date: new Date('2023-10-15T14:30:00Z'),
// type code here for "relation_one" field
},
{
title: 'VR Gaming: The Future is Now',
content: 'How virtual reality is changing the way we play games.',
published_date: new Date('2023-09-20T09:00:00Z'),
// type code here for "relation_one" field
},
];
const CommentsData = [
{
content: "Great article! Can't wait for these games.",
// type code here for "relation_one" field
// type code here for "relation_one" field
comment_date: new Date('2023-11-02T12:00:00Z'),
},
{
content: 'Esports has really changed the gaming landscape.',
// type code here for "relation_one" field
// type code here for "relation_one" field
comment_date: new Date('2023-10-16T10:30:00Z'),
},
{
content: 'VR is definitely the future of gaming.',
// type code here for "relation_one" field
// type code here for "relation_one" field
comment_date: new Date('2023-09-21T08:45:00Z'),
},
];
// Similar logic for "relation_many"
async function associateArticleWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Article0 = await Articles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Article0?.setAuthor) {
await Article0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Article1 = await Articles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Article1?.setAuthor) {
await Article1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Article2 = await Articles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Article2?.setAuthor) {
await Article2.setAuthor(relatedAuthor2);
}
}
async function associateCommentWithArticle() {
const relatedArticle0 = await Articles.findOne({
offset: Math.floor(Math.random() * (await Articles.count())),
});
const Comment0 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Comment0?.setArticle) {
await Comment0.setArticle(relatedArticle0);
}
const relatedArticle1 = await Articles.findOne({
offset: Math.floor(Math.random() * (await Articles.count())),
});
const Comment1 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Comment1?.setArticle) {
await Comment1.setArticle(relatedArticle1);
}
const relatedArticle2 = await Articles.findOne({
offset: Math.floor(Math.random() * (await Articles.count())),
});
const Comment2 = await Comments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Comment2?.setArticle) {
await Comment2.setArticle(relatedArticle2);
}
}
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);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Articles.bulkCreate(ArticlesData);
await Comments.bulkCreate(CommentsData);
await Promise.all([
// Similar logic for "relation_many"
await associateArticleWithAuthor(),
await associateCommentWithArticle(),
await associateCommentWithAuthor(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('articles', null, {});
await queryInterface.bulkDelete('comments', null, {});
},
};