29712/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-08 04:43:43 +00:00

108 lines
2.2 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Items = db.items;
const ItemsData = [
{
title: 'Lost Wallet',
status: 'Lost',
description: 'Black leather wallet with ID and cards',
location: 'Library',
date_reported: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
category: 'Books',
},
{
title: 'Found Keys',
status: 'Lost',
description: 'Set of keys with a red keychain',
location: 'Cafeteria',
date_reported: new Date('2023-10-02T12:30:00Z'),
// type code here for "relation_one" field
category: 'Accessories',
},
{
title: 'Lost Laptop',
status: 'Found',
description: 'Silver MacBook Pro with stickers',
location: 'Lecture Hall 3',
date_reported: new Date('2023-10-03T09:15:00Z'),
// type code here for "relation_one" field
category: 'Accessories',
},
];
// Similar logic for "relation_many"
async function associateItemWithReported_by() {
const relatedReported_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Item0 = await Items.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Item0?.setReported_by) {
await Item0.setReported_by(relatedReported_by0);
}
const relatedReported_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Item1 = await Items.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Item1?.setReported_by) {
await Item1.setReported_by(relatedReported_by1);
}
const relatedReported_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Item2 = await Items.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Item2?.setReported_by) {
await Item2.setReported_by(relatedReported_by2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Items.bulkCreate(ItemsData);
await Promise.all([
// Similar logic for "relation_many"
await associateItemWithReported_by(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('items', null, {});
},
};