const db = require('../models'); const Users = db.users; const AccessLogs = db.access_logs; const QrCodes = db.qr_codes; const Videos = db.videos; const AccessLogsData = [ { // type code here for "relation_one" field // type code here for "relation_one" field access_time: new Date('2023-10-01T10:05:00Z'), }, { // type code here for "relation_one" field // type code here for "relation_one" field access_time: new Date('2023-10-02T10:15:00Z'), }, { // type code here for "relation_one" field // type code here for "relation_one" field access_time: new Date('2023-10-03T10:25:00Z'), }, { // type code here for "relation_one" field // type code here for "relation_one" field access_time: new Date('2023-10-04T10:35:00Z'), }, ]; const QrCodesData = [ { code: 'QR123456', // type code here for "relation_one" field valid_from: new Date('2023-10-01T10:00:00Z'), valid_until: new Date('2023-10-01T12:00:00Z'), }, { code: 'QR654321', // type code here for "relation_one" field valid_from: new Date('2023-10-02T10:00:00Z'), valid_until: new Date('2023-10-02T12:00:00Z'), }, { code: 'QR112233', // type code here for "relation_one" field valid_from: new Date('2023-10-03T10:00:00Z'), valid_until: new Date('2023-10-03T12:00:00Z'), }, { code: 'QR445566', // type code here for "relation_one" field valid_from: new Date('2023-10-04T10:00:00Z'), valid_until: new Date('2023-10-04T12:00:00Z'), }, ]; const VideosData = [ { title: 'Introduction to QR Codes', url: 'https://www.example.com/video1', status: 'active', }, { title: 'Advanced QR Code Techniques', url: 'https://www.example.com/video2', status: 'active', }, { title: 'QR Code Security', url: 'https://www.example.com/video3', status: 'active', }, { title: 'QR Code Applications', url: 'https://www.example.com/video4', status: 'inactive', }, ]; // Similar logic for "relation_many" async function associateAccessLogWithUser() { const relatedUser0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const AccessLog0 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 0, }); if (AccessLog0?.setUser) { await AccessLog0.setUser(relatedUser0); } const relatedUser1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const AccessLog1 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 1, }); if (AccessLog1?.setUser) { await AccessLog1.setUser(relatedUser1); } const relatedUser2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const AccessLog2 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 2, }); if (AccessLog2?.setUser) { await AccessLog2.setUser(relatedUser2); } const relatedUser3 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const AccessLog3 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 3, }); if (AccessLog3?.setUser) { await AccessLog3.setUser(relatedUser3); } } async function associateAccessLogWithQr_code() { const relatedQr_code0 = await QrCodes.findOne({ offset: Math.floor(Math.random() * (await QrCodes.count())), }); const AccessLog0 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 0, }); if (AccessLog0?.setQr_code) { await AccessLog0.setQr_code(relatedQr_code0); } const relatedQr_code1 = await QrCodes.findOne({ offset: Math.floor(Math.random() * (await QrCodes.count())), }); const AccessLog1 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 1, }); if (AccessLog1?.setQr_code) { await AccessLog1.setQr_code(relatedQr_code1); } const relatedQr_code2 = await QrCodes.findOne({ offset: Math.floor(Math.random() * (await QrCodes.count())), }); const AccessLog2 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 2, }); if (AccessLog2?.setQr_code) { await AccessLog2.setQr_code(relatedQr_code2); } const relatedQr_code3 = await QrCodes.findOne({ offset: Math.floor(Math.random() * (await QrCodes.count())), }); const AccessLog3 = await AccessLogs.findOne({ order: [['id', 'ASC']], offset: 3, }); if (AccessLog3?.setQr_code) { await AccessLog3.setQr_code(relatedQr_code3); } } async function associateQrCodeWithVideo() { const relatedVideo0 = await Videos.findOne({ offset: Math.floor(Math.random() * (await Videos.count())), }); const QrCode0 = await QrCodes.findOne({ order: [['id', 'ASC']], offset: 0, }); if (QrCode0?.setVideo) { await QrCode0.setVideo(relatedVideo0); } const relatedVideo1 = await Videos.findOne({ offset: Math.floor(Math.random() * (await Videos.count())), }); const QrCode1 = await QrCodes.findOne({ order: [['id', 'ASC']], offset: 1, }); if (QrCode1?.setVideo) { await QrCode1.setVideo(relatedVideo1); } const relatedVideo2 = await Videos.findOne({ offset: Math.floor(Math.random() * (await Videos.count())), }); const QrCode2 = await QrCodes.findOne({ order: [['id', 'ASC']], offset: 2, }); if (QrCode2?.setVideo) { await QrCode2.setVideo(relatedVideo2); } const relatedVideo3 = await Videos.findOne({ offset: Math.floor(Math.random() * (await Videos.count())), }); const QrCode3 = await QrCodes.findOne({ order: [['id', 'ASC']], offset: 3, }); if (QrCode3?.setVideo) { await QrCode3.setVideo(relatedVideo3); } } module.exports = { up: async (queryInterface, Sequelize) => { await AccessLogs.bulkCreate(AccessLogsData); await QrCodes.bulkCreate(QrCodesData); await Videos.bulkCreate(VideosData); await Promise.all([ // Similar logic for "relation_many" await associateAccessLogWithUser(), await associateAccessLogWithQr_code(), await associateQrCodeWithVideo(), ]); }, down: async (queryInterface, Sequelize) => { await queryInterface.bulkDelete('access_logs', null, {}); await queryInterface.bulkDelete('qr_codes', null, {}); await queryInterface.bulkDelete('videos', null, {}); }, };