@livestore/livestore 0.4.0-dev.21 → 0.4.0-dev.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +0 -1
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/QueryCache.js +1 -1
  4. package/dist/QueryCache.js.map +1 -1
  5. package/dist/SqliteDbWrapper.d.ts +5 -5
  6. package/dist/SqliteDbWrapper.d.ts.map +1 -1
  7. package/dist/SqliteDbWrapper.js +8 -8
  8. package/dist/SqliteDbWrapper.js.map +1 -1
  9. package/dist/SqliteDbWrapper.test.js +2 -2
  10. package/dist/SqliteDbWrapper.test.js.map +1 -1
  11. package/dist/effect/LiveStore.d.ts +130 -2
  12. package/dist/effect/LiveStore.d.ts.map +1 -1
  13. package/dist/effect/LiveStore.js +185 -6
  14. package/dist/effect/LiveStore.js.map +1 -1
  15. package/dist/effect/LiveStore.test.d.ts +2 -0
  16. package/dist/effect/LiveStore.test.d.ts.map +1 -0
  17. package/dist/effect/LiveStore.test.js +42 -0
  18. package/dist/effect/LiveStore.test.js.map +1 -0
  19. package/dist/effect/mod.d.ts +1 -1
  20. package/dist/effect/mod.d.ts.map +1 -1
  21. package/dist/effect/mod.js +3 -1
  22. package/dist/effect/mod.js.map +1 -1
  23. package/dist/live-queries/base-class.d.ts +3 -3
  24. package/dist/live-queries/base-class.d.ts.map +1 -1
  25. package/dist/live-queries/base-class.js +2 -2
  26. package/dist/live-queries/base-class.js.map +1 -1
  27. package/dist/live-queries/client-document-get-query.d.ts +1 -1
  28. package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
  29. package/dist/live-queries/client-document-get-query.js +1 -1
  30. package/dist/live-queries/client-document-get-query.js.map +1 -1
  31. package/dist/live-queries/computed.d.ts.map +1 -1
  32. package/dist/live-queries/computed.js +2 -2
  33. package/dist/live-queries/computed.js.map +1 -1
  34. package/dist/live-queries/db-query.js +14 -14
  35. package/dist/live-queries/db-query.js.map +1 -1
  36. package/dist/live-queries/db-query.test.js +2 -2
  37. package/dist/live-queries/db-query.test.js.map +1 -1
  38. package/dist/live-queries/signal.test.js +2 -2
  39. package/dist/live-queries/signal.test.js.map +1 -1
  40. package/dist/mod.d.ts +2 -1
  41. package/dist/mod.d.ts.map +1 -1
  42. package/dist/mod.js +1 -0
  43. package/dist/mod.js.map +1 -1
  44. package/dist/reactive.d.ts +9 -9
  45. package/dist/reactive.d.ts.map +1 -1
  46. package/dist/reactive.js +9 -26
  47. package/dist/reactive.js.map +1 -1
  48. package/dist/reactive.test.js +2 -2
  49. package/dist/reactive.test.js.map +1 -1
  50. package/dist/store/StoreRegistry.d.ts +215 -0
  51. package/dist/store/StoreRegistry.d.ts.map +1 -0
  52. package/dist/store/StoreRegistry.js +267 -0
  53. package/dist/store/StoreRegistry.js.map +1 -0
  54. package/dist/store/StoreRegistry.test.d.ts +2 -0
  55. package/dist/store/StoreRegistry.test.d.ts.map +1 -0
  56. package/dist/store/StoreRegistry.test.js +381 -0
  57. package/dist/store/StoreRegistry.test.js.map +1 -0
  58. package/dist/store/create-store.d.ts +56 -6
  59. package/dist/store/create-store.d.ts.map +1 -1
  60. package/dist/store/create-store.js +32 -7
  61. package/dist/store/create-store.js.map +1 -1
  62. package/dist/store/devtools.d.ts +1 -1
  63. package/dist/store/devtools.d.ts.map +1 -1
  64. package/dist/store/devtools.js +16 -3
  65. package/dist/store/devtools.js.map +1 -1
  66. package/dist/store/store-eventstream.test.js +2 -2
  67. package/dist/store/store-eventstream.test.js.map +1 -1
  68. package/dist/store/store-types.d.ts +59 -9
  69. package/dist/store/store-types.d.ts.map +1 -1
  70. package/dist/store/store-types.js.map +1 -1
  71. package/dist/store/store-types.test.js +1 -1
  72. package/dist/store/store-types.test.js.map +1 -1
  73. package/dist/store/store.d.ts +102 -6
  74. package/dist/store/store.d.ts.map +1 -1
  75. package/dist/store/store.js +148 -47
  76. package/dist/store/store.js.map +1 -1
  77. package/dist/utils/dev.js.map +1 -1
  78. package/dist/utils/stack-info.js +2 -2
  79. package/dist/utils/stack-info.js.map +1 -1
  80. package/dist/utils/tests/fixture.d.ts +1 -1
  81. package/dist/utils/tests/fixture.d.ts.map +1 -1
  82. package/dist/utils/tests/fixture.js.map +1 -1
  83. package/dist/utils/tests/otel.d.ts.map +1 -1
  84. package/dist/utils/tests/otel.js +5 -5
  85. package/dist/utils/tests/otel.js.map +1 -1
  86. package/package.json +59 -18
  87. package/src/QueryCache.ts +1 -1
  88. package/src/SqliteDbWrapper.test.ts +4 -2
  89. package/src/SqliteDbWrapper.ts +12 -11
  90. package/src/ambient.d.ts +0 -7
  91. package/src/effect/LiveStore.test.ts +61 -0
  92. package/src/effect/LiveStore.ts +381 -8
  93. package/src/effect/mod.ts +13 -1
  94. package/src/live-queries/__snapshots__/db-query.test.ts.snap +336 -231
  95. package/src/live-queries/base-class.ts +7 -6
  96. package/src/live-queries/client-document-get-query.ts +4 -2
  97. package/src/live-queries/computed.ts +3 -2
  98. package/src/live-queries/db-query.test.ts +3 -2
  99. package/src/live-queries/db-query.ts +15 -15
  100. package/src/live-queries/signal.test.ts +3 -2
  101. package/src/mod.ts +2 -0
  102. package/src/reactive.test.ts +3 -2
  103. package/src/reactive.ts +22 -23
  104. package/src/store/StoreRegistry.test.ts +540 -0
  105. package/src/store/StoreRegistry.ts +418 -0
  106. package/src/store/create-store.ts +76 -15
  107. package/src/store/devtools.ts +20 -6
  108. package/src/store/store-eventstream.test.ts +4 -2
  109. package/src/store/store-types.test.ts +3 -1
  110. package/src/store/store-types.ts +64 -13
  111. package/src/store/store.ts +197 -60
  112. package/src/utils/dev.ts +2 -2
  113. package/src/utils/stack-info.ts +2 -2
  114. package/src/utils/tests/fixture.ts +2 -1
  115. package/src/utils/tests/otel.ts +8 -7
  116. package/docs/api/index.md +0 -3
  117. package/docs/building-with-livestore/complex-ui-state/index.md +0 -5
  118. package/docs/building-with-livestore/crud/index.md +0 -5
  119. package/docs/building-with-livestore/data-modeling/index.md +0 -1
  120. package/docs/building-with-livestore/debugging/index.md +0 -17
  121. package/docs/building-with-livestore/devtools/index.md +0 -79
  122. package/docs/building-with-livestore/events/index.md +0 -355
  123. package/docs/building-with-livestore/examples/ai-agent/index.md +0 -5
  124. package/docs/building-with-livestore/examples/index.md +0 -30
  125. package/docs/building-with-livestore/examples/todo-workspaces/index.md +0 -891
  126. package/docs/building-with-livestore/examples/turnbased-game/index.md +0 -7
  127. package/docs/building-with-livestore/opentelemetry/index.md +0 -208
  128. package/docs/building-with-livestore/production-checklist/index.md +0 -5
  129. package/docs/building-with-livestore/reactivity-system/index.md +0 -202
  130. package/docs/building-with-livestore/rules-for-ai-agents/index.md +0 -9
  131. package/docs/building-with-livestore/state/materializers/index.md +0 -300
  132. package/docs/building-with-livestore/state/sql-queries/index.md +0 -72
  133. package/docs/building-with-livestore/state/sqlite/index.md +0 -45
  134. package/docs/building-with-livestore/state/sqlite-schema/index.md +0 -306
  135. package/docs/building-with-livestore/state/sqlite-schema-effect/index.md +0 -300
  136. package/docs/building-with-livestore/store/index.md +0 -281
  137. package/docs/building-with-livestore/syncing/index.md +0 -136
  138. package/docs/building-with-livestore/tools/cli/index.md +0 -177
  139. package/docs/building-with-livestore/tools/mcp/index.md +0 -187
  140. package/docs/examples/cloudflare-adapter/index.md +0 -44
  141. package/docs/examples/expo-adapter/index.md +0 -44
  142. package/docs/examples/index.md +0 -55
  143. package/docs/examples/node-adapter/index.md +0 -44
  144. package/docs/examples/web-adapter/index.md +0 -52
  145. package/docs/framework-integrations/custom-elements/index.md +0 -142
  146. package/docs/framework-integrations/react-integration/index.md +0 -918
  147. package/docs/framework-integrations/solid-integration/index.md +0 -293
  148. package/docs/framework-integrations/svelte-integration/index.md +0 -42
  149. package/docs/framework-integrations/vue-integration/index.md +0 -294
  150. package/docs/getting-started/expo/index.md +0 -736
  151. package/docs/getting-started/node/index.md +0 -115
  152. package/docs/getting-started/react-web/index.md +0 -573
  153. package/docs/getting-started/solid/index.md +0 -3
  154. package/docs/getting-started/vue/index.md +0 -471
  155. package/docs/index.md +0 -209
  156. package/docs/llms.txt +0 -147
  157. package/docs/misc/CODE_OF_CONDUCT/index.md +0 -133
  158. package/docs/misc/FAQ/index.md +0 -37
  159. package/docs/misc/community/index.md +0 -88
  160. package/docs/misc/credits/index.md +0 -14
  161. package/docs/misc/design-partners/index.md +0 -13
  162. package/docs/misc/package-management/index.md +0 -21
  163. package/docs/misc/performance/index.md +0 -25
  164. package/docs/misc/resources/index.md +0 -46
  165. package/docs/misc/state-of-the-project/index.md +0 -37
  166. package/docs/misc/troubleshooting/index.md +0 -82
  167. package/docs/overview/concepts/index.md +0 -78
  168. package/docs/overview/how-livestore-works/index.md +0 -56
  169. package/docs/overview/introduction/index.md +0 -5
  170. package/docs/overview/technology-comparison/index.md +0 -40
  171. package/docs/overview/when-livestore/index.md +0 -81
  172. package/docs/overview/why-livestore/index.md +0 -5
  173. package/docs/patterns/ai/index.md +0 -15
  174. package/docs/patterns/anonymous-user-transition/index.md +0 -10
  175. package/docs/patterns/app-evolution/index.md +0 -72
  176. package/docs/patterns/auth/index.md +0 -226
  177. package/docs/patterns/effect/index.md +0 -1495
  178. package/docs/patterns/encryption/index.md +0 -6
  179. package/docs/patterns/external-data/index.md +0 -5
  180. package/docs/patterns/file-management/index.md +0 -11
  181. package/docs/patterns/file-structure/index.md +0 -14
  182. package/docs/patterns/list-ordering/index.md +0 -369
  183. package/docs/patterns/offline/index.md +0 -32
  184. package/docs/patterns/orm/index.md +0 -18
  185. package/docs/patterns/presence/index.md +0 -11
  186. package/docs/patterns/rich-text-editing/index.md +0 -11
  187. package/docs/patterns/server-side-clients/index.md +0 -97
  188. package/docs/patterns/side-effects/index.md +0 -11
  189. package/docs/patterns/state-machines/index.md +0 -11
  190. package/docs/patterns/storybook/index.md +0 -192
  191. package/docs/patterns/undo-redo/index.md +0 -9
  192. package/docs/patterns/version-control/index.md +0 -8
  193. package/docs/platform-adapters/cloudflare-durable-object-adapter/index.md +0 -453
  194. package/docs/platform-adapters/electron-adapter/index.md +0 -15
  195. package/docs/platform-adapters/expo-adapter/index.md +0 -245
  196. package/docs/platform-adapters/node-adapter/index.md +0 -160
  197. package/docs/platform-adapters/tauri-adapter/index.md +0 -15
  198. package/docs/platform-adapters/web-adapter/index.md +0 -218
  199. package/docs/sustainable-open-source/contributing/docs/index.md +0 -94
  200. package/docs/sustainable-open-source/contributing/info/index.md +0 -63
  201. package/docs/sustainable-open-source/contributing/monorepo/index.md +0 -195
  202. package/docs/sustainable-open-source/sponsoring/index.md +0 -104
  203. package/docs/sync-providers/cloudflare/index.md +0 -773
  204. package/docs/sync-providers/custom/index.md +0 -65
  205. package/docs/sync-providers/electricsql/index.md +0 -159
  206. package/docs/sync-providers/s2/index.md +0 -230
  207. package/docs/tutorial/0-welcome/index.md +0 -48
  208. package/docs/tutorial/1-setup-starter-project/index.md +0 -105
  209. package/docs/tutorial/2-deploy-to-cloudflare/index.md +0 -195
  210. package/docs/tutorial/3-read-and-write-todos-via-livestore/index.md +0 -511
  211. package/docs/tutorial/4-sync-data-via-cloudflare/index.md +0 -210
  212. package/docs/tutorial/5-expand-business-logic/index.md +0 -174
  213. package/docs/tutorial/6-persist-ui-state/index.md +0 -453
  214. package/docs/tutorial/7-next-steps/index.md +0 -22
  215. package/docs/understanding-livestore/design-decisions/index.md +0 -33
  216. package/docs/understanding-livestore/event-sourcing/index.md +0 -40
