31903/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-01 13:30:02 +00:00

219 lines
4.6 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Categories = db.categories;
const Expenses = db.expenses;
const CategoriesData = [
{
name: 'Food & Dining',
icon: 'mdiBus',
// type code here for "relation_many" field
},
{
name: 'Transportation',
icon: 'mdiSchool',
// type code here for "relation_many" field
},
{
name: 'Shopping',
icon: 'mdiSchool',
// type code here for "relation_many" field
},
{
name: 'Entertainment',
icon: 'mdiLightbulb',
// type code here for "relation_many" field
},
];
const ExpensesData = [
{
amount: 50.75,
// type code here for "relation_one" field
date_time: new Date('2023-10-01T12:00:00Z'),
description: 'Lunch at Italian restaurant',
payment_method: 'BankTransfer',
// type code here for "relation_one" field
},
{
amount: 120,
// type code here for "relation_one" field
date_time: new Date('2023-10-02T08:30:00Z'),
description: 'Monthly subway pass',
payment_method: 'Other',
// type code here for "relation_one" field
},
{
amount: 200.5,
// type code here for "relation_one" field
date_time: new Date('2023-10-03T15:45:00Z'),
description: 'New shoes',
payment_method: 'CreditCard',
// type code here for "relation_one" field
},
{
amount: 15,
// type code here for "relation_one" field
date_time: new Date('2023-10-04T19:00:00Z'),
description: 'Movie ticket',
payment_method: 'Other',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateExpenseWithCategory() {
const relatedCategory0 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Expense0 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Expense0?.setCategory) {
await Expense0.setCategory(relatedCategory0);
}
const relatedCategory1 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Expense1 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Expense1?.setCategory) {
await Expense1.setCategory(relatedCategory1);
}
const relatedCategory2 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Expense2 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Expense2?.setCategory) {
await Expense2.setCategory(relatedCategory2);
}
const relatedCategory3 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Expense3 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Expense3?.setCategory) {
await Expense3.setCategory(relatedCategory3);
}
}
async function associateExpenseWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Expense0 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Expense0?.setUser) {
await Expense0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Expense1 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Expense1?.setUser) {
await Expense1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Expense2 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Expense2?.setUser) {
await Expense2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Expense3 = await Expenses.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Expense3?.setUser) {
await Expense3.setUser(relatedUser3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Categories.bulkCreate(CategoriesData);
await Expenses.bulkCreate(ExpensesData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateExpenseWithCategory(),
await associateExpenseWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('categories', null, {});
await queryInterface.bulkDelete('expenses', null, {});
},
};