2024-01-16 08:10:14 +00:00
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
import path from 'path';
|
|
|
|
import debug from 'debug';
|
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
import { DatabaseConfig } from './config';
|
2024-01-17 11:29:59 +00:00
|
|
|
import { User } from './entity/User';
|
|
|
|
import { Organization } from './entity/Organization';
|
|
|
|
import { UserOrganization } from './entity/UserOrganization';
|
|
|
|
import { Project } from './entity/Project';
|
2024-01-18 08:36:49 +00:00
|
|
|
import { Deployment } from './entity/Deployment';
|
|
|
|
import { ProjectMember } from './entity/ProjectMember';
|
|
|
|
import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
2024-01-16 08:10:14 +00:00
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
const log = debug('snowball:database');
|
|
|
|
|
|
|
|
export class Database {
|
|
|
|
private dataSource: DataSource;
|
|
|
|
|
|
|
|
constructor ({ dbPath }: DatabaseConfig) {
|
|
|
|
this.dataSource = new DataSource({
|
2024-01-16 08:10:14 +00:00
|
|
|
type: 'better-sqlite3',
|
2024-01-17 05:23:01 +00:00
|
|
|
database: dbPath,
|
2024-01-16 08:10:14 +00:00
|
|
|
entities: [path.join(__dirname, '/entity/*')],
|
|
|
|
synchronize: true,
|
|
|
|
logging: false
|
|
|
|
});
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 08:10:14 +00:00
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
async init () : Promise<void> {
|
|
|
|
await this.dataSource.initialize();
|
2024-01-16 08:10:14 +00:00
|
|
|
log('database initialized');
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
|
|
|
|
async getUser (userId: number) : Promise<User | null> {
|
|
|
|
const userRepository = this.dataSource.getRepository(User);
|
|
|
|
const user = await userRepository.findOneBy({
|
|
|
|
id: userId
|
|
|
|
});
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
2024-01-17 11:29:59 +00:00
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
async getOrganizationsByUserId (userId: number) : Promise<Organization[]> {
|
2024-01-17 11:29:59 +00:00
|
|
|
const userOrganizationRepository = this.dataSource.getRepository(UserOrganization);
|
|
|
|
|
|
|
|
const userOrgs = await userOrganizationRepository.find({
|
|
|
|
relations: {
|
2024-01-18 08:36:49 +00:00
|
|
|
member: true,
|
2024-01-17 11:29:59 +00:00
|
|
|
organization: true
|
|
|
|
},
|
|
|
|
where: {
|
2024-01-18 08:36:49 +00:00
|
|
|
member: {
|
2024-01-17 11:29:59 +00:00
|
|
|
id: userId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const organizations = userOrgs.map(userOrg => userOrg.organization);
|
|
|
|
return organizations;
|
|
|
|
}
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
async getProjectsByOrganizationId (organizationId: number): Promise<Project[]> {
|
2024-01-17 11:29:59 +00:00
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
|
|
|
|
const projects = await projectRepository.find({
|
|
|
|
relations: {
|
|
|
|
organization: true,
|
|
|
|
owner: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
organization: {
|
|
|
|
id: organizationId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return projects;
|
|
|
|
}
|
2024-01-18 08:36:49 +00:00
|
|
|
|
|
|
|
async getDeploymentsByProjectId (projectId: string): Promise<Deployment[]> {
|
|
|
|
const deploymentRepository = this.dataSource.getRepository(Deployment);
|
|
|
|
|
|
|
|
const deployments = await deploymentRepository.find({
|
|
|
|
relations: {
|
|
|
|
project: true,
|
|
|
|
domain: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return deployments;
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
async getProjectMembersByProjectId (projectId: string): Promise<ProjectMember[]> {
|
2024-01-18 08:36:49 +00:00
|
|
|
const projectMemberRepository = this.dataSource.getRepository(ProjectMember);
|
|
|
|
|
|
|
|
const projectMembers = await projectMemberRepository.find({
|
|
|
|
relations: {
|
|
|
|
project: true,
|
|
|
|
member: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectMembers;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEnvironmentVariablesByProjectId (projectId: string): Promise<EnvironmentVariable[]> {
|
|
|
|
const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable);
|
|
|
|
|
|
|
|
const environmentVariables = await environmentVariableRepository.find({
|
|
|
|
relations: {
|
|
|
|
project: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return environmentVariables;
|
|
|
|
}
|
2024-01-22 09:42:08 +00:00
|
|
|
|
|
|
|
async removeProjectMemberByMemberId (memberId: string): Promise<boolean> {
|
|
|
|
// TODO: Check if user is authorized to delete members
|
|
|
|
const projectMemberRepository = this.dataSource.getRepository(ProjectMember);
|
|
|
|
const deleted = await projectMemberRepository.delete(memberId);
|
|
|
|
|
|
|
|
if (deleted.affected) {
|
|
|
|
return deleted.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|