@@ -1,511 +0,0 @@
1
- # 3. Read and write todos via LiveStore
2
-
3
- Now on to the fun part! In this section, you'll set up LiveStore so that the todos that you're creating will persist and survive page refreshes and dev server reloads.
4
-
5
- :::note[Help improve LiveStore]
6
- Remember that LiveStore is still early and its developer surface and API not yet fully optimized. Bear with us through the setup and boilerplate code that's needed for a running application—it'll be worth it!
7
-
8
- Also: If you have ideas for improving the developer experience for LiveStore, please [raise an issue](https://github.com/livestorejs/livestore/issues). This project is fully open-source and depends on people like you.
9
- :::
10
-
11
- ## Install LiveStore dependencies
12
-
13
- Start by installing the necessary dependencies:
14
-
15
- <Tabs syncKey="package-manager">
16
-
17
- <TabItem label="bun">
18
-
19
- <Code code={`bun add \\
20
- @livestore/livestore@0.4.0-dev.14 \\
21
- @livestore/wa-sqlite@0.4.0-dev.14 \\
22
- @livestore/adapter-web@0.4.0-dev.14 \\
23
- @livestore/react@0.4.0-dev.14 \\
24
- @livestore/peer-deps@0.4.0-dev.14`} lang="sh" />
25
-
26
- </TabItem>
27
-
28
- <TabItem label="pnpm">
29
-
30
- <Code code={`pnpm add \\
31
- @livestore/livestore@0.4.0-dev.14 \\
32
- @livestore/wa-sqlite@0.4.0-dev.14 \\
33
- @livestore/adapter-web@0.4.0-dev.14 \\
34
- @livestore/react@0.4.0-dev.14 \\
35
- @livestore/peer-deps@0.4.0-dev.14`} lang="sh" />
36
-
37
- </TabItem>
38
-
39
- </Tabs>
40
-
41
- <details>
42
- <summary>Expand to view details about the packages</summary>
43
-
44
- Here's an overview of each of these dependencies:
45
-
46
- - `@livestore/livestore@0.4.0-dev.14` → Implements the core LiveStore functionality (schema, events, queries, ...).
47
- - `@livestore/wa-sqlite@0.4.0-dev.14` → Implements usage of a [SQLite build in WebAssembly](https://github.com/livestorejs/wa-sqlite), so you can use SQLite inside your browser.
48
- - `@livestore/adapter-web@0.4.0-dev.14` → Implements the [LiveStore web adapter.](/platform-adapters/web-adapter)
49
- - `@livestore/react@0.4.0-dev.14` → Provides [LiveStore integration for React](https://github.com/livestorejs/wa-sqlite).
50
- - `@livestore/peer-deps@0.4.0-dev.14` → Required to [satisfy LiveStore peer dependencies](https://dev.docs.livestore.dev/misc/package-management/#peer-dependencies).
51
-
52
- </details>
53
-
54
- ## Define your LiveStore schema
55
-
56
- In this step, you're going to define the [schema](/building-with-livestore/state/sqlite-schema) that LiveStore uses to represent the data of your app. The schema is one of the core concepts of LiveStore.
57
-
58
- Your LiveStore-related files typically live in a `livestore` directory in your app, so create that first:
59
-
60
- ```bash
61
- mkdir src/livestore
62
- touch src/livestore/schema.ts
63
- ```
64
-
65
- The schema contains three major components:
66
-
67
- - The [**table**](/building-with-livestore/state/sqlite-schema#defining-tables) structures of your local SQLite database.
68
- - The [**events**](https://dev.docs.livestore.dev/reference/events/) that can be emitted by your app.
69
- - The [**materializers**](/building-with-livestore/state/materializers) that use the events to alter the state of your database.
70
-
71
- Here's how you define the schema for the current version of the todo app:
72
-
73
- ```ts title="src/livestore/schema.ts"
74
-
75
- export const tables = {
76
- todos: State.SQLite.table({
77
- name: 'todos',
78
- columns: {
79
- id: State.SQLite.integer({ primaryKey: true }),
80
- text: State.SQLite.text({ default: '' }),
81
- },
82
- })
83
- }
84
-
85
- export const events = {
86
- todoCreated: Events.synced({
87
- name: 'v1.TodoCreated',
88
- schema: Schema.Struct({ id: Schema.Number, text: Schema.String }),
89
- }),
90
- todoDeleted: Events.synced({
91
- name: 'v1.TodoDeleted',
92
- schema: Schema.Struct({ id: Schema.Number }),
93
- }),
94
- }
95
-
96
- const materializers = State.SQLite.materializers(events, {
97
- 'v1.TodoCreated': ({ id, text }) => tables.todos.insert({ id, text }),
98
- 'v1.TodoDeleted': ({ id }) => tables.todos.delete().where({ id: id }),
99
- })
100
-
101
- const state = State.SQLite.makeState({ tables, materializers })
102
- export const schema = makeSchema({ events, state })
103
- ```
104
-
105
- Here's a quick summary of the code:
106
- - `tables` contains the definitions of your SQLite table structures. It currently defines a single `todos` table with two columns called `id` and `text`.
107
- - `events` defines the types of events that your app can emit. It currently defines the `todoCreated` and `todoDeleted` events with an attached `schema` property which defines the shape of the event.
108
- - `materializers` are the functions that are invoked for each event. In them, you define what should happen when the event gets fired. Right now, the `v1.TodoCreated` event results in an `insert` operation in the database while `v1.TodoDeleted` will remove a row from the database via a `delete` operation.
109
-
110
- The `tables`, `events` and `materializers` are packaged up into a `schema` object that's needed in the parts of your app where you interact with LiveStore.
111
-
112
- :::note[Versioning events and materializers]
113
- You may have noticed that event and materializer names are prefixed with `v1`.
114
-
115
- It's good practice in LiveStore to version your events and materializers to ensure future compatibility between them as your app evolves.
116
- :::
117
-
118
- ## Configure your React/Vite app to use LiveStore
119
-
120
- To now "connect" LiveStore with your React app, you need to:
121
-
122
- 1. Create the [LiveStore web worker](/platform-adapters/web-adapter#web-worker) that is responsible for the logic of persisting data on your file system.
123
- 2. Create a LiveStore [adapter](/platform-adapters/web-adapter) that enables persistences with local SQLite via [OPFS](https://web.dev/articles/origin-private-file-system).
124
- 3. Wrap your root component in `main.tsx` with the LiveStore [provider](https://dev.docs.livestore.dev/reference/framework-integrations/react-integration/#livestoreprovider) (which will receive both the web worker and adapter from above as arguments).
125
- 4. Update your Vite Config to work with LiveStore.
126
-
127
- Let's get started!
128
-
129
- First, create a new file for the LiveStore web worker:
130
-
131
- ```bash
132
- touch src/livestore/livestore.worker.ts
133
- ```
134
-
135
- Then, add the following code to it:
136
-
137
- ```ts file="src/livestore/livestore.worker.ts"
138
-
139
- makeWorker({ schema })
140
- ```
141
-
142
- This is boilerplate code that you'll almost never need to touch. (In this tutorial, it'll only be edited once when we introduce syncing data to Cloudflare.)
143
-
144
- :::note[Special syntax for importing web worker files in Vite]
145
- When importing this file, make sure to add the `?worker` extension to the import path to ensure that [Vite treats it as a worker file](https://vite.dev/guide/features.html#web-workers).
146
- :::
147
-
148
- Next, update `main.tsx` to create an adapter and wrap your `App` component inside the `LiveStoreProvider`:
149
-
150
- ```tsx title="src/main.tsx"
151
-
152
- const adapter = makePersistedAdapter({
153
- storage: { type: 'opfs' },
154
- worker: LiveStoreWorker,
155
- sharedWorker: LiveStoreSharedWorker,
156
- })
157
-
158
- createRoot(document.getElementById('root')!).render(
159
- <LiveStoreProvider
160
- schema={schema}
161
- adapter={adapter}
162
- renderLoading={(_) => <div>Loading LiveStore ({_.stage})...</div>}
163
- storeId="todo-db-tutorial"
164
- batchUpdates={batchUpdates}
165
- >
166
- <App />
167
- </LiveStoreProvider>
168
- )
169
- ```
170
-
171
- :::note[So many "workers" ...]
172
- There's some ambiguity with the "worker" terminology in this tutorial. In general, there are three different kinds of Workers at play.
173
- - The **LiveStore web worker** (`LiveStoreWorker`); this is the Worker you defined in `livestore.worker.ts`. Technically, this is a _browser_ web worker, i.e. a separate JavaScript thread that runs in the browser, isolated from the main UI thread.
174
- - The **LiveStore shared web worker** (`LiveStoreSharedWorker`); you just imported it from `@livestore/adapter-web/shared-worker`, also a _browser_ web worker. It's actually more of an _implementation detail_ but currently required to be exposed, so that the setup works with Vite.
175
- - The **Cloudflare Worker** that will automatically sync the data in the background; you'll set this up in the next step.
176
-
177
- Note that both the LiveStore web worker and the LiveStore Shared web worker are regular [web workers,](https://www.dhiwise.com/post/web-workers-vs-service-workers-in-javascript#what-are-web-workers-) not [service workers](https://www.dhiwise.com/post/web-workers-vs-service-workers-in-javascript#what-are-service-workers-)!
178
-
179
- Here's how to think about the workers in the context of browser tabs (or windows):
180
-
181
- ```
182
- Tab 1 ──┐
183
- Tab 2 ──┼──→ Shared Worker (tab coordination) ──→ Web Worker (persistence)
184
- Tab 3 ──┘
185
- ```
186
-
187
- :::
188
-
189
- Finally, update your Vite Config to enable usage of WebAssembly:
190
-
191
- ```diff title="vite.config.ts" lang="ts"
192
-
193
- export default defineConfig({
194
- plugins: [
195
- react(),
196
- tailwindcss(),
197
- cloudflare(),
198
- ],
199
- + optimizeDeps: {
200
- + exclude: ['@livestore/wa-sqlite'],
201
- + },
202
- })
203
- ```
204
-
205
- Now, your app is set up to start reading and writing data in a local SQLite database via LiveStore. If you run the app via `pnpm dev`, you'll briefly see a loading screen (implemented via the `renderLoading` prop on the `LiveStoreProvider`) before the todo list UI from the previous steps will appear again:
206
-
207
- ![](../../../assets/tutorial/chapter-3/0-livestore-loading.gif)
208
-
209
- The todos themselves are not yet persisted because you haven't modified the logic for managing state in `App.tsx`. That's what you'll do next.
210
-
211
- ## Read and write todos from local SQLite via LiveStore
212
-
213
- The current version of the app still stores the todos _in-memory_ via the [state](https://react.dev/learn/state-a-components-memory) of your `App` component. However, with the basic LiveStore setup in place, you can now move to persisting your todos inside SQLite via LiveStore!
214
-
215
- In order to read todos, you need to define a LiveStore [query](/building-with-livestore/state/sql-queries). LiveStore queries are _live_ or _reactive_, meaning they will automatically update your UI components when the underlying data changes.
216
-
217
- Here's how you can define and use a query to fetch all todos from the local SQLite database using LiveStore inside the `App` component (you don't need to update your code yet, you'll get the full snippet at the end of this section):
218
-
219
- ```diff title="src/App.tsx" lang="ts"
220
- function App() {
221
-
222
- + const { store } = useStore()
223
-
224
- // The trailing `$` is a convention to indicate that this
225
- // variable is a "live query".
226
- + const todos$ = queryDb(() => tables.todos.select())
227
-
228
- // `todos` is an array containing all the todos from the DB.
229
- // When rendering the component, you can do {todos.map(todo => ...
230
- // and access `todo.text` and `todo.id` as before.
231
- + const todos = store.useQuery(todos$)
232
- - const [todos, setTodos] = useState<Todo[]>([])
233
-
234
- // ... remaining code for the `App` component
235
- }
236
- ```
237
-
238
- With this change, you're now reading the `todos` from LiveStore (where they're persisted) instead of using React's ephemeral state.
239
-
240
- This was only half of the job though: Right now your code would throw a type error because it still uses `setTodos` to update the local state. You need to update this to use the `todoCreated` and `todoDeleted` events you defined in `src/livestore/schema.ts`:
241
-
242
- ```diff title="src/App.tsx" lang="ts"
243
- function App() {
244
-
245
- const { store } = useStore()
246
-
247
- const todos$ = queryDb(() => tables.todos.select())
248
-
249
- const todos = store.useQuery(todos$)
250
-
251
- // Commit an event to the `store` instead of
252
- // updating local React state.
253
- const addTodo = () => {
254
- + store.commit(
255
- + events.todoCreated({ id: Date.now(), text: input }),
256
- + )
257
- setInput('')
258
- }
259
-
260
- // Commit an event to the `store` instead of
261
- // updating local React state.
262
- const deleteTodo = (id: number) => {
263
- + store.commit(
264
- + events.todoDeleted({ id }),
265
- + )
266
- }
267
-
268
- // ... remaining code for the `App` component
269
-
270
- }
271
- ```
272
-
273
- See how the data now will flow through the app _unidirectionally_ with this setup?
274
-
275
- Let's follow it from the first time the app is started:
276
-
277
- 1. The `App` component registers the `todos$` "live query" with the store.
278
- 1. The query fires initially; the returned `todos` array is empty.
279
- 1. The `App` component renders an empty list.
280
- 1. A user adds a `todo`; the `todoCreated` event is triggered and committed to the DB.
281
- 1. The `v1.TodoCreated` materializer is invoked and writes the data into the DB.
282
- 1. The `todos$` query fires again because the state of the DB changed; the returned `todos` array now contains the newly created todo.
283
- 1. The `App` component renders a single todo.
284
-
285
- Now try it out! Here's the full code for `App.tsx` that you can copy and paste:
286
-
287
- ```ts title="src/App.tsx"
288
-
289
- function App() {
290
-
291
- const { store } = useStore()
292
-
293
- const todos$ = queryDb(() => tables.todos.select())
294
- const todos = store.useQuery(todos$)
295
-
296
- const [input, setInput] = useState('')
297
-
298
- const addTodo = () => {
299
- if (input.trim()) {
300
- store.commit(
301
- events.todoCreated({ id: Date.now(), text: input }),
302
- )
303
- setInput('')
304
- }
305
- }
306
-
307
- const deleteTodo = (id: number) => {
308
- store.commit(
309
- events.todoDeleted({ id }),
310
- )
311
- }
312
-
313
- const handleKeyDown = (e: React.KeyboardEvent) => {
314
- if (e.key === 'Enter') {
315
- addTodo()
316
- }
317
- }
318
-
319
- return (
320
- <div className="min-h-screen bg-gray-50 flex items-center justify-center p-6">
321
- <div className="w-full max-w-lg">
322
- <h1 className="text-5xl font-bold text-gray-800 text-center mb-12">
323
- Todo List
324
- </h1>
325
-
326
- <div className="flex gap-3 mb-8">
327
- <input
328
- type="text"
329
- value={input}
330
- onChange={(e) => setInput(e.target.value)}
331
- onKeyDown={handleKeyDown}
332
- placeholder="Enter a todo..."
333
- className="flex-1 px-4 py-2 text-sm border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
334
- />
335
- <button
336
- onClick={addTodo}
337
- className="px-6 py-2 text-sm font-medium text-white bg-blue-500 rounded hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors"
338
- >
339
- Add
340
- </button>
341
- </div>
342
-
343
- <div className="space-y-3">
344
- {todos.map(todo => (
345
- <div
346
- key={todo.id}
347
- className="flex items-center justify-between bg-white px-4 py-3 rounded shadow-sm"
348
- >
349
- <span className="text-gray-700">{todo.text}</span>
350
- <button
351
- onClick={() => deleteTodo(todo.id)}
352
- className="px-4 py-1 text-sm font-medium text-white bg-red-500 rounded hover:bg-red-600 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 transition-colors"
353
- >
354
- Delete
355
- </button>
356
- </div>
357
- ))}
358
- </div>
359
-
360
- {todos.length === 0 && (
361
- <p className="text-center text-gray-400 mt-8">
362
- No todos yet. Add one above!
363
- </p>
364
- )}
365
- </div>
366
- </div>
367
- )
368
- }
369
-
370
- export default App
371
- ```
372
-
373
- Try adding a few todos and then refresh the browser (or restart your development server):
374
-
375
- ![](../../../assets/tutorial/chapter-3/1-livestore-persistence.gif)
376
-
377
- In addition to the persistence, you can now observe the following behaviour:
378
-
379
- - You can open multiple browser tabs/windows, make edits to the todo list and see live updates when you make changes in one of them.
380
- - You can stop and restart the local version of your app and the todos will be persisted.
381
- - If you open the app in an incognito tab or a different browser, the list of todos will be empty again; that's because the _Shared web worker_ only works in the same browser "session"; incognito tabs and different browsers use a different session.
382
-
383
- <details>
384
- <summary>Expand to learn about browser isolation/sessions</summary>
385
-
386
- **Regular browser tab/windows**
387
-
388
- - All regular (i.e. "not incognito") tabs/windows share the same browser session.
389
- - The LiveStore shared web worker is shared across all these tabs/windows.
390
- - OPFS storage is shared across the origin.
391
- - ✅ Real-time sync works because they're all using the same LiveStore shared web worker and storage.
392
-
393
- **Incognito windows**
394
-
395
- - Incognito mode creates a separate session from regular browsing.
396
- - However, multiple incognito tabs/windows opened in the same incognito session still share:
397
- - The same LiveStore shared web worker instance.
398
- - The same OPFS storage (within that incognito session).
399
- - Each incognito session gets isolated storage, but windows within that session are _not_ isolated from each other.
400
-
401
- Think of it this way:
402
-
403
- - Regular tabs/windows = Session A (all tabs/windows share the same data).
404
- - Incognito tabs/windows = Session B (all incognito tabs/windows share the same data).
405
-
406
- **To get true isolation**
407
-
408
- If you want completely isolated instances, you'd need to use:
409
-
410
- - **Different browser profiles** - Chrome profiles are truly isolated.
411
- - **Different browsers** - Chrome vs Firefox vs Safari.
412
- - **Different devices** (only possible with the deployed app) - Your computer vs your phone.
413
-
414
- </details>
415
-
416
- Now, you can also deploy the app:
417
-
418
- <Tabs syncKey="package-manager">
419
-
420
- <TabItem label="bun">
421
-
422
- <Code code={`bun run deploy`} lang="sh" />
423
-
424
- </TabItem>
425
-
426
- <TabItem label="pnpm">
427
-
428
- <Code code={`pnpm run deploy`} lang="sh" />
429
-
430
- </TabItem>
431
-
432
- </Tabs>
433
-
434
- Here's a little GIF demonstrating the current state of the live updates via browser isolation. On the left, we have two regular Chrome windows, on the right, two Safari windows.
435
-
436
- ![](../../../assets/tutorial/chapter-3/3-livestore-sync-local-480.gif)
437
-
438
- - When a change is made in one Chrome window, it is automatically reflected in the other Chrome window.
439
- - Similarly, when a change is made in one Safari window, it is automatically reflected in the other Safari window.
440
-
441
- To recap, here's a visualization of the data flow in the app at this point:
442
-
443
- <LivestoreDataFlowDiagram class="my-8" />
444
-
445
- ## Set up LiveStore DevTools
446
-
447
- You may have wondered: "If the data is persisted, _where_ is it?". If the data is somewhere on your file system, you _should_ be able to spin up a database viewer of your choice and inspect the local SQLite DB file.
448
-
449
- Unfortunately, that's not how OPFS works. While the SQLite files do exist _somewhere_ on your file system, they are hidden in the browser's internals and not easily accessible.
450
-
451
- That being said, there is a convenient way how you can actually see the data on your file system: Using [LiveStore DevTools](/building-with-livestore/devtools)!
452
-
453
- :::note[LiveStore DevTools are a sponsor-only feature]
454
-
455
- Our goal is to grow the project in a [sustainable](/sustainable-open-source/sponsoring#goal-sustainable-open-source) way (e.g. not rely on VC funding), that's why DevTools are currently a [sponsor-only](/sustainable-open-source/sponsoring) feature.
456
-
457
- That being said, we still include them in this tutorial since you'll be able to use them without sponsorship for one week.
458
-
459
- LiveStore can only exist thanks to its generous sponsors, please consider becoming one of them if the project is of value to you. ❤️
460
-
461
- :::
462
-
463
- To start using DevTools, first install the package for the web adapter you're using:
464
-
465
- <Tabs syncKey="package-manager">
466
-
467
- <TabItem label="bun">
468
-
469
- <Code code={`bun add @livestore/devtools-vite@0.4.0-dev.14`} lang="sh" />
470
-
471
- </TabItem>
472
-
473
- <TabItem label="pnpm">
474
-
475
- <Code code={`pnpm add @livestore/devtools-vite@0.4.0-dev.14`} lang="sh" />
476
-
477
- </TabItem>
478
-
479
- </Tabs>
480
-
481
- Next, update your Vite Config to add the `livestoreDevtoolsPlugin`. It should look as follows:
482
-
483
- ```diff title="vite.config.ts" lang="ts"
484
-
485
- +import { livestoreDevtoolsPlugin } from '@livestore/devtools-vite'
486
-
487
- // https://vite.dev/config/
488
- export default defineConfig({
489
- plugins: [
490
- react(),
491
- tailwindcss(),
492
- cloudflare(),
493
- + livestoreDevtoolsPlugin({ schemaPath: './src/livestore/schema.ts' })
494
- ],
495
- optimizeDeps: {
496
- exclude: ['@livestore/wa-sqlite'],
497
- },
498
- })
499
- ```
500
-
501
- Now, in the developer console of your browser, you can find the **LiveStore** tab with details about the current LiveStore internals:
502
-
503
- ![](../../../assets/tutorial/chapter-3/4-livestore-devtools.png)
504
-
505
- There are several tabs in the LiveStore DevTools:
506
-
507
- - **Database**: Browse the data that's currently in your DB, send SQL queries, export the database to your file system, and more.
508
- - **Queries**: Shows the last results of the currently active live queries.
509
- - **Events**: The [event log](/understanding-livestore/event-sourcing) that stores all the events emitted by your app.
510
-
511
- If you're curious, add and delete a few todos via the UI and observe what's happening in the three tabs.