31661/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-20 11:25:27 +00:00

359 lines
9.0 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const CommentSuggestions = db.comment_suggestions;
const Influencers = db.influencers;
const Tweets = db.tweets;
const Organizations = db.organizations;
const CommentSuggestionsData = [
{
// type code here for "relation_one" field
suggestion: "That's an exciting development!",
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
suggestion: 'Absolutely, DeFi is the future!',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
suggestion: 'NFTs are indeed a game-changer!',
// type code here for "relation_one" field
},
];
const InfluencersData = [
{
name: 'Alice Johnson',
twitter_handle: '@alicejohnson',
// type code here for "relation_one" field
},
{
name: 'Bob Smith',
twitter_handle: '@bobsmith',
// type code here for "relation_one" field
},
{
name: 'Charlie Brown',
twitter_handle: '@charliebrown',
// type code here for "relation_one" field
},
];
const TweetsData = [
{
// type code here for "relation_one" field
content: 'Exploring new horizons in Web3!',
posted_at: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
content: 'The future of DeFi is bright!',
posted_at: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
content: 'NFTs are changing the art world.',
posted_at: new Date('2023-10-03T09:15:00Z'),
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'Crypto Innovators',
},
{
name: 'Web3 Pioneers',
},
{
name: 'Blockchain Builders',
},
];
// Similar logic for "relation_many"
async function associateUserWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setOrganization) {
await User0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setOrganization) {
await User1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setOrganization) {
await User2.setOrganization(relatedOrganization2);
}
}
async function associateCommentSuggestionWithTweet() {
const relatedTweet0 = await Tweets.findOne({
offset: Math.floor(Math.random() * (await Tweets.count())),
});
const CommentSuggestion0 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (CommentSuggestion0?.setTweet) {
await CommentSuggestion0.setTweet(relatedTweet0);
}
const relatedTweet1 = await Tweets.findOne({
offset: Math.floor(Math.random() * (await Tweets.count())),
});
const CommentSuggestion1 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (CommentSuggestion1?.setTweet) {
await CommentSuggestion1.setTweet(relatedTweet1);
}
const relatedTweet2 = await Tweets.findOne({
offset: Math.floor(Math.random() * (await Tweets.count())),
});
const CommentSuggestion2 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (CommentSuggestion2?.setTweet) {
await CommentSuggestion2.setTweet(relatedTweet2);
}
}
async function associateCommentSuggestionWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const CommentSuggestion0 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (CommentSuggestion0?.setOrganization) {
await CommentSuggestion0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const CommentSuggestion1 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (CommentSuggestion1?.setOrganization) {
await CommentSuggestion1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const CommentSuggestion2 = await CommentSuggestions.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (CommentSuggestion2?.setOrganization) {
await CommentSuggestion2.setOrganization(relatedOrganization2);
}
}
async function associateInfluencerWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Influencer0 = await Influencers.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Influencer0?.setOrganization) {
await Influencer0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Influencer1 = await Influencers.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Influencer1?.setOrganization) {
await Influencer1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Influencer2 = await Influencers.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Influencer2?.setOrganization) {
await Influencer2.setOrganization(relatedOrganization2);
}
}
async function associateTweetWithInfluencer() {
const relatedInfluencer0 = await Influencers.findOne({
offset: Math.floor(Math.random() * (await Influencers.count())),
});
const Tweet0 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Tweet0?.setInfluencer) {
await Tweet0.setInfluencer(relatedInfluencer0);
}
const relatedInfluencer1 = await Influencers.findOne({
offset: Math.floor(Math.random() * (await Influencers.count())),
});
const Tweet1 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Tweet1?.setInfluencer) {
await Tweet1.setInfluencer(relatedInfluencer1);
}
const relatedInfluencer2 = await Influencers.findOne({
offset: Math.floor(Math.random() * (await Influencers.count())),
});
const Tweet2 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Tweet2?.setInfluencer) {
await Tweet2.setInfluencer(relatedInfluencer2);
}
}
async function associateTweetWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Tweet0 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Tweet0?.setOrganization) {
await Tweet0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Tweet1 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Tweet1?.setOrganization) {
await Tweet1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Tweet2 = await Tweets.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Tweet2?.setOrganization) {
await Tweet2.setOrganization(relatedOrganization2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await CommentSuggestions.bulkCreate(CommentSuggestionsData);
await Influencers.bulkCreate(InfluencersData);
await Tweets.bulkCreate(TweetsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateCommentSuggestionWithTweet(),
await associateCommentSuggestionWithOrganization(),
await associateInfluencerWithOrganization(),
await associateTweetWithInfluencer(),
await associateTweetWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('comment_suggestions', null, {});
await queryInterface.bulkDelete('influencers', null, {});
await queryInterface.bulkDelete('tweets', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};