31113/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-30 16:27:13 +00:00

291 lines
6.4 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Matches = db.matches;
const Results = db.results;
const Tournaments = db.tournaments;
const MatchesData = [
{
match_name: 'Match 1',
match_date: new Date('2023-06-01T12:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_many" field
status: 'scheduled',
},
{
match_name: 'Match 2',
match_date: new Date('2023-06-02T14:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_many" field
status: 'ongoing',
},
{
match_name: 'Match 3',
match_date: new Date('2023-12-01T12:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_many" field
status: 'completed',
},
];
const ResultsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 3,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 2,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
score: 5,
},
];
const TournamentsData = [
{
name: 'Summer Showdown',
start_date: new Date('2023-06-01T10:00:00Z'),
end_date: new Date('2023-06-10T18:00:00Z'),
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Winter Clash',
start_date: new Date('2023-12-01T10:00:00Z'),
end_date: new Date('2023-12-10T18:00:00Z'),
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Spring Cup',
start_date: new Date('2023-03-15T10:00:00Z'),
end_date: new Date('2023-03-20T18:00:00Z'),
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateMatchWithTournament() {
const relatedTournament0 = await Tournaments.findOne({
offset: Math.floor(Math.random() * (await Tournaments.count())),
});
const Match0 = await Matches.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Match0?.setTournament) {
await Match0.setTournament(relatedTournament0);
}
const relatedTournament1 = await Tournaments.findOne({
offset: Math.floor(Math.random() * (await Tournaments.count())),
});
const Match1 = await Matches.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Match1?.setTournament) {
await Match1.setTournament(relatedTournament1);
}
const relatedTournament2 = await Tournaments.findOne({
offset: Math.floor(Math.random() * (await Tournaments.count())),
});
const Match2 = await Matches.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Match2?.setTournament) {
await Match2.setTournament(relatedTournament2);
}
}
// Similar logic for "relation_many"
async function associateResultWithMatch() {
const relatedMatch0 = await Matches.findOne({
offset: Math.floor(Math.random() * (await Matches.count())),
});
const Result0 = await Results.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Result0?.setMatch) {
await Result0.setMatch(relatedMatch0);
}
const relatedMatch1 = await Matches.findOne({
offset: Math.floor(Math.random() * (await Matches.count())),
});
const Result1 = await Results.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Result1?.setMatch) {
await Result1.setMatch(relatedMatch1);
}
const relatedMatch2 = await Matches.findOne({
offset: Math.floor(Math.random() * (await Matches.count())),
});
const Result2 = await Results.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Result2?.setMatch) {
await Result2.setMatch(relatedMatch2);
}
}
async function associateResultWithPlayer() {
const relatedPlayer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Result0 = await Results.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Result0?.setPlayer) {
await Result0.setPlayer(relatedPlayer0);
}
const relatedPlayer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Result1 = await Results.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Result1?.setPlayer) {
await Result1.setPlayer(relatedPlayer1);
}
const relatedPlayer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Result2 = await Results.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Result2?.setPlayer) {
await Result2.setPlayer(relatedPlayer2);
}
}
// Similar logic for "relation_many"
async function associateTournamentWithOrganizer() {
const relatedOrganizer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Tournament0 = await Tournaments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Tournament0?.setOrganizer) {
await Tournament0.setOrganizer(relatedOrganizer0);
}
const relatedOrganizer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Tournament1 = await Tournaments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Tournament1?.setOrganizer) {
await Tournament1.setOrganizer(relatedOrganizer1);
}
const relatedOrganizer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Tournament2 = await Tournaments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Tournament2?.setOrganizer) {
await Tournament2.setOrganizer(relatedOrganizer2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Matches.bulkCreate(MatchesData);
await Results.bulkCreate(ResultsData);
await Tournaments.bulkCreate(TournamentsData);
await Promise.all([
// Similar logic for "relation_many"
await associateMatchWithTournament(),
// Similar logic for "relation_many"
await associateResultWithMatch(),
await associateResultWithPlayer(),
// Similar logic for "relation_many"
await associateTournamentWithOrganizer(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('matches', null, {});
await queryInterface.bulkDelete('results', null, {});
await queryInterface.bulkDelete('tournaments', null, {});
},
};