@durable-streams/state 0.2.4 → 0.2.6
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 +1 -0
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -2
- package/package.json +3 -3
- package/skills/stream-db/SKILL.md +16 -4
- package/src/stream-db.ts +5 -2
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -448,7 +448,7 @@ function createStateSchema(collections) {
|
|
|
448
448
|
* ```
|
|
449
449
|
*/
|
|
450
450
|
function createStreamDB(options) {
|
|
451
|
-
const { streamOptions, state, actions: actionsFactory } = options;
|
|
451
|
+
const { streamOptions, state, actions: actionsFactory, live = true } = options;
|
|
452
452
|
const stream = new __durable_streams_client.DurableStream(streamOptions);
|
|
453
453
|
const dispatcher = new EventDispatcher();
|
|
454
454
|
const collectionInstances = {};
|
|
@@ -473,7 +473,7 @@ function createStreamDB(options) {
|
|
|
473
473
|
if (consumerStarted) return;
|
|
474
474
|
consumerStarted = true;
|
|
475
475
|
streamResponse = await stream.stream({
|
|
476
|
-
live
|
|
476
|
+
live,
|
|
477
477
|
signal: abortController.signal
|
|
478
478
|
});
|
|
479
479
|
streamResponse.subscribeJson((batch) => {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Collection, Collection as Collection$1, SyncConfig, and, avg, count, createCollection, createOptimisticAction, createOptimisticAction as createOptimisticAction$1, eq, gt, gte, ilike, inArray, isNull, isUndefined, like, lt, lte, max, min, not, or, sum } from "@tanstack/db";
|
|
2
2
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
-
import { DurableStream, DurableStreamOptions } from "@durable-streams/client";
|
|
3
|
+
import { DurableStream, DurableStreamOptions, LiveMode } from "@durable-streams/client";
|
|
4
4
|
|
|
5
5
|
//#region src/types.d.ts
|
|
6
6
|
/**
|
|
@@ -188,6 +188,8 @@ type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in
|
|
|
188
188
|
interface CreateStreamDBOptions<TDef extends StreamStateDefinition = StreamStateDefinition, TActions extends Record<string, ActionDefinition<any>> = Record<string, never>> {
|
|
189
189
|
/** Options for creating the durable stream (stream is created lazily on preload) */
|
|
190
190
|
streamOptions: DurableStreamOptions;
|
|
191
|
+
/** Live read mode used by the StreamDB consumer. Defaults to true. */
|
|
192
|
+
live?: LiveMode;
|
|
191
193
|
/** The stream state definition */
|
|
192
194
|
state: TDef;
|
|
193
195
|
/** Optional factory function to create actions with db and stream context */
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Collection, Collection as Collection$1, SyncConfig, and, avg, count, createCollection, createOptimisticAction, createOptimisticAction as createOptimisticAction$1, eq, gt, gte, ilike, inArray, isNull, isUndefined, like, lt, lte, max, min, not, or, sum } from "@tanstack/db";
|
|
2
|
-
import { DurableStream, DurableStreamOptions } from "@durable-streams/client";
|
|
2
|
+
import { DurableStream, DurableStreamOptions, LiveMode } from "@durable-streams/client";
|
|
3
3
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
4
4
|
|
|
5
5
|
//#region src/types.d.ts
|
|
@@ -188,6 +188,8 @@ type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in
|
|
|
188
188
|
interface CreateStreamDBOptions<TDef extends StreamStateDefinition = StreamStateDefinition, TActions extends Record<string, ActionDefinition<any>> = Record<string, never>> {
|
|
189
189
|
/** Options for creating the durable stream (stream is created lazily on preload) */
|
|
190
190
|
streamOptions: DurableStreamOptions;
|
|
191
|
+
/** Live read mode used by the StreamDB consumer. Defaults to true. */
|
|
192
|
+
live?: LiveMode;
|
|
191
193
|
/** The stream state definition */
|
|
192
194
|
state: TDef;
|
|
193
195
|
/** Optional factory function to create actions with db and stream context */
|
package/dist/index.js
CHANGED
|
@@ -424,7 +424,7 @@ function createStateSchema(collections) {
|
|
|
424
424
|
* ```
|
|
425
425
|
*/
|
|
426
426
|
function createStreamDB(options) {
|
|
427
|
-
const { streamOptions, state, actions: actionsFactory } = options;
|
|
427
|
+
const { streamOptions, state, actions: actionsFactory, live = true } = options;
|
|
428
428
|
const stream = new DurableStream(streamOptions);
|
|
429
429
|
const dispatcher = new EventDispatcher();
|
|
430
430
|
const collectionInstances = {};
|
|
@@ -449,7 +449,7 @@ function createStreamDB(options) {
|
|
|
449
449
|
if (consumerStarted) return;
|
|
450
450
|
consumerStarted = true;
|
|
451
451
|
streamResponse = await stream.stream({
|
|
452
|
-
live
|
|
452
|
+
live,
|
|
453
453
|
signal: abortController.signal
|
|
454
454
|
});
|
|
455
455
|
streamResponse.subscribeJson((batch) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@durable-streams/state",
|
|
3
3
|
"description": "State change event protocol for Durable Streams",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"author": "Durable Stream contributors",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@standard-schema/spec": "^1.0.0",
|
|
53
|
-
"@durable-streams/client": "0.2.
|
|
53
|
+
"@durable-streams/client": "0.2.4"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"@tanstack/db": ">=0.5.0"
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@tanstack/db": "latest",
|
|
60
60
|
"@tanstack/intent": "latest",
|
|
61
61
|
"tsdown": "^0.9.0",
|
|
62
|
-
"@durable-streams/server": "0.3.
|
|
62
|
+
"@durable-streams/server": "0.3.2"
|
|
63
63
|
},
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">=18.0.0"
|
|
@@ -77,22 +77,34 @@ const messages = db.collections.messages
|
|
|
77
77
|
|
|
78
78
|
### Reactive queries with TanStack DB
|
|
79
79
|
|
|
80
|
-
StreamDB collections are TanStack DB collections. Use framework adapters for reactive queries
|
|
80
|
+
StreamDB collections are TanStack DB collections. Use framework adapters for reactive queries.
|
|
81
|
+
|
|
82
|
+
**IMPORTANT**: `useLiveQuery` returns `{ data }`, NOT the array directly. Always destructure with a default:
|
|
81
83
|
|
|
82
84
|
```typescript
|
|
83
85
|
import { useLiveQuery } from "@tanstack/react-db"
|
|
84
86
|
import { eq } from "@durable-streams/state"
|
|
85
87
|
|
|
88
|
+
// List query — destructure { data } with a default empty array
|
|
89
|
+
function UserList() {
|
|
90
|
+
const { data: users = [] } = useLiveQuery((q) =>
|
|
91
|
+
q.from({ users: db.collections.users })
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
return users.map(u => <div key={u.id}>{u.name}</div>)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Single item query — use findOne(), data is T | undefined
|
|
86
98
|
function UserProfile({ userId }: { userId: string }) {
|
|
87
|
-
const
|
|
99
|
+
const { data: user } = useLiveQuery((q) =>
|
|
88
100
|
q
|
|
89
101
|
.from({ users: db.collections.users })
|
|
90
102
|
.where(({ users }) => eq(users.id, userId))
|
|
91
103
|
.findOne()
|
|
92
104
|
)
|
|
93
105
|
|
|
94
|
-
if (!
|
|
95
|
-
return <div>{
|
|
106
|
+
if (!user) return null
|
|
107
|
+
return <div>{user.name}</div>
|
|
96
108
|
}
|
|
97
109
|
```
|
|
98
110
|
|
package/src/stream-db.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { StandardSchemaV1 } from "@standard-schema/spec"
|
|
|
7
7
|
import type {
|
|
8
8
|
DurableStream,
|
|
9
9
|
DurableStreamOptions,
|
|
10
|
+
LiveMode,
|
|
10
11
|
StreamResponse,
|
|
11
12
|
} from "@durable-streams/client"
|
|
12
13
|
|
|
@@ -121,6 +122,8 @@ export interface CreateStreamDBOptions<
|
|
|
121
122
|
> {
|
|
122
123
|
/** Options for creating the durable stream (stream is created lazily on preload) */
|
|
123
124
|
streamOptions: DurableStreamOptions
|
|
125
|
+
/** Live read mode used by the StreamDB consumer. Defaults to true. */
|
|
126
|
+
live?: LiveMode
|
|
124
127
|
/** The stream state definition */
|
|
125
128
|
state: TDef
|
|
126
129
|
/** Optional factory function to create actions with db and stream context */
|
|
@@ -762,7 +765,7 @@ export function createStreamDB<
|
|
|
762
765
|
): TActions extends Record<string, never>
|
|
763
766
|
? StreamDB<TDef>
|
|
764
767
|
: StreamDBWithActions<TDef, TActions> {
|
|
765
|
-
const { streamOptions, state, actions: actionsFactory } = options
|
|
768
|
+
const { streamOptions, state, actions: actionsFactory, live = true } = options
|
|
766
769
|
|
|
767
770
|
// Create a stream handle (lightweight, doesn't connect until stream() is called)
|
|
768
771
|
const stream = new DurableStreamClass(streamOptions)
|
|
@@ -807,7 +810,7 @@ export function createStreamDB<
|
|
|
807
810
|
|
|
808
811
|
// Start streaming (this is where the connection actually happens)
|
|
809
812
|
streamResponse = await stream.stream<StateEvent>({
|
|
810
|
-
live
|
|
813
|
+
live,
|
|
811
814
|
signal: abortController.signal,
|
|
812
815
|
})
|
|
813
816
|
|