Skip to content

Commit bb94a6e

Browse files
committed
upgrade example to guestbook
1 parent a64ded7 commit bb94a6e

File tree

20 files changed

+278
-222
lines changed

20 files changed

+278
-222
lines changed

.github/workflows/actions.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ jobs:
4545
run: just test-sqlsync-reducer
4646
- name: build sqlsync react and worker packages
4747
run: just package-sqlsync-react package-sqlsync-worker
48+
- name: build frontend
49+
run: cd demo/frontend && pnpm build
50+
- name: build examples
51+
run: cd examples/guestbook-react && pnpm build

Cargo.lock

Lines changed: 25 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ members = [
77
"lib/sqlsync-reducer",
88
"lib/sqlite-vfs",
99
"lib/testutil",
10-
"examples/count-reducer",
10+
11+
"examples/reducer-guestbook",
1112

1213
"demo/demo-reducer",
1314
"demo/cloudflare-backend",

GUIDE.md

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ strip = "debuginfo"
4848
codegen-units = 1
4949

5050
[dependencies]
51-
sqlsync-reducer = "0.1"
51+
sqlsync-reducer = "0.2"
5252
serde = { version = "1.0", features = ["derive"] }
5353
serde_json = "1.0"
5454
log = "0.4"
@@ -118,17 +118,12 @@ Also, make sure your JS bundling tool supports importing assets from the file sy
118118
Create a file which will contain type information for your Mutations, the reducer URL, and export some useful React hooks for your app to consume. It should look something like this:
119119

120120
```typescript
121-
import {
122-
DocType,
123-
createDocHooks,
124-
serializeMutationAsJSON,
125-
} from "@orbitinghail/sqlsync-react";
126-
127-
// Path to your compiled reducer artifact, your js bundler should handle making
128-
// this a working URL that resolves during development and in production.
121+
import { createDocHooks } from "@orbitinghail/sqlsync-react";
122+
import { DocType, serializeMutationAsJSON } from "@orbitinghail/sqlsync-worker";
123+
129124
const REDUCER_URL = new URL(
130-
"../reducer/target/wasm32-unknown-unknown/release/reducer.wasm",
131-
import.meta.url
125+
"../../../target/wasm32-unknown-unknown/release/reducer_guestbook.wasm",
126+
import.meta.url,
132127
);
133128

134129
// Must match the Mutation type in the Rust Reducer code
@@ -147,8 +142,7 @@ export const TaskDocType: DocType<Mutation> = {
147142
serializeMutation: serializeMutationAsJSON,
148143
};
149144

150-
export const { useMutate, useQuery, useSetConnectionEnabled } =
151-
createDocHooks(TaskDocType);
145+
export const { useMutate, useQuery, useSetConnectionEnabled } = createDocHooks(TaskDocType);
152146
```
153147

154148
## Step 3: Hooking it up to your app
@@ -158,31 +152,32 @@ Using the hooks exported from the file in [Step 2](#step-2-install-and-configure
158152
Here is a complete example of a very trivial guestbook application which uses the reducer we created above.
159153

160154
```tsx
161-
import React, { FormEvent, useEffect } from "react";
162-
import ReactDOM from "react-dom/client";
155+
import { SQLSyncProvider } from "@orbitinghail/sqlsync-react";
156+
import { journalIdFromString, sql } from "@orbitinghail/sqlsync-worker";
163157

164158
// this example uses the uuid library (`npm install uuid`)
165159
import { v4 as uuidv4 } from "uuid";
166160

161+
import React, { FormEvent, useCallback, useEffect } from "react";
162+
import ReactDOM from "react-dom/client";
163+
167164
// You'll need to configure your build system to make these entrypoints
168-
// available as urls. Vite does this automatically via the `?url` suffix.
165+
// available as urls. Vite does this automatically via the `?url` and `?worker&url` suffix.
169166
import sqlSyncWasmUrl from "@orbitinghail/sqlsync-worker/sqlsync.wasm?url";
170-
import workerUrl from "@orbitinghail/sqlsync-worker/worker.js?url";
167+
import workerUrl from "@orbitinghail/sqlsync-worker/worker.js?worker&url";
171168

172-
// import the SQLSync provider and hooks
173-
import { SQLSyncProvider, sql } from "@orbitinghail/sqlsync-react";
174169
import { useMutate, useQuery } from "./doctype";
175170

176171
// Create a DOC_ID to use, each DOC_ID will correspond to a different SQLite
177172
// database. We use a static doc id so we can play with cross-tab sync.
178-
import { journalIdFromString } from "@orbitinghail/sqlsync-worker";
179173
const DOC_ID = journalIdFromString("VM7fC4gKxa52pbdtrgd9G9");
180174

181175
// Configure the SQLSync provider near the top of the React tree
176+
// biome-ignore lint/style/noNonNullAssertion: we know this element exists
182177
ReactDOM.createRoot(document.getElementById("root")!).render(
183178
<SQLSyncProvider wasmUrl={sqlSyncWasmUrl} workerUrl={workerUrl}>
184179
<App />
185-
</SQLSyncProvider>
180+
</SQLSyncProvider>,
186181
);
187182

188183
// Use SQLSync hooks in your app
@@ -201,7 +196,7 @@ export function App() {
201196
}, [mutate]);
202197

203198
// create a callback which knows how to trigger the add message mutation
204-
const handleSubmit = React.useCallback(
199+
const handleSubmit = useCallback(
205200
(e: FormEvent<HTMLFormElement>) => {
206201
// Prevent the browser from reloading the page
207202
e.preventDefault();
@@ -218,7 +213,7 @@ export function App() {
218213
setMsg("");
219214
}
220215
},
221-
[mutate, msg]
216+
[mutate, msg, setMsg],
222217
);
223218

224219
// finally, query SQLSync for all the messages, sorted by created_at
@@ -227,7 +222,7 @@ export function App() {
227222
sql`
228223
select id, msg from messages
229224
order by created_at
230-
`
225+
`,
231226
);
232227

233228
return (
@@ -242,12 +237,7 @@ export function App() {
242237
<form onSubmit={handleSubmit}>
243238
<label>
244239
Msg:
245-
<input
246-
type="text"
247-
name="msg"
248-
value={msg}
249-
onChange={(e) => setMsg(e.target.value)}
250-
/>
240+
<input type="text" name="msg" value={msg} onChange={(e) => setMsg(e.target.value)} />
251241
</label>
252242
<input type="submit" value="Submit" />
253243
</form>

examples/count-reducer/Cargo.toml

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/count-reducer/src/lib.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

examples/simple-counter-react/package.json renamed to examples/guestbook-react/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "simple-counter-react",
2+
"name": "guestbook-react",
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
@@ -9,14 +9,16 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12+
"@orbitinghail/sqlsync-react": "workspace:*",
13+
"@orbitinghail/sqlsync-worker": "workspace:*",
1214
"react": "^18.2.0",
1315
"react-dom": "^18.2.0",
14-
"@orbitinghail/sqlsync-react": "workspace:*",
15-
"@orbitinghail/sqlsync-worker": "workspace:*"
16+
"uuid": "^9.0.1"
1617
},
1718
"devDependencies": {
1819
"@types/react": "^18.2.43",
1920
"@types/react-dom": "^18.2.17",
21+
"@types/uuid": "^9.0.6",
2022
"@vitejs/plugin-react": "^4.2.1",
2123
"typescript": "^5.2.2",
2224
"vite": "^5.0.8"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createDocHooks } from "@orbitinghail/sqlsync-react";
2+
import { DocType, serializeMutationAsJSON } from "@orbitinghail/sqlsync-worker";
3+
4+
const REDUCER_URL = new URL(
5+
"../../../target/wasm32-unknown-unknown/release/reducer_guestbook.wasm",
6+
import.meta.url,
7+
);
8+
9+
// Must match the Mutation type in the Rust Reducer code
10+
export type Mutation =
11+
| {
12+
tag: "InitSchema";
13+
}
14+
| {
15+
tag: "AddMessage";
16+
id: string;
17+
msg: string;
18+
};
19+
20+
export const TaskDocType: DocType<Mutation> = {
21+
reducerUrl: REDUCER_URL,
22+
serializeMutation: serializeMutationAsJSON,
23+
};
24+
25+
export const { useMutate, useQuery, useSetConnectionEnabled } = createDocHooks(TaskDocType);

0 commit comments

Comments
 (0)