30217/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-26 22:35:41 +00:00

127 lines
2.6 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const EepromFiles = db.eeprom_files;
const EepromFilesData = [
{
file_name: 'eeprom_01.bin',
// type code here for "files" field
password: '1234',
new_password: '5678',
uploaded_at: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
file_name: 'eeprom_02.bin',
// type code here for "files" field
password: 'abcd',
new_password: 'efgh',
uploaded_at: new Date('2023-10-02T11:00:00Z'),
// type code here for "relation_one" field
},
{
file_name: 'eeprom_03.bin',
// type code here for "files" field
password: '4321',
new_password: '8765',
uploaded_at: new Date('2023-10-03T12:00:00Z'),
// type code here for "relation_one" field
},
{
file_name: 'eeprom_04.bin',
// type code here for "files" field
password: 'dcba',
new_password: 'hgfe',
uploaded_at: new Date('2023-10-04T13:00:00Z'),
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateEepromFileWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EepromFile0 = await EepromFiles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (EepromFile0?.setUser) {
await EepromFile0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EepromFile1 = await EepromFiles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (EepromFile1?.setUser) {
await EepromFile1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EepromFile2 = await EepromFiles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (EepromFile2?.setUser) {
await EepromFile2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EepromFile3 = await EepromFiles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (EepromFile3?.setUser) {
await EepromFile3.setUser(relatedUser3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await EepromFiles.bulkCreate(EepromFilesData);
await Promise.all([
// Similar logic for "relation_many"
await associateEepromFileWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('eeprom_files', null, {});
},
};