2024-01-25 05:47:44 +00:00
|
|
|
import { DataSource, DeepPartial } from 'typeorm';
|
2024-01-16 08:10:14 +00:00
|
|
|
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-25 12:04:13 +00:00
|
|
|
import { Deployment, Environment } from './entity/Deployment';
|
2024-01-18 08:36:49 +00:00
|
|
|
import { ProjectMember } from './entity/ProjectMember';
|
|
|
|
import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
2024-01-29 12:48:43 +00:00
|
|
|
import { Domain } from './entity/Domain';
|
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-30 10:18:50 +00:00
|
|
|
async init (): Promise<void> {
|
2024-01-17 05:23:01 +00:00
|
|
|
await this.dataSource.initialize();
|
2024-01-16 08:10:14 +00:00
|
|
|
log('database initialized');
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
|
2024-01-30 10:18:50 +00:00
|
|
|
async getUser (userId: number): Promise<User | null> {
|
2024-01-17 05:23:01 +00:00
|
|
|
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-30 10:18:50 +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
|
|
|
|
2024-01-29 11:01:03 +00:00
|
|
|
async getProjectById (projectId: string): Promise<Project | null> {
|
2024-01-25 06:28:38 +00:00
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
|
2024-01-29 11:01:03 +00:00
|
|
|
const project = await projectRepository
|
|
|
|
.createQueryBuilder('project')
|
|
|
|
.leftJoinAndSelect('project.deployments', 'deployments', 'deployments.isCurrent = true')
|
|
|
|
.leftJoinAndSelect('deployments.domain', 'domain')
|
|
|
|
.leftJoinAndSelect('project.owner', 'owner')
|
|
|
|
.where('project.id = :projectId', {
|
|
|
|
projectId
|
|
|
|
})
|
|
|
|
.getOne();
|
2024-01-25 06:28:38 +00:00
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
2024-01-29 11:01:03 +00:00
|
|
|
async getProjectsInOrganization (userId: string, organizationId: string): Promise<Project[]> {
|
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
|
|
|
|
const projects = await projectRepository
|
|
|
|
.createQueryBuilder('project')
|
|
|
|
.leftJoinAndSelect('project.deployments', 'deployments', 'deployments.isCurrent = true')
|
|
|
|
.leftJoinAndSelect('deployments.domain', 'domain')
|
|
|
|
.leftJoin('project.projectMembers', 'projectMembers')
|
|
|
|
.where('(project.ownerId = :userId OR projectMembers.userId = :userId) AND project.organizationId = :organizationId', {
|
|
|
|
userId,
|
|
|
|
organizationId
|
|
|
|
})
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
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,
|
2024-01-30 10:18:50 +00:00
|
|
|
domain: true,
|
|
|
|
createdBy: true
|
2024-01-18 08:36:49 +00:00
|
|
|
},
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
2024-01-30 10:18:50 +00:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
createdAt: 'DESC'
|
2024-01-18 08:36:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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({
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return environmentVariables;
|
|
|
|
}
|
2024-01-22 09:42:08 +00:00
|
|
|
|
2024-01-25 05:47:44 +00:00
|
|
|
async removeProjectMemberById (memberId: string): Promise<boolean> {
|
2024-01-22 09:42:08 +00:00
|
|
|
const projectMemberRepository = this.dataSource.getRepository(ProjectMember);
|
2024-01-23 09:31:33 +00:00
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
const deleted = await projectMemberRepository.delete(memberId);
|
|
|
|
|
|
|
|
if (deleted.affected) {
|
|
|
|
return deleted.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 09:31:33 +00:00
|
|
|
|
2024-01-30 13:47:55 +00:00
|
|
|
async addEnvironmentVariablesByProjectId (projectId: string, environmentVariables: {
|
|
|
|
environments: string[];
|
|
|
|
key: string;
|
|
|
|
value: string;
|
|
|
|
}[]): Promise<boolean> {
|
2024-01-23 09:31:33 +00:00
|
|
|
const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable);
|
|
|
|
|
2024-01-30 13:47:55 +00:00
|
|
|
const formattedEnvironmentVariables = environmentVariables.map((environmentVariable) => {
|
|
|
|
return environmentVariable.environments.map((environment) => {
|
|
|
|
return ({
|
|
|
|
key: environmentVariable.key,
|
|
|
|
value: environmentVariable.value,
|
|
|
|
environment: environment as Environment,
|
|
|
|
project: Object.assign(new Project(), {
|
|
|
|
id: projectId
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).flat();
|
|
|
|
|
|
|
|
const savedEnvironmentVariables = await environmentVariableRepository.save(formattedEnvironmentVariables);
|
2024-01-23 09:31:33 +00:00
|
|
|
return savedEnvironmentVariables.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProjectMemberByMemberId (memberId: string): Promise<ProjectMember> {
|
|
|
|
const projectMemberRepository = this.dataSource.getRepository(ProjectMember);
|
|
|
|
|
|
|
|
const projectMemberWithProject = await projectMemberRepository.find({
|
|
|
|
relations: {
|
|
|
|
project: {
|
|
|
|
owner: true
|
|
|
|
},
|
|
|
|
member: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: Number(memberId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (projectMemberWithProject.length === 0) {
|
|
|
|
throw new Error('Member does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectMemberWithProject[0];
|
|
|
|
}
|
2024-01-24 14:47:43 +00:00
|
|
|
|
|
|
|
async getProjectsBySearchText (userId: number, searchText: string): Promise<Project[]> {
|
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
|
|
|
|
const projects = await projectRepository
|
|
|
|
.createQueryBuilder('project')
|
|
|
|
.leftJoinAndSelect('project.organization', 'organization')
|
|
|
|
.leftJoin('project.projectMembers', 'projectMembers')
|
|
|
|
.where('(project.owner = :userId OR projectMembers.member.id = :userId) AND project.name LIKE :searchText', {
|
|
|
|
userId,
|
|
|
|
searchText: `%${searchText}%`
|
|
|
|
})
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
return projects;
|
|
|
|
}
|
2024-01-25 05:47:44 +00:00
|
|
|
|
|
|
|
async updateDeploymentById (deploymentId: string, updates: DeepPartial<Deployment>): Promise<boolean> {
|
|
|
|
const deploymentRepository = this.dataSource.getRepository(Deployment);
|
2024-01-25 06:28:38 +00:00
|
|
|
const updateResult = await deploymentRepository.update({ id: Number(deploymentId) }, updates);
|
|
|
|
|
|
|
|
if (updateResult.affected) {
|
|
|
|
return updateResult.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateProjectById (projectId: string, updates: DeepPartial<Project>): Promise<boolean> {
|
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
const updateResult = await projectRepository.update({ id: projectId }, updates);
|
2024-01-25 05:47:44 +00:00
|
|
|
|
2024-01-25 06:28:38 +00:00
|
|
|
if (updateResult.affected) {
|
|
|
|
return updateResult.affected > 0;
|
2024-01-25 05:47:44 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 12:04:13 +00:00
|
|
|
|
2024-01-30 10:18:50 +00:00
|
|
|
async redeployToProdById (userId: string, deploymentId: string): Promise<Deployment> {
|
2024-01-25 12:04:13 +00:00
|
|
|
const deploymentRepository = this.dataSource.getRepository(Deployment);
|
|
|
|
const deployment = await deploymentRepository.findOne({
|
|
|
|
relations: {
|
|
|
|
project: true,
|
2024-01-30 10:18:50 +00:00
|
|
|
domain: true,
|
|
|
|
createdBy: true
|
2024-01-25 12:04:13 +00:00
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: Number(deploymentId)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (deployment === null) {
|
|
|
|
throw new Error('Deployment not found');
|
|
|
|
}
|
|
|
|
const { id, createdAt, updatedAt, ...updatedDeployment } = deployment;
|
|
|
|
|
|
|
|
if (updatedDeployment.environment === Environment.Production) {
|
|
|
|
// TODO: Put isCurrent field in project
|
|
|
|
updatedDeployment.isCurrent = true;
|
2024-01-30 10:18:50 +00:00
|
|
|
updatedDeployment.createdBy = Object.assign(new User(), {
|
|
|
|
id: Number(userId)
|
|
|
|
});
|
2024-01-25 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await deploymentRepository.update({ id: Number(deploymentId) }, { domain: null, isCurrent: false });
|
|
|
|
|
2024-01-29 12:48:43 +00:00
|
|
|
return deploymentRepository.save(updatedDeployment);
|
2024-01-25 12:04:13 +00:00
|
|
|
}
|
2024-01-29 11:01:03 +00:00
|
|
|
|
|
|
|
async deleteProjectById (projectId: string): Promise<boolean> {
|
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
const deleteResult = await projectRepository.softDelete({ id: projectId });
|
|
|
|
|
|
|
|
if (deleteResult.affected) {
|
|
|
|
return deleteResult.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 12:09:51 +00:00
|
|
|
|
|
|
|
async rollbackDeploymentById (projectId: string, deploymentId: string): Promise<boolean> {
|
|
|
|
const deploymentRepository = this.dataSource.getRepository(Deployment);
|
|
|
|
|
|
|
|
// TODO: Implement transactions
|
2024-01-30 09:10:04 +00:00
|
|
|
const oldCurrentDeployment = await deploymentRepository.findOne({
|
|
|
|
relations: {
|
|
|
|
domain: true
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
},
|
|
|
|
isCurrent: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const oldCurrentDeploymentUpdate = await deploymentRepository.update({ project: { id: projectId }, isCurrent: true }, { isCurrent: false, domain: null });
|
|
|
|
|
|
|
|
const newCurrentDeploymentUpdate = await deploymentRepository.update({ id: Number(deploymentId) }, { isCurrent: true, domain: oldCurrentDeployment?.domain });
|
2024-01-29 12:09:51 +00:00
|
|
|
|
|
|
|
if (oldCurrentDeploymentUpdate.affected && newCurrentDeploymentUpdate.affected) {
|
|
|
|
return oldCurrentDeploymentUpdate.affected > 0 && newCurrentDeploymentUpdate.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 12:48:43 +00:00
|
|
|
|
|
|
|
async addDomainByProjectId (projectId: string, domainDetails: { name: string }): Promise<Domain[]> {
|
|
|
|
const domainRepository = this.dataSource.getRepository(Domain);
|
|
|
|
const projectRepository = this.dataSource.getRepository(Project);
|
|
|
|
|
|
|
|
const currentProject = await projectRepository.findOneBy({
|
|
|
|
id: projectId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (currentProject === null) {
|
|
|
|
throw new Error(`Project with ${projectId} not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryDomainDetails = {
|
|
|
|
...domainDetails,
|
|
|
|
isRedirected: false,
|
|
|
|
branch: currentProject.prodBranch,
|
|
|
|
project: currentProject
|
|
|
|
};
|
|
|
|
|
|
|
|
const primaryDomain = domainRepository.create(primaryDomainDetails as DeepPartial<Domain>);
|
|
|
|
|
|
|
|
const domainArr = domainDetails.name.split('www.');
|
|
|
|
|
|
|
|
const redirectedDomainDetails = {
|
|
|
|
name: domainArr.length > 1 ? domainArr[1] : `www.${domainArr[0]}`,
|
|
|
|
isRedirected: true,
|
|
|
|
branch: currentProject.prodBranch,
|
|
|
|
project: currentProject
|
|
|
|
};
|
|
|
|
|
|
|
|
const savedPrimaryDomain = await domainRepository.save(primaryDomain);
|
|
|
|
|
|
|
|
const redirectedDomain = domainRepository.create(redirectedDomainDetails as DeepPartial<Domain>);
|
|
|
|
const savedRedirectedDomain = await domainRepository.save(redirectedDomain);
|
|
|
|
|
|
|
|
return [savedPrimaryDomain, savedRedirectedDomain];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDomainsByProjectId (projectId: string): Promise<Domain[]> {
|
|
|
|
const domainRepository = this.dataSource.getRepository(Domain);
|
|
|
|
|
|
|
|
const domains = await domainRepository.find({
|
|
|
|
where: {
|
|
|
|
project: {
|
|
|
|
id: projectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return domains;
|
|
|
|
}
|
2024-01-30 08:31:09 +00:00
|
|
|
|
|
|
|
async updateDomainById (domainId: string, updates: DeepPartial<Domain>): Promise<boolean> {
|
|
|
|
const domainRepository = this.dataSource.getRepository(Domain);
|
|
|
|
|
|
|
|
const updateResult = await domainRepository.update({ id: Number(domainId) }, updates);
|
|
|
|
|
|
|
|
if (updateResult.affected) {
|
|
|
|
return updateResult.affected > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|