|
| 1 | +import { SQLSyncProvider } from "@orbitinghail/sqlsync-solid-js"; |
| 2 | +import { journalIdFromString, sql } from "@orbitinghail/sqlsync-worker"; |
| 3 | + |
| 4 | +// this example uses the uuid library (`npm install uuid`) |
| 5 | +import { JSX } from "solid-js/jsx-runtime"; |
| 6 | +import { v4 as uuidv4 } from "uuid"; |
| 7 | + |
| 8 | +// You'll need to configure your build system to make these entrypoints |
| 9 | +// available as urls. Vite does this automatically via the `?url` and `?worker&url` suffix. |
| 10 | +import sqlSyncWasmUrl from "@orbitinghail/sqlsync-worker/sqlsync.wasm?url"; |
| 11 | +import workerUrl from "@orbitinghail/sqlsync-worker/worker.ts?worker&url"; |
| 12 | + |
| 13 | +import { createEffect, createSignal } from "solid-js"; |
| 14 | +import { For, render } from "solid-js/web"; |
| 15 | +import { useMutate, useQuery } from "./doctype"; |
| 16 | + |
| 17 | +// Create a DOC_ID to use, each DOC_ID will correspond to a different SQLite |
| 18 | +// database. We use a static doc id so we can play with cross-tab sync. |
| 19 | +const DOC_ID = journalIdFromString("VM7fC4gKxa52pbdtrgd9G9"); |
| 20 | + |
| 21 | +// Use SQLSync hooks in your app |
| 22 | +export function App() { |
| 23 | + // we will use the standard useState hook to handle the message input box |
| 24 | + const [msg, setMsg] = createSignal(""); |
| 25 | + |
| 26 | + // create a mutate function for our document |
| 27 | + const mutate = useMutate(DOC_ID); |
| 28 | + |
| 29 | + // initialize the schema; eventually this will be handled by SQLSync automatically |
| 30 | + createEffect(() => { |
| 31 | + mutate({ tag: "InitSchema" }).catch((err) => { |
| 32 | + console.error("Failed to init schema", err); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + // create a callback which knows how to trigger the add message mutation |
| 37 | + const handleSubmit: JSX.EventHandler<HTMLFormElement, SubmitEvent> = (e) => { |
| 38 | + // Prevent the browser from reloading the page |
| 39 | + e.preventDefault(); |
| 40 | + |
| 41 | + // create a unique message id |
| 42 | + const id = crypto.randomUUID ? crypto.randomUUID() : uuidv4(); |
| 43 | + |
| 44 | + // don't add empty messages |
| 45 | + if (msg().trim() !== "") { |
| 46 | + mutate({ tag: "AddMessage", id, msg: msg() }).catch((err) => { |
| 47 | + console.error("Failed to add message", err); |
| 48 | + }); |
| 49 | + // clear the message |
| 50 | + setMsg(""); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + // create a callback to delete a message |
| 55 | + const handleDelete = (id: string) => { |
| 56 | + mutate({ tag: "DeleteMessage", id }).catch((err) => { |
| 57 | + console.error("Failed to delete message", err); |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + // finally, query SQLSync for all the messages, sorted by created_at |
| 62 | + const queryState = useQuery<{ id: string; msg: string }>( |
| 63 | + () => DOC_ID, |
| 64 | + () => sql` |
| 65 | + select id, msg from messages |
| 66 | + order by created_at |
| 67 | + `, |
| 68 | + ); |
| 69 | + |
| 70 | + const rows = () => queryState().rows ?? []; |
| 71 | + |
| 72 | + return ( |
| 73 | + <div> |
| 74 | + <h1>Guestbook:</h1> |
| 75 | + <ul> |
| 76 | + <For each={rows()}> |
| 77 | + {(row) => { |
| 78 | + return ( |
| 79 | + <li> |
| 80 | + {row.msg} |
| 81 | + <button |
| 82 | + type="button" |
| 83 | + onClick={() => handleDelete(row.id)} |
| 84 | + style={{ "margin-left": "40px" }} |
| 85 | + > |
| 86 | + remove msg |
| 87 | + </button> |
| 88 | + </li> |
| 89 | + ); |
| 90 | + }} |
| 91 | + </For> |
| 92 | + </ul> |
| 93 | + <h3>Leave a message:</h3> |
| 94 | + <form onSubmit={handleSubmit}> |
| 95 | + <label> |
| 96 | + Msg: |
| 97 | + <input type="text" name="msg" value={msg()} onChange={(e) => setMsg(e.target.value)} /> |
| 98 | + </label> |
| 99 | + <input type="submit" value="Submit" /> |
| 100 | + </form> |
| 101 | + </div> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +// Configure the SQLSync provider near the top of the React tree |
| 106 | +render( |
| 107 | + () => ( |
| 108 | + <SQLSyncProvider wasmUrl={sqlSyncWasmUrl} workerUrl={workerUrl}> |
| 109 | + <App /> |
| 110 | + </SQLSyncProvider> |
| 111 | + ), |
| 112 | + // biome-ignore lint/style/noNonNullAssertion: we know this element exists |
| 113 | + document.getElementById("root")!, |
| 114 | +); |
0 commit comments