327 lines
7.5 KiB
JavaScript
327 lines
7.5 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Matches = db.matches;
|
|
|
|
const Predictions = db.predictions;
|
|
|
|
const Purchases = db.purchases;
|
|
|
|
const MatchesData = [
|
|
{
|
|
match_name: 'Team A vs Team B',
|
|
|
|
match_date: new Date('2023-11-01T15:00:00Z'),
|
|
},
|
|
|
|
{
|
|
match_name: 'Team C vs Team D',
|
|
|
|
match_date: new Date('2023-11-02T18:00:00Z'),
|
|
},
|
|
|
|
{
|
|
match_name: 'Team E vs Team F',
|
|
|
|
match_date: new Date('2023-11-03T20:00:00Z'),
|
|
},
|
|
|
|
{
|
|
match_name: 'Team G vs Team H',
|
|
|
|
match_date: new Date('2023-11-04T17:00:00Z'),
|
|
},
|
|
];
|
|
|
|
const PredictionsData = [
|
|
{
|
|
prediction_name: 'Team A wins',
|
|
|
|
price: 10.99,
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
prediction_name: 'Team C wins',
|
|
|
|
price: 12.99,
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
prediction_name: 'Draw between Team E and Team F',
|
|
|
|
price: 8.99,
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
prediction_name: 'Team G wins',
|
|
|
|
price: 9.99,
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
const PurchasesData = [
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
purchase_date: new Date('2023-11-01T16:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
purchase_date: new Date('2023-11-02T19:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
purchase_date: new Date('2023-11-03T21:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
purchase_date: new Date('2023-11-04T18:00:00Z'),
|
|
},
|
|
];
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associatePredictionWithMatch() {
|
|
const relatedMatch0 = await Matches.findOne({
|
|
offset: Math.floor(Math.random() * (await Matches.count())),
|
|
});
|
|
const Prediction0 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Prediction0?.setMatch) {
|
|
await Prediction0.setMatch(relatedMatch0);
|
|
}
|
|
|
|
const relatedMatch1 = await Matches.findOne({
|
|
offset: Math.floor(Math.random() * (await Matches.count())),
|
|
});
|
|
const Prediction1 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Prediction1?.setMatch) {
|
|
await Prediction1.setMatch(relatedMatch1);
|
|
}
|
|
|
|
const relatedMatch2 = await Matches.findOne({
|
|
offset: Math.floor(Math.random() * (await Matches.count())),
|
|
});
|
|
const Prediction2 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Prediction2?.setMatch) {
|
|
await Prediction2.setMatch(relatedMatch2);
|
|
}
|
|
|
|
const relatedMatch3 = await Matches.findOne({
|
|
offset: Math.floor(Math.random() * (await Matches.count())),
|
|
});
|
|
const Prediction3 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Prediction3?.setMatch) {
|
|
await Prediction3.setMatch(relatedMatch3);
|
|
}
|
|
}
|
|
|
|
async function associatePredictionWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Prediction0 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Prediction0?.setUser) {
|
|
await Prediction0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Prediction1 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Prediction1?.setUser) {
|
|
await Prediction1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Prediction2 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Prediction2?.setUser) {
|
|
await Prediction2.setUser(relatedUser2);
|
|
}
|
|
|
|
const relatedUser3 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Prediction3 = await Predictions.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Prediction3?.setUser) {
|
|
await Prediction3.setUser(relatedUser3);
|
|
}
|
|
}
|
|
|
|
async function associatePurchaseWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Purchase0 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Purchase0?.setUser) {
|
|
await Purchase0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Purchase1 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Purchase1?.setUser) {
|
|
await Purchase1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Purchase2 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Purchase2?.setUser) {
|
|
await Purchase2.setUser(relatedUser2);
|
|
}
|
|
|
|
const relatedUser3 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Purchase3 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Purchase3?.setUser) {
|
|
await Purchase3.setUser(relatedUser3);
|
|
}
|
|
}
|
|
|
|
async function associatePurchaseWithPrediction() {
|
|
const relatedPrediction0 = await Predictions.findOne({
|
|
offset: Math.floor(Math.random() * (await Predictions.count())),
|
|
});
|
|
const Purchase0 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Purchase0?.setPrediction) {
|
|
await Purchase0.setPrediction(relatedPrediction0);
|
|
}
|
|
|
|
const relatedPrediction1 = await Predictions.findOne({
|
|
offset: Math.floor(Math.random() * (await Predictions.count())),
|
|
});
|
|
const Purchase1 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Purchase1?.setPrediction) {
|
|
await Purchase1.setPrediction(relatedPrediction1);
|
|
}
|
|
|
|
const relatedPrediction2 = await Predictions.findOne({
|
|
offset: Math.floor(Math.random() * (await Predictions.count())),
|
|
});
|
|
const Purchase2 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Purchase2?.setPrediction) {
|
|
await Purchase2.setPrediction(relatedPrediction2);
|
|
}
|
|
|
|
const relatedPrediction3 = await Predictions.findOne({
|
|
offset: Math.floor(Math.random() * (await Predictions.count())),
|
|
});
|
|
const Purchase3 = await Purchases.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Purchase3?.setPrediction) {
|
|
await Purchase3.setPrediction(relatedPrediction3);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Matches.bulkCreate(MatchesData);
|
|
|
|
await Predictions.bulkCreate(PredictionsData);
|
|
|
|
await Purchases.bulkCreate(PurchasesData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
await associatePredictionWithMatch(),
|
|
|
|
await associatePredictionWithUser(),
|
|
|
|
await associatePurchaseWithUser(),
|
|
|
|
await associatePurchaseWithPrediction(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('matches', null, {});
|
|
|
|
await queryInterface.bulkDelete('predictions', null, {});
|
|
|
|
await queryInterface.bulkDelete('purchases', null, {});
|
|
},
|
|
};
|