31122/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-30 16:36:29 +00:00

202 lines
4.1 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Beaches = db.beaches;
const Reviews = db.reviews;
const BeachesData = [
{
name: 'Sunny Beach',
location: 'California, USA',
description: 'A beautiful sunny beach with golden sands and clear waters.',
// type code here for "images" field
latitude: 34.0194,
longitude: -118.4912,
type: 'Private',
// type code here for "relation_many" field
},
{
name: 'Emerald Cove',
location: 'Queensland, Australia',
description:
'A secluded cove with emerald green waters and lush surroundings.',
// type code here for "images" field
latitude: -16.9186,
longitude: 145.7781,
type: 'Public',
// type code here for "relation_many" field
},
{
name: 'Coral Bay',
location: 'Hawaii, USA',
description:
'A vibrant beach known for its coral reefs and snorkeling opportunities.',
// type code here for "images" field
latitude: 20.7984,
longitude: -156.3319,
type: 'Resort',
// type code here for "relation_many" field
},
];
const ReviewsData = [
{
title: 'Amazing Beach!',
content:
'The beach was absolutely stunning and the water was crystal clear.',
rating: 5,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Great for Snorkeling',
content: 'Loved the coral reefs and the variety of fish.',
rating: 4,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Perfect for Relaxation',
content: 'A quiet and peaceful spot to unwind.',
rating: 5,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateReviewWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Review0 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Review0?.setUser) {
await Review0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Review1 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Review1?.setUser) {
await Review1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Review2 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Review2?.setUser) {
await Review2.setUser(relatedUser2);
}
}
async function associateReviewWithBeach() {
const relatedBeach0 = await Beaches.findOne({
offset: Math.floor(Math.random() * (await Beaches.count())),
});
const Review0 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Review0?.setBeach) {
await Review0.setBeach(relatedBeach0);
}
const relatedBeach1 = await Beaches.findOne({
offset: Math.floor(Math.random() * (await Beaches.count())),
});
const Review1 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Review1?.setBeach) {
await Review1.setBeach(relatedBeach1);
}
const relatedBeach2 = await Beaches.findOne({
offset: Math.floor(Math.random() * (await Beaches.count())),
});
const Review2 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Review2?.setBeach) {
await Review2.setBeach(relatedBeach2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Beaches.bulkCreate(BeachesData);
await Reviews.bulkCreate(ReviewsData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateReviewWithUser(),
await associateReviewWithBeach(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('beaches', null, {});
await queryInterface.bulkDelete('reviews', null, {});
},
};