add: databases WIP
This commit is contained in:
73
lib/deploy/databases/database.ts
Normal file
73
lib/deploy/databases/database.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
0
lib/deploy/databases/index.ts
Normal file
0
lib/deploy/databases/index.ts
Normal file
18
lib/deploy/databases/vitess-database.ts
Normal file
18
lib/deploy/databases/vitess-database.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user