postgres
This commit is contained in:
parent
b0f3e6c576
commit
e44d419b58
15 changed files with 233 additions and 82 deletions
|
@ -1,16 +1,35 @@
|
|||
"use server";
|
||||
import { initConnection } from "@/components/db-utils";
|
||||
import { initConnectionPostgres, initConnectionSurreal } from "@/components/db-utils";
|
||||
|
||||
export async function querydb(slug: string) {
|
||||
let long_url = undefined;
|
||||
try {
|
||||
let db = await initConnection();
|
||||
let long_url = await db.query(`
|
||||
update url:[$id] set clicks = clicks + 1 ;
|
||||
select * from url:[$id];
|
||||
`, { id: slug });
|
||||
if (process.env.DB_TYPE === "surrealdb") {
|
||||
|
||||
// @ts-ignore
|
||||
long_url = long_url[0][0].long_url;
|
||||
let db = await initConnectionSurreal();
|
||||
let long_url = await db.query(`
|
||||
update url:[$id]
|
||||
set clicks = clicks + 1;
|
||||
select * from url:[$id];
|
||||
`, { id: slug });
|
||||
|
||||
// @ts-ignore
|
||||
long_url = long_url[0][0].long_url;
|
||||
}
|
||||
|
||||
if (process.env.DB_TYPE === "postgres") {
|
||||
let sql = await initConnectionPostgres();
|
||||
long_url = await sql`
|
||||
update url set
|
||||
clicks = clicks + 1,
|
||||
date_accessed = now()
|
||||
where id = ${slug}
|
||||
returning long_url;
|
||||
`;
|
||||
long_url = long_url[0].long_url;
|
||||
}
|
||||
|
||||
console.log(long_url);
|
||||
return long_url;
|
||||
} catch (e) {
|
||||
return;
|
||||
|
|
|
@ -1,30 +1,73 @@
|
|||
"use server";
|
||||
import { initConnection } from "@/components/db-utils";
|
||||
import { initConnectionPostgres, initConnectionSurreal } from "@/components/db-utils";
|
||||
import { formSchema } from "./schema";
|
||||
|
||||
export async function querydb(prevState: any, formData: FormData) {
|
||||
try {
|
||||
const values = formSchema.safeParse(formData)
|
||||
if (!values.success) {
|
||||
return { error: values.error };
|
||||
}
|
||||
const long_url = values.data.url;
|
||||
|
||||
let db = await initConnection();
|
||||
let url = await db.query(`
|
||||
create url:[rand::string(8)] CONTENT {
|
||||
long_url: string::replace(string::replace($long_url, "https://", ""), "http://", ""),
|
||||
clicks: 0,
|
||||
date_added: time::now(),
|
||||
date_accessed: <future> { time::now() }
|
||||
} return id[0];
|
||||
`, { long_url: long_url });
|
||||
|
||||
// @ts-ignore
|
||||
url = url[0][0].id;
|
||||
|
||||
return { url: url };
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
function generateRandomString(length: number) {
|
||||
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
let randomString = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * charset.length);
|
||||
randomString += charset[randomIndex];
|
||||
}
|
||||
return randomString;
|
||||
}
|
||||
|
||||
export async function querydb(prevState: any, formData: FormData) {
|
||||
const values = formSchema.safeParse(formData)
|
||||
if (!values.success) {
|
||||
return { error: values.error };
|
||||
}
|
||||
const long_url = values.data.url;
|
||||
let url = undefined;
|
||||
|
||||
try {
|
||||
if (process.env.DB_TYPE === "surrealdb") {
|
||||
let db = await initConnectionSurreal();
|
||||
url = await db.query(`
|
||||
create url:[rand::string(8)] CONTENT {
|
||||
long_url: $long_url,
|
||||
clicks: 0,
|
||||
date_added: time::now(),
|
||||
date_accessed: <future> { time::now() }
|
||||
} return id[0];
|
||||
`, {
|
||||
long_url: long_url.replace("https://", "").replace("http://", "")
|
||||
});
|
||||
// @ts-ignore
|
||||
url = url[0][0].id;
|
||||
}
|
||||
|
||||
console.log(long_url);
|
||||
if (process.env.DB_TYPE === "postgres") {
|
||||
let sql = await initConnectionPostgres();
|
||||
await sql`
|
||||
create table if not exists url (
|
||||
id text primary key,
|
||||
clicks integer not null,
|
||||
long_url text not null,
|
||||
date_added timestamp not null,
|
||||
date_accessed timestamp not null
|
||||
);
|
||||
`;
|
||||
|
||||
url = await sql`
|
||||
insert into url (id, long_url, clicks, date_added, date_accessed)
|
||||
values (
|
||||
${generateRandomString(8)},
|
||||
${long_url.replace("https://", "").replace("http://", "")},
|
||||
0,
|
||||
now(),
|
||||
now()
|
||||
)
|
||||
returning id;
|
||||
`;
|
||||
|
||||
url = url[0].id;
|
||||
}
|
||||
console.log(url);
|
||||
|
||||
return { url: url };
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,16 @@ import {
|
|||
CardTitle
|
||||
} from "@/components/ui/card";
|
||||
|
||||
export default function GlobalError() {
|
||||
export default function GlobalError({
|
||||
error,
|
||||
}: {
|
||||
error: Error & { digest?: string }
|
||||
}) {
|
||||
return (
|
||||
<CardGrid max_rows={1}>
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-center text-2xl text-red-400">Error</CardTitle>
|
||||
<CardTitle className="text-center text-2xl text-red-400">Error + {String(error)}</CardTitle>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
</CardGrid>
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
|
||||
export default function NotFound() {
|
||||
return (
|
||||
<CardGrid max_rows={1}>
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-center text-2xl text-red-400">404 - Not Found</CardTitle>
|
||||
|
|
|
@ -1,18 +1,30 @@
|
|||
"use server";
|
||||
import { initConnection } from "@/components/db-utils";
|
||||
import { initConnectionPostgres, initConnectionSurreal } from "@/components/db-utils";
|
||||
|
||||
export async function querydb() {
|
||||
try {
|
||||
let db = await initConnection();
|
||||
// console.log(db);
|
||||
let stats = await db.query(`
|
||||
select * from url
|
||||
order by clicks desc
|
||||
limit 50;
|
||||
`);
|
||||
let stats = [];
|
||||
if (process.env.DB_TYPE === "surrealdb") {
|
||||
let db = await initConnectionSurreal();
|
||||
// console.log(db);
|
||||
stats = await db.query(`
|
||||
select * from url
|
||||
order by clicks desc
|
||||
limit 50;
|
||||
`);
|
||||
|
||||
// @ts-ignore
|
||||
stats = stats[0];
|
||||
// @ts-ignore
|
||||
stats = stats[0];
|
||||
}
|
||||
|
||||
if (process.env.DB_TYPE === "postgres") {
|
||||
let sql = await initConnectionPostgres();
|
||||
stats = await sql`
|
||||
select * from url
|
||||
order by clicks desc
|
||||
limit 50;
|
||||
`;
|
||||
}
|
||||
|
||||
return stats;
|
||||
} catch (e) {
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
|
||||
export default function Loading() {
|
||||
return (
|
||||
<CardGrid max_rows={1}>
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-1xl text-amber-400">Loading...</CardTitle>
|
||||
|
|
|
@ -44,7 +44,7 @@ export default function StatsPage() {
|
|||
return data.length === 0 ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<CardGrid max_rows={1}>
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
{/* @ts-ignore */}
|
||||
<DataTable columns={columns} data={data} />
|
||||
|
|
|
@ -1,37 +1,30 @@
|
|||
"use client";
|
||||
export default function CardGrid({
|
||||
max_rows = 4,
|
||||
maxCols: maxCols = 4,
|
||||
children,
|
||||
className,
|
||||
...props
|
||||
}: {
|
||||
max_rows?: number;
|
||||
maxCols?: number;
|
||||
children?: React.ReactNode;
|
||||
className?: string;
|
||||
}) {
|
||||
let baseClassName = `hidden items-start justify-center gap-6 rounded-lg p-8 md:grid`;
|
||||
let baseClassName = `hidden items-start justify-center gap-6 rounded-lg p-8 md:grid md:grid-cols-1`;
|
||||
|
||||
switch (max_rows) {
|
||||
case 1:
|
||||
baseClassName += " md:grid-cols-1 ";
|
||||
break;
|
||||
case 2:
|
||||
baseClassName += " md:grid-cols-1 lg:grid-cols-2 ";
|
||||
break;
|
||||
case 3:
|
||||
baseClassName += " md:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 ";
|
||||
break;
|
||||
case 4:
|
||||
baseClassName += " md:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 ";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
if (maxCols >= 2) {
|
||||
baseClassName += " lg:grid-cols-2";
|
||||
}
|
||||
if (maxCols >= 3) {
|
||||
baseClassName += " xl:grid-cols-3";
|
||||
}
|
||||
if (maxCols >= 4) {
|
||||
baseClassName += " 2xl:grid-cols-4";
|
||||
}
|
||||
|
||||
if (className == undefined) {
|
||||
className = baseClassName;
|
||||
} else {
|
||||
className = baseClassName + className;
|
||||
className = baseClassName + " " + className;
|
||||
}
|
||||
return (
|
||||
<div
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
"use server";
|
||||
import "dotenv";
|
||||
import postgres, { Sql } from 'postgres';
|
||||
import { Surreal } from "surrealdb.js";
|
||||
const db = new Surreal();
|
||||
|
||||
export async function initConnection(): Promise<Surreal> {
|
||||
export async function initConnectionSurreal(): Promise<Surreal> {
|
||||
try {
|
||||
db.connect("ws://" + process.env.DB_URL_PORT + "/rpc", {
|
||||
namespace: "url",
|
||||
|
@ -19,3 +19,17 @@ export async function initConnection(): Promise<Surreal> {
|
|||
|
||||
return db;
|
||||
}
|
||||
|
||||
export async function initConnectionPostgres(): Promise<Sql<{}>> {
|
||||
const DB_URL_PORT = (process.env.DB_URL_PORT || "postgres:5432").split(":");
|
||||
const sql = postgres({
|
||||
host: DB_URL_PORT[0],
|
||||
port: parseInt(DB_URL_PORT[1]),
|
||||
user: process.env.POSTGRES_USER || "root",
|
||||
password: process.env.POSTGRES_PASSWORD || "root",
|
||||
database: process.env.POSTGRES_DB || "url",
|
||||
max: 100,
|
||||
onnotice: () => { },
|
||||
});
|
||||
return sql;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue