33648/backend/src/db/seeders/20231127130745-sample-data.js
2025-08-27 03:34:26 +00:00

204 lines
3.8 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Orders = db.orders;
const Products = db.products;
const OrdersData = [
{
order_date: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
quantity: 20,
status: 'Cancelled',
},
{
order_date: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
quantity: 15,
status: 'Cancelled',
},
{
order_date: new Date('2023-10-03T09:45:00Z'),
// type code here for "relation_one" field
quantity: 30,
status: 'Cancelled',
},
{
order_date: new Date('2023-10-04T14:20:00Z'),
// type code here for "relation_one" field
quantity: 10,
status: 'Cancelled',
},
{
order_date: new Date('2023-10-05T16:00:00Z'),
// type code here for "relation_one" field
quantity: 25,
status: 'Cancelled',
},
];
const ProductsData = [
{
name: 'Aspirin',
description: 'Pain reliever and anti-inflammatory',
price: 5.99,
stock_quantity: 100,
// type code here for "relation_many" field
},
{
name: 'Ibuprofen',
description: 'Nonsteroidal anti-inflammatory drug',
price: 8.49,
stock_quantity: 200,
// type code here for "relation_many" field
},
{
name: 'Paracetamol',
description: 'Analgesic and antipyretic',
price: 4.99,
stock_quantity: 150,
// type code here for "relation_many" field
},
{
name: 'Amoxicillin',
description: 'Antibiotic for bacterial infections',
price: 12.99,
stock_quantity: 80,
// type code here for "relation_many" field
},
{
name: 'Cough Syrup',
description: 'Relieves cough and throat irritation',
price: 6.49,
stock_quantity: 120,
// type code here for "relation_many" field
},
];
// Similar logic for "relation_many"
async function associateOrderWithProduct() {
const relatedProduct0 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setProduct) {
await Order0.setProduct(relatedProduct0);
}
const relatedProduct1 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setProduct) {
await Order1.setProduct(relatedProduct1);
}
const relatedProduct2 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setProduct) {
await Order2.setProduct(relatedProduct2);
}
const relatedProduct3 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Order3 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Order3?.setProduct) {
await Order3.setProduct(relatedProduct3);
}
const relatedProduct4 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Order4 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Order4?.setProduct) {
await Order4.setProduct(relatedProduct4);
}
}
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Orders.bulkCreate(OrdersData);
await Products.bulkCreate(ProductsData);
await Promise.all([
// Similar logic for "relation_many"
await associateOrderWithProduct(),
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('orders', null, {});
await queryInterface.bulkDelete('products', null, {});
},
};