add: wip databases
This commit is contained in:
@@ -1,16 +1,65 @@
|
||||
"use server";
|
||||
|
||||
import { IDatabaseConfig } from "@/app/(deploy)/[workspace]/databases/database-new-form";
|
||||
import prisma from "../prisma";
|
||||
import { DatabaseProvider } from "@prisma/client";
|
||||
|
||||
export async function deployDatabase(config: IDatabaseConfig) {
|
||||
|
||||
return fetch("http://127.0.0.1:8080/databases", {
|
||||
// TODO: Refactor using transactions
|
||||
const database = await prisma.database.create({
|
||||
data: {
|
||||
name: config.name,
|
||||
provider: DatabaseProvider.REDIS,
|
||||
password: config.user.password,
|
||||
username: config.user.username,
|
||||
workspaceId: config.workspaceId,
|
||||
host: "",
|
||||
port: 0,
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await 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);
|
||||
});
|
||||
body: JSON.stringify({
|
||||
workspaceId: config.workspaceId,
|
||||
id: database.id,
|
||||
provider: {
|
||||
id: config.provider.id,
|
||||
},
|
||||
user: {
|
||||
username: config.user.username,
|
||||
password: config.user.password,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const json = await res.json();
|
||||
console.log(json)
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(json.message);
|
||||
}
|
||||
|
||||
await prisma.database.update({
|
||||
where: {
|
||||
id: database.id,
|
||||
},
|
||||
data: {
|
||||
host: "toset",
|
||||
port: json.port,
|
||||
}
|
||||
});
|
||||
} catch(err) {
|
||||
await prisma.database.delete({
|
||||
where: {
|
||||
id: database.id,
|
||||
}
|
||||
})
|
||||
throw new Error("Failed to deploy database");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import NextAuth, { NextAuthOptions, TokenSet, User } from "next-auth"
|
||||
import Github from "next-auth/providers/github"
|
||||
import prisma from "./prisma"
|
||||
import { Adapter } from "next-auth/adapters"
|
||||
import { createWorkspace } from "./deploy/workspace"
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
@@ -95,7 +96,7 @@ export const authOptions: NextAuthOptions = {
|
||||
},
|
||||
events: {
|
||||
createUser: async ({ user }) => {
|
||||
await prisma.workspace.create({
|
||||
const worksapce = await prisma.workspace.create({
|
||||
data: {
|
||||
name: user.username + "'s Personal",
|
||||
ownerId: user.id,
|
||||
|
||||
Reference in New Issue
Block a user