Initial commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Bet = require('./models/bet.model');
|
||||
|
||||
const seedDatabase = async (sequelize) => {
|
||||
const { User, Group, Friendship, FriendRequest } = sequelize.models;
|
||||
|
||||
try {
|
||||
const userCount = await User.count();
|
||||
if (userCount > 0) {
|
||||
// console.log('PostgreSQL database is not empty. Skipping seed.');
|
||||
return;
|
||||
}
|
||||
console.log('Seeding databases...');
|
||||
|
||||
// PostgreSQL tables are already empty due to `sync({ force: true })` in server.js
|
||||
// Clear MongoDB data
|
||||
await Bet.deleteMany({});
|
||||
|
||||
// --- SEED POSTGRESQL ---
|
||||
|
||||
// Create Users
|
||||
const usersData = [
|
||||
{ id: 1, name: 'Mario Rossi', email: 'mario.rossi@example.com', avatarUrl: 'https://picsum.photos/id/1005/200/200', score: 120 },
|
||||
{ id: 2, name: 'Luigi Verdi', email: 'luigi.verdi@example.com', avatarUrl: 'https://picsum.photos/id/1012/200/200', score: 95 },
|
||||
{ id: 3, name: 'Giovanna Bianchi', email: 'giovanna.bianchi@example.com', avatarUrl: 'https://picsum.photos/id/1027/200/200', score: 150 },
|
||||
{ id: 4, name: 'Anna Neri', email: 'anna.neri@example.com', avatarUrl: 'https://picsum.photos/id/1011/200/200', score: 80 },
|
||||
{ id: 5, name: 'Carlo Gialli', email: 'carlo.gialli@example.com', avatarUrl: 'https://picsum.photos/id/20/200/200', score: 200 },
|
||||
{ id: 6, name: 'Paolo Marroni', email: 'paolo.marroni@example.com', avatarUrl: 'https://picsum.photos/id/30/200/200', score: 50 },
|
||||
];
|
||||
await User.bulkCreate(usersData);
|
||||
|
||||
// Create Friendships
|
||||
const friendshipsData = [
|
||||
{ user1Id: 1, user2Id: 2 },
|
||||
{ user1Id: 1, user2Id: 3 },
|
||||
{ user1Id: 2, user2Id: 3 },
|
||||
];
|
||||
await Friendship.bulkCreate(friendshipsData);
|
||||
|
||||
// Create Friend Requests
|
||||
const friendRequestsData = [
|
||||
{ fromUserId: 4, toUserId: 1, status: 'PENDING' },
|
||||
{ fromUserId: 1, toUserId: 5, status: 'PENDING' },
|
||||
];
|
||||
await FriendRequest.bulkCreate(friendRequestsData);
|
||||
|
||||
// Create Groups and add members
|
||||
const group1 = await Group.create({ id: 1, name: 'Amici Stretti' });
|
||||
await group1.setMembers([1, 2, 3]);
|
||||
const group2 = await Group.create({ id: 2, name: 'Colleghi' });
|
||||
await group2.setMembers([1, 4, 5]);
|
||||
|
||||
// --- SEED MONGODB ---
|
||||
|
||||
// Create Bets, referencing PostgreSQL IDs
|
||||
const betsData = [
|
||||
{ title: 'Chi vince a calcetto?', description: 'Partita di venerdì sera', stake: 'Una pizza', participants: [{ user: 1, role: 'PARTICIPANT' }, { user: 2, role: 'PARTICIPANT' }], status: 'ACTIVE', startDate: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), endDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), isPublic: false, groupId: group1.id.toString() },
|
||||
{ title: 'Prossimo film al cinema', description: 'Quale film vedremo?', stake: 'Offre i biglietti', participants: [{ user: 1, role: 'PARTICIPANT' }, { user: 3, role: 'PARTICIPANT' }], status: 'AWAITING_VALIDATION', startDate: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), isPublic: false, groupId: group1.id.toString() },
|
||||
{ title: 'Riuscirò a finire il report?', description: 'Entro la deadline di domani!', stake: 'Un caffè', participants: [{ user: 1, role: 'PARTICIPANT' }, { user: 4, role: 'OBSERVER' }], status: 'ACTIVE', startDate: new Date(), endDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000), isPublic: true },
|
||||
{ title: 'Scommessa passata', description: 'Chi ha mangiato più sushi', stake: '€20', participants: [{ user: 1, role: 'PARTICIPANT' }, { user: 2, role: 'PARTICIPANT' }], status: 'COMPLETED', startDate: new Date(Date.now() - 12 * 24 * 60 * 60 * 1000), endDate: new Date(Date.now() - 10 * 24 * 60 * 60 * 1000), isPublic: false, groupId: group1.id.toString(), winner: 2 },
|
||||
{ title: 'Scommessa pubblica', description: 'Quanti goal segnerà la nazionale?', stake: 'Gloria eterna', participants: [{ user: 5, role: 'PARTICIPANT' }, { user: 6, role: 'PARTICIPANT' }], status: 'ACTIVE', startDate: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000), endDate: null, isPublic: true },
|
||||
];
|
||||
await Bet.insertMany(betsData);
|
||||
|
||||
console.log('Database seeded successfully.');
|
||||
} catch (error) {
|
||||
console.error('Error seeding database:', error);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { seedDatabase };
|
||||
Reference in New Issue
Block a user