2024-01-24 05:00:19 +00:00
|
|
|
import { DataSource, DeepPartial, EntityTarget, ObjectLiteral } from 'typeorm';
|
|
|
|
import * as fs from 'fs/promises';
|
|
|
|
import debug from 'debug';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { User } from '../src/entity/User';
|
|
|
|
import { Organization } from '../src/entity/Organization';
|
|
|
|
import { Project } from '../src/entity/Project';
|
|
|
|
import { UserOrganization } from '../src/entity/UserOrganization';
|
|
|
|
import { EnvironmentVariable } from '../src/entity/EnvironmentVariable';
|
|
|
|
import { Domain } from '../src/entity/Domain';
|
|
|
|
import { ProjectMember } from '../src/entity/ProjectMember';
|
|
|
|
import { Deployment } from '../src/entity/Deployment';
|
2024-01-25 14:19:09 +00:00
|
|
|
import { getConfig } from '../src/utils';
|
|
|
|
import { Config } from '../src/config';
|
|
|
|
import { DEFAULT_CONFIG_FILE_PATH } from '../src/constants';
|
2024-01-24 05:00:19 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:initialize-database');
|
|
|
|
|
|
|
|
const USER_DATA_PATH = './fixtures/users.json';
|
|
|
|
const PROJECT_DATA_PATH = './fixtures/projects.json';
|
|
|
|
const ORGANIZATION_DATA_PATH = './fixtures/organizations.json';
|
2024-02-21 04:00:14 +00:00
|
|
|
const USER_ORGANIZATION_DATA_PATH = './fixtures/user-organizations.json';
|
2024-01-24 11:28:07 +00:00
|
|
|
const PROJECT_MEMBER_DATA_PATH = './fixtures/project-members.json';
|
2024-01-31 12:25:19 +00:00
|
|
|
const PRIMARY_DOMAIN_DATA_PATH = './fixtures/primary-domains.json';
|
2024-01-24 11:28:07 +00:00
|
|
|
const DEPLOYMENT_DATA_PATH = './fixtures/deployments.json';
|
|
|
|
const ENVIRONMENT_VARIABLE_DATA_PATH = './fixtures/environment-variables.json';
|
2024-01-31 12:25:19 +00:00
|
|
|
const REDIRECTED_DOMAIN_DATA_PATH = './fixtures/redirected-domains.json';
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const loadAndSaveData = async <Entity extends ObjectLiteral>(entityType: EntityTarget<Entity>, dataSource: DataSource, filePath: string, relations?: any | undefined) => {
|
2024-01-24 05:00:19 +00:00
|
|
|
const entitiesData = await fs.readFile(filePath, 'utf-8');
|
2024-01-24 11:28:07 +00:00
|
|
|
const entities = JSON.parse(entitiesData);
|
2024-01-24 05:00:19 +00:00
|
|
|
const entityRepository = dataSource.getRepository(entityType);
|
|
|
|
|
|
|
|
const savedEntity:Entity[] = [];
|
|
|
|
|
|
|
|
for (const entityData of entities) {
|
2024-01-24 11:28:07 +00:00
|
|
|
let entity = entityRepository.create(entityData as DeepPartial<Entity>);
|
|
|
|
|
|
|
|
if (relations) {
|
|
|
|
for (const field in relations) {
|
|
|
|
const valueIndex = String(field + 'Index');
|
|
|
|
|
|
|
|
entity = {
|
|
|
|
...entity,
|
|
|
|
[field]: relations[field][entityData[valueIndex]]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 05:00:19 +00:00
|
|
|
const dbEntity = await entityRepository.save(entity);
|
|
|
|
savedEntity.push(dbEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return savedEntity;
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateTestData = async (dataSource: DataSource) => {
|
|
|
|
const savedUsers = await loadAndSaveData(User, dataSource, path.resolve(__dirname, USER_DATA_PATH));
|
|
|
|
const savedOrgs = await loadAndSaveData(Organization, dataSource, path.resolve(__dirname, ORGANIZATION_DATA_PATH));
|
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const projectRelations = {
|
|
|
|
owner: savedUsers,
|
|
|
|
organization: savedOrgs
|
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const savedProjects = await loadAndSaveData(Project, dataSource, path.resolve(__dirname, PROJECT_DATA_PATH), projectRelations);
|
|
|
|
|
2024-02-05 09:26:28 +00:00
|
|
|
const domainRepository = dataSource.getRepository(Domain);
|
|
|
|
|
2024-01-31 12:25:19 +00:00
|
|
|
const domainPrimaryRelations = {
|
2024-01-29 12:48:43 +00:00
|
|
|
project: savedProjects
|
|
|
|
};
|
|
|
|
|
2024-01-31 12:25:19 +00:00
|
|
|
const savedPrimaryDomains = await loadAndSaveData(Domain, dataSource, path.resolve(__dirname, PRIMARY_DOMAIN_DATA_PATH), domainPrimaryRelations);
|
|
|
|
|
|
|
|
const domainRedirectedRelations = {
|
|
|
|
project: savedProjects,
|
|
|
|
redirectTo: savedPrimaryDomains
|
|
|
|
};
|
|
|
|
|
|
|
|
await loadAndSaveData(Domain, dataSource, path.resolve(__dirname, REDIRECTED_DOMAIN_DATA_PATH), domainRedirectedRelations);
|
|
|
|
|
|
|
|
const savedDomains = await domainRepository.find();
|
2024-01-29 12:48:43 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const userOrganizationRelations = {
|
|
|
|
member: savedUsers,
|
|
|
|
organization: savedOrgs
|
|
|
|
};
|
|
|
|
|
|
|
|
await loadAndSaveData(UserOrganization, dataSource, path.resolve(__dirname, USER_ORGANIZATION_DATA_PATH), userOrganizationRelations);
|
|
|
|
|
|
|
|
const projectMemberRelations = {
|
|
|
|
member: savedUsers,
|
|
|
|
project: savedProjects
|
|
|
|
};
|
|
|
|
|
|
|
|
await loadAndSaveData(ProjectMember, dataSource, path.resolve(__dirname, PROJECT_MEMBER_DATA_PATH), projectMemberRelations);
|
|
|
|
|
|
|
|
const deploymentRelations = {
|
|
|
|
project: savedProjects,
|
2024-01-30 10:18:50 +00:00
|
|
|
domain: savedDomains,
|
|
|
|
createdBy: savedUsers
|
2024-01-24 11:28:07 +00:00
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await loadAndSaveData(Deployment, dataSource, path.resolve(__dirname, DEPLOYMENT_DATA_PATH), deploymentRelations);
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const environmentVariableRelations = {
|
|
|
|
project: savedProjects
|
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await loadAndSaveData(EnvironmentVariable, dataSource, path.resolve(__dirname, ENVIRONMENT_VARIABLE_DATA_PATH), environmentVariableRelations);
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkFileExists = async (filePath: string) => {
|
|
|
|
try {
|
|
|
|
await fs.access(filePath, fs.constants.F_OK);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
2024-01-24 05:00:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
2024-01-25 14:19:09 +00:00
|
|
|
const config = await getConfig<Config>(DEFAULT_CONFIG_FILE_PATH);
|
|
|
|
const isDbPresent = await checkFileExists(config.database.dbPath);
|
2024-01-24 11:28:07 +00:00
|
|
|
|
|
|
|
if (!isDbPresent) {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
type: 'better-sqlite3',
|
2024-01-25 14:19:09 +00:00
|
|
|
database: config.database.dbPath,
|
2024-01-24 11:28:07 +00:00
|
|
|
synchronize: true,
|
|
|
|
logging: true,
|
|
|
|
entities: [path.join(__dirname, '../src/entity/*')]
|
|
|
|
});
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await dataSource.initialize();
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await generateTestData(dataSource);
|
2024-01-25 12:04:13 +00:00
|
|
|
log('Data loaded successfully');
|
2024-01-25 05:47:44 +00:00
|
|
|
} else {
|
2024-01-25 12:04:13 +00:00
|
|
|
log('WARNING: Database already exists');
|
2024-01-24 11:28:07 +00:00
|
|
|
}
|
2024-01-24 05:00:19 +00:00
|
|
|
};
|
|
|
|
|
2024-01-25 12:04:13 +00:00
|
|
|
main().catch((err) => {
|
|
|
|
log(err);
|
|
|
|
});
|