32514/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-28 13:06:20 +00:00

199 lines
4.4 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const AnalysisResults = db.analysis_results;
const Photos = db.photos;
const AnalysisResultsData = [
{
// type code here for "relation_one" field
animal_name: 'Tiger',
similarity_score: 0.85,
analyzed_at: new Date('2023-10-01T10:10:00Z'),
},
{
// type code here for "relation_one" field
animal_name: 'Elephant',
similarity_score: 0.78,
analyzed_at: new Date('2023-10-02T11:40:00Z'),
},
{
// type code here for "relation_one" field
animal_name: 'Eagle',
similarity_score: 0.92,
analyzed_at: new Date('2023-10-03T12:55:00Z'),
},
{
// type code here for "relation_one" field
animal_name: 'Dolphin',
similarity_score: 0.88,
analyzed_at: new Date('2023-10-04T14:10:00Z'),
},
];
const PhotosData = [
{
// type code here for "images" field
uploaded_at: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "images" field
uploaded_at: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "images" field
uploaded_at: new Date('2023-10-03T12:45:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "images" field
uploaded_at: new Date('2023-10-04T14:00:00Z'),
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateAnalysisResultWithPhoto() {
const relatedPhoto0 = await Photos.findOne({
offset: Math.floor(Math.random() * (await Photos.count())),
});
const AnalysisResult0 = await AnalysisResults.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AnalysisResult0?.setPhoto) {
await AnalysisResult0.setPhoto(relatedPhoto0);
}
const relatedPhoto1 = await Photos.findOne({
offset: Math.floor(Math.random() * (await Photos.count())),
});
const AnalysisResult1 = await AnalysisResults.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AnalysisResult1?.setPhoto) {
await AnalysisResult1.setPhoto(relatedPhoto1);
}
const relatedPhoto2 = await Photos.findOne({
offset: Math.floor(Math.random() * (await Photos.count())),
});
const AnalysisResult2 = await AnalysisResults.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AnalysisResult2?.setPhoto) {
await AnalysisResult2.setPhoto(relatedPhoto2);
}
const relatedPhoto3 = await Photos.findOne({
offset: Math.floor(Math.random() * (await Photos.count())),
});
const AnalysisResult3 = await AnalysisResults.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AnalysisResult3?.setPhoto) {
await AnalysisResult3.setPhoto(relatedPhoto3);
}
}
async function associatePhotoWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Photo0 = await Photos.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Photo0?.setUser) {
await Photo0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Photo1 = await Photos.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Photo1?.setUser) {
await Photo1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Photo2 = await Photos.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Photo2?.setUser) {
await Photo2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Photo3 = await Photos.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Photo3?.setUser) {
await Photo3.setUser(relatedUser3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await AnalysisResults.bulkCreate(AnalysisResultsData);
await Photos.bulkCreate(PhotosData);
await Promise.all([
// Similar logic for "relation_many"
await associateAnalysisResultWithPhoto(),
await associatePhotoWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('analysis_results', null, {});
await queryInterface.bulkDelete('photos', null, {});
},
};