30577/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-08 19:40:37 +00:00

296 lines
6.9 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Comments = db.comments;
const ModerationLogs = db.moderation_logs;
const Posts = db.posts;
const CommentsData = [
{
text: 'Great post! Thanks for sharing.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
text: 'Looking forward to the event!',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
text: 'Very informative article.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const ModerationLogsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
action: 'Approved post',
timestamp: new Date('2023-10-01T12:30:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
action: 'Flagged post for review',
timestamp: new Date('2023-10-02T14:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
action: 'Approved post',
timestamp: new Date('2023-10-03T11:00:00Z'),
},
];
const PostsData = [
{
content: 'Exploring the new features of our platform!',
// type code here for "relation_one" field
},
{
content: 'Check out this amazing article on tech trends.',
// type code here for "relation_one" field
},
{
content: 'Join our community event this weekend!',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
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 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 associateModerationLogWithModerator() {
const relatedModerator0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ModerationLog0 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ModerationLog0?.setModerator) {
await ModerationLog0.setModerator(relatedModerator0);
}
const relatedModerator1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ModerationLog1 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ModerationLog1?.setModerator) {
await ModerationLog1.setModerator(relatedModerator1);
}
const relatedModerator2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ModerationLog2 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ModerationLog2?.setModerator) {
await ModerationLog2.setModerator(relatedModerator2);
}
}
async function associateModerationLogWithPost() {
const relatedPost0 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const ModerationLog0 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ModerationLog0?.setPost) {
await ModerationLog0.setPost(relatedPost0);
}
const relatedPost1 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const ModerationLog1 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ModerationLog1?.setPost) {
await ModerationLog1.setPost(relatedPost1);
}
const relatedPost2 = await Posts.findOne({
offset: Math.floor(Math.random() * (await Posts.count())),
});
const ModerationLog2 = await ModerationLogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ModerationLog2?.setPost) {
await ModerationLog2.setPost(relatedPost2);
}
}
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);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Comments.bulkCreate(CommentsData);
await ModerationLogs.bulkCreate(ModerationLogsData);
await Posts.bulkCreate(PostsData);
await Promise.all([
// Similar logic for "relation_many"
await associateCommentWithPost(),
await associateCommentWithAuthor(),
await associateModerationLogWithModerator(),
await associateModerationLogWithPost(),
await associatePostWithAuthor(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('comments', null, {});
await queryInterface.bulkDelete('moderation_logs', null, {});
await queryInterface.bulkDelete('posts', null, {});
},
};