Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimondev committed May 2, 2024
1 parent 31f4224 commit 2487374
Show file tree
Hide file tree
Showing 44 changed files with 1,010 additions and 1,136 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ vdbs stands for **"vision database sql"** This project allows you to convert you

2. Copy the npm command generated after your SQL schema is ready and paste it into your app. This creates a migration file containing the SQL schema.

[![command demo](https://res.cloudinary.com/marcomontalbano/image/upload/v1713756126/video_to_markdown/images/video--9db6fb0d29e2e07d39d8ecc98c8a7518-c05b58ac6eb4c4700831b2b3070cd403.jpg)](https://res.cloudinary.com/xavimon/video/upload/v1713756082/vdbs/tzwaieulccqjtn6sq8u5.mp4 "command demo")
[![command demo](https://res.cloudinary.com/marcomontalbano/image/upload/v1713756126/video_to_markdown/images/video--9db6fb0d29e2e07d39d8ecc98c8a7518-c05b58ac6eb4c4700831b2b3070cd403.jpg)](https://res.cloudinary.com/xavimon/video/upload/v1713756082/vdbs/tzwaieulccqjtn6sq8u5.mp4 'command demo')

3. Use your database connection string, and the SQL schema will be deployed to your remote Supabase project.

[![deploy demo](https://res.cloudinary.com/marcomontalbano/image/upload/v1713755928/video_to_markdown/images/video--f7104dd1f8f5624d627e730fc3a3af46-c05b58ac6eb4c4700831b2b3070cd403.jpg)](https://res.cloudinary.com/xavimon/video/upload/v1713754365/vdbs/yytclbkf7ottvfudjpxl.mp4 "deploy demo")
[![deploy demo](https://res.cloudinary.com/marcomontalbano/image/upload/v1713755928/video_to_markdown/images/video--f7104dd1f8f5624d627e730fc3a3af46-c05b58ac6eb4c4700831b2b3070cd403.jpg)](https://res.cloudinary.com/xavimon/video/upload/v1713754365/vdbs/yytclbkf7ottvfudjpxl.mp4 'deploy demo')

vdbs is inspired by [v0.dev from Vercel](https://v0.dev/), which allows you to generate UI based on images or your ideas. You can copy the code or use the npm command and integrate the generated code into your project.

Expand Down Expand Up @@ -116,4 +116,4 @@ Upgrade bun's version
bun upgrade
```

More details: **https://twitter.com/bunjavascript/status/1734470860755566815**
More details: **https://twitter.com/bunjavascript/status/1734470860755566815**
41 changes: 19 additions & 22 deletions apps/web/actions.ts
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
}
40 changes: 20 additions & 20 deletions apps/web/app/api/check-connection/route.ts
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' })
}
80 changes: 37 additions & 43 deletions apps/web/app/api/code-generation/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import OpenAI from "openai";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
import OpenAI from 'openai'
import { OpenAIStream, StreamingTextResponse } from 'ai'
// import { Ratelimit } from "@upstash/ratelimit";
// import { Redis } from "@upstash/redis";
// import { TOTAL_GENERATIONS } from "@/constants";
import { PROMPT } from "@/prompt";
import { PROMPT } from '@/prompt'

const openai = new OpenAI();
const openai = new OpenAI()

export const runtime = "edge";
export const runtime = 'edge'

// const ratelimit =
// process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
Expand All @@ -24,35 +24,34 @@ export const runtime = "edge";
// : false;

export async function POST(req: Request) {
const customApiKey = cookies().get("api-key")?.value;
const customApiKey = cookies().get('api-key')?.value

if (process.env.NODE_ENV === "production" && !customApiKey) {
if (process.env.NODE_ENV === 'production' && !customApiKey) {
return NextResponse.json(
{
data: undefined,
message: "Missing API KEY – make sure to set it.",
message: 'Missing API KEY – make sure to set it.'
},
{ status: 400 }
);
)
}

if (
process.env.NODE_ENV === "development" &&
(!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "")
process.env.NODE_ENV === 'development' &&
(!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === '')
) {
return NextResponse.json(
{
data: undefined,
message:
"Missing OPENAI_API_KEY – make sure to add it to your .env file.",
message: 'Missing OPENAI_API_KEY – make sure to add it to your .env file.'
},
{ status: 400 }
);
)
}

if (customApiKey) {
// Set user's api key
openai.apiKey = customApiKey as string;
openai.apiKey = customApiKey as string
}

// const hasCustomApiKey = customApiKey && customApiKey.trim().length > 0;
Expand All @@ -76,51 +75,46 @@ export async function POST(req: Request) {
// }
// }

const { prompt: base64 } = await req.json();
const { prompt: base64 } = await req.json()

try {
const response = await openai.chat.completions.create({
model: "gpt-4-turbo",
model: 'gpt-4-turbo',
stream: true,
max_tokens: 4096,
temperature: 0.2,
messages: [
{
role: "user",
role: 'user',
content: [
{
type: "text",
text: PROMPT,
type: 'text',
text: PROMPT
},
{
type: "image_url",
type: 'image_url',
image_url: {
url: base64,
},
},
],
},
],
});
url: base64
}
}
]
}
]
})

const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
} catch (error) {
if (error instanceof OpenAI.APIError) {
let errorMessage =
"An error has ocurred with API Completions. Please try again.";
if (error.code === "invalid_api_key") {
errorMessage =
"The provided API Key is invalid. Please enter a valid API Key.";
let errorMessage = 'An error has ocurred with API Completions. Please try again.'
if (error.code === 'invalid_api_key') {
errorMessage = 'The provided API Key is invalid. Please enter a valid API Key.'
}

const { name, status, headers } = error;
return NextResponse.json(
{ name, status, headers, message: errorMessage },
{ status }
);
const { name, status, headers } = error
return NextResponse.json({ name, status, headers, message: errorMessage }, { status })
} else {
throw error;
throw error
}
}
}
46 changes: 23 additions & 23 deletions apps/web/app/api/deploy/route.ts
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'
})
}
28 changes: 14 additions & 14 deletions apps/web/app/api/get-generation/route.ts
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
}
);
)
}
}

0 comments on commit 2487374

Please sign in to comment.