30408/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-02 07:37:07 +00:00

320 lines
6.3 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Categories = db.categories;
const CoffeeBlends = db.coffee_blends;
const Customers = db.customers;
const Orders = db.orders;
const Payments = db.payments;
const Reports = db.reports;
const CategoriesData = [
{
name: 'Espresso',
},
{
name: 'Single Origin',
},
{
name: 'Flavored',
},
];
const CoffeeBlendsData = [
{
name: 'Espresso Roast',
price: 12.99,
stock_level: 100,
// type code here for "relation_one" field
},
{
name: 'Colombian Single Origin',
price: 14.99,
stock_level: 50,
// type code here for "relation_one" field
},
{
name: 'French Vanilla',
price: 10.99,
stock_level: 75,
// type code here for "relation_one" field
},
];
const CustomersData = [
{
full_name: 'John Doe',
email: 'john.doe@example.com',
// type code here for "relation_many" field
},
{
full_name: 'Jane Smith',
email: 'jane.smith@example.com',
// type code here for "relation_many" field
},
{
full_name: 'Emily Johnson',
email: 'emily.johnson@example.com',
// type code here for "relation_many" field
},
];
const OrdersData = [
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-01T10:00:00Z'),
status: 'shipped',
},
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-02T11:30:00Z'),
status: 'shipped',
},
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-03T14:00:00Z'),
status: 'pending',
},
];
const PaymentsData = [
{
// type code here for "relation_one" field
amount: 38.97,
status: 'pending',
},
{
// type code here for "relation_one" field
amount: 32.97,
status: 'pending',
},
{
// type code here for "relation_one" field
amount: 47.97,
status: 'refunded',
},
];
const ReportsData = [
{
generated_at: new Date('2023-10-06T08:00:00Z'),
report_type: 'Sales Overview',
},
{
generated_at: new Date('2023-10-07T08:00:00Z'),
report_type: 'Customer Insights',
},
{
generated_at: new Date('2023-10-08T08:00:00Z'),
report_type: 'Inventory Status',
},
];
// Similar logic for "relation_many"
async function associateCoffeeBlendWithCategory() {
const relatedCategory0 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const CoffeeBlend0 = await CoffeeBlends.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (CoffeeBlend0?.setCategory) {
await CoffeeBlend0.setCategory(relatedCategory0);
}
const relatedCategory1 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const CoffeeBlend1 = await CoffeeBlends.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (CoffeeBlend1?.setCategory) {
await CoffeeBlend1.setCategory(relatedCategory1);
}
const relatedCategory2 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const CoffeeBlend2 = await CoffeeBlends.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (CoffeeBlend2?.setCategory) {
await CoffeeBlend2.setCategory(relatedCategory2);
}
}
// Similar logic for "relation_many"
async function associateOrderWithCustomer() {
const relatedCustomer0 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setCustomer) {
await Order0.setCustomer(relatedCustomer0);
}
const relatedCustomer1 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setCustomer) {
await Order1.setCustomer(relatedCustomer1);
}
const relatedCustomer2 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setCustomer) {
await Order2.setCustomer(relatedCustomer2);
}
}
// Similar logic for "relation_many"
async function associatePaymentWithOrder() {
const relatedOrder0 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Payment0 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Payment0?.setOrder) {
await Payment0.setOrder(relatedOrder0);
}
const relatedOrder1 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Payment1 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Payment1?.setOrder) {
await Payment1.setOrder(relatedOrder1);
}
const relatedOrder2 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Payment2 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Payment2?.setOrder) {
await Payment2.setOrder(relatedOrder2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Categories.bulkCreate(CategoriesData);
await CoffeeBlends.bulkCreate(CoffeeBlendsData);
await Customers.bulkCreate(CustomersData);
await Orders.bulkCreate(OrdersData);
await Payments.bulkCreate(PaymentsData);
await Reports.bulkCreate(ReportsData);
await Promise.all([
// Similar logic for "relation_many"
await associateCoffeeBlendWithCategory(),
// Similar logic for "relation_many"
await associateOrderWithCustomer(),
// Similar logic for "relation_many"
await associatePaymentWithOrder(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('categories', null, {});
await queryInterface.bulkDelete('coffee_blends', null, {});
await queryInterface.bulkDelete('customers', null, {});
await queryInterface.bulkDelete('orders', null, {});
await queryInterface.bulkDelete('payments', null, {});
await queryInterface.bulkDelete('reports', null, {});
},
};