32023/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-05 14:09:03 +00:00

165 lines
3.6 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Documents = db.documents;
const QrCodes = db.qr_codes;
const DocumentsData = [
{
title: 'Project Plan',
description: 'Detailed project plan for Q1',
expiration_date: new Date('2024-01-15T00:00:00Z'),
auth_code: 'AUTH123456',
// type code here for "files" field
// type code here for "relation_one" field
},
{
title: 'Financial Report',
description: 'Annual financial report for 2023',
expiration_date: new Date('2024-12-31T00:00:00Z'),
auth_code: 'AUTH654321',
// type code here for "files" field
// type code here for "relation_one" field
},
{
title: 'HR Policy',
description: 'Updated HR policies for 2024',
expiration_date: new Date('2025-01-01T00:00:00Z'),
auth_code: 'AUTH789012',
// type code here for "files" field
// type code here for "relation_one" field
},
];
const QrCodesData = [
{
qr_code_data: 'QR123456',
// type code here for "relation_one" field
},
{
qr_code_data: 'QR654321',
// type code here for "relation_one" field
},
{
qr_code_data: 'QR789012',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateDocumentWithUploaded_by() {
const relatedUploaded_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document0 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Document0?.setUploaded_by) {
await Document0.setUploaded_by(relatedUploaded_by0);
}
const relatedUploaded_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document1 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Document1?.setUploaded_by) {
await Document1.setUploaded_by(relatedUploaded_by1);
}
const relatedUploaded_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Document2 = await Documents.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Document2?.setUploaded_by) {
await Document2.setUploaded_by(relatedUploaded_by2);
}
}
async function associateQrCodeWithDocument() {
const relatedDocument0 = await Documents.findOne({
offset: Math.floor(Math.random() * (await Documents.count())),
});
const QrCode0 = await QrCodes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (QrCode0?.setDocument) {
await QrCode0.setDocument(relatedDocument0);
}
const relatedDocument1 = await Documents.findOne({
offset: Math.floor(Math.random() * (await Documents.count())),
});
const QrCode1 = await QrCodes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (QrCode1?.setDocument) {
await QrCode1.setDocument(relatedDocument1);
}
const relatedDocument2 = await Documents.findOne({
offset: Math.floor(Math.random() * (await Documents.count())),
});
const QrCode2 = await QrCodes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (QrCode2?.setDocument) {
await QrCode2.setDocument(relatedDocument2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Documents.bulkCreate(DocumentsData);
await QrCodes.bulkCreate(QrCodesData);
await Promise.all([
// Similar logic for "relation_many"
await associateDocumentWithUploaded_by(),
await associateQrCodeWithDocument(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('documents', null, {});
await queryInterface.bulkDelete('qr_codes', null, {});
},
};