-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31f4224
commit 2487374
Showing
44 changed files
with
1,010 additions
and
1,136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
"use server"; | ||
'use server' | ||
|
||
import { Redis } from "@upstash/redis"; | ||
import { revalidatePath } from "next/cache"; | ||
import { cookies } from "next/headers"; | ||
import { Redis } from '@upstash/redis' | ||
import { revalidatePath } from 'next/cache' | ||
import { cookies } from 'next/headers' | ||
|
||
export const saveGeneration = async (data: { | ||
sqlSchema: string; | ||
cmdCode: string; | ||
}) => { | ||
const { cmdCode, sqlSchema } = data; | ||
const redis = Redis.fromEnv(); | ||
const res = await redis.set(cmdCode, sqlSchema); | ||
return res; | ||
}; | ||
export const saveGeneration = async (data: { sqlSchema: string; cmdCode: string }) => { | ||
const { cmdCode, sqlSchema } = data | ||
const redis = Redis.fromEnv() | ||
const res = await redis.set(cmdCode, sqlSchema) | ||
return res | ||
} | ||
|
||
export const setApiKey = (prevState: any, formData: FormData) => { | ||
const apiKey = formData.get("key") as string; | ||
cookies().set("api-key", apiKey, { | ||
secure: true, | ||
}); | ||
revalidatePath("/"); | ||
return { msg: "Key Saved Successfully" }; | ||
}; | ||
const apiKey = formData.get('key') as string | ||
cookies().set('api-key', apiKey, { | ||
secure: true | ||
}) | ||
revalidatePath('/') | ||
return { msg: 'Key Saved Successfully' } | ||
} | ||
|
||
export const getApiKey = async () => { | ||
return cookies().get("api-key")?.value; | ||
}; | ||
return cookies().get('api-key')?.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import { NextResponse } from "next/server"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import postgres from "postgres"; | ||
import { sql } from "drizzle-orm"; | ||
import { NextResponse } from 'next/server' | ||
import { drizzle } from 'drizzle-orm/postgres-js' | ||
import postgres from 'postgres' | ||
import { sql } from 'drizzle-orm' | ||
|
||
type ResponseJson = { | ||
url: string; | ||
}; | ||
url: string | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const { url } = (await req.json()) as ResponseJson; | ||
const { url } = (await req.json()) as ResponseJson | ||
|
||
if (url === "") { | ||
if (url === '') { | ||
return NextResponse.json( | ||
{ | ||
error: | ||
"We couldn't find a connection URL. Please try again with the correct connection URL.", | ||
"We couldn't find a connection URL. Please try again with the correct connection URL." | ||
}, | ||
{ status: 400 } | ||
); | ||
) | ||
} | ||
// Disable prefetch as it is not supported for "Transaction" pool mode | ||
const client = postgres(url, { prepare: false }); | ||
const db = drizzle(client); | ||
const client = postgres(url, { prepare: false }) | ||
const db = drizzle(client) | ||
|
||
// Check if connection is successful | ||
try { | ||
await db.execute(sql`SELECT NOW()`); | ||
await db.execute(sql`SELECT NOW()`) | ||
} catch (error) { | ||
// @ts-ignore | ||
let message = error.code; | ||
if (message === "SASL_SIGNATURE_MISMATCH") { | ||
message = "Database password is missing."; | ||
} else if (message === "ENOTFOUND") { | ||
let message = error.code | ||
if (message === 'SASL_SIGNATURE_MISMATCH') { | ||
message = 'Database password is missing.' | ||
} else if (message === 'ENOTFOUND') { | ||
message = | ||
"Your connection URL is invalid. Please double-check it and make the necessary corrections."; | ||
'Your connection URL is invalid. Please double-check it and make the necessary corrections.' | ||
} | ||
return NextResponse.json({ error: message }, { status: 500 }); | ||
return NextResponse.json({ error: message }, { status: 500 }) | ||
} | ||
|
||
return NextResponse.json({ message: "Connection stablished succesfully" }); | ||
return NextResponse.json({ message: 'Connection stablished succesfully' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
import { NextResponse } from "next/server"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import postgres from "postgres"; | ||
import { sql } from "drizzle-orm"; | ||
import { NextResponse } from 'next/server' | ||
import { drizzle } from 'drizzle-orm/postgres-js' | ||
import postgres from 'postgres' | ||
import { sql } from 'drizzle-orm' | ||
|
||
type ResponseJson = { | ||
url: string; | ||
sqlSchema: string; | ||
}; | ||
url: string | ||
sqlSchema: string | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const { url, sqlSchema } = (await req.json()) as ResponseJson; | ||
const { url, sqlSchema } = (await req.json()) as ResponseJson | ||
|
||
if (url === "" || !sqlSchema) { | ||
if (url === '' || !sqlSchema) { | ||
return NextResponse.json( | ||
{ | ||
error: | ||
"We couldn't find a connection URL or a SQL Schema. Please try again with the correct information.", | ||
"We couldn't find a connection URL or a SQL Schema. Please try again with the correct information." | ||
}, | ||
{ status: 400 } | ||
); | ||
) | ||
} | ||
// Disable prefetch as it is not supported for "Transaction" pool mode | ||
const client = postgres(url, { prepare: false }); | ||
const db = drizzle(client); | ||
const client = postgres(url, { prepare: false }) | ||
const db = drizzle(client) | ||
// Check if connection is successful | ||
try { | ||
await db.execute(sql`SELECT NOW()`); | ||
await db.execute(sql`SELECT NOW()`) | ||
} catch (error) { | ||
// @ts-ignore | ||
let message = error.code; | ||
if (message === "SASL_SIGNATURE_MISMATCH") { | ||
message = "Database password is missing."; | ||
} else if (message === "ENOTFOUND") { | ||
let message = error.code | ||
if (message === 'SASL_SIGNATURE_MISMATCH') { | ||
message = 'Database password is missing.' | ||
} else if (message === 'ENOTFOUND') { | ||
message = | ||
"Your connection URL is invalid. Please double-check it and make the necessary corrections."; | ||
'Your connection URL is invalid. Please double-check it and make the necessary corrections.' | ||
} | ||
return NextResponse.json({ error: message }, { status: 500 }); | ||
return NextResponse.json({ error: message }, { status: 500 }) | ||
} | ||
|
||
// Execute the migration | ||
await db.execute(sql.raw(sqlSchema)); | ||
await db.execute(sql.raw(sqlSchema)) | ||
|
||
return NextResponse.json({ | ||
message: "Database Schema deployed successfully", | ||
}); | ||
message: 'Database Schema deployed successfully' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { Redis } from "@upstash/redis"; | ||
import { NextResponse } from "next/server"; | ||
import { Redis } from '@upstash/redis' | ||
import { NextResponse } from 'next/server' | ||
|
||
export const runtime = "edge"; | ||
export const runtime = 'edge' | ||
|
||
const redis = Redis.fromEnv(); | ||
const redis = Redis.fromEnv() | ||
|
||
export async function GET(req: Request) { | ||
const { searchParams } = new URL(req.url); | ||
const codeGeneration = searchParams.get("code"); | ||
const { searchParams } = new URL(req.url) | ||
const codeGeneration = searchParams.get('code') | ||
if (!codeGeneration) { | ||
return NextResponse.json( | ||
{ error: "Code generation was not provided." }, | ||
{ error: 'Code generation was not provided.' }, | ||
{ | ||
status: 400, | ||
status: 400 | ||
} | ||
); | ||
) | ||
} | ||
|
||
try { | ||
const code = await redis.get(codeGeneration); | ||
const code = await redis.get(codeGeneration) | ||
|
||
return NextResponse.json({ data: code }); | ||
return NextResponse.json({ data: code }) | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ error: "An error has ocurred while fetching sql code." }, | ||
{ error: 'An error has ocurred while fetching sql code.' }, | ||
{ | ||
status: 500, | ||
status: 500 | ||
} | ||
); | ||
) | ||
} | ||
} |
Oops, something went wrong.