add: specs & database status
This commit is contained in:
80
lib/deploy/database-config.ts
Normal file
80
lib/deploy/database-config.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { generateName } from "@/utils/name-generator";
|
||||
import { generatePassword } from "@/utils/password-generator";
|
||||
|
||||
export interface IDatabaseProvider {
|
||||
id: string;
|
||||
display: string;
|
||||
image: {
|
||||
alt: string;
|
||||
src: string;
|
||||
};
|
||||
versions?: IDatabaseProviderVersion[];
|
||||
}
|
||||
|
||||
export interface IDatabaseProviderVersion {
|
||||
id: string;
|
||||
display: string;
|
||||
}
|
||||
|
||||
export interface IDatabaseSpecs {
|
||||
id: string;
|
||||
cpu: number;
|
||||
memory?: number;
|
||||
storage?: number;
|
||||
maxConnections?: number;
|
||||
}
|
||||
|
||||
export interface IDatabaseConfiguration {
|
||||
workspaceId: string;
|
||||
name: string;
|
||||
provider: IDatabaseProvider;
|
||||
user: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
specs: IDatabaseSpecs;
|
||||
}
|
||||
|
||||
export function defaultDatabaseConfiguration(workspaceId: string): IDatabaseConfiguration {
|
||||
return {
|
||||
workspaceId,
|
||||
name: generateName().toLowerCase(),
|
||||
provider: databaseProviders[0],
|
||||
user: {
|
||||
username: "deployadmin",
|
||||
password: generatePassword({ length: 20, numberOfDigits: 4, numberOfSpecialCharacters: 4 }),
|
||||
},
|
||||
specs: {
|
||||
cpu: 0,
|
||||
memory: 0,
|
||||
storage: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const databaseProviders: IDatabaseProvider[] = [
|
||||
{
|
||||
id: 'vitess',
|
||||
display: 'Vitess',
|
||||
image: {
|
||||
alt: 'Vitess',
|
||||
src: '/vitess.png',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'redis',
|
||||
display: 'Redis',
|
||||
image: {
|
||||
alt: 'Redis',
|
||||
src: '/redis.svg',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'postgres',
|
||||
display: 'Postgres',
|
||||
image: {
|
||||
alt: 'Postgres',
|
||||
src: '/postgres.png',
|
||||
},
|
||||
},
|
||||
];
|
||||
@@ -1,12 +1,11 @@
|
||||
"use server";
|
||||
|
||||
import { IDatabaseConfig } from "@/app/(deploy)/[workspace]/databases/database-new-form";
|
||||
import { IDatabaseConfiguration } from "@/lib/deploy/database-config";
|
||||
import prisma from "../prisma";
|
||||
import { DatabaseProvider } from "@prisma/client";
|
||||
|
||||
export async function deployDatabase(config: IDatabaseConfig) {
|
||||
export async function deployDatabase(config: IDatabaseConfiguration) {
|
||||
|
||||
// TODO: Refactor using transactions
|
||||
const database = await prisma.database.create({
|
||||
data: {
|
||||
name: config.name,
|
||||
@@ -35,11 +34,11 @@ export async function deployDatabase(config: IDatabaseConfig) {
|
||||
username: config.user.username,
|
||||
password: config.user.password,
|
||||
},
|
||||
specs: config.specs,
|
||||
}),
|
||||
});
|
||||
|
||||
const json = await res.json();
|
||||
console.log(json)
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(json.message);
|
||||
@@ -62,4 +61,65 @@ export async function deployDatabase(config: IDatabaseConfig) {
|
||||
})
|
||||
throw new Error("Failed to deploy database");
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDatabaseDeleteConfig {
|
||||
id: string;
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export async function deleteDatabase(config: IDatabaseDeleteConfig) {
|
||||
const database = await prisma.database.findUnique({
|
||||
where: {
|
||||
id: config.id
|
||||
}
|
||||
});
|
||||
|
||||
if (!database) {
|
||||
throw new Error("Database not found");
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`http://127.0.0.1:8080/${config.workspaceId}/databases/${config.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
const json = await res.json();
|
||||
|
||||
if (!res.ok && !(res.status === 404)) {
|
||||
throw new Error(json.error);
|
||||
}
|
||||
|
||||
await prisma.database.delete({
|
||||
where: {
|
||||
id: config.id,
|
||||
}
|
||||
});
|
||||
|
||||
return json;
|
||||
} catch(err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDatabaseGetConfig {
|
||||
id: string;
|
||||
workspaceId: string;
|
||||
}
|
||||
export async function getDatabase(config: IDatabaseGetConfig) {
|
||||
try {
|
||||
const res = await fetch(`http://127.0.0.1:8080/${config.workspaceId}/databases/${config.id}`, {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
const json = await res.json();
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(json.error);
|
||||
}
|
||||
|
||||
return json;
|
||||
} catch(err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user