307 lines
6.1 KiB
JavaScript
307 lines
6.1 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Customers = db.customers;
|
|
|
|
const Orders = db.orders;
|
|
|
|
const Products = db.products;
|
|
|
|
const Shipments = db.shipments;
|
|
|
|
const CustomersData = [
|
|
{
|
|
name: 'John Doe',
|
|
|
|
email: 'john.doe@example.com',
|
|
|
|
address: '123 Elm Street, Springfield',
|
|
},
|
|
|
|
{
|
|
name: 'Jane Smith',
|
|
|
|
email: 'jane.smith@example.com',
|
|
|
|
address: '456 Oak Avenue, Metropolis',
|
|
},
|
|
|
|
{
|
|
name: 'Alice Johnson',
|
|
|
|
email: 'alice.johnson@example.com',
|
|
|
|
address: '789 Pine Road, Gotham',
|
|
},
|
|
|
|
{
|
|
name: 'Bob Brown',
|
|
|
|
email: 'bob.brown@example.com',
|
|
|
|
address: '101 Maple Lane, Star City',
|
|
},
|
|
];
|
|
|
|
const OrdersData = [
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_many" field
|
|
|
|
status: 'Pending',
|
|
|
|
order_date: new Date('2023-10-01T10:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_many" field
|
|
|
|
status: 'Delivered',
|
|
|
|
order_date: new Date('2023-10-02T11:30:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_many" field
|
|
|
|
status: 'Shipped',
|
|
|
|
order_date: new Date('2023-10-03T14:45:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_many" field
|
|
|
|
status: 'Shipped',
|
|
|
|
order_date: new Date('2023-10-04T09:15:00Z'),
|
|
},
|
|
];
|
|
|
|
const ProductsData = [
|
|
{
|
|
name: 'Phone Case',
|
|
|
|
description: 'Durable and stylish phone case',
|
|
|
|
price: 19.99,
|
|
|
|
// type code here for "images" field
|
|
|
|
category: 'PhoneCases',
|
|
},
|
|
|
|
{
|
|
name: 'Ring Holder',
|
|
|
|
description: 'Convenient ring holder for phones',
|
|
|
|
price: 9.99,
|
|
|
|
// type code here for "images" field
|
|
|
|
category: 'ChargingAccessories',
|
|
},
|
|
|
|
{
|
|
name: 'Earbuds Case',
|
|
|
|
description: 'Protective case for earbuds',
|
|
|
|
price: 14.99,
|
|
|
|
// type code here for "images" field
|
|
|
|
category: 'ScreenProtectors',
|
|
},
|
|
|
|
{
|
|
name: 'Magnetic Power Bank',
|
|
|
|
description: 'Portable magnetic power bank',
|
|
|
|
price: 29.99,
|
|
|
|
// type code here for "images" field
|
|
|
|
category: 'MagneticPowerBanks',
|
|
},
|
|
];
|
|
|
|
const ShipmentsData = [
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
tracking_number: 'TRACK123456',
|
|
|
|
shipped_date: new Date('2023-10-02T12:00:00Z'),
|
|
|
|
delivery_date: new Date('2023-10-05T15:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
tracking_number: 'TRACK654321',
|
|
|
|
shipped_date: new Date('2023-10-03T13:00:00Z'),
|
|
|
|
delivery_date: new Date('2023-10-06T17:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
tracking_number: 'TRACK112233',
|
|
|
|
shipped_date: new Date('2023-10-04T14:00:00Z'),
|
|
|
|
delivery_date: new Date('2023-10-07T18:00:00Z'),
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
tracking_number: 'TRACK445566',
|
|
|
|
shipped_date: new Date('2023-10-05T15:00:00Z'),
|
|
|
|
delivery_date: new Date('2023-10-08T19:00:00Z'),
|
|
},
|
|
];
|
|
|
|
// 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);
|
|
}
|
|
|
|
const relatedCustomer3 = await Customers.findOne({
|
|
offset: Math.floor(Math.random() * (await Customers.count())),
|
|
});
|
|
const Order3 = await Orders.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Order3?.setCustomer) {
|
|
await Order3.setCustomer(relatedCustomer3);
|
|
}
|
|
}
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associateShipmentWithOrder() {
|
|
const relatedOrder0 = await Orders.findOne({
|
|
offset: Math.floor(Math.random() * (await Orders.count())),
|
|
});
|
|
const Shipment0 = await Shipments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Shipment0?.setOrder) {
|
|
await Shipment0.setOrder(relatedOrder0);
|
|
}
|
|
|
|
const relatedOrder1 = await Orders.findOne({
|
|
offset: Math.floor(Math.random() * (await Orders.count())),
|
|
});
|
|
const Shipment1 = await Shipments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Shipment1?.setOrder) {
|
|
await Shipment1.setOrder(relatedOrder1);
|
|
}
|
|
|
|
const relatedOrder2 = await Orders.findOne({
|
|
offset: Math.floor(Math.random() * (await Orders.count())),
|
|
});
|
|
const Shipment2 = await Shipments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Shipment2?.setOrder) {
|
|
await Shipment2.setOrder(relatedOrder2);
|
|
}
|
|
|
|
const relatedOrder3 = await Orders.findOne({
|
|
offset: Math.floor(Math.random() * (await Orders.count())),
|
|
});
|
|
const Shipment3 = await Shipments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Shipment3?.setOrder) {
|
|
await Shipment3.setOrder(relatedOrder3);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Customers.bulkCreate(CustomersData);
|
|
|
|
await Orders.bulkCreate(OrdersData);
|
|
|
|
await Products.bulkCreate(ProductsData);
|
|
|
|
await Shipments.bulkCreate(ShipmentsData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
await associateOrderWithCustomer(),
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
await associateShipmentWithOrder(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('customers', null, {});
|
|
|
|
await queryInterface.bulkDelete('orders', null, {});
|
|
|
|
await queryInterface.bulkDelete('products', null, {});
|
|
|
|
await queryInterface.bulkDelete('shipments', null, {});
|
|
},
|
|
};
|