32700/backend/src/db/seeders/20231127130745-sample-data.js
2025-07-09 03:13:48 +00:00

111 lines
2.4 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Records = db.records;
const RecordsData = [
{
title: 'Quarterly Report Q1',
submission_date: new Date('2023-01-15T10:00:00Z'),
// type code here for "relation_one" field
status: 'pending',
},
{
title: 'Annual Budget Proposal',
submission_date: new Date('2023-02-20T14:30:00Z'),
// type code here for "relation_one" field
status: 'pending',
},
{
title: 'Project Plan Update',
submission_date: new Date('2023-03-05T09:15:00Z'),
// type code here for "relation_one" field
status: 'rejected',
},
{
title: 'Team Meeting Notes',
submission_date: new Date('2023-03-10T11:45:00Z'),
// type code here for "relation_one" field
status: 'approved',
},
];
// Similar logic for "relation_many"
async function associateRecordWithSubmitted_by() {
const relatedSubmitted_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Record0 = await Records.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Record0?.setSubmitted_by) {
await Record0.setSubmitted_by(relatedSubmitted_by0);
}
const relatedSubmitted_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Record1 = await Records.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Record1?.setSubmitted_by) {
await Record1.setSubmitted_by(relatedSubmitted_by1);
}
const relatedSubmitted_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Record2 = await Records.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Record2?.setSubmitted_by) {
await Record2.setSubmitted_by(relatedSubmitted_by2);
}
const relatedSubmitted_by3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Record3 = await Records.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Record3?.setSubmitted_by) {
await Record3.setSubmitted_by(relatedSubmitted_by3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Records.bulkCreate(RecordsData);
await Promise.all([
// Similar logic for "relation_many"
await associateRecordWithSubmitted_by(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('records', null, {});
},
};