@durable-streams/state 0.2.9 → 0.3.1

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.
@@ -0,0 +1,173 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/types.d.ts
4
+ /**
5
+ * Operation types for change events
6
+ */
7
+ /**
8
+ * Operation types for change events
9
+ */
10
+ type Operation = `insert` | `update` | `delete` | `upsert`;
11
+ /**
12
+ * A generic value type supporting primitives, arrays, and objects
13
+ */
14
+ type Value<Extensions = never> = string | number | boolean | bigint | null | Array<Value<Extensions>> | {
15
+ [key: string]: Value<Extensions>;
16
+ } | Extensions;
17
+ /**
18
+ * A row is a record of values
19
+ */
20
+ type Row<Extensions = never> = Record<string, Value<Extensions>>;
21
+ /**
22
+ * Headers for change messages
23
+ */
24
+ type ChangeHeaders = {
25
+ operation: Operation;
26
+ txid?: string;
27
+ timestamp?: string;
28
+ from?: string;
29
+ offset?: string;
30
+ };
31
+ /**
32
+ * A change event represents a state change event (insert/update/delete)
33
+ */
34
+ type ChangeEvent<T = unknown> = {
35
+ type: string;
36
+ key: string;
37
+ value?: T;
38
+ old_value?: T;
39
+ headers: ChangeHeaders;
40
+ };
41
+ /**
42
+ * Control event types for stream management
43
+ */
44
+ type ControlEvent = {
45
+ headers: {
46
+ control: `snapshot-start` | `snapshot-end` | `reset`;
47
+ offset?: string;
48
+ };
49
+ };
50
+ /**
51
+ * A state event is either a change event or a control event
52
+ */
53
+ type StateEvent<T = unknown> = ChangeEvent<T> | ControlEvent;
54
+ /**
55
+ * Type guard to check if an event is a change event
56
+ */
57
+ declare function isChangeEvent<T = unknown>(event: StateEvent<T>): event is ChangeEvent<T>;
58
+ /**
59
+ * Type guard to check if an event is a control event
60
+ */
61
+ declare function isControlEvent<T = unknown>(event: StateEvent<T>): event is ControlEvent;
62
+
63
+ //#endregion
64
+ //#region src/materialized-state.d.ts
65
+ /**
66
+ * MaterializedState maintains an in-memory view of state from change events.
67
+ *
68
+ * It organizes data by type, where each type contains a map of key -> value.
69
+ * This supports multi-type streams where different entity types can coexist.
70
+ */
71
+ declare class MaterializedState {
72
+ private data;
73
+ constructor();
74
+ /**
75
+ * Apply a single change event to update the materialized state
76
+ */
77
+ apply(event: ChangeEvent): void;
78
+ /**
79
+ * Apply a batch of change events
80
+ */
81
+ applyBatch(events: Array<ChangeEvent>): void;
82
+ /**
83
+ * Get a specific value by type and key
84
+ */
85
+ get<T = unknown>(type: string, key: string): T | undefined;
86
+ /**
87
+ * Get all entries for a specific type
88
+ */
89
+ getType(type: string): Map<string, unknown>;
90
+ /**
91
+ * Clear all state
92
+ */
93
+ clear(): void;
94
+ /**
95
+ * Get the number of types in the state
96
+ */
97
+ get typeCount(): number;
98
+ /**
99
+ * Get all type names
100
+ */
101
+ get types(): Array<string>;
102
+ }
103
+
104
+ //#endregion
105
+ //#region src/schema.d.ts
106
+ /**
107
+ * Definition for a single collection in the stream state
108
+ */
109
+ interface CollectionDefinition<T = unknown> {
110
+ /** Standard Schema for validating values */
111
+ schema: StandardSchemaV1<T>;
112
+ /** The type field value in change events that map to this collection */
113
+ type: string;
114
+ /** The property name in T that serves as the primary key */
115
+ primaryKey: string;
116
+ }
117
+ /**
118
+ * Helper methods for creating change events for a collection
119
+ */
120
+ interface CollectionEventHelpers<T> {
121
+ /**
122
+ * Create an insert change event
123
+ */
124
+ insert: (params: {
125
+ key?: string;
126
+ value: T;
127
+ headers?: Omit<Record<string, string>, `operation`>;
128
+ }) => ChangeEvent<T>;
129
+ /**
130
+ * Create an update change event
131
+ */
132
+ update: (params: {
133
+ key?: string;
134
+ value: T;
135
+ oldValue?: T;
136
+ headers?: Omit<Record<string, string>, `operation`>;
137
+ }) => ChangeEvent<T>;
138
+ /**
139
+ * Create a delete change event
140
+ */
141
+ delete: (params: {
142
+ key?: string;
143
+ oldValue?: T;
144
+ headers?: Omit<Record<string, string>, `operation`>;
145
+ }) => ChangeEvent<T>;
146
+ /**
147
+ * Create an upsert change event (insert or update)
148
+ */
149
+ upsert: (params: {
150
+ key?: string;
151
+ value: T;
152
+ headers?: Omit<Record<string, string>, `operation`>;
153
+ }) => ChangeEvent<T>;
154
+ }
155
+ /**
156
+ * Collection definition enhanced with event creation helpers
157
+ */
158
+ type CollectionWithHelpers<T = unknown> = CollectionDefinition<T> & CollectionEventHelpers<T>;
159
+ /**
160
+ * Stream state definition containing all collections
161
+ */
162
+ type StreamStateDefinition = Record<string, CollectionDefinition>;
163
+ /**
164
+ * Stream state schema with helper methods for creating change events
165
+ */
166
+ type StateSchema<T extends Record<string, CollectionDefinition>> = { [K in keyof T]: CollectionWithHelpers<T[K] extends CollectionDefinition<infer U> ? U : unknown> };
167
+ /**
168
+ * Create a state schema definition with typed collections and event helpers
169
+ */
170
+ declare function createStateSchema<T extends Record<string, CollectionDefinition>>(collections: T): StateSchema<T>;
171
+
172
+ //#endregion
173
+ export { ChangeEvent, ChangeHeaders, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, MaterializedState, Operation, Row, StateEvent, StateSchema, StreamStateDefinition, Value, createStateSchema, isChangeEvent, isControlEvent };
@@ -0,0 +1,173 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/types.d.ts
4
+ /**
5
+ * Operation types for change events
6
+ */
7
+ /**
8
+ * Operation types for change events
9
+ */
10
+ type Operation = `insert` | `update` | `delete` | `upsert`;
11
+ /**
12
+ * A generic value type supporting primitives, arrays, and objects
13
+ */
14
+ type Value<Extensions = never> = string | number | boolean | bigint | null | Array<Value<Extensions>> | {
15
+ [key: string]: Value<Extensions>;
16
+ } | Extensions;
17
+ /**
18
+ * A row is a record of values
19
+ */
20
+ type Row<Extensions = never> = Record<string, Value<Extensions>>;
21
+ /**
22
+ * Headers for change messages
23
+ */
24
+ type ChangeHeaders = {
25
+ operation: Operation;
26
+ txid?: string;
27
+ timestamp?: string;
28
+ from?: string;
29
+ offset?: string;
30
+ };
31
+ /**
32
+ * A change event represents a state change event (insert/update/delete)
33
+ */
34
+ type ChangeEvent<T = unknown> = {
35
+ type: string;
36
+ key: string;
37
+ value?: T;
38
+ old_value?: T;
39
+ headers: ChangeHeaders;
40
+ };
41
+ /**
42
+ * Control event types for stream management
43
+ */
44
+ type ControlEvent = {
45
+ headers: {
46
+ control: `snapshot-start` | `snapshot-end` | `reset`;
47
+ offset?: string;
48
+ };
49
+ };
50
+ /**
51
+ * A state event is either a change event or a control event
52
+ */
53
+ type StateEvent<T = unknown> = ChangeEvent<T> | ControlEvent;
54
+ /**
55
+ * Type guard to check if an event is a change event
56
+ */
57
+ declare function isChangeEvent<T = unknown>(event: StateEvent<T>): event is ChangeEvent<T>;
58
+ /**
59
+ * Type guard to check if an event is a control event
60
+ */
61
+ declare function isControlEvent<T = unknown>(event: StateEvent<T>): event is ControlEvent;
62
+
63
+ //#endregion
64
+ //#region src/materialized-state.d.ts
65
+ /**
66
+ * MaterializedState maintains an in-memory view of state from change events.
67
+ *
68
+ * It organizes data by type, where each type contains a map of key -> value.
69
+ * This supports multi-type streams where different entity types can coexist.
70
+ */
71
+ declare class MaterializedState {
72
+ private data;
73
+ constructor();
74
+ /**
75
+ * Apply a single change event to update the materialized state
76
+ */
77
+ apply(event: ChangeEvent): void;
78
+ /**
79
+ * Apply a batch of change events
80
+ */
81
+ applyBatch(events: Array<ChangeEvent>): void;
82
+ /**
83
+ * Get a specific value by type and key
84
+ */
85
+ get<T = unknown>(type: string, key: string): T | undefined;
86
+ /**
87
+ * Get all entries for a specific type
88
+ */
89
+ getType(type: string): Map<string, unknown>;
90
+ /**
91
+ * Clear all state
92
+ */
93
+ clear(): void;
94
+ /**
95
+ * Get the number of types in the state
96
+ */
97
+ get typeCount(): number;
98
+ /**
99
+ * Get all type names
100
+ */
101
+ get types(): Array<string>;
102
+ }
103
+
104
+ //#endregion
105
+ //#region src/schema.d.ts
106
+ /**
107
+ * Definition for a single collection in the stream state
108
+ */
109
+ interface CollectionDefinition<T = unknown> {
110
+ /** Standard Schema for validating values */
111
+ schema: StandardSchemaV1<T>;
112
+ /** The type field value in change events that map to this collection */
113
+ type: string;
114
+ /** The property name in T that serves as the primary key */
115
+ primaryKey: string;
116
+ }
117
+ /**
118
+ * Helper methods for creating change events for a collection
119
+ */
120
+ interface CollectionEventHelpers<T> {
121
+ /**
122
+ * Create an insert change event
123
+ */
124
+ insert: (params: {
125
+ key?: string;
126
+ value: T;
127
+ headers?: Omit<Record<string, string>, `operation`>;
128
+ }) => ChangeEvent<T>;
129
+ /**
130
+ * Create an update change event
131
+ */
132
+ update: (params: {
133
+ key?: string;
134
+ value: T;
135
+ oldValue?: T;
136
+ headers?: Omit<Record<string, string>, `operation`>;
137
+ }) => ChangeEvent<T>;
138
+ /**
139
+ * Create a delete change event
140
+ */
141
+ delete: (params: {
142
+ key?: string;
143
+ oldValue?: T;
144
+ headers?: Omit<Record<string, string>, `operation`>;
145
+ }) => ChangeEvent<T>;
146
+ /**
147
+ * Create an upsert change event (insert or update)
148
+ */
149
+ upsert: (params: {
150
+ key?: string;
151
+ value: T;
152
+ headers?: Omit<Record<string, string>, `operation`>;
153
+ }) => ChangeEvent<T>;
154
+ }
155
+ /**
156
+ * Collection definition enhanced with event creation helpers
157
+ */
158
+ type CollectionWithHelpers<T = unknown> = CollectionDefinition<T> & CollectionEventHelpers<T>;
159
+ /**
160
+ * Stream state definition containing all collections
161
+ */
162
+ type StreamStateDefinition = Record<string, CollectionDefinition>;
163
+ /**
164
+ * Stream state schema with helper methods for creating change events
165
+ */
166
+ type StateSchema<T extends Record<string, CollectionDefinition>> = { [K in keyof T]: CollectionWithHelpers<T[K] extends CollectionDefinition<infer U> ? U : unknown> };
167
+ /**
168
+ * Create a state schema definition with typed collections and event helpers
169
+ */
170
+ declare function createStateSchema<T extends Record<string, CollectionDefinition>>(collections: T): StateSchema<T>;
171
+
172
+ //#endregion
173
+ export { ChangeEvent, ChangeHeaders, CollectionDefinition, CollectionEventHelpers, CollectionWithHelpers, ControlEvent, MaterializedState as MaterializedState$1, Operation, Row, StateEvent, StateSchema, StreamStateDefinition, Value, createStateSchema as createStateSchema$1, isChangeEvent as isChangeEvent$1, isControlEvent as isControlEvent$1 };