31930/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-02 12:13:13 +00:00

543 lines
12 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Companies = db.companies;
const Images = db.images;
const Listings = db.listings;
const Notifications = db.notifications;
const Orders = db.orders;
const CompaniesData = [
{
name: 'Tech Innovators',
description: 'Leading company in tech innovations.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Fashion Forward',
description: 'Trendy fashion company.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Home Comforts',
description: 'Quality furniture and home decor.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Adventure Gear',
description: 'Outdoor and adventure equipment.',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
];
const ImagesData = [
{
url: 'https://example.com/images/sofa.jpg',
// type code here for "relation_one" field
},
{
url: 'https://example.com/images/bike.jpg',
// type code here for "relation_one" field
},
{
url: 'https://example.com/images/smartphone.jpg',
// type code here for "relation_one" field
},
{
url: 'https://example.com/images/dress.jpg',
// type code here for "relation_one" field
},
];
const ListingsData = [
{
title: 'Vintage Sofa',
description: 'A comfortable vintage sofa in excellent condition.',
price: 250,
// type code here for "relation_one" field
// type code here for "relation_many" field
category: 'clothing',
},
{
title: 'Mountain Bike',
description: 'A sturdy mountain bike suitable for all terrains.',
price: 150,
// type code here for "relation_one" field
// type code here for "relation_many" field
category: 'vehicles',
},
{
title: 'Smartphone',
description: 'Latest model smartphone with all accessories.',
price: 500,
// type code here for "relation_one" field
// type code here for "relation_many" field
category: 'vehicles',
},
{
title: 'Designer Dress',
description: 'Elegant designer dress for special occasions.',
price: 120,
// type code here for "relation_one" field
// type code here for "relation_many" field
category: 'clothing',
},
];
const NotificationsData = [
{
// type code here for "relation_one" field
message: 'New listing available: Vintage Sofa',
sent_at: new Date('2023-10-01T10:05:00Z'),
},
{
// type code here for "relation_one" field
message: 'Order placed for Mountain Bike',
sent_at: new Date('2023-10-02T11:05:00Z'),
},
{
// type code here for "relation_one" field
message: 'Listing approved: Smartphone',
sent_at: new Date('2023-10-03T12:05:00Z'),
},
{
// type code here for "relation_one" field
message: 'Order shipped: Designer Dress',
sent_at: new Date('2023-10-04T13:05:00Z'),
},
];
const OrdersData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
order_date: new Date('2023-10-06T15:00:00Z'),
total_price: 250,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
order_date: new Date('2023-10-07T16:00:00Z'),
total_price: 150,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
order_date: new Date('2023-10-08T17:00:00Z'),
total_price: 500,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
order_date: new Date('2023-10-09T18:00:00Z'),
total_price: 120,
},
];
// Similar logic for "relation_many"
async function associateCompanyWithOwner() {
const relatedOwner0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Company0 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Company0?.setOwner) {
await Company0.setOwner(relatedOwner0);
}
const relatedOwner1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Company1 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Company1?.setOwner) {
await Company1.setOwner(relatedOwner1);
}
const relatedOwner2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Company2 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Company2?.setOwner) {
await Company2.setOwner(relatedOwner2);
}
const relatedOwner3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Company3 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Company3?.setOwner) {
await Company3.setOwner(relatedOwner3);
}
}
// Similar logic for "relation_many"
async function associateImageWithListing() {
const relatedListing0 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Image0 = await Images.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Image0?.setListing) {
await Image0.setListing(relatedListing0);
}
const relatedListing1 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Image1 = await Images.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Image1?.setListing) {
await Image1.setListing(relatedListing1);
}
const relatedListing2 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Image2 = await Images.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Image2?.setListing) {
await Image2.setListing(relatedListing2);
}
const relatedListing3 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Image3 = await Images.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Image3?.setListing) {
await Image3.setListing(relatedListing3);
}
}
async function associateListingWithSeller() {
const relatedSeller0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Listing0 = await Listings.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Listing0?.setSeller) {
await Listing0.setSeller(relatedSeller0);
}
const relatedSeller1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Listing1 = await Listings.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Listing1?.setSeller) {
await Listing1.setSeller(relatedSeller1);
}
const relatedSeller2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Listing2 = await Listings.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Listing2?.setSeller) {
await Listing2.setSeller(relatedSeller2);
}
const relatedSeller3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Listing3 = await Listings.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Listing3?.setSeller) {
await Listing3.setSeller(relatedSeller3);
}
}
// Similar logic for "relation_many"
async function associateNotificationWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification0 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Notification0?.setUser) {
await Notification0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification1 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Notification1?.setUser) {
await Notification1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification2 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Notification2?.setUser) {
await Notification2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification3 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Notification3?.setUser) {
await Notification3.setUser(relatedUser3);
}
}
async function associateOrderWithBuyer() {
const relatedBuyer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setBuyer) {
await Order0.setBuyer(relatedBuyer0);
}
const relatedBuyer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setBuyer) {
await Order1.setBuyer(relatedBuyer1);
}
const relatedBuyer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setBuyer) {
await Order2.setBuyer(relatedBuyer2);
}
const relatedBuyer3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order3 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Order3?.setBuyer) {
await Order3.setBuyer(relatedBuyer3);
}
}
async function associateOrderWithListing() {
const relatedListing0 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setListing) {
await Order0.setListing(relatedListing0);
}
const relatedListing1 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setListing) {
await Order1.setListing(relatedListing1);
}
const relatedListing2 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setListing) {
await Order2.setListing(relatedListing2);
}
const relatedListing3 = await Listings.findOne({
offset: Math.floor(Math.random() * (await Listings.count())),
});
const Order3 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Order3?.setListing) {
await Order3.setListing(relatedListing3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Companies.bulkCreate(CompaniesData);
await Images.bulkCreate(ImagesData);
await Listings.bulkCreate(ListingsData);
await Notifications.bulkCreate(NotificationsData);
await Orders.bulkCreate(OrdersData);
await Promise.all([
// Similar logic for "relation_many"
await associateCompanyWithOwner(),
// Similar logic for "relation_many"
await associateImageWithListing(),
await associateListingWithSeller(),
// Similar logic for "relation_many"
await associateNotificationWithUser(),
await associateOrderWithBuyer(),
await associateOrderWithListing(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('companies', null, {});
await queryInterface.bulkDelete('images', null, {});
await queryInterface.bulkDelete('listings', null, {});
await queryInterface.bulkDelete('notifications', null, {});
await queryInterface.bulkDelete('orders', null, {});
},
};