33591/backend/src/db/seeders/20231127130745-sample-data.js
2025-08-24 10:37:55 +00:00

260 lines
5.8 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Blogs = db.blogs;
const Inquiries = db.inquiries;
const Properties = db.properties;
const BlogsData = [
{
title: 'The Rise of Luxury Real Estate in Dubai',
content:
'Dubai has become a hub for luxury real estate, attracting investors from around the world.',
published_date: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
title: 'Top 5 Neighborhoods to Live in Dubai',
content:
'Discover the best neighborhoods in Dubai for families, singles, and professionals.',
published_date: new Date('2023-09-15T10:00:00Z'),
// type code here for "relation_one" field
},
{
title: 'How to Invest in Dubai Real Estate',
content: "A guide to investing in Dubai's booming real estate market.",
published_date: new Date('2023-08-20T10:00:00Z'),
// type code here for "relation_one" field
},
];
const InquiriesData = [
{
visitor_name: 'Alice Johnson',
visitor_email: 'alice.johnson@example.com',
message: 'I am interested in the villa in Palm Jumeirah.',
// type code here for "relation_one" field
},
{
visitor_name: 'Bob Martin',
visitor_email: 'bob.martin@example.com',
message: 'Can I schedule a viewing for the apartment in Downtown?',
// type code here for "relation_one" field
},
{
visitor_name: 'Charlie Lee',
visitor_email: 'charlie.lee@example.com',
message: 'Is the studio in JLT still available?',
// type code here for "relation_one" field
},
];
const PropertiesData = [
{
title: 'Luxury Villa in Palm Jumeirah',
description: 'A stunning villa with a private beach and pool.',
price: 15000000,
status: 'rented',
// type code here for "relation_one" field
// type code here for "images" field
location: 'Palm Jumeirah, Dubai',
},
{
title: 'Modern Apartment in Downtown',
description: 'A sleek apartment with city views and modern amenities.',
price: 2500000,
status: 'available',
// type code here for "relation_one" field
// type code here for "images" field
location: 'Downtown Dubai',
},
{
title: 'Cozy Studio in JLT',
description: 'A compact studio perfect for singles or couples.',
price: 800000,
status: 'available',
// type code here for "relation_one" field
// type code here for "images" field
location: 'Jumeirah Lake Towers, Dubai',
},
];
// Similar logic for "relation_many"
async function associateBlogWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Blog0 = await Blogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Blog0?.setAuthor) {
await Blog0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Blog1 = await Blogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Blog1?.setAuthor) {
await Blog1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Blog2 = await Blogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Blog2?.setAuthor) {
await Blog2.setAuthor(relatedAuthor2);
}
}
async function associateInquiryWithProperty() {
const relatedProperty0 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Inquiry0 = await Inquiries.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Inquiry0?.setProperty) {
await Inquiry0.setProperty(relatedProperty0);
}
const relatedProperty1 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Inquiry1 = await Inquiries.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Inquiry1?.setProperty) {
await Inquiry1.setProperty(relatedProperty1);
}
const relatedProperty2 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Inquiry2 = await Inquiries.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Inquiry2?.setProperty) {
await Inquiry2.setProperty(relatedProperty2);
}
}
async function associatePropertyWithAgent() {
const relatedAgent0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property0 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Property0?.setAgent) {
await Property0.setAgent(relatedAgent0);
}
const relatedAgent1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property1 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Property1?.setAgent) {
await Property1.setAgent(relatedAgent1);
}
const relatedAgent2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property2 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Property2?.setAgent) {
await Property2.setAgent(relatedAgent2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Blogs.bulkCreate(BlogsData);
await Inquiries.bulkCreate(InquiriesData);
await Properties.bulkCreate(PropertiesData);
await Promise.all([
// Similar logic for "relation_many"
await associateBlogWithAuthor(),
await associateInquiryWithProperty(),
await associatePropertyWithAgent(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('blogs', null, {});
await queryInterface.bulkDelete('inquiries', null, {});
await queryInterface.bulkDelete('properties', null, {});
},
};