db create
This commit is contained in:
parent
19ae57d449
commit
809fe18090
9 changed files with 93 additions and 35 deletions
|
@ -1,5 +1,7 @@
|
||||||
## Example .env
|
## Example .env
|
||||||
```
|
```
|
||||||
|
# If not using docker, use 0.0.0.0:8000
|
||||||
|
DB_PORT=surrealdb:8000
|
||||||
DB_USER=root
|
DB_USER=root
|
||||||
DB_PASSWORD=root
|
DB_PASSWORD=root
|
||||||
```
|
```
|
||||||
|
|
52
package-lock.json
generated
52
package-lock.json
generated
|
@ -18,6 +18,7 @@
|
||||||
"@tanstack/react-table": "^8.12.0",
|
"@tanstack/react-table": "^8.12.0",
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
"clsx": "^2.1.0",
|
"clsx": "^2.1.0",
|
||||||
|
"dotenv": "^16.4.4",
|
||||||
"next": "14.1.0",
|
"next": "14.1.0",
|
||||||
"next-themes": "^0.2.1",
|
"next-themes": "^0.2.1",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
@ -27,6 +28,7 @@
|
||||||
"swr": "^2.2.4",
|
"swr": "^2.2.4",
|
||||||
"tailwind-merge": "^2.2.1",
|
"tailwind-merge": "^2.2.1",
|
||||||
"tailwindcss-animate": "^1.0.7",
|
"tailwindcss-animate": "^1.0.7",
|
||||||
|
"ws": "^8.16.0",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -3164,6 +3166,17 @@
|
||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/dotenv": {
|
||||||
|
"version": "16.4.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.4.tgz",
|
||||||
|
"integrity": "sha512-XvPXc8XAQThSjAbY6cQ/9PcBXmFoWuw1sQ3b8HqUCR6ziGXjkTi//kB9SWa2UwqlgdAIuRqAa/9hVljzPehbYg==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/eastasianwidth": {
|
"node_modules/eastasianwidth": {
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz",
|
||||||
|
@ -10932,26 +10945,6 @@
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/surrealdb.js/node_modules/ws": {
|
|
||||||
"version": "8.16.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz",
|
|
||||||
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=10.0.0"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"bufferutil": "^4.0.1",
|
|
||||||
"utf-8-validate": ">=5.0.2"
|
|
||||||
},
|
|
||||||
"peerDependenciesMeta": {
|
|
||||||
"bufferutil": {
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"utf-8-validate": {
|
|
||||||
"optional": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/swr": {
|
"node_modules/swr": {
|
||||||
"version": "2.2.4",
|
"version": "2.2.4",
|
||||||
"resolved": "https://registry.npmjs.org/swr/-/swr-2.2.4.tgz",
|
"resolved": "https://registry.npmjs.org/swr/-/swr-2.2.4.tgz",
|
||||||
|
@ -11371,7 +11364,24 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/ws": {
|
"node_modules/ws": {
|
||||||
"peer": true
|
"version": "8.16.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz",
|
||||||
|
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": ">=5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"node_modules/zod": {
|
"node_modules/zod": {
|
||||||
"version": "3.22.4",
|
"version": "3.22.4",
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
"@tanstack/react-table": "^8.12.0",
|
"@tanstack/react-table": "^8.12.0",
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
"clsx": "^2.1.0",
|
"clsx": "^2.1.0",
|
||||||
|
"dotenv": "^16.4.4",
|
||||||
"next": "14.1.0",
|
"next": "14.1.0",
|
||||||
"next-themes": "^0.2.1",
|
"next-themes": "^0.2.1",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
@ -28,6 +29,7 @@
|
||||||
"swr": "^2.2.4",
|
"swr": "^2.2.4",
|
||||||
"tailwind-merge": "^2.2.1",
|
"tailwind-merge": "^2.2.1",
|
||||||
"tailwindcss-animate": "^1.0.7",
|
"tailwindcss-animate": "^1.0.7",
|
||||||
|
"ws": "^8.16.0",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
19
src/app/create/db.tsx
Normal file
19
src/app/create/db.tsx
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
"use server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { formSchema } from "./page";
|
||||||
|
|
||||||
|
export async function querydb(values: z.infer<typeof formSchema>) {
|
||||||
|
// let db = await initConnection();
|
||||||
|
// let url = await db.query(`
|
||||||
|
// create url:[rand::string(8)] CONTENT {
|
||||||
|
// long_url: string::replace($long_url, "http://", "https://"),
|
||||||
|
// clicks: 0,
|
||||||
|
// date_added: time::now(),
|
||||||
|
// date_accessed: <future> { time::now() }
|
||||||
|
// } return id[0];
|
||||||
|
// `, { long_url: values.url });
|
||||||
|
let url = "test";
|
||||||
|
|
||||||
|
console.log(values.url, url)
|
||||||
|
return url;
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
"use client"
|
"use client";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
|
@ -16,18 +16,23 @@ import {
|
||||||
} from "@/components/ui/form";
|
} from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
import { useFormState } from "react-dom";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { querydb } from "./db";
|
||||||
|
|
||||||
const formSchema = z.object({
|
export const formSchema = z.object({
|
||||||
url: z.string().min(4,
|
url: z.string().min(4,
|
||||||
{ message: "The URL must be at least 4 characters long" }
|
{ message: "The URL must be at least 4 characters long" }
|
||||||
).max(100
|
).max(100
|
||||||
, { message: "The URL must be at most 100 characters long" }),
|
, { message: "The URL must be at most 100 characters long" }),
|
||||||
})
|
});
|
||||||
|
const initialState = {
|
||||||
|
message: null,
|
||||||
|
}
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
const [state, formAction] = useFormState(querydb, initialState);
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
|
@ -35,10 +40,6 @@ export default function Home() {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
|
||||||
console.log(values)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="hidden items-start justify-center gap-6 rounded-lg p-8 md:grid lg:grid-cols-1 xl:grid-cols-1">
|
<div className="hidden items-start justify-center gap-6 rounded-lg p-8 md:grid lg:grid-cols-1 xl:grid-cols-1">
|
||||||
<Card>
|
<Card>
|
||||||
|
@ -47,15 +48,15 @@ export default function Home() {
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
<form action={form.handleSubmit(querydb)} className="space-y-8">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="url"
|
name="url"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Enter url</FormLabel>
|
<FormLabel>Enter a url</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input placeholder="shadcn" {...field} />
|
<Input placeholder="nexveridian.com" {...field} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
@ -64,6 +65,7 @@ export default function Home() {
|
||||||
<Button type="submit">Submit</Button>
|
<Button type="submit">Submit</Button>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div >
|
</div >
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"use client"
|
"use client";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"use client";
|
"use client";;
|
||||||
import { MoonIcon, SunIcon } from "@radix-ui/react-icons";
|
import { MoonIcon, SunIcon } from "@radix-ui/react-icons";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
|
|
||||||
|
|
23
src/components/db-utils.tsx
Normal file
23
src/components/db-utils.tsx
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { Surreal } from 'surrealdb.js';
|
||||||
|
require('dotenv');
|
||||||
|
|
||||||
|
const db = new Surreal();
|
||||||
|
|
||||||
|
export async function initConnection(): Promise<Surreal> {
|
||||||
|
try {
|
||||||
|
db.connect(process.env.DB_PORT + '/rpc', {
|
||||||
|
namespace: 'url',
|
||||||
|
database: 'url',
|
||||||
|
|
||||||
|
auth: {
|
||||||
|
username: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASSWORD,
|
||||||
|
scope: '',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error('ERROR', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
"use client";
|
"use client";;
|
||||||
import { ThemeProvider as NextThemesProvider } from "next-themes";
|
import { ThemeProvider as NextThemesProvider } from "next-themes";
|
||||||
import { type ThemeProviderProps } from "next-themes/dist/types";
|
import { type ThemeProviderProps } from "next-themes/dist/types";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue