32710/backend/src/db/seeders/20231127130745-sample-data.js
2025-07-09 17:10:28 +00:00

387 lines
8.1 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Products = db.products;
const Sales = db.sales;
const Shops = db.shops;
const Companies = db.companies;
const ProductsData = [
{
name: 'T-shirt',
price: 20,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Mug',
price: 15,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Poster',
price: 10,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const SalesData = [
{
order_amount: 150.75,
platform: 'Shopify',
order_date: new Date('2023-10-01T10:00:00Z'),
order_count: 3,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
order_amount: 200.5,
platform: 'Etsy',
order_date: new Date('2023-10-02T11:30:00Z'),
order_count: 5,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
order_amount: 300,
platform: 'Printful',
order_date: new Date('2023-10-03T14:00:00Z'),
order_count: 2,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const ShopsData = [
{
name: 'Shopify Store',
platform: 'Etsy',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Etsy Boutique',
platform: 'Shopify',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Printful Shop',
platform: 'Etsy',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const CompaniesData = [
{
name: 'TechCorp',
},
{
name: 'ShopifyPlus',
},
{
name: 'EtsyPro',
},
];
// Similar logic for "relation_many"
async function associateUserWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setCompany) {
await User0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setCompany) {
await User1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setCompany) {
await User2.setCompany(relatedCompany2);
}
}
// Similar logic for "relation_many"
async function associateProductWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setCompany) {
await Product0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setCompany) {
await Product1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setCompany) {
await Product2.setCompany(relatedCompany2);
}
}
async function associateSaleWithShop() {
const relatedShop0 = await Shops.findOne({
offset: Math.floor(Math.random() * (await Shops.count())),
});
const Sale0 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Sale0?.setShop) {
await Sale0.setShop(relatedShop0);
}
const relatedShop1 = await Shops.findOne({
offset: Math.floor(Math.random() * (await Shops.count())),
});
const Sale1 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Sale1?.setShop) {
await Sale1.setShop(relatedShop1);
}
const relatedShop2 = await Shops.findOne({
offset: Math.floor(Math.random() * (await Shops.count())),
});
const Sale2 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Sale2?.setShop) {
await Sale2.setShop(relatedShop2);
}
}
async function associateSaleWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Sale0 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Sale0?.setCompany) {
await Sale0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Sale1 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Sale1?.setCompany) {
await Sale1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Sale2 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Sale2?.setCompany) {
await Sale2.setCompany(relatedCompany2);
}
}
async function associateShopWithOwner() {
const relatedOwner0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Shop0 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Shop0?.setOwner) {
await Shop0.setOwner(relatedOwner0);
}
const relatedOwner1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Shop1 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Shop1?.setOwner) {
await Shop1.setOwner(relatedOwner1);
}
const relatedOwner2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Shop2 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Shop2?.setOwner) {
await Shop2.setOwner(relatedOwner2);
}
}
async function associateShopWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Shop0 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Shop0?.setCompany) {
await Shop0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Shop1 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Shop1?.setCompany) {
await Shop1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Shop2 = await Shops.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Shop2?.setCompany) {
await Shop2.setCompany(relatedCompany2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Products.bulkCreate(ProductsData);
await Sales.bulkCreate(SalesData);
await Shops.bulkCreate(ShopsData);
await Companies.bulkCreate(CompaniesData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithCompany(),
// Similar logic for "relation_many"
await associateProductWithCompany(),
await associateSaleWithShop(),
await associateSaleWithCompany(),
await associateShopWithOwner(),
await associateShopWithCompany(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('products', null, {});
await queryInterface.bulkDelete('sales', null, {});
await queryInterface.bulkDelete('shops', null, {});
await queryInterface.bulkDelete('companies', null, {});
},
};