30311/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-29 14:21:04 +00:00

318 lines
7.3 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Orders = db.orders;
const Products = db.products;
const Organizations = db.organizations;
const OrdersData = [
{
// type code here for "relation_one" field
order_date: new Date('2023-10-01T10:00:00Z'),
total_amount: 59.98,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
order_date: new Date('2023-10-02T14:30:00Z'),
total_amount: 89.99,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
order_date: new Date('2023-10-03T09:15:00Z'),
total_amount: 24.99,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const ProductsData = [
{
name: 'Wireless Mouse',
description: 'Ergonomic wireless mouse with USB receiver',
price: 29.99,
stock: 150,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Bluetooth Headphones',
description: 'Noise-cancelling over-ear headphones',
price: 89.99,
stock: 75,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Smartphone Stand',
description: 'Adjustable stand for smartphones and tablets',
price: 19.99,
stock: 200,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'Alfred Wegener',
},
{
name: 'John Bardeen',
},
{
name: 'August Kekule',
},
];
// Similar logic for "relation_many"
async function associateUserWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setOrganization) {
await User0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setOrganization) {
await User1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setOrganization) {
await User2.setOrganization(relatedOrganization2);
}
}
async function associateOrderWithCustomer() {
const relatedCustomer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setCustomer) {
await Order0.setCustomer(relatedCustomer0);
}
const relatedCustomer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setCustomer) {
await Order1.setCustomer(relatedCustomer1);
}
const relatedCustomer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.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 associateOrderWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setOrganization) {
await Order0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setOrganization) {
await Order1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setOrganization) {
await Order2.setOrganization(relatedOrganization2);
}
}
async function associateProductWithVendor() {
const relatedVendor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setVendor) {
await Product0.setVendor(relatedVendor0);
}
const relatedVendor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setVendor) {
await Product1.setVendor(relatedVendor1);
}
const relatedVendor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setVendor) {
await Product2.setVendor(relatedVendor2);
}
}
async function associateProductWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setOrganization) {
await Product0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setOrganization) {
await Product1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setOrganization) {
await Product2.setOrganization(relatedOrganization2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Orders.bulkCreate(OrdersData);
await Products.bulkCreate(ProductsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateOrderWithCustomer(),
// Similar logic for "relation_many"
await associateOrderWithOrganization(),
await associateProductWithVendor(),
await associateProductWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('orders', null, {});
await queryInterface.bulkDelete('products', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};