add: databases WIP

This commit is contained in:
2024-05-03 15:18:40 +02:00
parent c05af7e7aa
commit 9ba19c386f
10 changed files with 207 additions and 101 deletions

16
lib/deploy/database.ts Normal file
View File

@@ -0,0 +1,16 @@
"use server";
import { IDatabaseConfig } from "@/app/(deploy)/[workspace]/databases/database-new-form";
export async function deployDatabase(config: IDatabaseConfig) {
return fetch("http://127.0.0.1:8080/databases", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(config),
}).then((res) => res.json()).catch((err) => {
console.error(err);
});
}

View File

@@ -1,73 +0,0 @@
import prisma from "@/lib/prisma";
import { Database, DatabaseProvider } from "@prisma/client";
type DatabaseType = DatabaseProvider;
export class CreateDatabase {
private workspaceId: string;
private id: string;
public name: string;
public type: DatabaseType;
public username: string;
private password: string;
private host: string | undefined;
private port: number | undefined;
constructor(workspaceId: string, name: string, type: DatabaseType) {
this.id = "new-database-uuid"; // TODO: Generate UUID
this.username = "root"; // TODO: Generate random username
this.password = "toor"; // TODO: Generate random password
this.workspaceId = workspaceId;
this.name = name;
this.type = type;
}
// Deploying the database to the cluster
public async deploy(): Promise<Database> {
throw new Error("Not implemented");
}
// Saving the newly created database to the database
protected async save(): Promise<Database> {
// Checking if the connection details are provided
if(!this.host || !this.port) throw new MissingConnectionDetails();
const database = await prisma.database.create({
data: {
id: this.id,
provider: this.type,
name: this.name,
workspaceId: this.workspaceId,
username: this.username,
password: this.password,
host: this.host,
port: this.port
}
});
return database;
}
}
class DatabaseError extends Error {
constructor(message: string) {
super(message);
}
}
export class SavingDatabaseError extends DatabaseError {
constructor() {
super("Error saving the database");
}
}
export class MissingConnectionDetails extends DatabaseError {
constructor() {
super("Missing connection details");
}
}

View File

@@ -1,18 +0,0 @@
import { Database } from "@prisma/client";
import { CreateDatabase, SavingDatabaseError } from "./database";
export class CreateVitessDatabase extends CreateDatabase {
constructor(workspaceId: string, name: string) {
super(workspaceId, name, "VITESS");
}
public async deploy(): Promise<Database> {
try {
// Saving the database to the database
const database = await this.save();
return database;
} catch (error) {
throw new SavingDatabaseError();
}
}
}