@durable-streams/state 0.1.5 → 0.2.0
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 +11 -5
- package/dist/index.cjs +122 -2
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +5 -5
- package/package.json +7 -4
- package/src/index.ts +30 -0
- package/src/stream-db.ts +1 -1
package/README.md
CHANGED
|
@@ -5,9 +5,11 @@ Building blocks for transmitting structured state over Durable Streams. Use thes
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
pnpm add @durable-streams/state
|
|
8
|
+
pnpm add @durable-streams/state @tanstack/db
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
> **Note:** `@tanstack/db` is a peer dependency that must be installed alongside this package. This ensures type compatibility when using StreamDB collections with TanStack DB's query utilities like `useLiveQuery` from `@tanstack/react-db`.
|
|
12
|
+
|
|
11
13
|
## Overview
|
|
12
14
|
|
|
13
15
|
This package provides flexible primitives for streaming structured state. You choose how much structure you need:
|
|
@@ -515,9 +517,11 @@ const schema = createStateSchema({
|
|
|
515
517
|
})
|
|
516
518
|
|
|
517
519
|
// Different types coexist in the same stream
|
|
518
|
-
await stream.append(schema.users.insert({ value: user }))
|
|
519
|
-
await stream.append(schema.messages.insert({ value: message }))
|
|
520
|
-
await stream.append(
|
|
520
|
+
await stream.append(JSON.stringify(schema.users.insert({ value: user })))
|
|
521
|
+
await stream.append(JSON.stringify(schema.messages.insert({ value: message })))
|
|
522
|
+
await stream.append(
|
|
523
|
+
JSON.stringify(schema.reactions.insert({ value: reaction }))
|
|
524
|
+
)
|
|
521
525
|
```
|
|
522
526
|
|
|
523
527
|
## Best Practices
|
|
@@ -562,7 +566,9 @@ For critical operations, always use transaction IDs to ensure confirmation:
|
|
|
562
566
|
|
|
563
567
|
```typescript
|
|
564
568
|
const txid = crypto.randomUUID()
|
|
565
|
-
await stream.append(
|
|
569
|
+
await stream.append(
|
|
570
|
+
JSON.stringify(schema.users.insert({ value: user, headers: { txid } }))
|
|
571
|
+
)
|
|
566
572
|
await db.utils.awaitTxId(txid, 10000) // Wait up to 10 seconds
|
|
567
573
|
```
|
|
568
574
|
|
package/dist/index.cjs
CHANGED
|
@@ -479,7 +479,7 @@ function createStreamDB(options) {
|
|
|
479
479
|
if (consumerStarted) return;
|
|
480
480
|
consumerStarted = true;
|
|
481
481
|
streamResponse = await stream.stream({
|
|
482
|
-
live:
|
|
482
|
+
live: true,
|
|
483
483
|
signal: abortController.signal
|
|
484
484
|
});
|
|
485
485
|
let batchCount = 0;
|
|
@@ -553,7 +553,127 @@ function createStreamDB(options) {
|
|
|
553
553
|
|
|
554
554
|
//#endregion
|
|
555
555
|
exports.MaterializedState = MaterializedState
|
|
556
|
+
Object.defineProperty(exports, 'and', {
|
|
557
|
+
enumerable: true,
|
|
558
|
+
get: function () {
|
|
559
|
+
return __tanstack_db.and;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
Object.defineProperty(exports, 'avg', {
|
|
563
|
+
enumerable: true,
|
|
564
|
+
get: function () {
|
|
565
|
+
return __tanstack_db.avg;
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
Object.defineProperty(exports, 'count', {
|
|
569
|
+
enumerable: true,
|
|
570
|
+
get: function () {
|
|
571
|
+
return __tanstack_db.count;
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
Object.defineProperty(exports, 'createCollection', {
|
|
575
|
+
enumerable: true,
|
|
576
|
+
get: function () {
|
|
577
|
+
return __tanstack_db.createCollection;
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
Object.defineProperty(exports, 'createOptimisticAction', {
|
|
581
|
+
enumerable: true,
|
|
582
|
+
get: function () {
|
|
583
|
+
return __tanstack_db.createOptimisticAction;
|
|
584
|
+
}
|
|
585
|
+
});
|
|
556
586
|
exports.createStateSchema = createStateSchema
|
|
557
587
|
exports.createStreamDB = createStreamDB
|
|
588
|
+
Object.defineProperty(exports, 'eq', {
|
|
589
|
+
enumerable: true,
|
|
590
|
+
get: function () {
|
|
591
|
+
return __tanstack_db.eq;
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
Object.defineProperty(exports, 'gt', {
|
|
595
|
+
enumerable: true,
|
|
596
|
+
get: function () {
|
|
597
|
+
return __tanstack_db.gt;
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
Object.defineProperty(exports, 'gte', {
|
|
601
|
+
enumerable: true,
|
|
602
|
+
get: function () {
|
|
603
|
+
return __tanstack_db.gte;
|
|
604
|
+
}
|
|
605
|
+
});
|
|
606
|
+
Object.defineProperty(exports, 'ilike', {
|
|
607
|
+
enumerable: true,
|
|
608
|
+
get: function () {
|
|
609
|
+
return __tanstack_db.ilike;
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
Object.defineProperty(exports, 'inArray', {
|
|
613
|
+
enumerable: true,
|
|
614
|
+
get: function () {
|
|
615
|
+
return __tanstack_db.inArray;
|
|
616
|
+
}
|
|
617
|
+
});
|
|
558
618
|
exports.isChangeEvent = isChangeEvent
|
|
559
|
-
exports.isControlEvent = isControlEvent
|
|
619
|
+
exports.isControlEvent = isControlEvent
|
|
620
|
+
Object.defineProperty(exports, 'isNull', {
|
|
621
|
+
enumerable: true,
|
|
622
|
+
get: function () {
|
|
623
|
+
return __tanstack_db.isNull;
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
Object.defineProperty(exports, 'isUndefined', {
|
|
627
|
+
enumerable: true,
|
|
628
|
+
get: function () {
|
|
629
|
+
return __tanstack_db.isUndefined;
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
Object.defineProperty(exports, 'like', {
|
|
633
|
+
enumerable: true,
|
|
634
|
+
get: function () {
|
|
635
|
+
return __tanstack_db.like;
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
Object.defineProperty(exports, 'lt', {
|
|
639
|
+
enumerable: true,
|
|
640
|
+
get: function () {
|
|
641
|
+
return __tanstack_db.lt;
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
Object.defineProperty(exports, 'lte', {
|
|
645
|
+
enumerable: true,
|
|
646
|
+
get: function () {
|
|
647
|
+
return __tanstack_db.lte;
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
Object.defineProperty(exports, 'max', {
|
|
651
|
+
enumerable: true,
|
|
652
|
+
get: function () {
|
|
653
|
+
return __tanstack_db.max;
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
Object.defineProperty(exports, 'min', {
|
|
657
|
+
enumerable: true,
|
|
658
|
+
get: function () {
|
|
659
|
+
return __tanstack_db.min;
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
Object.defineProperty(exports, 'not', {
|
|
663
|
+
enumerable: true,
|
|
664
|
+
get: function () {
|
|
665
|
+
return __tanstack_db.not;
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
Object.defineProperty(exports, 'or', {
|
|
669
|
+
enumerable: true,
|
|
670
|
+
get: function () {
|
|
671
|
+
return __tanstack_db.or;
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
Object.defineProperty(exports, 'sum', {
|
|
675
|
+
enumerable: true,
|
|
676
|
+
get: function () {
|
|
677
|
+
return __tanstack_db.sum;
|
|
678
|
+
}
|
|
679
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Collection, createOptimisticAction } from "@tanstack/db";
|
|
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
3
|
import { DurableStream, DurableStreamOptions } from "@durable-streams/client";
|
|
4
4
|
|
|
@@ -181,7 +181,7 @@ type ActionFactory<TDef extends StreamStateDefinition, TActions extends Record<s
|
|
|
181
181
|
/**
|
|
182
182
|
* Map action definitions to callable action functions
|
|
183
183
|
*/
|
|
184
|
-
type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in keyof TActions]: ReturnType<typeof createOptimisticAction<any>> };
|
|
184
|
+
type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in keyof TActions]: ReturnType<typeof createOptimisticAction$1<any>> };
|
|
185
185
|
/**
|
|
186
186
|
* Options for creating a stream DB
|
|
187
187
|
*/
|
|
@@ -200,7 +200,7 @@ type ExtractCollectionType<T extends CollectionDefinition> = T extends Collectio
|
|
|
200
200
|
/**
|
|
201
201
|
* Map collection definitions to TanStack DB Collection types
|
|
202
202
|
*/
|
|
203
|
-
type CollectionMap<TDef extends StreamStateDefinition> = { [K in keyof TDef]: Collection<ExtractCollectionType<TDef[K]> & object, string> };
|
|
203
|
+
type CollectionMap<TDef extends StreamStateDefinition> = { [K in keyof TDef]: Collection$1<ExtractCollectionType<TDef[K]> & object, string> };
|
|
204
204
|
/**
|
|
205
205
|
* The StreamDB interface - provides typed access to collections
|
|
206
206
|
*/
|
|
@@ -281,4 +281,4 @@ declare function createStateSchema<T extends Record<string, CollectionDefinition
|
|
|
281
281
|
declare function createStreamDB<TDef extends StreamStateDefinition, TActions extends Record<string, ActionDefinition<any>> = Record<string, never>>(options: CreateStreamDBOptions<TDef, TActions>): TActions extends Record<string, never> ? StreamDB<TDef> : StreamDBWithActions<TDef, TActions>;
|
|
282
282
|
|
|
283
283
|
//#endregion
|
|
284
|
-
export { ActionDefinition, ActionFactory, ActionMap, ChangeEvent, ChangeHeaders, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, CreateStreamDBOptions, MaterializedState, Operation, Row, StateEvent, StateSchema, StreamDB, StreamDBMethods, StreamDBUtils, StreamDBWithActions, StreamStateDefinition, Value, createStateSchema, createStreamDB, isChangeEvent, isControlEvent };
|
|
284
|
+
export { ActionDefinition, ActionFactory, ActionMap, ChangeEvent, ChangeHeaders, Collection, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, CreateStreamDBOptions, MaterializedState, Operation, Row, StateEvent, StateSchema, StreamDB, StreamDBMethods, StreamDBUtils, StreamDBWithActions, StreamStateDefinition, SyncConfig, Value, and, avg, count, createCollection, createOptimisticAction, createStateSchema, createStreamDB, eq, gt, gte, ilike, inArray, isChangeEvent, isControlEvent, isNull, isUndefined, like, lt, lte, max, min, not, or, sum };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Collection, createOptimisticAction } from "@tanstack/db";
|
|
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 { DurableStream, DurableStreamOptions } from "@durable-streams/client";
|
|
3
3
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
4
4
|
|
|
@@ -181,7 +181,7 @@ type ActionFactory<TDef extends StreamStateDefinition, TActions extends Record<s
|
|
|
181
181
|
/**
|
|
182
182
|
* Map action definitions to callable action functions
|
|
183
183
|
*/
|
|
184
|
-
type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in keyof TActions]: ReturnType<typeof createOptimisticAction<any>> };
|
|
184
|
+
type ActionMap<TActions extends Record<string, ActionDefinition<any>>> = { [K in keyof TActions]: ReturnType<typeof createOptimisticAction$1<any>> };
|
|
185
185
|
/**
|
|
186
186
|
* Options for creating a stream DB
|
|
187
187
|
*/
|
|
@@ -200,7 +200,7 @@ type ExtractCollectionType<T extends CollectionDefinition> = T extends Collectio
|
|
|
200
200
|
/**
|
|
201
201
|
* Map collection definitions to TanStack DB Collection types
|
|
202
202
|
*/
|
|
203
|
-
type CollectionMap<TDef extends StreamStateDefinition> = { [K in keyof TDef]: Collection<ExtractCollectionType<TDef[K]> & object, string> };
|
|
203
|
+
type CollectionMap<TDef extends StreamStateDefinition> = { [K in keyof TDef]: Collection$1<ExtractCollectionType<TDef[K]> & object, string> };
|
|
204
204
|
/**
|
|
205
205
|
* The StreamDB interface - provides typed access to collections
|
|
206
206
|
*/
|
|
@@ -281,4 +281,4 @@ declare function createStateSchema<T extends Record<string, CollectionDefinition
|
|
|
281
281
|
declare function createStreamDB<TDef extends StreamStateDefinition, TActions extends Record<string, ActionDefinition<any>> = Record<string, never>>(options: CreateStreamDBOptions<TDef, TActions>): TActions extends Record<string, never> ? StreamDB<TDef> : StreamDBWithActions<TDef, TActions>;
|
|
282
282
|
|
|
283
283
|
//#endregion
|
|
284
|
-
export { ActionDefinition, ActionFactory, ActionMap, ChangeEvent, ChangeHeaders, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, CreateStreamDBOptions, MaterializedState, Operation, Row, StateEvent, StateSchema, StreamDB, StreamDBMethods, StreamDBUtils, StreamDBWithActions, StreamStateDefinition, Value, createStateSchema, createStreamDB, isChangeEvent, isControlEvent };
|
|
284
|
+
export { ActionDefinition, ActionFactory, ActionMap, ChangeEvent, ChangeHeaders, Collection, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, CreateStreamDBOptions, MaterializedState, Operation, Row, StateEvent, StateSchema, StreamDB, StreamDBMethods, StreamDBUtils, StreamDBWithActions, StreamStateDefinition, SyncConfig, Value, and, avg, count, createCollection, createOptimisticAction, createStateSchema, createStreamDB, eq, gt, gte, ilike, inArray, isChangeEvent, isControlEvent, isNull, isUndefined, like, lt, lte, max, min, not, or, sum };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createCollection, createOptimisticAction } from "@tanstack/db";
|
|
1
|
+
import { and, avg, count, createCollection, createCollection as createCollection$1, 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 { DurableStream } from "@durable-streams/client";
|
|
3
3
|
|
|
4
4
|
//#region src/types.ts
|
|
@@ -429,7 +429,7 @@ function createStreamDB(options) {
|
|
|
429
429
|
const dispatcher = new EventDispatcher();
|
|
430
430
|
const collectionInstances = {};
|
|
431
431
|
for (const [name, definition] of Object.entries(state)) {
|
|
432
|
-
const collection = createCollection({
|
|
432
|
+
const collection = createCollection$1({
|
|
433
433
|
id: `stream-db:${name}`,
|
|
434
434
|
schema: definition.schema,
|
|
435
435
|
getKey: (item) => String(item[definition.primaryKey]),
|
|
@@ -455,7 +455,7 @@ function createStreamDB(options) {
|
|
|
455
455
|
if (consumerStarted) return;
|
|
456
456
|
consumerStarted = true;
|
|
457
457
|
streamResponse = await stream.stream({
|
|
458
|
-
live:
|
|
458
|
+
live: true,
|
|
459
459
|
signal: abortController.signal
|
|
460
460
|
});
|
|
461
461
|
let batchCount = 0;
|
|
@@ -515,7 +515,7 @@ function createStreamDB(options) {
|
|
|
515
515
|
stream
|
|
516
516
|
});
|
|
517
517
|
const wrappedActions = {};
|
|
518
|
-
for (const [name, def] of Object.entries(actionDefs)) wrappedActions[name] = createOptimisticAction({
|
|
518
|
+
for (const [name, def] of Object.entries(actionDefs)) wrappedActions[name] = createOptimisticAction$1({
|
|
519
519
|
onMutate: def.onMutate,
|
|
520
520
|
mutationFn: def.mutationFn
|
|
521
521
|
});
|
|
@@ -528,4 +528,4 @@ function createStreamDB(options) {
|
|
|
528
528
|
}
|
|
529
529
|
|
|
530
530
|
//#endregion
|
|
531
|
-
export { MaterializedState, createStateSchema, createStreamDB, isChangeEvent, isControlEvent };
|
|
531
|
+
export { MaterializedState, and, avg, count, createCollection, createOptimisticAction, createStateSchema, createStreamDB, eq, gt, gte, ilike, inArray, isChangeEvent, isControlEvent, isNull, isUndefined, like, lt, lte, max, min, not, or, sum };
|
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.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"author": "Durable Stream contributors",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -44,12 +44,15 @@
|
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@standard-schema/spec": "^1.0.0",
|
|
47
|
-
"@
|
|
48
|
-
|
|
47
|
+
"@durable-streams/client": "0.2.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@tanstack/db": ">=0.5.0"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {
|
|
53
|
+
"@tanstack/db": "latest",
|
|
51
54
|
"tsdown": "^0.9.0",
|
|
52
|
-
"@durable-streams/server": "0.1.
|
|
55
|
+
"@durable-streams/server": "0.1.7"
|
|
53
56
|
},
|
|
54
57
|
"engines": {
|
|
55
58
|
"node": ">=18.0.0"
|
package/src/index.ts
CHANGED
|
@@ -31,3 +31,33 @@ export type {
|
|
|
31
31
|
ActionMap,
|
|
32
32
|
ActionDefinition,
|
|
33
33
|
} from "./stream-db"
|
|
34
|
+
|
|
35
|
+
// Re-export key types and utilities from @tanstack/db for convenience
|
|
36
|
+
// This ensures consumers can use the same module resolution for type compatibility
|
|
37
|
+
export type { Collection, SyncConfig } from "@tanstack/db"
|
|
38
|
+
export {
|
|
39
|
+
createCollection,
|
|
40
|
+
createOptimisticAction,
|
|
41
|
+
// Comparison operators
|
|
42
|
+
eq,
|
|
43
|
+
gt,
|
|
44
|
+
gte,
|
|
45
|
+
lt,
|
|
46
|
+
lte,
|
|
47
|
+
like,
|
|
48
|
+
ilike,
|
|
49
|
+
inArray,
|
|
50
|
+
// Logical operators
|
|
51
|
+
and,
|
|
52
|
+
or,
|
|
53
|
+
not,
|
|
54
|
+
// Null checking
|
|
55
|
+
isNull,
|
|
56
|
+
isUndefined,
|
|
57
|
+
// Aggregate functions
|
|
58
|
+
count,
|
|
59
|
+
sum,
|
|
60
|
+
avg,
|
|
61
|
+
min,
|
|
62
|
+
max,
|
|
63
|
+
} from "@tanstack/db"
|
package/src/stream-db.ts
CHANGED