Skip to content

Commit

Permalink
feat:Added the pull and push api
Browse files Browse the repository at this point in the history
  • Loading branch information
SumitKumar-17 committed Jul 7, 2024
1 parent 202afd7 commit fc1a8ff
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 24 deletions.
8 changes: 0 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = ""
}
14 changes: 8 additions & 6 deletions src/app/api/schema/route.ts → src/app/api/schemapull/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const execPromise = util.promisify(exec);


const DbUrlSchema = z.object({
dbUrl: z.string()
dbUrl: z.string(),
dbType: z.string()
});

export async function POST(req: NextRequest) {
Expand All @@ -21,18 +22,19 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
}

const { dbUrl } = parsedBody.data;
const { dbUrl,dbType } = parsedBody.data;
const schemaPath = path.join(process.cwd(), "prisma", "schema.prisma");

const schemaContent = `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
provider = "${dbType}"
url = "${dbUrl}"
}
generator client {
provider = "prisma-client-js"
}
`;

fs.writeFileSync(schemaPath, schemaContent);
Expand Down
64 changes: 64 additions & 0 deletions src/app/api/schemapush/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { exec } from "child_process";
import fs from "fs";
import path from "path";
import util from "util";

const execPromise = util.promisify(exec);


const DbUrlSchema = z.object({
dbUrl: z.string(),
dbType: z.string()
});

export async function POST(req: NextRequest) {
try {
const body = await req.json();
const parsedBody = DbUrlSchema.safeParse(body);

if (!parsedBody.success) {
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
}

const { dbUrl,dbType } = parsedBody.data;

// const schemaPath = path.join(process.cwd(), "prisma", "schema.prisma");

// const schemaContent = `
// generator client {
// provider = "prisma-client-js"
// }

// datasource db {
// provider = "${dbType}"
// url = "${dbUrl}"
// }

// `;

// fs.writeFileSync(schemaPath, schemaContent);

const { stdout, stderr } = await execPromise("npx prisma db push");
if (stderr) {
console.error(stderr);
return NextResponse.json(
{ error: "Failed to push database schema" },
{ status: 500 }
);
}
// return NextResponse.json({message:schemaPath}, { status: 200 });

return NextResponse.json({
message: "Schema updated and database pushed successfully",
output: stdout,
});
} catch (error) {
console.error(error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
71 changes: 61 additions & 10 deletions src/components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@ import { useToast } from "@/components/ui/use-toast"

const HomePage = () => {
const { toast } = useToast()


const [dbType ,setdbType]=useState<string>("");
const [dbUrl, setdbUrl] = useState<string>("");
const [uploading, setUploading] = useState(false);
const [uploadingPull, setUploadingPull] = useState(false);
const [uploadingPush, setUploadingPush] = useState(false);
const [generated, setGenerated] = useState(false);
const [message, setMessage] = useState<string>("");
const [output, setOutput] = useState<string>("");
const [studioRunning, setStudioRunning] = useState(false);

const handlePrismaPush = async () => {
setUploading(true);
const handlePrismaPull = async () => {
setUploadingPull(true);

try {
const response = await axios.post('/api/schema', {
dbUrl: dbUrl
const response = await axios.post('/api/schemapull', {
dbUrl: dbUrl,
dbType: dbType
});

console.log('POST response:', response.data);

setMessage(response.data.message);
setOutput(response.data.output);

setUploading(false);
setUploadingPull(false);
setGenerated(true);
toast({
title: "Schema Generated",
Expand All @@ -40,7 +43,41 @@ const HomePage = () => {
});
} catch (error) {
console.error('Error during POST request:', error);
setUploading(false);
setUploadingPull(false);
setGenerated(false);
toast({
title: "Error",
description: "Error during POST request",
duration: 5000,
variant: "destructive",
});
}
}

const handlePrismaPush = async () => {
setUploadingPush(true);

try {
const response = await axios.post('/api/schemapush', {
dbUrl: dbUrl,
dbType: dbType
});

console.log('POST response:', response.data);

setMessage(response.data.message);
setOutput(response.data.output);

setUploadingPush(false);
setGenerated(true);
toast({
title: "Schema Generated",
description: "Your schema is successfully pushed!",
duration: 5000
});
} catch (error) {
console.error('Error during POST request:', error);
setUploadingPush(false);
setGenerated(false);
toast({
title: "Error",
Expand Down Expand Up @@ -79,6 +116,15 @@ const HomePage = () => {
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="Database-type">Database type</Label>
<Input
onChange={(e) => {
setdbType(e.target.value);
}}
placeholder="database tpye"
/>
</div>
<div className="space-y-2">
<Label htmlFor="Database-url">Database Repository URL</Label>
<Input
Expand All @@ -88,8 +134,11 @@ const HomePage = () => {
placeholder="database url"
/>
</div>
<Button onClick={handlePrismaPull} className="w-full" type="submit">
{uploadingPull ? "Pulling ..." : "Pull"}
</Button>
<Button onClick={handlePrismaPush} className="w-full" type="submit">
{uploading ? "Pushing ..." : "Push"}
{uploadingPush ? "Pushing ..." : "Push"}
</Button>
</div>
</CardContent>
Expand All @@ -102,7 +151,8 @@ const HomePage = () => {
<CardDescription>{output}</CardDescription>
</CardHeader>
<CardContent>
<Button
<div className='flex gap-3'>
<Button
disabled={studioRunning}

onClick={handleClick}
Expand All @@ -118,6 +168,7 @@ const HomePage = () => {
>
Stop Prisma Studio
</Button>
</div>
</CardContent>

</Card>}
Expand Down

0 comments on commit fc1a8ff

Please sign in to comment.