31215/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-03 19:48:07 +00:00

190 lines
4.2 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Configurations = db.configurations;
const Tenants = db.tenants;
const ConfigurationsData = [
{
key: 'max_connections',
value: '100',
scope: 'Global',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
key: 'api_timeout',
value: '30s',
scope: 'Global',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
key: 'feature_toggle',
value: 'enabled',
scope: 'Global',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const TenantsData = [
{
name: 'Acme Corp',
},
{
name: 'Globex Inc',
},
{
name: 'Initech',
},
];
// Similar logic for "relation_many"
async function associateUserWithTenant() {
const relatedTenant0 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setTenant) {
await User0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setTenant) {
await User1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setTenant) {
await User2.setTenant(relatedTenant2);
}
}
async function associateConfigurationWithTenant() {
const relatedTenant0 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration0 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Configuration0?.setTenant) {
await Configuration0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration1 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Configuration1?.setTenant) {
await Configuration1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration2 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Configuration2?.setTenant) {
await Configuration2.setTenant(relatedTenant2);
}
}
async function associateConfigurationWithTenant() {
const relatedTenant0 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration0 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Configuration0?.setTenant) {
await Configuration0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration1 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Configuration1?.setTenant) {
await Configuration1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenants.findOne({
offset: Math.floor(Math.random() * (await Tenants.count())),
});
const Configuration2 = await Configurations.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Configuration2?.setTenant) {
await Configuration2.setTenant(relatedTenant2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Configurations.bulkCreate(ConfigurationsData);
await Tenants.bulkCreate(TenantsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithTenant(),
await associateConfigurationWithTenant(),
await associateConfigurationWithTenant(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('configurations', null, {});
await queryInterface.bulkDelete('tenants', null, {});
},
};