199 lines
4.3 KiB
JavaScript
199 lines
4.3 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Devices = db.devices;
|
|
|
|
const Translations = db.translations;
|
|
|
|
const DevicesData = [
|
|
{
|
|
device_name: 'Brilliant Glasses 1',
|
|
|
|
device_id: 'BG12345',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
device_name: 'Brilliant Glasses 2',
|
|
|
|
device_id: 'BG67890',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
device_name: 'Brilliant Glasses 3',
|
|
|
|
device_id: 'BG11223',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
device_name: 'Brilliant Glasses 4',
|
|
|
|
device_id: 'BG44556',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
const TranslationsData = [
|
|
{
|
|
source_language: 'English',
|
|
|
|
target_language: 'Spanish',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
translation_time: new Date('2023-10-01T10:00:00Z'),
|
|
},
|
|
|
|
{
|
|
source_language: 'French',
|
|
|
|
target_language: 'German',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
translation_time: new Date('2023-10-02T11:00:00Z'),
|
|
},
|
|
|
|
{
|
|
source_language: 'Chinese',
|
|
|
|
target_language: 'Japanese',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
translation_time: new Date('2023-10-03T12:00:00Z'),
|
|
},
|
|
|
|
{
|
|
source_language: 'Italian',
|
|
|
|
target_language: 'Portuguese',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
translation_time: new Date('2023-10-04T13:00:00Z'),
|
|
},
|
|
];
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associateDeviceWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Device0 = await Devices.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Device0?.setUser) {
|
|
await Device0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Device1 = await Devices.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Device1?.setUser) {
|
|
await Device1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Device2 = await Devices.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Device2?.setUser) {
|
|
await Device2.setUser(relatedUser2);
|
|
}
|
|
|
|
const relatedUser3 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Device3 = await Devices.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Device3?.setUser) {
|
|
await Device3.setUser(relatedUser3);
|
|
}
|
|
}
|
|
|
|
async function associateTranslationWithDevice() {
|
|
const relatedDevice0 = await Devices.findOne({
|
|
offset: Math.floor(Math.random() * (await Devices.count())),
|
|
});
|
|
const Translation0 = await Translations.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Translation0?.setDevice) {
|
|
await Translation0.setDevice(relatedDevice0);
|
|
}
|
|
|
|
const relatedDevice1 = await Devices.findOne({
|
|
offset: Math.floor(Math.random() * (await Devices.count())),
|
|
});
|
|
const Translation1 = await Translations.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Translation1?.setDevice) {
|
|
await Translation1.setDevice(relatedDevice1);
|
|
}
|
|
|
|
const relatedDevice2 = await Devices.findOne({
|
|
offset: Math.floor(Math.random() * (await Devices.count())),
|
|
});
|
|
const Translation2 = await Translations.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Translation2?.setDevice) {
|
|
await Translation2.setDevice(relatedDevice2);
|
|
}
|
|
|
|
const relatedDevice3 = await Devices.findOne({
|
|
offset: Math.floor(Math.random() * (await Devices.count())),
|
|
});
|
|
const Translation3 = await Translations.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Translation3?.setDevice) {
|
|
await Translation3.setDevice(relatedDevice3);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Devices.bulkCreate(DevicesData);
|
|
|
|
await Translations.bulkCreate(TranslationsData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
await associateDeviceWithUser(),
|
|
|
|
await associateTranslationWithDevice(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('devices', null, {});
|
|
|
|
await queryInterface.bulkDelete('translations', null, {});
|
|
},
|
|
};
|