31532/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-14 21:33:10 +00:00

169 lines
3.4 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Playlists = db.playlists;
const Tracks = db.tracks;
const PlaylistsData = [
{
name: 'Chill Vibes',
// type code here for "relation_one" field
// type code here for "relation_many" field
transfer_complete: true,
},
{
name: 'Workout Hits',
// type code here for "relation_one" field
// type code here for "relation_many" field
transfer_complete: false,
},
{
name: 'Road Trip Tunes',
// type code here for "relation_one" field
// type code here for "relation_many" field
transfer_complete: true,
},
];
const TracksData = [
{
title: 'Song A',
artist: 'Artist 1',
status: 'duplicate',
// type code here for "relation_one" field
},
{
title: 'Song B',
artist: 'Artist 2',
status: 'duplicate',
// type code here for "relation_one" field
},
{
title: 'Song C',
artist: 'Artist 3',
status: 'transferred',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associatePlaylistWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Playlist0 = await Playlists.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Playlist0?.setUser) {
await Playlist0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Playlist1 = await Playlists.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Playlist1?.setUser) {
await Playlist1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Playlist2 = await Playlists.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Playlist2?.setUser) {
await Playlist2.setUser(relatedUser2);
}
}
// Similar logic for "relation_many"
async function associateTrackWithPlaylist() {
const relatedPlaylist0 = await Playlists.findOne({
offset: Math.floor(Math.random() * (await Playlists.count())),
});
const Track0 = await Tracks.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Track0?.setPlaylist) {
await Track0.setPlaylist(relatedPlaylist0);
}
const relatedPlaylist1 = await Playlists.findOne({
offset: Math.floor(Math.random() * (await Playlists.count())),
});
const Track1 = await Tracks.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Track1?.setPlaylist) {
await Track1.setPlaylist(relatedPlaylist1);
}
const relatedPlaylist2 = await Playlists.findOne({
offset: Math.floor(Math.random() * (await Playlists.count())),
});
const Track2 = await Tracks.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Track2?.setPlaylist) {
await Track2.setPlaylist(relatedPlaylist2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Playlists.bulkCreate(PlaylistsData);
await Tracks.bulkCreate(TracksData);
await Promise.all([
// Similar logic for "relation_many"
await associatePlaylistWithUser(),
// Similar logic for "relation_many"
await associateTrackWithPlaylist(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('playlists', null, {});
await queryInterface.bulkDelete('tracks', null, {});
},
};