33833/backend/src/db/seeders/20231127130745-sample-data.js
2025-09-02 21:25:15 +00:00

367 lines
7.9 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Giochi = db.giochi;
const Prenotazioni = db.prenotazioni;
const Serate = db.serate;
const Tavoli = db.tavoli;
const GiochiData = [
{
nome: 'Wingspan',
// type code here for "images" field
},
{
nome: 'Catan',
// type code here for "images" field
},
{
nome: 'Carcassonne',
// type code here for "images" field
},
{
nome: 'Ticket to Ride',
// type code here for "images" field
},
];
const PrenotazioniData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
data_prenotazione: new Date('2023-11-10T10:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
data_prenotazione: new Date('2023-11-11T11:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
data_prenotazione: new Date('2023-11-12T12:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
data_prenotazione: new Date('2023-11-13T13:00:00Z'),
},
];
const SerateData = [
{
tema: 'Serata novità di Essen',
data: new Date('2023-11-15T19:00:00Z'),
numero_tavoli: 5,
},
{
tema: 'Serata giochi di strategia',
data: new Date('2023-11-22T19:00:00Z'),
numero_tavoli: 4,
},
{
tema: 'Serata giochi di carte',
data: new Date('2023-11-29T19:00:00Z'),
numero_tavoli: 6,
},
{
tema: 'Serata giochi da tavolo classici',
data: new Date('2023-12-06T19:00:00Z'),
numero_tavoli: 3,
},
];
const TavoliData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
posti_totali: 4,
posti_occupati: 2,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
posti_totali: 5,
posti_occupati: 3,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
posti_totali: 6,
posti_occupati: 4,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
posti_totali: 3,
posti_occupati: 1,
},
];
// Similar logic for "relation_many"
async function associatePrenotazioniWithUtente() {
const relatedUtente0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Prenotazioni0 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Prenotazioni0?.setUtente) {
await Prenotazioni0.setUtente(relatedUtente0);
}
const relatedUtente1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Prenotazioni1 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Prenotazioni1?.setUtente) {
await Prenotazioni1.setUtente(relatedUtente1);
}
const relatedUtente2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Prenotazioni2 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Prenotazioni2?.setUtente) {
await Prenotazioni2.setUtente(relatedUtente2);
}
const relatedUtente3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Prenotazioni3 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Prenotazioni3?.setUtente) {
await Prenotazioni3.setUtente(relatedUtente3);
}
}
async function associatePrenotazioniWithTavolo() {
const relatedTavolo0 = await Tavoli.findOne({
offset: Math.floor(Math.random() * (await Tavoli.count())),
});
const Prenotazioni0 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Prenotazioni0?.setTavolo) {
await Prenotazioni0.setTavolo(relatedTavolo0);
}
const relatedTavolo1 = await Tavoli.findOne({
offset: Math.floor(Math.random() * (await Tavoli.count())),
});
const Prenotazioni1 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Prenotazioni1?.setTavolo) {
await Prenotazioni1.setTavolo(relatedTavolo1);
}
const relatedTavolo2 = await Tavoli.findOne({
offset: Math.floor(Math.random() * (await Tavoli.count())),
});
const Prenotazioni2 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Prenotazioni2?.setTavolo) {
await Prenotazioni2.setTavolo(relatedTavolo2);
}
const relatedTavolo3 = await Tavoli.findOne({
offset: Math.floor(Math.random() * (await Tavoli.count())),
});
const Prenotazioni3 = await Prenotazioni.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Prenotazioni3?.setTavolo) {
await Prenotazioni3.setTavolo(relatedTavolo3);
}
}
async function associateTavoliWithSeratum() {
const relatedSeratum0 = await Serate.findOne({
offset: Math.floor(Math.random() * (await Serate.count())),
});
const Tavoli0 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Tavoli0?.setSeratum) {
await Tavoli0.setSeratum(relatedSeratum0);
}
const relatedSeratum1 = await Serate.findOne({
offset: Math.floor(Math.random() * (await Serate.count())),
});
const Tavoli1 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Tavoli1?.setSeratum) {
await Tavoli1.setSeratum(relatedSeratum1);
}
const relatedSeratum2 = await Serate.findOne({
offset: Math.floor(Math.random() * (await Serate.count())),
});
const Tavoli2 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Tavoli2?.setSeratum) {
await Tavoli2.setSeratum(relatedSeratum2);
}
const relatedSeratum3 = await Serate.findOne({
offset: Math.floor(Math.random() * (await Serate.count())),
});
const Tavoli3 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Tavoli3?.setSeratum) {
await Tavoli3.setSeratum(relatedSeratum3);
}
}
async function associateTavoliWithGioco() {
const relatedGioco0 = await Giochi.findOne({
offset: Math.floor(Math.random() * (await Giochi.count())),
});
const Tavoli0 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Tavoli0?.setGioco) {
await Tavoli0.setGioco(relatedGioco0);
}
const relatedGioco1 = await Giochi.findOne({
offset: Math.floor(Math.random() * (await Giochi.count())),
});
const Tavoli1 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Tavoli1?.setGioco) {
await Tavoli1.setGioco(relatedGioco1);
}
const relatedGioco2 = await Giochi.findOne({
offset: Math.floor(Math.random() * (await Giochi.count())),
});
const Tavoli2 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Tavoli2?.setGioco) {
await Tavoli2.setGioco(relatedGioco2);
}
const relatedGioco3 = await Giochi.findOne({
offset: Math.floor(Math.random() * (await Giochi.count())),
});
const Tavoli3 = await Tavoli.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Tavoli3?.setGioco) {
await Tavoli3.setGioco(relatedGioco3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Giochi.bulkCreate(GiochiData);
await Prenotazioni.bulkCreate(PrenotazioniData);
await Serate.bulkCreate(SerateData);
await Tavoli.bulkCreate(TavoliData);
await Promise.all([
// Similar logic for "relation_many"
await associatePrenotazioniWithUtente(),
await associatePrenotazioniWithTavolo(),
await associateTavoliWithSeratum(),
await associateTavoliWithGioco(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('giochi', null, {});
await queryInterface.bulkDelete('prenotazioni', null, {});
await queryInterface.bulkDelete('serate', null, {});
await queryInterface.bulkDelete('tavoli', null, {});
},
};