30923/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-22 21:06:41 +00:00

222 lines
4.6 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Scripts = db.scripts;
const Subscriptions = db.subscriptions;
const Videos = db.videos;
const ScriptsData = [
{
title: 'How to Cook Pasta',
content: 'Boil water, add pasta, cook for 10 minutes.',
// type code here for "relation_one" field
},
{
title: 'Yoga for Beginners',
content: 'Start with breathing exercises, then basic poses.',
// type code here for "relation_one" field
},
{
title: 'Daily Motivation',
content: 'Believe in yourself and all that you are.',
// type code here for "relation_one" field
},
];
const SubscriptionsData = [
{
name: 'Basic Plan',
price: 9.99,
duration: 'yearly',
},
{
name: 'Pro Plan',
price: 19.99,
duration: 'monthly',
},
{
name: 'Enterprise Plan',
price: 49.99,
duration: 'monthly',
},
];
const VideosData = [
{
title: 'Pasta Cooking Guide',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Beginner Yoga Session',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Morning Motivation',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateScriptWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Script0 = await Scripts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Script0?.setUser) {
await Script0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Script1 = await Scripts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Script1?.setUser) {
await Script1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Script2 = await Scripts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Script2?.setUser) {
await Script2.setUser(relatedUser2);
}
}
async function associateVideoWithScript() {
const relatedScript0 = await Scripts.findOne({
offset: Math.floor(Math.random() * (await Scripts.count())),
});
const Video0 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Video0?.setScript) {
await Video0.setScript(relatedScript0);
}
const relatedScript1 = await Scripts.findOne({
offset: Math.floor(Math.random() * (await Scripts.count())),
});
const Video1 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Video1?.setScript) {
await Video1.setScript(relatedScript1);
}
const relatedScript2 = await Scripts.findOne({
offset: Math.floor(Math.random() * (await Scripts.count())),
});
const Video2 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Video2?.setScript) {
await Video2.setScript(relatedScript2);
}
}
async function associateVideoWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Video0 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Video0?.setUser) {
await Video0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Video1 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Video1?.setUser) {
await Video1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Video2 = await Videos.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Video2?.setUser) {
await Video2.setUser(relatedUser2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Scripts.bulkCreate(ScriptsData);
await Subscriptions.bulkCreate(SubscriptionsData);
await Videos.bulkCreate(VideosData);
await Promise.all([
// Similar logic for "relation_many"
await associateScriptWithUser(),
await associateVideoWithScript(),
await associateVideoWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('scripts', null, {});
await queryInterface.bulkDelete('subscriptions', null, {});
await queryInterface.bulkDelete('videos', null, {});
},
};