30335/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-30 20:16:26 +00:00

248 lines
4.6 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Comparisons = db.comparisons;
const Countries = db.countries;
const News = db.news;
const Rankings = db.rankings;
const Statistics = db.statistics;
const ComparisonsData = [
{
// type code here for "relation_many" field
comparison_type: 'GDP vs Population',
date: new Date('2023-01-01T00:00:00Z'),
},
{
// type code here for "relation_many" field
comparison_type: 'Literacy Rate vs Life Expectancy',
date: new Date('2023-01-01T00:00:00Z'),
},
{
// type code here for "relation_many" field
comparison_type: 'GDP Growth',
date: new Date('2023-01-01T00:00:00Z'),
},
];
const CountriesData = [
{
name: 'United States',
gdp: 21433226,
population: 331002651,
literacy_rate: 99,
life_expectancy: 78.9,
// type code here for "relation_many" field
},
{
name: 'China',
gdp: 14342903,
population: 1439323776,
literacy_rate: 96.8,
life_expectancy: 76.7,
// type code here for "relation_many" field
},
{
name: 'India',
gdp: 2875142,
population: 1380004385,
literacy_rate: 74.4,
life_expectancy: 69.4,
// type code here for "relation_many" field
},
];
const NewsData = [
{
title: 'Global Economic Outlook',
content: 'The global economy is expected to grow by 4% in 2023.',
published_date: new Date('2023-01-01T00:00:00Z'),
},
{
title: 'Population Trends',
content: 'World population is projected to reach 8 billion by 2025.',
published_date: new Date('2023-01-01T00:00:00Z'),
},
{
title: 'Literacy Initiatives',
content: 'New programs aim to increase literacy rates worldwide.',
published_date: new Date('2023-01-01T00:00:00Z'),
},
];
const RankingsData = [
{
category: 'Top Economies',
// type code here for "relation_many" field
date: new Date('2023-01-01T00:00:00Z'),
},
{
category: 'Most Populous',
// type code here for "relation_many" field
date: new Date('2023-01-01T00:00:00Z'),
},
{
category: 'Highest Literacy Rate',
// type code here for "relation_many" field
date: new Date('2023-01-01T00:00:00Z'),
},
];
const StatisticsData = [
{
// type code here for "relation_one" field
parameter: 'GDP',
value: 21433226,
date: new Date('2023-01-01T00:00:00Z'),
},
{
// type code here for "relation_one" field
parameter: 'GDP',
value: 14342903,
date: new Date('2023-01-01T00:00:00Z'),
},
{
// type code here for "relation_one" field
parameter: 'GDP',
value: 2875142,
date: new Date('2023-01-01T00:00:00Z'),
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateStatisticWithCountry() {
const relatedCountry0 = await Countries.findOne({
offset: Math.floor(Math.random() * (await Countries.count())),
});
const Statistic0 = await Statistics.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Statistic0?.setCountry) {
await Statistic0.setCountry(relatedCountry0);
}
const relatedCountry1 = await Countries.findOne({
offset: Math.floor(Math.random() * (await Countries.count())),
});
const Statistic1 = await Statistics.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Statistic1?.setCountry) {
await Statistic1.setCountry(relatedCountry1);
}
const relatedCountry2 = await Countries.findOne({
offset: Math.floor(Math.random() * (await Countries.count())),
});
const Statistic2 = await Statistics.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Statistic2?.setCountry) {
await Statistic2.setCountry(relatedCountry2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Comparisons.bulkCreate(ComparisonsData);
await Countries.bulkCreate(CountriesData);
await News.bulkCreate(NewsData);
await Rankings.bulkCreate(RankingsData);
await Statistics.bulkCreate(StatisticsData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateStatisticWithCountry(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('comparisons', null, {});
await queryInterface.bulkDelete('countries', null, {});
await queryInterface.bulkDelete('news', null, {});
await queryInterface.bulkDelete('rankings', null, {});
await queryInterface.bulkDelete('statistics', null, {});
},
};