29775/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-10 19:37:02 +00:00

330 lines
7.0 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Inmates = db.inmates;
const Reviews = db.reviews;
const Prisons = db.prisons;
const InmatesData = [
{
name: 'James Wilson',
age: 34,
photo: 'james_wilson.jpg',
crime: 'Theft',
sentence_length: 5,
debt_amount: 20000,
death_row_status: true,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Robert Johnson',
age: 29,
photo: 'robert_johnson.jpg',
crime: 'Fraud',
sentence_length: 10,
debt_amount: 50000,
death_row_status: false,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Michael Smith',
age: 42,
photo: 'michael_smith.jpg',
crime: 'Assault',
sentence_length: 15,
debt_amount: 100000,
death_row_status: true,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const ReviewsData = [
{
content: 'A very cooperative individual.',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'Shows potential for rehabilitation.',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'High risk but manageable.',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const PrisonsData = [
{
name: 'Hans Selye',
},
{
name: 'William Bayliss',
},
{
name: 'Pierre Simon de Laplace',
},
];
// Similar logic for "relation_many"
async function associateUserWithPrison() {
const relatedPrison0 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setPrison) {
await User0.setPrison(relatedPrison0);
}
const relatedPrison1 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setPrison) {
await User1.setPrison(relatedPrison1);
}
const relatedPrison2 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setPrison) {
await User2.setPrison(relatedPrison2);
}
}
// Similar logic for "relation_many"
async function associateInmateWithPrison() {
const relatedPrison0 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Inmate0 = await Inmates.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Inmate0?.setPrison) {
await Inmate0.setPrison(relatedPrison0);
}
const relatedPrison1 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Inmate1 = await Inmates.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Inmate1?.setPrison) {
await Inmate1.setPrison(relatedPrison1);
}
const relatedPrison2 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Inmate2 = await Inmates.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Inmate2?.setPrison) {
await Inmate2.setPrison(relatedPrison2);
}
}
async function associateReviewWithInmate() {
const relatedInmate0 = await Inmates.findOne({
offset: Math.floor(Math.random() * (await Inmates.count())),
});
const Review0 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Review0?.setInmate) {
await Review0.setInmate(relatedInmate0);
}
const relatedInmate1 = await Inmates.findOne({
offset: Math.floor(Math.random() * (await Inmates.count())),
});
const Review1 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Review1?.setInmate) {
await Review1.setInmate(relatedInmate1);
}
const relatedInmate2 = await Inmates.findOne({
offset: Math.floor(Math.random() * (await Inmates.count())),
});
const Review2 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Review2?.setInmate) {
await Review2.setInmate(relatedInmate2);
}
}
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 associateReviewWithPrison() {
const relatedPrison0 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Review0 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Review0?.setPrison) {
await Review0.setPrison(relatedPrison0);
}
const relatedPrison1 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Review1 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Review1?.setPrison) {
await Review1.setPrison(relatedPrison1);
}
const relatedPrison2 = await Prisons.findOne({
offset: Math.floor(Math.random() * (await Prisons.count())),
});
const Review2 = await Reviews.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Review2?.setPrison) {
await Review2.setPrison(relatedPrison2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Inmates.bulkCreate(InmatesData);
await Reviews.bulkCreate(ReviewsData);
await Prisons.bulkCreate(PrisonsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithPrison(),
// Similar logic for "relation_many"
await associateInmateWithPrison(),
await associateReviewWithInmate(),
await associateReviewWithUser(),
await associateReviewWithPrison(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('inmates', null, {});
await queryInterface.bulkDelete('reviews', null, {});
await queryInterface.bulkDelete('prisons', null, {});
},
};