diff --git a/.gitignore b/.gitignore index fb463e6..831bca0 100644 --- a/.gitignore +++ b/.gitignore @@ -106,4 +106,5 @@ dist .parcel-cache/ dev.db dev.db-journal -examples/prisma/migrations/ \ No newline at end of file +examples/prisma/migrations/ +bun.lockb \ No newline at end of file diff --git a/examples/prisma/README.md b/examples/prisma/README.md new file mode 100644 index 0000000..2834957 --- /dev/null +++ b/examples/prisma/README.md @@ -0,0 +1,4 @@ +## How to Run this? + +1. Run `npm i` or your preferred installer on the `examples` directory. +2. `cd` into the prisma directory and run `npx prisma migrate dev`. diff --git a/examples/prisma/schema.prisma b/examples/prisma/schema.prisma index bad748f..885bb69 100644 --- a/examples/prisma/schema.prisma +++ b/examples/prisma/schema.prisma @@ -20,7 +20,7 @@ model User { model Todo { id String @id @default(cuid()) - status Boolean? @default(false) + status Boolean @default(false) text String owner User? @relation(fields: [userId], references: [id]) createdAt DateTime @default(now()) diff --git a/examples/react/App.tsx b/examples/react/App.tsx index 58ae648..7ac474c 100644 --- a/examples/react/App.tsx +++ b/examples/react/App.tsx @@ -1,53 +1,10 @@ -import React, { useEffect, useState } from "react"; -import { Client } from "../../src"; -import { User } from "@prisma/client"; -import { API } from "../prisma/server"; - -const client = Client("http://localhost:8080"); - -const { sum } = client; +import React from "react"; +import Rocket from "./rocket"; export function App() { - const [helloResult, setHelloResult] = useState("loading..."); - const [users, setUsers] = useState([]); - - useEffect(() => { - client.ping("World!").then(setHelloResult); - }, []); - - const [num1, setNum1] = useState(0); - const [num2, setNum2] = useState(0); - const [sumResult, setSumResult] = useState(0); - - useEffect(() => { - sum(num1, num2).then(setSumResult); - }, [num1, num2]); - - useEffect(() => { - client.prisma.user.findMany().then(setUsers); - }, []); - - console.log({ users }); - return (
-

Hello world!

-

Ping Result: {helloResult}

-

- Sum of{" "} - setNum1(parseInt(e.target.value))} - type="number" - />{" "} - and{" "} - setNum2(parseInt(e.target.value))} - type="number" - />{" "} - is {sumResult} -

+
); } diff --git a/examples/react/Todos.tsx b/examples/react/Todos.tsx new file mode 100644 index 0000000..5eade4d --- /dev/null +++ b/examples/react/Todos.tsx @@ -0,0 +1,53 @@ +import React from "react"; + +export type Todo = { text: string; status: boolean; id: string }; + +export const Todos = ({ + notes, + addNote, + onDone, + onDelete, +}: { + notes: Todo[]; + addNote: (newNote: string) => void; + onDone: (index: string, done: boolean) => void; + onDelete: (index: string) => void; +}) => { + return ( +
+
{ + e.preventDefault(); + + // Read the form data + const form = e.target as HTMLFormElement; + const formData = new FormData(form); + + const newTodo = formData.get("newTodo") as string; + if (newTodo) { + addNote(newTodo); + form.reset(); + } + }} + > + + +
+ {notes.map((note) => ( +

+ onDone(note.id, e.target.checked)} + type="checkbox" + /> + {note.text} + +

+ ))} +
+ ); +}; \ No newline at end of file diff --git a/examples/react/rocket.tsx b/examples/react/rocket.tsx new file mode 100644 index 0000000..9e3d960 --- /dev/null +++ b/examples/react/rocket.tsx @@ -0,0 +1,62 @@ +import React, { useEffect, useState } from "react"; +import type { Todo } from "./Todos"; +import { Todos } from "./Todos"; +import { useClient } from "./useRocketRpc"; + +export default function Rocket() { + const [notes, setNotes] = useState([]); + + const client = useClient("http://localhost:8080"); + const { prisma } = client; + + useEffect(() => { + prisma.todo.findMany().then(setNotes); + }, []); + + useEffect(() => { + console.log({ client }, client.hello()); + }, [client]); + + return ( + { + prisma.todo + .create({ + data: { text: newNote, status: false }, + }) + .then((newNote) => setNotes([...notes, newNote])); + }} + onDone={(id, status) => { + prisma.todo + .update({ + where: { + id, + }, + data: { + status, + }, + }) + .then(({ id }) => + setNotes( + notes.map((note) => ({ + ...note, + status: note.id === id ? status : note.status, + })) + ) + ); + }} + onDelete={(id) => { + prisma.todo + .delete({ + where: { + id, + }, + }) + .then(({ id }) => { + setNotes(notes.filter((note) => note.id !== id)); + }); + }} + /> + ); +} diff --git a/examples/react/useRocketRpc.tsx b/examples/react/useRocketRpc.tsx new file mode 100644 index 0000000..2c57162 --- /dev/null +++ b/examples/react/useRocketRpc.tsx @@ -0,0 +1,29 @@ +import { useEffect, useState } from "react"; +import { Client } from "../../src"; +import type { API } from "../../examples/prisma/server"; + +export const useClient = (endpoint: string) => { + return Client(endpoint); +}; + +export const useRocketRpc = (endpoint: string) => { + const [client, setClient] = useState> | null>( + null + ); + + useEffect(() => { + setClient(Client(endpoint)); + + return () => { + client?._rocketRpcContext.closeConnection(); + }; + }, [endpoint]); + + console.log(client?._rocketRpcContext); + + if (!client?._rocketRpcContext?.socket.connected) { + return null; + } + + return client; +};