@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.
- package/README.md +0 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/QueryCache.js +1 -1
- package/dist/QueryCache.js.map +1 -1
- package/dist/SqliteDbWrapper.d.ts +5 -5
- package/dist/SqliteDbWrapper.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.js +8 -8
- package/dist/SqliteDbWrapper.js.map +1 -1
- package/dist/SqliteDbWrapper.test.js +2 -2
- package/dist/SqliteDbWrapper.test.js.map +1 -1
- package/dist/effect/LiveStore.d.ts +130 -2
- package/dist/effect/LiveStore.d.ts.map +1 -1
- package/dist/effect/LiveStore.js +185 -6
- package/dist/effect/LiveStore.js.map +1 -1
- package/dist/effect/LiveStore.test.d.ts +2 -0
- package/dist/effect/LiveStore.test.d.ts.map +1 -0
- package/dist/effect/LiveStore.test.js +42 -0
- package/dist/effect/LiveStore.test.js.map +1 -0
- package/dist/effect/mod.d.ts +1 -1
- package/dist/effect/mod.d.ts.map +1 -1
- package/dist/effect/mod.js +3 -1
- package/dist/effect/mod.js.map +1 -1
- package/dist/live-queries/base-class.d.ts +3 -3
- package/dist/live-queries/base-class.d.ts.map +1 -1
- package/dist/live-queries/base-class.js +2 -2
- package/dist/live-queries/base-class.js.map +1 -1
- package/dist/live-queries/client-document-get-query.d.ts +1 -1
- package/dist/live-queries/client-document-get-query.d.ts.map +1 -1
- package/dist/live-queries/client-document-get-query.js +1 -1
- package/dist/live-queries/client-document-get-query.js.map +1 -1
- package/dist/live-queries/computed.d.ts.map +1 -1
- package/dist/live-queries/computed.js +2 -2
- package/dist/live-queries/computed.js.map +1 -1
- package/dist/live-queries/db-query.js +14 -14
- package/dist/live-queries/db-query.js.map +1 -1
- package/dist/live-queries/db-query.test.js +2 -2
- package/dist/live-queries/db-query.test.js.map +1 -1
- package/dist/live-queries/signal.test.js +2 -2
- package/dist/live-queries/signal.test.js.map +1 -1
- package/dist/mod.d.ts +2 -1
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +1 -0
- package/dist/mod.js.map +1 -1
- package/dist/reactive.d.ts +9 -9
- package/dist/reactive.d.ts.map +1 -1
- package/dist/reactive.js +9 -26
- package/dist/reactive.js.map +1 -1
- package/dist/reactive.test.js +2 -2
- package/dist/reactive.test.js.map +1 -1
- package/dist/store/StoreRegistry.d.ts +215 -0
- package/dist/store/StoreRegistry.d.ts.map +1 -0
- package/dist/store/StoreRegistry.js +267 -0
- package/dist/store/StoreRegistry.js.map +1 -0
- package/dist/store/StoreRegistry.test.d.ts +2 -0
- package/dist/store/StoreRegistry.test.d.ts.map +1 -0
- package/dist/store/StoreRegistry.test.js +381 -0
- package/dist/store/StoreRegistry.test.js.map +1 -0
- package/dist/store/create-store.d.ts +56 -6
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/create-store.js +32 -7
- package/dist/store/create-store.js.map +1 -1
- package/dist/store/devtools.d.ts +1 -1
- package/dist/store/devtools.d.ts.map +1 -1
- package/dist/store/devtools.js +16 -3
- package/dist/store/devtools.js.map +1 -1
- package/dist/store/store-eventstream.test.js +2 -2
- package/dist/store/store-eventstream.test.js.map +1 -1
- package/dist/store/store-types.d.ts +59 -9
- package/dist/store/store-types.d.ts.map +1 -1
- package/dist/store/store-types.js.map +1 -1
- package/dist/store/store-types.test.js +1 -1
- package/dist/store/store-types.test.js.map +1 -1
- package/dist/store/store.d.ts +102 -6
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +148 -47
- package/dist/store/store.js.map +1 -1
- package/dist/utils/dev.js.map +1 -1
- package/dist/utils/stack-info.js +2 -2
- package/dist/utils/stack-info.js.map +1 -1
- package/dist/utils/tests/fixture.d.ts +1 -1
- package/dist/utils/tests/fixture.d.ts.map +1 -1
- package/dist/utils/tests/fixture.js.map +1 -1
- package/dist/utils/tests/otel.d.ts.map +1 -1
- package/dist/utils/tests/otel.js +5 -5
- package/dist/utils/tests/otel.js.map +1 -1
- package/package.json +59 -18
- package/src/QueryCache.ts +1 -1
- package/src/SqliteDbWrapper.test.ts +4 -2
- package/src/SqliteDbWrapper.ts +12 -11
- package/src/ambient.d.ts +0 -7
- package/src/effect/LiveStore.test.ts +61 -0
- package/src/effect/LiveStore.ts +381 -8
- package/src/effect/mod.ts +13 -1
- package/src/live-queries/__snapshots__/db-query.test.ts.snap +336 -231
- package/src/live-queries/base-class.ts +7 -6
- package/src/live-queries/client-document-get-query.ts +4 -2
- package/src/live-queries/computed.ts +3 -2
- package/src/live-queries/db-query.test.ts +3 -2
- package/src/live-queries/db-query.ts +15 -15
- package/src/live-queries/signal.test.ts +3 -2
- package/src/mod.ts +2 -0
- package/src/reactive.test.ts +3 -2
- package/src/reactive.ts +22 -23
- package/src/store/StoreRegistry.test.ts +540 -0
- package/src/store/StoreRegistry.ts +418 -0
- package/src/store/create-store.ts +76 -15
- package/src/store/devtools.ts +20 -6
- package/src/store/store-eventstream.test.ts +4 -2
- package/src/store/store-types.test.ts +3 -1
- package/src/store/store-types.ts +64 -13
- package/src/store/store.ts +197 -60
- package/src/utils/dev.ts +2 -2
- package/src/utils/stack-info.ts +2 -2
- package/src/utils/tests/fixture.ts +2 -1
- package/src/utils/tests/otel.ts +8 -7
- package/docs/api/index.md +0 -3
- package/docs/building-with-livestore/complex-ui-state/index.md +0 -5
- package/docs/building-with-livestore/crud/index.md +0 -5
- package/docs/building-with-livestore/data-modeling/index.md +0 -1
- package/docs/building-with-livestore/debugging/index.md +0 -17
- package/docs/building-with-livestore/devtools/index.md +0 -79
- package/docs/building-with-livestore/events/index.md +0 -355
- package/docs/building-with-livestore/examples/ai-agent/index.md +0 -5
- package/docs/building-with-livestore/examples/index.md +0 -30
- package/docs/building-with-livestore/examples/todo-workspaces/index.md +0 -891
- package/docs/building-with-livestore/examples/turnbased-game/index.md +0 -7
- package/docs/building-with-livestore/opentelemetry/index.md +0 -208
- package/docs/building-with-livestore/production-checklist/index.md +0 -5
- package/docs/building-with-livestore/reactivity-system/index.md +0 -202
- package/docs/building-with-livestore/rules-for-ai-agents/index.md +0 -9
- package/docs/building-with-livestore/state/materializers/index.md +0 -300
- package/docs/building-with-livestore/state/sql-queries/index.md +0 -72
- package/docs/building-with-livestore/state/sqlite/index.md +0 -45
- package/docs/building-with-livestore/state/sqlite-schema/index.md +0 -306
- package/docs/building-with-livestore/state/sqlite-schema-effect/index.md +0 -300
- package/docs/building-with-livestore/store/index.md +0 -281
- package/docs/building-with-livestore/syncing/index.md +0 -136
- package/docs/building-with-livestore/tools/cli/index.md +0 -177
- package/docs/building-with-livestore/tools/mcp/index.md +0 -187
- package/docs/examples/cloudflare-adapter/index.md +0 -44
- package/docs/examples/expo-adapter/index.md +0 -44
- package/docs/examples/index.md +0 -55
- package/docs/examples/node-adapter/index.md +0 -44
- package/docs/examples/web-adapter/index.md +0 -52
- package/docs/framework-integrations/custom-elements/index.md +0 -142
- package/docs/framework-integrations/react-integration/index.md +0 -918
- package/docs/framework-integrations/solid-integration/index.md +0 -293
- package/docs/framework-integrations/svelte-integration/index.md +0 -42
- package/docs/framework-integrations/vue-integration/index.md +0 -294
- package/docs/getting-started/expo/index.md +0 -736
- package/docs/getting-started/node/index.md +0 -115
- package/docs/getting-started/react-web/index.md +0 -573
- package/docs/getting-started/solid/index.md +0 -3
- package/docs/getting-started/vue/index.md +0 -471
- package/docs/index.md +0 -209
- package/docs/llms.txt +0 -147
- package/docs/misc/CODE_OF_CONDUCT/index.md +0 -133
- package/docs/misc/FAQ/index.md +0 -37
- package/docs/misc/community/index.md +0 -88
- package/docs/misc/credits/index.md +0 -14
- package/docs/misc/design-partners/index.md +0 -13
- package/docs/misc/package-management/index.md +0 -21
- package/docs/misc/performance/index.md +0 -25
- package/docs/misc/resources/index.md +0 -46
- package/docs/misc/state-of-the-project/index.md +0 -37
- package/docs/misc/troubleshooting/index.md +0 -82
- package/docs/overview/concepts/index.md +0 -78
- package/docs/overview/how-livestore-works/index.md +0 -56
- package/docs/overview/introduction/index.md +0 -5
- package/docs/overview/technology-comparison/index.md +0 -40
- package/docs/overview/when-livestore/index.md +0 -81
- package/docs/overview/why-livestore/index.md +0 -5
- package/docs/patterns/ai/index.md +0 -15
- package/docs/patterns/anonymous-user-transition/index.md +0 -10
- package/docs/patterns/app-evolution/index.md +0 -72
- package/docs/patterns/auth/index.md +0 -226
- package/docs/patterns/effect/index.md +0 -1495
- package/docs/patterns/encryption/index.md +0 -6
- package/docs/patterns/external-data/index.md +0 -5
- package/docs/patterns/file-management/index.md +0 -11
- package/docs/patterns/file-structure/index.md +0 -14
- package/docs/patterns/list-ordering/index.md +0 -369
- package/docs/patterns/offline/index.md +0 -32
- package/docs/patterns/orm/index.md +0 -18
- package/docs/patterns/presence/index.md +0 -11
- package/docs/patterns/rich-text-editing/index.md +0 -11
- package/docs/patterns/server-side-clients/index.md +0 -97
- package/docs/patterns/side-effects/index.md +0 -11
- package/docs/patterns/state-machines/index.md +0 -11
- package/docs/patterns/storybook/index.md +0 -192
- package/docs/patterns/undo-redo/index.md +0 -9
- package/docs/patterns/version-control/index.md +0 -8
- package/docs/platform-adapters/cloudflare-durable-object-adapter/index.md +0 -453
- package/docs/platform-adapters/electron-adapter/index.md +0 -15
- package/docs/platform-adapters/expo-adapter/index.md +0 -245
- package/docs/platform-adapters/node-adapter/index.md +0 -160
- package/docs/platform-adapters/tauri-adapter/index.md +0 -15
- package/docs/platform-adapters/web-adapter/index.md +0 -218
- package/docs/sustainable-open-source/contributing/docs/index.md +0 -94
- package/docs/sustainable-open-source/contributing/info/index.md +0 -63
- package/docs/sustainable-open-source/contributing/monorepo/index.md +0 -195
- package/docs/sustainable-open-source/sponsoring/index.md +0 -104
- package/docs/sync-providers/cloudflare/index.md +0 -773
- package/docs/sync-providers/custom/index.md +0 -65
- package/docs/sync-providers/electricsql/index.md +0 -159
- package/docs/sync-providers/s2/index.md +0 -230
- package/docs/tutorial/0-welcome/index.md +0 -48
- package/docs/tutorial/1-setup-starter-project/index.md +0 -105
- package/docs/tutorial/2-deploy-to-cloudflare/index.md +0 -195
- package/docs/tutorial/3-read-and-write-todos-via-livestore/index.md +0 -511
- package/docs/tutorial/4-sync-data-via-cloudflare/index.md +0 -210
- package/docs/tutorial/5-expand-business-logic/index.md +0 -174
- package/docs/tutorial/6-persist-ui-state/index.md +0 -453
- package/docs/tutorial/7-next-steps/index.md +0 -22
- package/docs/understanding-livestore/design-decisions/index.md +0 -33
- package/docs/understanding-livestore/event-sourcing/index.md +0 -40
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
# Concepts
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
- Adapter (platform adapter)
|
|
8
|
-
- An adapter can instantiate a client session for a given platform (e.g. web, Expo)
|
|
9
|
-
- Client
|
|
10
|
-
- A logical group of client sessions
|
|
11
|
-
- Identified by a `clientId` - a randomly generated 6-char nanoid
|
|
12
|
-
- Each client has at least one client session
|
|
13
|
-
- Sessions within a client share local data
|
|
14
|
-
- Client session
|
|
15
|
-
- An instance within a client
|
|
16
|
-
- Identified by a `sessionId`
|
|
17
|
-
- In web: sessionId can persist across tab reloads
|
|
18
|
-
- Multiple sessions can exist within a single client (e.g., multiple browser tabs)
|
|
19
|
-
- Store
|
|
20
|
-
- Reactivity graph
|
|
21
|
-
- [Devtools](/building-with-livestore/devtools)
|
|
22
|
-
- [Events](/building-with-livestore/events)
|
|
23
|
-
- Event definition
|
|
24
|
-
- Eventlog
|
|
25
|
-
- Synced vs client-only events
|
|
26
|
-
- Framework integration
|
|
27
|
-
- A framework integration is a package that provides a way to integrate LiveStore with a framework (e.g. React, Solid, Svelte, etc.)
|
|
28
|
-
- [Reactivity system](/building-with-livestore/reactivity-system)
|
|
29
|
-
- Db queries `queryDb()`
|
|
30
|
-
- Computed queries `computed()`
|
|
31
|
-
- Signals `signal()`
|
|
32
|
-
- Schema
|
|
33
|
-
- LiveStore uses schema definitions for the following cases:
|
|
34
|
-
- [Event definitions](/building-with-livestore/events)
|
|
35
|
-
- [SQLite state schema](/building-with-livestore/state/sqlite-schema)
|
|
36
|
-
- [Query result schemas](/building-with-livestore/state/sql-queries)
|
|
37
|
-
- LiveStore uses the [Effect Schema module](/patterns/effect) to define fine-granular schemas
|
|
38
|
-
- State
|
|
39
|
-
- Derived from the eventlog via materializers
|
|
40
|
-
- Materializer
|
|
41
|
-
- Event handler function that maps an event to a state change
|
|
42
|
-
- SQLite state / database
|
|
43
|
-
- In-memory SQLite database within the client session thread (usually main thread)
|
|
44
|
-
- Used by the reactivity graph
|
|
45
|
-
- Persisted SQLite database (usually running on the leader thread)
|
|
46
|
-
- Fully derived from the eventlog
|
|
47
|
-
- [Store](/building-with-livestore/store)
|
|
48
|
-
- A store exposes most of LiveStore's functionality to the application layer and is the main entry point for using LiveStore.
|
|
49
|
-
- To create a store you need to provide a schema and a platform adapter which creates a client session.
|
|
50
|
-
- A store is often created, managed and accessed through a framework integration (like React).
|
|
51
|
-
- A store is identified by a `storeId` which is also used for syncing events between clients.
|
|
52
|
-
- Sync provider
|
|
53
|
-
- A sync provider is a package that provides a sync backend and a sync client.
|
|
54
|
-
- Sync backend
|
|
55
|
-
- A central server that is responsible for syncing the eventlog between clients
|
|
56
|
-
|
|
57
|
-
### Implementation details
|
|
58
|
-
|
|
59
|
-
- Leader thread
|
|
60
|
-
- Responsible for syncing and persisting of data
|
|
61
|
-
- Sync processor
|
|
62
|
-
- LeaderSyncProcessor
|
|
63
|
-
- ClientSessionSyncProcessor
|
|
64
|
-
|
|
65
|
-
## Pluggable architecture
|
|
66
|
-
|
|
67
|
-
LiveStore is designed to be pluggable in various ways:
|
|
68
|
-
|
|
69
|
-
- Platform adapters
|
|
70
|
-
- Sync providers
|
|
71
|
-
- Framework integrations
|
|
72
|
-
|
|
73
|
-
## Important notes on identity
|
|
74
|
-
|
|
75
|
-
- LiveStore does not have built-in concepts of "users" or "devices"
|
|
76
|
-
- User identity must be modeled within your application domain through events and application logic
|
|
77
|
-
- The `clientId` identifies a client instance, not a user
|
|
78
|
-
- Multiple clients can represent the same user (e.g., different browsers or devices)
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
# How LiveStore works
|
|
2
|
-
|
|
3
|
-
### TLDR
|
|
4
|
-
|
|
5
|
-
LiveStore uses event sourcing to sync events across clients and materialize state into a local, reactive SQLite database.
|
|
6
|
-
|
|
7
|
-
<HowItWorksDiagram class="my-8" />
|
|
8
|
-
|
|
9
|
-
## How LiveStore works client-side
|
|
10
|
-
|
|
11
|
-
On the client, LiveStore provides a reactive SQLite database for application state, which is kept consistent through an underlying event sourcing mechanism.
|
|
12
|
-
|
|
13
|
-
#### Local reactive SQLite
|
|
14
|
-
|
|
15
|
-
Application state is materialized into a local SQLite database, offering high-performance, offline-capable data access. This SQLite database is reactive: UI components subscribe to data changes and update automatically when the state changes. LiveStore uses in-memory SQLite for sub-millisecond queries and persistent SQLite for durable storage across application sessions.
|
|
16
|
-
|
|
17
|
-
#### Event Sourcing
|
|
18
|
-
|
|
19
|
-
Underpinning the reactive state, LiveStore implements the event sourcing pattern. All data modifications are captured as an immutable, ordered sequence of events. This eventlog serves as the canonical history, enabling reliable state reconstruction and providing inherent auditability, which aids in debugging. The reactive SQLite state is a projection of this eventlog.
|
|
20
|
-
|
|
21
|
-
#### Client-side event flow
|
|
22
|
-
|
|
23
|
-
1. **Event Committing:** User interactions within the application generate events detailing the specific action (e.g., `TodoCreated`, `TaskCompleted`).
|
|
24
|
-
2. **Local Persistence & Materialization:** The committed event is atomically persisted to the local eventlog and immediately materialized as state into the SQLite database.
|
|
25
|
-
3. **UI Reactivity:** Changes to the SQLite database trigger the reactivity system, causing subscribed UI components (e.g. React components) to automatically update and reflect the new state.
|
|
26
|
-
|
|
27
|
-
## How LiveStore syncing works
|
|
28
|
-
|
|
29
|
-
LiveStore extends its local event-sourcing model globally by synchronizing events across all clients, typically through a central sync backend. This ensures that the eventlog, serving as the single source of truth, is consistently replicated, leading to an eventually consistent state for all participants.
|
|
30
|
-
|
|
31
|
-
#### Push/pull event synchronization
|
|
32
|
-
|
|
33
|
-
Inspired by Git, LiveStore employs a push/pull model for event synchronization. Clients must first pull the latest events from the sync backend to ensure their local eventlog is up-to-date before they can push their own newly committed local events. This model helps maintain a global total order of events. Local pending events that haven't been pushed are rebased on top of the latest upstream events before being pushed.
|
|
34
|
-
|
|
35
|
-
#### Sync provider integration
|
|
36
|
-
|
|
37
|
-
LiveStore supports various sync backend implementations, and it's straightforward for developers to create their own. The sync backend is responsible for storing events, enforcing the total event order, and notifying clients of new events.
|
|
38
|
-
|
|
39
|
-
#### Conflict resolution
|
|
40
|
-
|
|
41
|
-
When concurrent operations from different clients lead to conflicting events, LiveStore defaults to a "last-write-wins" strategy. However, it also provides the capability for developers to implement custom merge conflict resolution logic tailored to their application's specific needs.
|
|
42
|
-
|
|
43
|
-
#### Overall syncing data flow
|
|
44
|
-
|
|
45
|
-
After a local event is committed and materialized (as per the client-side flow), LiveStore attempts to push this event to the sync backend. Simultaneously, LiveStore is pulling events from the sync backend in the background.
|
|
46
|
-
|
|
47
|
-
Two main scenarios can occur during a push attempt:
|
|
48
|
-
|
|
49
|
-
1. **Client In Sync:** If the client's local eventlog is already up-to-date with the sync backend (i.e., no new remote events have arrived since the last pull/push), the local event is pushed directly.
|
|
50
|
-
2. **Concurrent Incoming Events:** If new remote events have been pulled in the background, or are discovered during the push attempt, the client first processes these incoming remote events. Any local, unpushed events are then rebased on top of these new remote events before being pushed to the sync backend.
|
|
51
|
-
|
|
52
|
-
In both scenarios, once remote events are received (either through background pulling or during a push cycle), they are persisted to the local eventlog, materialized into the local SQLite database, and the UI reacts to the new state, ensuring eventual consistency.
|
|
53
|
-
|
|
54
|
-
## Platform adapters
|
|
55
|
-
|
|
56
|
-
LiveStore includes platform adapters to integrate with various environments, such as web browsers, mobile applications (iOS/Android), desktop applications, and Node.js.
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# Technology comparison
|
|
2
|
-
|
|
3
|
-
## TLDR of what sets LiveStore apart
|
|
4
|
-
|
|
5
|
-
- Uses combination of reactive, in-memory + synced, persisted SQLite for instant, synchronous queries
|
|
6
|
-
- Based on event-sourcing methodologies
|
|
7
|
-
- Client-centric (with great devtools)
|
|
8
|
-
|
|
9
|
-
## Other local-first/syncing technologies
|
|
10
|
-
|
|
11
|
-
To compare LiveStore with other local-first/syncing technologies, please see the [Local-First Landscape](https://www.localfirst.fm/landscape) resource.
|
|
12
|
-
|
|
13
|
-
## LiveStore vs Redux
|
|
14
|
-
|
|
15
|
-
LiveStore shares a lot of similarities with Redux in that sense that both are based on event-sourcing methodologies. Let's compare some of the core concepts:
|
|
16
|
-
|
|
17
|
-
- Redux actions are similar to LiveStore events: Both are used to describe "things that have happened"
|
|
18
|
-
- Redux views are similar to LiveStore's state (e.g. SQLite tables): Both are derived from the history of events/actions.
|
|
19
|
-
- A major difference here is that LiveStore's state materialized as a SQLite database allows for a lot more flexibility via dynamic queries and aggregations vs Redux's static views.
|
|
20
|
-
- Redux reducers are similar to LiveStore's materializers: Both are used to transform events/actions into a final state.
|
|
21
|
-
- Both Redux and LiveStore are client-centric.
|
|
22
|
-
- Both Redux and LiveStore provide powerful [devtools](/building-with-livestore/devtools).
|
|
23
|
-
|
|
24
|
-
While LiveStore can be used for the same use cases as Redux, LiveStore goes far beyond Redux in the following ways:
|
|
25
|
-
|
|
26
|
-
- LiveStore leverages SQLite for a more powerful state model allowing for flexible queries and aggregations with much simpler materialization logic.
|
|
27
|
-
- LiveStore supports client-persistence out of the box.
|
|
28
|
-
- LiveStore comes with a built-in [sync engine](/building-with-livestore/syncing) syncing events between clients.
|
|
29
|
-
|
|
30
|
-
As a downside compared to Redux, LiveStore has a slightly larger bundle size.
|
|
31
|
-
|
|
32
|
-
## Other state management libraries
|
|
33
|
-
|
|
34
|
-
- Zustand
|
|
35
|
-
- Redux Toolkit (RTK)
|
|
36
|
-
- MobX
|
|
37
|
-
- Jotai
|
|
38
|
-
- Xstate
|
|
39
|
-
- Recoil
|
|
40
|
-
- TanStack Query
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
# When to use LiveStore (and when not)
|
|
2
|
-
|
|
3
|
-
Choosing a data layer for a local-first app is a big decision and should be considered carefully. On a high level, LiveStore can be a good fit if ...
|
|
4
|
-
- you are looking for a principled data layer that works across platforms
|
|
5
|
-
- you want to use SQLite for your queries
|
|
6
|
-
- you like [event sourcing](/understanding-livestore/event-sourcing) to model data changes
|
|
7
|
-
- you are working on a new app as LiveStore doesn't yet provide a way to [re-use an existing database](/misc/faq#existing-database)
|
|
8
|
-
- the current [state of the project](/misc/state-of-the-project) aligns with your own timeline and requirements
|
|
9
|
-
|
|
10
|
-
## Evaluation exercise
|
|
11
|
-
|
|
12
|
-
A great way to evaluate whether LiveStore is a good fit for your application, is by trying to model your application events (and optionally state) schema. This exercise can be done in a few minutes and can give you a good indication of whether LiveStore is a good fit for your application.
|
|
13
|
-
|
|
14
|
-
### Example: Calendar/scheduling app
|
|
15
|
-
|
|
16
|
-
Let's say you are building a calendar/scheduling app, your events might include:
|
|
17
|
-
|
|
18
|
-
- `AppointmentScheduled`
|
|
19
|
-
- `AppointmentRescheduled`
|
|
20
|
-
- `AppointmentCancelled`
|
|
21
|
-
- `ParticipantInvitedToAppointment`
|
|
22
|
-
- `ParticipantRespondedToInvite`
|
|
23
|
-
|
|
24
|
-
From this you might want to derive the following state (modeled as SQLite tables):
|
|
25
|
-
|
|
26
|
-
- `Appointment`
|
|
27
|
-
- `id`
|
|
28
|
-
- `title`
|
|
29
|
-
- `description`
|
|
30
|
-
- `participants`
|
|
31
|
-
- `Participant`
|
|
32
|
-
- `id`
|
|
33
|
-
- `name`
|
|
34
|
-
- `email`
|
|
35
|
-
|
|
36
|
-
## Great use cases for LiveStore
|
|
37
|
-
|
|
38
|
-
- High-performance desktop/web/mobile apps
|
|
39
|
-
- e.g. productivity apps like
|
|
40
|
-
- AI agents
|
|
41
|
-
- Apps that need ...
|
|
42
|
-
- solid offline support
|
|
43
|
-
- audit logs
|
|
44
|
-
|
|
45
|
-
## Benefits of LiveStore
|
|
46
|
-
|
|
47
|
-
- Unified data layer combining local reactive state with globally synced data
|
|
48
|
-
- Easy to ...
|
|
49
|
-
- reason about
|
|
50
|
-
- debug
|
|
51
|
-
- test
|
|
52
|
-
- evolve
|
|
53
|
-
- operate
|
|
54
|
-
|
|
55
|
-
## Reasons when not to use LiveStore
|
|
56
|
-
|
|
57
|
-
- You have an existing database which is the source of truth of your data. (Better use [Zero](https://zero.rocicorp.dev) or [ElectricSQL](https://www.electricsql.com) for this.)
|
|
58
|
-
- Your app data is highly connected across users (like a social network / marketplace / etc.) or modeling your data via read-write model separation/event sourcing doesn't seem feasible.
|
|
59
|
-
- You want to build a more traditional client-server application with your primary data source being a remote server.
|
|
60
|
-
- You want a full-stack batteries-included solution (e.g. auth, storage, etc.). (Technologies like [Jazz](https://jazz.tools) or [Instant](https://instantdb.com) might be a better fit.)
|
|
61
|
-
- You don't like to model your data via read-write model separation/event sourcing or the trade-offs it involves.
|
|
62
|
-
- You're a new developer and are just getting started. LiveStore is a relatively advanced technology with many design trade-offs that might make most sense after you have already experienced some of the problems LiveStore is trying to solve.
|
|
63
|
-
- You want to keep your app bundle size as small as possible. LiveStore adds a few hundred kB to your app bundle size (mostly due to bundling SQLite).
|
|
64
|
-
|
|
65
|
-
## Considerations
|
|
66
|
-
|
|
67
|
-
### Database constraints
|
|
68
|
-
|
|
69
|
-
- All the client app data should fit into a in-memory SQLite database
|
|
70
|
-
- Depending on the target device having databases up to 1GB in size should be okay.
|
|
71
|
-
- If you you have more data, you can consider segmenting your database into multiple SQLite database (e.g. segmented per project, workspace, document, ...).
|
|
72
|
-
- You can either use the `storeId` option for the segmentation or there could also be a way to use the [SQLite attach feature](https://www.sqlite.org/lang_attach.html) to dynamically attach/detach databases.
|
|
73
|
-
|
|
74
|
-
### Syncing
|
|
75
|
-
|
|
76
|
-
LiveStore's syncing system is designed for small/medium-level concurrency scenarios (e.g. 10s / low 100s of users collaborating on the same thing for a given eventlog).
|
|
77
|
-
- Collaboration on multiple different eventlogs concurrently is supported and should be used to "scale horizontally".
|
|
78
|
-
|
|
79
|
-
### Other considerations
|
|
80
|
-
|
|
81
|
-
- How data flows / what's the source of truth?
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# AI
|
|
2
|
-
|
|
3
|
-
- LiveStore is a great fit for building AI applications.
|
|
4
|
-
- Scenarios:
|
|
5
|
-
- Local RAG (via sqlite-vec (see [feature request](https://github.com/livestorejs/livestore/issues/127)) + local LLM e.g. Gemini Nano embedded in Chrome)
|
|
6
|
-
- Agentic applications
|
|
7
|
-
|
|
8
|
-
- Event <> tool calls
|
|
9
|
-
- Nice mapping
|
|
10
|
-
|
|
11
|
-
## Example
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
// TODO (contribution welcome)
|
|
15
|
-
```
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# Anonymous user transition
|
|
2
|
-
|
|
3
|
-
## Basic idea
|
|
4
|
-
|
|
5
|
-
- Locally choose a unique identifier for the user (e.g. via `crypto.randomUUID()`).
|
|
6
|
-
- You might want to handle the very unlikely case that the identifier is not unique (collision) on the sync backend.
|
|
7
|
-
- Persist this identifier locally (either via a separate LiveStore instance or via `localStorage`).
|
|
8
|
-
- Use this identifier in the `storeId` for the user-related LiveStore instance.
|
|
9
|
-
- Initially when the user is anonymous, the store won't be synced yet (i.e. no sync backend used in adapter).
|
|
10
|
-
- As part of the auth flow, the LiveStore instance is now synced with the same `storeId` to a sync backend which will sync all local events to the sync backend making sure the user keeps all their data.
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# App evolution
|
|
2
|
-
|
|
3
|
-
When building an app with LiveStore, you'll need to keep some things in mind when evolving your app.
|
|
4
|
-
|
|
5
|
-
## Schema changes
|
|
6
|
-
|
|
7
|
-
### State schema changes
|
|
8
|
-
|
|
9
|
-
Generally any kind of changes to your state schema (e.g. SQLite tables, ...) can be done at any time without any further considerations assuming the event materializer is updated to support the new schema.
|
|
10
|
-
|
|
11
|
-
### Event schema changes
|
|
12
|
-
|
|
13
|
-
Event schema changes require a bit more consideration. Changes to the event schema should generally be done in a backwards-compatible way. See [Event schema evolution](/building-with-livestore/events#schema-evolution) for more details.
|
|
14
|
-
|
|
15
|
-
## Parallel different app versions
|
|
16
|
-
|
|
17
|
-
In scenarios where you have multiple app versions rolled out in parallel (e.g. app version v3 with event schema v3 and app version v4 with event schema v4), you'll need to keep the following in mind:
|
|
18
|
-
|
|
19
|
-
App instances running version 4 might commit events that are not yet supported by version 3. Your app needs to decide how to handle this scenario in one of the following ways:
|
|
20
|
-
|
|
21
|
-
- Ignore unknown events
|
|
22
|
-
- Cause an error in the app for unknown events
|
|
23
|
-
- Handle events with a "catch all" event handler
|
|
24
|
-
- Let app render a "app update required" screen. App can still be used in read-only mode.
|
|
25
|
-
- ...
|
|
26
|
-
|
|
27
|
-
LiveStore exposes a dedicated `unknownEventHandling` configuration on `makeSchema` so you can codify the desired behaviour instead of sprinkling ad-hoc checks across your app. The default is `'warn'`, which logs every unknown event and keeps processing.
|
|
28
|
-
|
|
29
|
-
## `reference/events/unknown-event-handling.ts`
|
|
30
|
-
|
|
31
|
-
```ts filename="reference/events/unknown-event-handling.ts"
|
|
32
|
-
|
|
33
|
-
const tables = {
|
|
34
|
-
todos: State.SQLite.table({
|
|
35
|
-
name: 'todos',
|
|
36
|
-
columns: {
|
|
37
|
-
id: State.SQLite.text({ primaryKey: true }),
|
|
38
|
-
text: State.SQLite.text(),
|
|
39
|
-
},
|
|
40
|
-
}),
|
|
41
|
-
} as const
|
|
42
|
-
|
|
43
|
-
const events = {
|
|
44
|
-
todoCreated: Events.synced({
|
|
45
|
-
name: 'v1.TodoCreated',
|
|
46
|
-
schema: Schema.Struct({ id: Schema.String, text: Schema.String }),
|
|
47
|
-
}),
|
|
48
|
-
} as const
|
|
49
|
-
|
|
50
|
-
const materializers = State.SQLite.materializers(events, {
|
|
51
|
-
[events.todoCreated.name]: defineMaterializer(events.todoCreated, ({ id, text }) =>
|
|
52
|
-
tables.todos.insert({ id, text }),
|
|
53
|
-
),
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
const state = State.SQLite.makeState({ tables, materializers })
|
|
57
|
-
|
|
58
|
-
// ---cut---
|
|
59
|
-
|
|
60
|
-
const _schema = makeSchema({
|
|
61
|
-
events,
|
|
62
|
-
state,
|
|
63
|
-
unknownEventHandling: {
|
|
64
|
-
strategy: 'callback',
|
|
65
|
-
onUnknownEvent: (event, error) => {
|
|
66
|
-
console.warn('LiveStore saw an unknown event', { event, reason: error.reason })
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
})
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Set the strategy to `'ignore'` to silently skip forward-only events, `'fail'` to stop immediately (useful during development), or `'callback'` to forward them to custom telemetry while continuing to replay the log.
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
# Auth
|
|
2
|
-
|
|
3
|
-
LiveStore doesn't include built-in authentication or authorization support, but you can implement it in your app's logic.
|
|
4
|
-
|
|
5
|
-
## Pass an auth payload to the sync backend
|
|
6
|
-
|
|
7
|
-
Use the `syncPayload` store option to send a custom payload to your sync backend.
|
|
8
|
-
|
|
9
|
-
### Example
|
|
10
|
-
|
|
11
|
-
The following example sends the authenticated user's JWT to the server.
|
|
12
|
-
|
|
13
|
-
## `patterns/auth/live-store-provider.tsx`
|
|
14
|
-
|
|
15
|
-
```tsx filename="patterns/auth/live-store-provider.tsx"
|
|
16
|
-
|
|
17
|
-
const schema = {} as Parameters<typeof LiveStoreProvider>[0]['schema']
|
|
18
|
-
const storeId = 'demo-store'
|
|
19
|
-
const user = { jwt: 'user-token' }
|
|
20
|
-
const children: ReactNode = null
|
|
21
|
-
const adapter = makeInMemoryAdapter()
|
|
22
|
-
|
|
23
|
-
// ---cut---
|
|
24
|
-
export const AuthenticatedProvider = () => (
|
|
25
|
-
<LiveStoreProvider
|
|
26
|
-
schema={schema}
|
|
27
|
-
storeId={storeId}
|
|
28
|
-
adapter={adapter}
|
|
29
|
-
batchUpdates={batchUpdates}
|
|
30
|
-
syncPayload={{
|
|
31
|
-
authToken: user.jwt, // Using a JWT
|
|
32
|
-
}}
|
|
33
|
-
>
|
|
34
|
-
{/* ... */}
|
|
35
|
-
{children}
|
|
36
|
-
</LiveStoreProvider>
|
|
37
|
-
)
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
On the sync server, validate the token and allow or reject the sync based on the result. See the following example:
|
|
41
|
-
|
|
42
|
-
## `patterns/auth/pass-auth-payload.ts`
|
|
43
|
-
|
|
44
|
-
```ts filename="patterns/auth/pass-auth-payload.ts"
|
|
45
|
-
|
|
46
|
-
const JWT_SECRET = 'a-string-secret-at-least-256-bits-long'
|
|
47
|
-
|
|
48
|
-
export class SyncBackendDO extends makeDurableObject({
|
|
49
|
-
onPush: async (message) => {
|
|
50
|
-
console.log('onPush', message.batch)
|
|
51
|
-
},
|
|
52
|
-
onPull: async (message) => {
|
|
53
|
-
console.log('onPull', message)
|
|
54
|
-
},
|
|
55
|
-
}) {}
|
|
56
|
-
|
|
57
|
-
export default makeWorker({
|
|
58
|
-
syncBackendBinding: 'SYNC_BACKEND_DO',
|
|
59
|
-
validatePayload: async (payload: any, context) => {
|
|
60
|
-
const { storeId } = context
|
|
61
|
-
const { authToken } = payload
|
|
62
|
-
|
|
63
|
-
if (!authToken) {
|
|
64
|
-
throw new Error('No auth token provided')
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const user = await getUserFromToken(authToken)
|
|
68
|
-
|
|
69
|
-
if (!user) {
|
|
70
|
-
throw new Error('Invalid auth token')
|
|
71
|
-
} else {
|
|
72
|
-
// User is authenticated!
|
|
73
|
-
console.log('Sync backend payload', JSON.stringify(user, null, 2))
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Check if token is expired
|
|
77
|
-
if (payload.exp && payload.exp < Date.now() / 1000) {
|
|
78
|
-
throw new Error('Token expired')
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
await checkUserAccess(user, storeId)
|
|
82
|
-
},
|
|
83
|
-
enableCORS: true,
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
async function getUserFromToken(token: string): Promise<jose.JWTPayload | undefined> {
|
|
87
|
-
try {
|
|
88
|
-
const { payload } = await jose.jwtVerify(token, new TextEncoder().encode(JWT_SECRET))
|
|
89
|
-
return payload
|
|
90
|
-
} catch (error) {
|
|
91
|
-
console.log('⚠️ Error verifying token', error)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async function checkUserAccess(payload: jose.JWTPayload, storeId: string): Promise<void> {
|
|
96
|
-
// Check if user is authorized to access the store
|
|
97
|
-
console.log('Checking access for store', storeId, 'with payload', payload)
|
|
98
|
-
}
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
The above example uses [`jose`](https://www.npmjs.com/package/jose), a popular JavaScript module that supports JWTs. It works across various runtimes, including Node.js, Cloudflare Workers, Deno, Bun, and others.
|
|
102
|
-
|
|
103
|
-
The `validatePayload` function receives the `authToken`, checks if the payload exists, and verifies that it's valid and hasn't expired. If all checks pass, sync continues as normal. If any check fails, the server rejects the sync.
|
|
104
|
-
|
|
105
|
-
The client app still works as expected, but saves data locally. If the user re-authenticates or refreshes the token later, LiveStore syncs any local changes made while the user was unauthenticated.
|
|
106
|
-
|
|
107
|
-
## Re-validate payload inside the Durable Object
|
|
108
|
-
|
|
109
|
-
When you rely on `syncPayload`, treat it as untrusted input. Decode the token inside `validatePayload` to gate the connection, and then repeat the same verification inside the Durable Object before trusting per-push metadata.
|
|
110
|
-
|
|
111
|
-
## `patterns/auth/keep-payload-canonical.ts`
|
|
112
|
-
|
|
113
|
-
```ts filename="patterns/auth/keep-payload-canonical.ts"
|
|
114
|
-
|
|
115
|
-
// ---cut---
|
|
116
|
-
type SyncPayload = { authToken?: string; userId?: string }
|
|
117
|
-
|
|
118
|
-
type AuthorizedSession = {
|
|
119
|
-
authToken: string
|
|
120
|
-
userId: string
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const ensureAuthorized = (payload: unknown): AuthorizedSession => {
|
|
124
|
-
if (payload === undefined || payload === null || typeof payload !== 'object') {
|
|
125
|
-
throw new Error('Missing auth payload')
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const { authToken, userId } = payload as SyncPayload
|
|
129
|
-
if (!authToken) {
|
|
130
|
-
throw new Error('Missing auth token')
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const claims = verifyJwt(authToken)
|
|
134
|
-
if (!claims.sub) {
|
|
135
|
-
throw new Error('Token missing subject claim')
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (userId !== undefined && userId !== claims.sub) {
|
|
139
|
-
throw new Error('Payload userId mismatch')
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return { authToken, userId: claims.sub }
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export default makeWorker({
|
|
146
|
-
syncBackendBinding: 'SYNC_BACKEND_DO',
|
|
147
|
-
validatePayload: (payload) => {
|
|
148
|
-
ensureAuthorized(payload)
|
|
149
|
-
},
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
export class SyncBackendDO extends makeDurableObject({
|
|
153
|
-
onPush: async (message: SyncMessage.PushRequest, { payload }) => {
|
|
154
|
-
const { userId } = ensureAuthorized(payload)
|
|
155
|
-
await ensureTenantAccess(userId, message.batch)
|
|
156
|
-
},
|
|
157
|
-
}) {}
|
|
158
|
-
|
|
159
|
-
const ensureTenantAccess = async (_userId: string, _batch: SyncMessage.PushRequest['batch']) => {
|
|
160
|
-
// Replace with your application-specific access checks.
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### `patterns/auth/verify-jwt.ts`
|
|
165
|
-
|
|
166
|
-
```ts filename="patterns/auth/verify-jwt.ts"
|
|
167
|
-
export type Claims = {
|
|
168
|
-
sub?: string
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export const verifyJwt = (token: string): Claims => {
|
|
172
|
-
if (token.length === 0) {
|
|
173
|
-
throw new Error('Missing token')
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Replace with real JWT verification (e.g. via `jose`)
|
|
177
|
-
return { sub: token }
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
- `validatePayload` runs once per connection and rejects mismatched tokens before LiveStore upgrades to WebSocket.
|
|
182
|
-
- `onPush` (and `onPull`, if you need it) must repeat the verification because the payload forwarded to the Durable Object is the original client input.
|
|
183
|
-
- The HTTP transport does not forward payloads today; embed the necessary authorization context directly in the events or move those clients to WebSocket/DO-RPC if you must rely on shared payload metadata.
|
|
184
|
-
|
|
185
|
-
You can extend `ensureAuthorized` to project additional claims, memoise verification per `authToken`, or enforce application-specific policies without changing LiveStore internals.
|
|
186
|
-
|
|
187
|
-
## Client identity vs user identity
|
|
188
|
-
|
|
189
|
-
LiveStore's `clientId` identifies a client instance, while user identity is an application-level concern that must be modeled through your application's events and logic.
|
|
190
|
-
|
|
191
|
-
### Key points
|
|
192
|
-
- `clientId`: Automatically managed by LiveStore, identifies a client instance
|
|
193
|
-
- User identity: Managed by your application through events and syncPayload
|
|
194
|
-
|
|
195
|
-
### Using syncPayload for authentication
|
|
196
|
-
|
|
197
|
-
The `syncPayload` is primarily intended for authentication purposes:
|
|
198
|
-
|
|
199
|
-
## `patterns/auth/live-store-provider.tsx`
|
|
200
|
-
|
|
201
|
-
```tsx filename="patterns/auth/live-store-provider.tsx"
|
|
202
|
-
|
|
203
|
-
const schema = {} as Parameters<typeof LiveStoreProvider>[0]['schema']
|
|
204
|
-
const storeId = 'demo-store'
|
|
205
|
-
const user = { jwt: 'user-token' }
|
|
206
|
-
const children: ReactNode = null
|
|
207
|
-
const adapter = makeInMemoryAdapter()
|
|
208
|
-
|
|
209
|
-
// ---cut---
|
|
210
|
-
export const AuthenticatedProvider = () => (
|
|
211
|
-
<LiveStoreProvider
|
|
212
|
-
schema={schema}
|
|
213
|
-
storeId={storeId}
|
|
214
|
-
adapter={adapter}
|
|
215
|
-
batchUpdates={batchUpdates}
|
|
216
|
-
syncPayload={{
|
|
217
|
-
authToken: user.jwt, // Using a JWT
|
|
218
|
-
}}
|
|
219
|
-
>
|
|
220
|
-
{/* ... */}
|
|
221
|
-
{children}
|
|
222
|
-
</LiveStoreProvider>
|
|
223
|
-
)
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
User identification and semantic data (like user IDs) should typically be handled through your event payloads and application state rather than relying solely on the sync payload.
|