@livestore/common 0.0.54-dev.3 → 0.0.54-dev.31

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 (90) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/__tests__/fixture.d.ts +0 -2
  3. package/dist/__tests__/fixture.d.ts.map +1 -1
  4. package/dist/adapter-types.d.ts +75 -16
  5. package/dist/adapter-types.d.ts.map +1 -1
  6. package/dist/adapter-types.js +18 -0
  7. package/dist/adapter-types.js.map +1 -1
  8. package/dist/bounded-collections.d.ts +1 -1
  9. package/dist/bounded-collections.d.ts.map +1 -1
  10. package/dist/bounded-collections.js.map +1 -1
  11. package/dist/debug-info.d.ts +4 -0
  12. package/dist/debug-info.d.ts.map +1 -1
  13. package/dist/debug-info.js +6 -9
  14. package/dist/debug-info.js.map +1 -1
  15. package/dist/derived-mutations.d.ts +12 -13
  16. package/dist/derived-mutations.d.ts.map +1 -1
  17. package/dist/devtools/devtools-messages.d.ts +248 -46
  18. package/dist/devtools/devtools-messages.d.ts.map +1 -1
  19. package/dist/devtools/devtools-messages.js +110 -48
  20. package/dist/devtools/devtools-messages.js.map +1 -1
  21. package/dist/devtools/devtools-window-message.d.ts +29 -0
  22. package/dist/devtools/devtools-window-message.d.ts.map +1 -0
  23. package/dist/devtools/devtools-window-message.js +33 -0
  24. package/dist/devtools/devtools-window-message.js.map +1 -0
  25. package/dist/devtools/index.d.ts +1 -0
  26. package/dist/devtools/index.d.ts.map +1 -1
  27. package/dist/devtools/index.js +1 -0
  28. package/dist/devtools/index.js.map +1 -1
  29. package/dist/index.d.ts +1 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/mutation.d.ts.map +1 -1
  34. package/dist/rehydrate-from-mutationlog.d.ts +8 -3
  35. package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
  36. package/dist/rehydrate-from-mutationlog.js +71 -56
  37. package/dist/rehydrate-from-mutationlog.js.map +1 -1
  38. package/dist/schema/index.d.ts.map +1 -1
  39. package/dist/schema/mutations.d.ts +2 -1
  40. package/dist/schema/mutations.d.ts.map +1 -1
  41. package/dist/schema/mutations.js +4 -2
  42. package/dist/schema/mutations.js.map +1 -1
  43. package/dist/schema/parse-utils.d.ts +3 -14
  44. package/dist/schema/parse-utils.d.ts.map +1 -1
  45. package/dist/schema/schema-helpers.d.ts +3 -13
  46. package/dist/schema/schema-helpers.d.ts.map +1 -1
  47. package/dist/schema/system-tables.d.ts +0 -5
  48. package/dist/schema/system-tables.d.ts.map +1 -1
  49. package/dist/schema/table-def.d.ts +3 -21
  50. package/dist/schema/table-def.d.ts.map +1 -1
  51. package/dist/schema/table-def.js +0 -1
  52. package/dist/schema/table-def.js.map +1 -1
  53. package/dist/schema-management/common.d.ts.map +1 -1
  54. package/dist/schema-management/migrations.d.ts +10 -5
  55. package/dist/schema-management/migrations.d.ts.map +1 -1
  56. package/dist/schema-management/migrations.js +24 -13
  57. package/dist/schema-management/migrations.js.map +1 -1
  58. package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
  59. package/dist/sql-queries/misc.d.ts.map +1 -1
  60. package/dist/sql-queries/sql-queries.d.ts.map +1 -1
  61. package/dist/sql-queries/sql-query-builder.d.ts +33 -33
  62. package/dist/sql-queries/sql-query-builder.d.ts.map +1 -1
  63. package/dist/sql-queries/types.d.ts.map +1 -1
  64. package/dist/sync/sync.d.ts +3 -3
  65. package/dist/util.d.ts +0 -1
  66. package/dist/util.d.ts.map +1 -1
  67. package/dist/util.js +1 -1
  68. package/dist/util.js.map +1 -1
  69. package/dist/version.d.ts +2 -0
  70. package/dist/version.d.ts.map +1 -0
  71. package/dist/version.js +3 -0
  72. package/dist/version.js.map +1 -0
  73. package/package.json +4 -8
  74. package/src/adapter-types.ts +53 -15
  75. package/src/bounded-collections.ts +1 -1
  76. package/src/debug-info.ts +6 -18
  77. package/src/devtools/devtools-messages.ts +146 -65
  78. package/src/devtools/devtools-window-message.ts +27 -0
  79. package/src/devtools/index.ts +1 -0
  80. package/src/index.ts +1 -0
  81. package/src/rehydrate-from-mutationlog.ts +99 -64
  82. package/src/schema/mutations.ts +8 -5
  83. package/src/schema/table-def.ts +0 -4
  84. package/src/schema-management/migrations.ts +104 -84
  85. package/src/util.ts +1 -1
  86. package/src/version.ts +3 -0
  87. package/dist/devtools/index copy.d.ts +0 -214
  88. package/dist/devtools/index copy.d.ts.map +0 -1
  89. package/dist/devtools/index copy.js +0 -137
  90. package/dist/devtools/index copy.js.map +0 -1
@@ -1,11 +1,10 @@
1
- import { version as pkgVersion } from '@livestore/common/package.json'
2
- import { Schema } from '@livestore/utils/effect'
3
- import { type SqliteDsl as __SqliteDsl } from 'effect-db-schema'
1
+ import { Schema, Transferable } from '@livestore/utils/effect'
4
2
 
5
3
  import { NetworkStatus } from '../adapter-types.js'
6
4
  import { DebugInfo } from '../debug-info.js'
7
5
  import { mutationEventSchemaEncodedAny } from '../schema/mutations.js'
8
6
  import { PreparedBindValues } from '../util.js'
7
+ import { liveStoreVersion as pkgVersion } from '../version.js'
9
8
 
10
9
  const requestId = Schema.String
11
10
  const channelId = Schema.String
@@ -15,36 +14,67 @@ export class SnapshotReq extends Schema.TaggedStruct('LSD.SnapshotReq', {
15
14
  liveStoreVersion,
16
15
  requestId,
17
16
  channelId,
18
- }) {}
17
+ }).annotations({ identifier: 'LSD.SnapshotReq' }) {}
19
18
 
20
19
  export class SnapshotRes extends Schema.TaggedStruct('LSD.SnapshotRes', {
21
20
  liveStoreVersion,
22
21
  requestId,
23
- snapshot: Schema.Uint8Array,
24
- }) {}
22
+ snapshot: Transferable.Uint8Array,
23
+ }).annotations({ identifier: 'LSD.SnapshotRes' }) {}
24
+
25
+ export class LoadDatabaseFileReq extends Schema.TaggedStruct('LSD.LoadDatabaseFileReq', {
26
+ liveStoreVersion,
27
+ requestId,
28
+ channelId,
29
+ data: Transferable.Uint8Array,
30
+ }).annotations({ identifier: 'LSD.LoadDatabaseFileReq' }) {}
31
+
32
+ export class LoadDatabaseFileRes extends Schema.TaggedStruct('LSD.LoadDatabaseFileRes', {
33
+ liveStoreVersion,
34
+ requestId,
35
+ status: Schema.Literal('ok', 'unsupported-file', 'unsupported-database'),
36
+ }).annotations({ identifier: 'LSD.LoadDatabaseFileRes' }) {}
25
37
 
26
38
  export class DebugInfoReq extends Schema.TaggedStruct('LSD.DebugInfoReq', {
27
39
  liveStoreVersion,
28
40
  requestId,
29
41
  channelId,
30
- }) {}
42
+ }).annotations({ identifier: 'LSD.DebugInfoReq' }) {}
31
43
 
32
44
  export class DebugInfoRes extends Schema.TaggedStruct('LSD.DebugInfoRes', {
33
45
  liveStoreVersion,
34
46
  requestId,
35
47
  debugInfo: DebugInfo,
36
- }) {}
48
+ }).annotations({ identifier: 'LSD.DebugInfoRes' }) {}
49
+
50
+ export class DebugInfoHistorySubscribe extends Schema.TaggedStruct('LSD.DebugInfoHistorySubscribe', {
51
+ liveStoreVersion,
52
+ requestId,
53
+ channelId,
54
+ }).annotations({ identifier: 'LSD.DebugInfoHistorySubscribe' }) {}
55
+
56
+ export class DebugInfoHistoryRes extends Schema.TaggedStruct('LSD.DebugInfoHistoryRes', {
57
+ liveStoreVersion,
58
+ requestId,
59
+ debugInfoHistory: Schema.Array(DebugInfo),
60
+ }).annotations({ identifier: 'LSD.DebugInfoHistoryRes' }) {}
61
+
62
+ export class DebugInfoHistoryUnsubscribe extends Schema.TaggedStruct('LSD.DebugInfoHistoryUnsubscribe', {
63
+ liveStoreVersion,
64
+ requestId,
65
+ channelId,
66
+ }).annotations({ identifier: 'LSD.DebugInfoHistoryUnsubscribe' }) {}
37
67
 
38
68
  export class DebugInfoResetReq extends Schema.TaggedStruct('LSD.DebugInfoResetReq', {
39
69
  liveStoreVersion,
40
70
  requestId,
41
71
  channelId,
42
- }) {}
72
+ }).annotations({ identifier: 'LSD.DebugInfoResetReq' }) {}
43
73
 
44
74
  export class DebugInfoResetRes extends Schema.TaggedStruct('LSD.DebugInfoResetRes', {
45
75
  liveStoreVersion,
46
76
  requestId,
47
- }) {}
77
+ }).annotations({ identifier: 'LSD.DebugInfoResetRes' }) {}
48
78
 
49
79
  export class DebugInfoRerunQueryReq extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryReq', {
50
80
  liveStoreVersion,
@@ -53,62 +83,76 @@ export class DebugInfoRerunQueryReq extends Schema.TaggedStruct('LSD.DebugInfoRe
53
83
  queryStr: Schema.String,
54
84
  bindValues: Schema.UndefinedOr(PreparedBindValues),
55
85
  queriedTables: Schema.ReadonlySet(Schema.String),
56
- }) {}
86
+ }).annotations({ identifier: 'LSD.DebugInfoRerunQueryReq' }) {}
57
87
 
58
88
  export class DebugInfoRerunQueryRes extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryRes', {
59
89
  liveStoreVersion,
60
90
  requestId,
61
- }) {}
91
+ }).annotations({ identifier: 'LSD.DebugInfoRerunQueryRes' }) {}
62
92
 
63
93
  export class MutationBroadcast extends Schema.TaggedStruct('LSD.MutationBroadcast', {
94
+ liveStoreVersion,
95
+ mutationEventEncoded: mutationEventSchemaEncodedAny,
96
+ persisted: Schema.Boolean,
97
+ }).annotations({ identifier: 'LSD.MutationBroadcast' }) {}
98
+
99
+ export class RunMutationReq extends Schema.TaggedStruct('LSD.RunMutationReq', {
64
100
  liveStoreVersion,
65
101
  requestId,
102
+ channelId,
66
103
  mutationEventEncoded: mutationEventSchemaEncodedAny,
67
104
  persisted: Schema.Boolean,
68
- }) {}
105
+ }).annotations({ identifier: 'LSD.RunMutationReq' }) {}
106
+
107
+ export class RunMutationRes extends Schema.TaggedStruct('LSD.RunMutationRes', {
108
+ liveStoreVersion,
109
+ requestId,
110
+ channelId,
111
+ }).annotations({ identifier: 'LSD.RunMutationRes' }) {}
69
112
 
70
113
  export class MutationLogReq extends Schema.TaggedStruct('LSD.MutationLogReq', {
71
114
  liveStoreVersion,
72
115
  requestId,
73
116
  channelId,
74
- }) {}
117
+ }).annotations({ identifier: 'LSD.MutationLogReq' }) {}
75
118
 
76
119
  export class MutationLogRes extends Schema.TaggedStruct('LSD.MutationLogRes', {
77
120
  liveStoreVersion,
78
121
  requestId,
79
- mutationLog: Schema.Uint8Array,
80
- }) {}
122
+ channelId,
123
+ mutationLog: Transferable.Uint8Array,
124
+ }).annotations({ identifier: 'LSD.MutationLogRes' }) {}
81
125
 
82
- export class SignalsSubscribe extends Schema.TaggedStruct('LSD.SignalsSubscribe', {
126
+ export class ReactivityGraphSubscribe extends Schema.TaggedStruct('LSD.ReactivityGraphSubscribe', {
83
127
  liveStoreVersion,
84
128
  requestId,
85
129
  channelId,
86
130
  includeResults: Schema.Boolean,
87
- }) {}
131
+ }).annotations({ identifier: 'LSD.ReactivityGraphSubscribe' }) {}
88
132
 
89
- export class SignalsUnsubscribe extends Schema.TaggedStruct('LSD.SignalsUnsubscribe', {
133
+ export class ReactivityGraphUnsubscribe extends Schema.TaggedStruct('LSD.ReactivityGraphUnsubscribe', {
90
134
  liveStoreVersion,
91
135
  requestId,
92
136
  channelId,
93
- }) {}
137
+ }).annotations({ identifier: 'LSD.ReactivityGraphUnsubscribe' }) {}
94
138
 
95
- export class SignalsRes extends Schema.TaggedStruct('LSD.SignalsRes', {
139
+ export class ReactivityGraphRes extends Schema.TaggedStruct('LSD.ReactivityGraphRes', {
96
140
  liveStoreVersion,
97
141
  requestId,
98
- signals: Schema.Any,
99
- }) {}
142
+ reactivityGraph: Schema.Any,
143
+ }).annotations({ identifier: 'LSD.ReactivityGraphRes' }) {}
100
144
 
101
145
  export class LiveQueriesSubscribe extends Schema.TaggedStruct('LSD.LiveQueriesSubscribe', {
102
146
  liveStoreVersion,
103
147
  requestId,
104
148
  channelId,
105
- }) {}
149
+ }).annotations({ identifier: 'LSD.LiveQueriesSubscribe' }) {}
106
150
 
107
151
  export class LiveQueriesUnsubscribe extends Schema.TaggedStruct('LSD.LiveQueriesUnsubscribe', {
108
152
  liveStoreVersion,
109
153
  requestId,
110
154
  channelId,
111
- }) {}
155
+ }).annotations({ identifier: 'LSD.LiveQueriesUnsubscribe' }) {}
112
156
 
113
157
  export class SerializedLiveQuery extends Schema.Struct({
114
158
  _tag: Schema.Literal('js', 'sql', 'graphql'),
@@ -120,94 +164,131 @@ export class SerializedLiveQuery extends Schema.Struct({
120
164
  activeSubscriptions: Schema.Array(
121
165
  Schema.Struct({ frames: Schema.Array(Schema.Struct({ name: Schema.String, filePath: Schema.String })) }),
122
166
  ),
123
- }) {}
167
+ }).annotations({ identifier: 'SerializedLiveQuery' }) {}
124
168
 
125
169
  export class LiveQueriesRes extends Schema.TaggedStruct('LSD.LiveQueriesRes', {
126
170
  liveStoreVersion,
127
171
  requestId,
128
172
  liveQueries: Schema.Array(SerializedLiveQuery),
129
- }) {}
173
+ }).annotations({ identifier: 'LSD.LiveQueriesRes' }) {}
130
174
 
131
175
  export class ResetAllDataReq extends Schema.TaggedStruct('LSD.ResetAllDataReq', {
132
176
  liveStoreVersion,
133
177
  requestId,
134
178
  channelId,
135
179
  mode: Schema.Literal('all-data', 'only-app-db'),
136
- }) {}
180
+ }).annotations({ identifier: 'LSD.ResetAllDataReq' }) {}
137
181
 
138
182
  export class ResetAllDataRes extends Schema.TaggedStruct('LSD.ResetAllDataRes', {
139
183
  liveStoreVersion,
140
184
  requestId,
141
- }) {}
185
+ }).annotations({ identifier: 'LSD.ResetAllDataRes' }) {}
142
186
 
143
- export class NetworkStatusBroadcast extends Schema.TaggedStruct('LSD.NetworkStatusBroadcast', {
187
+ export class MessagePortForStoreReq extends Schema.TaggedStruct('LSD.MessagePortForStoreReq', {
188
+ liveStoreVersion,
189
+ requestId,
190
+ channelId,
191
+ }).annotations({ identifier: 'LSD.MessagePortForStoreReq' }) {}
192
+
193
+ export class MessagePortForStoreRes extends Schema.TaggedStruct('LSD.MessagePortForStoreRes', {
194
+ liveStoreVersion,
195
+ requestId,
196
+ channelId,
197
+ port: Transferable.MessagePort,
198
+ }).annotations({ identifier: 'LSD.MessagePortForStoreRes' }) {}
199
+
200
+ export class NetworkStatusChanged extends Schema.TaggedStruct('LSD.NetworkStatusChanged', {
144
201
  liveStoreVersion,
145
202
  channelId,
146
203
  networkStatus: NetworkStatus,
147
- }) {}
204
+ }).annotations({ identifier: 'LSD.NetworkStatusChanged' }) {}
148
205
 
149
- export class DevtoolsReadyBroadcast extends Schema.TaggedStruct('LSD.DevtoolsReadyBroadcast', {
206
+ export class DevtoolsReady extends Schema.TaggedStruct('LSD.DevtoolsReady', {
150
207
  liveStoreVersion,
151
- }) {}
208
+ }).annotations({ identifier: 'LSD.DevtoolsReady' }) {}
152
209
 
153
210
  export class DevtoolsConnected extends Schema.TaggedStruct('LSD.DevtoolsConnected', {
154
211
  liveStoreVersion,
155
212
  channelId,
156
- }) {}
213
+ }).annotations({ identifier: 'LSD.DevtoolsConnected' }) {}
157
214
 
158
- export class AppHostReadyBroadcast extends Schema.TaggedStruct('LSD.AppHostReadyBroadcast', {
215
+ export class AppHostReady extends Schema.TaggedStruct('LSD.AppHostReady', {
159
216
  liveStoreVersion,
160
217
  channelId,
161
- }) {}
218
+ isLeaderTab: Schema.Boolean,
219
+ }).annotations({ identifier: 'LSD.AppHostReady' }) {}
162
220
 
163
221
  export class Disconnect extends Schema.TaggedStruct('LSD.Disconnect', {
164
222
  liveStoreVersion,
165
223
  requestId,
166
224
  channelId,
167
- }) {}
225
+ }).annotations({ identifier: 'LSD.Disconnect' }) {}
168
226
 
169
- // export class SchemaChanged extends Schema.TaggedStruct('LSD.SchemaChanged', {
170
- // requestId,
171
- // }) {}
227
+ export class Ping extends Schema.TaggedStruct('LSD.Ping', {
228
+ liveStoreVersion,
229
+ requestId,
230
+ channelId,
231
+ }).annotations({ identifier: 'LSD.Ping' }) {}
232
+
233
+ export class Pong extends Schema.TaggedStruct('LSD.Pong', {
234
+ liveStoreVersion,
235
+ requestId,
236
+ }).annotations({ identifier: 'LSD.Pong' }) {}
172
237
 
173
- export const MessageToAppHost = Schema.Union(
238
+ export const MessageToAppHostCoordinator = Schema.Union(
174
239
  SnapshotReq,
240
+ LoadDatabaseFileReq,
175
241
  MutationLogReq,
242
+ ResetAllDataReq,
243
+ MessagePortForStoreRes,
244
+ DevtoolsReady,
245
+ Disconnect,
246
+ DevtoolsConnected,
247
+ RunMutationReq,
248
+ Ping,
249
+ ).annotations({ identifier: 'LSD.MessageToAppHostCoordinator' })
250
+
251
+ export type MessageToAppHostCoordinator = typeof MessageToAppHostCoordinator.Type
252
+
253
+ export const MessageToAppHostStore = Schema.Union(
176
254
  DebugInfoReq,
255
+ DebugInfoHistorySubscribe,
256
+ DebugInfoHistoryUnsubscribe,
177
257
  DebugInfoResetReq,
178
258
  DebugInfoRerunQueryReq,
179
- SignalsSubscribe,
180
- SignalsUnsubscribe,
259
+ ReactivityGraphSubscribe,
260
+ ReactivityGraphUnsubscribe,
181
261
  LiveQueriesSubscribe,
182
262
  LiveQueriesUnsubscribe,
183
- ResetAllDataReq,
184
- DevtoolsReadyBroadcast,
185
- Disconnect,
186
- DevtoolsConnected,
187
- )
263
+ // Ping,
264
+ ).annotations({ identifier: 'LSD.MessageToAppHostStore' })
188
265
 
189
- export type MessageToAppHost = typeof MessageToAppHost.Type
266
+ export type MessageToAppHostStore = typeof MessageToAppHostStore.Type
190
267
 
191
- export const MessageFromAppHost = Schema.Union(
268
+ export const MessageFromAppHostCoordinator = Schema.Union(
192
269
  SnapshotRes,
270
+ LoadDatabaseFileRes,
193
271
  MutationLogRes,
194
- DebugInfoRes,
195
- DebugInfoResetRes,
196
- DebugInfoRerunQueryRes,
197
- SignalsRes,
198
- LiveQueriesRes,
199
272
  ResetAllDataRes,
273
+ MessagePortForStoreReq,
200
274
  Disconnect,
201
- // SchemaChanged,
202
275
  MutationBroadcast,
203
- AppHostReadyBroadcast,
204
- NetworkStatusBroadcast,
205
- )
276
+ AppHostReady,
277
+ NetworkStatusChanged,
278
+ RunMutationRes,
279
+ Pong,
280
+ ).annotations({ identifier: 'LSD.MessageFromAppHostCoordinator' })
206
281
 
207
- export type MessageFromAppHost = typeof MessageFromAppHost.Type
282
+ export type MessageFromAppHostCoordinator = typeof MessageFromAppHostCoordinator.Type
283
+
284
+ export const MessageFromAppHostStore = Schema.Union(
285
+ DebugInfoRes,
286
+ DebugInfoHistoryRes,
287
+ DebugInfoResetRes,
288
+ DebugInfoRerunQueryRes,
289
+ ReactivityGraphRes,
290
+ LiveQueriesRes,
291
+ // Pong,
292
+ ).annotations({ identifier: 'LSD.MessageFromAppHostStore' })
208
293
 
209
- // TODO make specific over app key
210
- export const makeBroadcastChannels = () => ({
211
- fromAppHost: new BroadcastChannel(`livestore-devtools-from-app-host`),
212
- toAppHost: new BroadcastChannel(`livestore-devtools-to-app-host`),
213
- })
294
+ export type MessageFromAppHostStore = typeof MessageFromAppHostStore.Type
@@ -0,0 +1,27 @@
1
+ import { Schema, Transferable } from '@livestore/utils/effect'
2
+
3
+ const channelId = Schema.String
4
+
5
+ export namespace DevtoolsWindowMessage {
6
+ /** Message is being created in contentscript-iframe, sent to contentscript and then sent to Store */
7
+ export class MessagePortReady extends Schema.TaggedStruct('LSD.WindowMessage.MessagePortForStore', {
8
+ port: Transferable.MessagePort,
9
+ channelId,
10
+ }) {}
11
+
12
+ export class ContentscriptListening extends Schema.TaggedStruct('LSD.WindowMessage.ContentscriptListening', {}) {}
13
+
14
+ // export class ContentscriptReady extends Schema.TaggedStruct('LSD.WindowMessage.ContentscriptReady', {
15
+ // channelId,
16
+ // }) {}
17
+
18
+ export class LoadIframe extends Schema.TaggedStruct('LSD.WindowMessage.LoadIframe', {}) {}
19
+
20
+ export class StoreReady extends Schema.TaggedStruct('LSD.WindowMessage.StoreReady', {
21
+ channelId,
22
+ }) {}
23
+
24
+ export class MessageForStore extends Schema.Union(MessagePortReady, ContentscriptListening) {}
25
+
26
+ export class MessageForContentscript extends Schema.Union(StoreReady, LoadIframe) {}
27
+ }
@@ -1 +1,2 @@
1
1
  export * from './devtools-messages.js'
2
+ export * from './devtools-window-message.js'
package/src/index.ts CHANGED
@@ -11,3 +11,4 @@ export * from './sync/index.js'
11
11
  export * as Devtools from './devtools/index.js'
12
12
  export * from './debug-info.js'
13
13
  export * from './bounded-collections.js'
14
+ export * from './version.js'
@@ -1,91 +1,126 @@
1
1
  import { shouldNeverHappen } from '@livestore/utils'
2
- import { Schema } from '@livestore/utils/effect'
2
+ import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
3
3
 
4
- import type { InMemoryDatabase, MigrationOptionsFromMutationLog } from './adapter-types.js'
4
+ import { type InMemoryDatabase, type MigrationOptionsFromMutationLog, SqliteError } from './adapter-types.js'
5
5
  import { getExecArgsFromMutation } from './mutation.js'
6
6
  import type { LiveStoreSchema, MutationLogMetaRow } from './schema/index.js'
7
7
  import { MUTATION_LOG_META_TABLE } from './schema/index.js'
8
+ import type { PreparedBindValues } from './util.js'
9
+ import { sql } from './util.js'
8
10
 
9
- export const rehydrateFromMutationLog = async ({
11
+ export const rehydrateFromMutationLog = ({
10
12
  logDb,
11
13
  db,
12
14
  schema,
13
15
  migrationOptions,
16
+ onProgress,
14
17
  }: {
15
18
  logDb: InMemoryDatabase
16
19
  db: InMemoryDatabase
17
20
  schema: LiveStoreSchema
18
21
  migrationOptions: MigrationOptionsFromMutationLog
19
- }) => {
20
- try {
21
- // TODO possibly implement this in a streaming fashion
22
- const stmt = logDb.prepare(`SELECT * FROM ${MUTATION_LOG_META_TABLE} ORDER BY id ASC`)
23
- const results = stmt.select<MutationLogMetaRow>(undefined)
22
+ onProgress: (_: { done: number; total: number }) => Effect.Effect<void>
23
+ }) =>
24
+ Effect.gen(function* () {
25
+ const mutationsCount = logDb
26
+ .prepare(`SELECT COUNT(*) AS count FROM ${MUTATION_LOG_META_TABLE}`)
27
+ .select<{ count: number }>(undefined)[0]!.count
24
28
 
25
- performance.mark('livestore:hydrate-from-mutationlog:start')
29
+ const processMutation = (row: MutationLogMetaRow) =>
30
+ Effect.gen(function* () {
31
+ const mutationDef = schema.mutations.get(row.mutation) ?? shouldNeverHappen(`Unknown mutation ${row.mutation}`)
26
32
 
27
- for (const row of results) {
28
- const mutationDef = schema.mutations.get(row.mutation) ?? shouldNeverHappen(`Unknown mutation ${row.mutation}`)
33
+ if (migrationOptions.excludeMutations?.has(row.mutation) === true) return
29
34
 
30
- if (migrationOptions.excludeMutations?.has(row.mutation) === true) continue
31
-
32
- if (Schema.hash(mutationDef.schema) !== row.schemaHash) {
33
- console.warn(`Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`)
34
- }
35
+ if (Schema.hash(mutationDef.schema) !== row.schemaHash) {
36
+ console.warn(`Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`)
37
+ }
35
38
 
36
- const argsDecodedEither = Schema.decodeUnknownEither(Schema.parseJson(mutationDef.schema))(row.argsJson)
37
- if (argsDecodedEither._tag === 'Left') {
38
- return shouldNeverHappen(`\
39
+ const argsDecodedEither = Schema.decodeUnknownEither(Schema.parseJson(mutationDef.schema))(row.argsJson)
40
+ if (argsDecodedEither._tag === 'Left') {
41
+ return shouldNeverHappen(`\
39
42
  There was an error decoding the persisted mutation event args for mutation "${row.mutation}".
40
43
  This likely means the schema has changed in an incompatible way.
41
44
 
42
45
  Error: ${argsDecodedEither.left}
43
46
  `)
44
- }
45
-
46
- const mutationEventDecoded = {
47
- id: row.id,
48
- mutation: row.mutation,
49
- args: argsDecodedEither.right,
50
- }
51
- // const argsEncoded = JSON.parse(row.args_json)
52
- // const mutationSqlRes =
53
- // typeof mutation.sql === 'string'
54
- // ? mutation.sql
55
- // : mutation.sql(Schema.decodeUnknownSync(mutation.schema)(argsEncoded))
56
- // const mutationSql = typeof mutationSqlRes === 'string' ? mutationSqlRes : mutationSqlRes.sql
57
- // const bindValues = typeof mutationSqlRes === 'string' ? argsEncoded : mutationSqlRes.bindValues
58
-
59
- const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
60
-
61
- for (const { statementSql, bindValues } of execArgsArr) {
62
- try {
63
- const getRowsChanged = db.execute(statementSql, bindValues)
64
- if (
65
- import.meta.env.DEV &&
66
- getRowsChanged() === 0 &&
67
- migrationOptions.logging?.excludeAffectedRows?.(statementSql) !== true
68
- ) {
69
- console.warn(`Mutation "${mutationDef.name}" did not affect any rows:`, statementSql, bindValues)
47
+ }
48
+
49
+ const mutationEventDecoded = {
50
+ id: row.id,
51
+ mutation: row.mutation,
52
+ args: argsDecodedEither.right,
53
+ }
54
+ // const argsEncoded = JSON.parse(row.args_json)
55
+ // const mutationSqlRes =
56
+ // typeof mutation.sql === 'string'
57
+ // ? mutation.sql
58
+ // : mutation.sql(Schema.decodeUnknownSync(mutation.schema)(argsEncoded))
59
+ // const mutationSql = typeof mutationSqlRes === 'string' ? mutationSqlRes : mutationSqlRes.sql
60
+ // const bindValues = typeof mutationSqlRes === 'string' ? argsEncoded : mutationSqlRes.bindValues
61
+
62
+ const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
63
+
64
+ for (const { statementSql, bindValues } of execArgsArr) {
65
+ try {
66
+ // TODO cache prepared statements for mutations
67
+ const getRowsChanged = db.execute(statementSql, bindValues)
68
+ if (
69
+ import.meta.env.DEV &&
70
+ getRowsChanged() === 0 &&
71
+ migrationOptions.logging?.excludeAffectedRows?.(statementSql) !== true
72
+ ) {
73
+ console.warn(`Mutation "${mutationDef.name}" did not affect any rows:`, statementSql, bindValues)
74
+ }
75
+ // console.log(`Re-executed mutation ${mutationSql}`, bindValues)
76
+ } catch (e) {
77
+ yield* new SqliteError({
78
+ sql: statementSql,
79
+ bindValues,
80
+ code: (e as any).resultCode,
81
+ cause: e,
82
+ })
70
83
  }
71
- // console.log(`Re-executed mutation ${mutationSql}`, bindValues)
72
- } catch (e) {
73
- console.error(`Error executing migration for mutation ${statementSql}`, bindValues, e)
74
- debugger
75
- throw e
76
84
  }
77
- }
78
- }
79
- } catch (e) {
80
- console.error('Error while rehydrating database from mutation log', e)
81
- debugger
82
- throw e
83
- } finally {
84
- performance.mark('livestore:hydrate-from-mutationlog:end')
85
- performance.measure(
86
- 'livestore:hydrate-from-mutationlog',
87
- 'livestore:hydrate-from-mutationlog:start',
88
- 'livestore:hydrate-from-mutationlog:end',
85
+ }).pipe(Effect.withSpan(`@livestore/common:rehydrateFromMutationLog:processMutation`))
86
+
87
+ const CHUNK_SIZE = 100
88
+
89
+ const stmt = logDb.prepare(sql`\
90
+ SELECT * FROM ${MUTATION_LOG_META_TABLE}
91
+ WHERE id > COALESCE($id, '')
92
+ ORDER BY id ASC
93
+ LIMIT ${CHUNK_SIZE}
94
+ `)
95
+
96
+ let processedMutations = 0
97
+
98
+ yield* Stream.unfoldChunk<Chunk.Chunk<MutationLogMetaRow> | { _tag: 'Initial ' }, MutationLogMetaRow>(
99
+ { _tag: 'Initial ' },
100
+ (item) => {
101
+ // End stream if no more rows
102
+ if (Chunk.isChunk(item) && item.length === 0) return Option.none()
103
+
104
+ const lastId = Chunk.isChunk(item) ? Chunk.last(item).pipe(Option.getOrUndefined)?.id : undefined
105
+ const nextItem = Chunk.fromIterable(
106
+ stmt.select<MutationLogMetaRow>({ $id: lastId } as any as PreparedBindValues),
107
+ )
108
+ const prevItem = Chunk.isChunk(item) ? item : Chunk.empty()
109
+ return Option.some([prevItem, nextItem])
110
+ },
111
+ ).pipe(
112
+ Stream.bufferChunks({ capacity: 2 }),
113
+ Stream.tap((row) =>
114
+ Effect.gen(function* () {
115
+ yield* processMutation(row)
116
+
117
+ processedMutations++
118
+ yield* onProgress({ done: processedMutations, total: mutationsCount })
119
+ }),
120
+ ),
121
+ Stream.runDrain,
89
122
  )
90
- }
91
- }
123
+ }).pipe(
124
+ Effect.withPerformanceMeasure('@livestore/common:rehydrateFromMutationLog'),
125
+ Effect.withSpan('@livestore/common:rehydrateFromMutationLog'),
126
+ )
@@ -1,3 +1,4 @@
1
+ import { memoizeByRef } from '@livestore/utils'
1
2
  import { cuid } from '@livestore/utils/cuid'
2
3
  import { Schema } from '@livestore/utils/effect'
3
4
 
@@ -92,7 +93,7 @@ export const rawSqlMutation = defineMutation(
92
93
  'livestore.RawSql',
93
94
  Schema.Struct({
94
95
  sql: Schema.String,
95
- bindValues: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
96
+ bindValues: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
96
97
  writeTables: Schema.optional(Schema.ReadonlySet(Schema.String)),
97
98
  }),
98
99
  ({ sql, bindValues, writeTables }) => ({ sql, bindValues: bindValues ?? {}, writeTables }),
@@ -139,11 +140,11 @@ export type MutationEventSchema<TMutationsDefRecord extends MutationDefRecord> =
139
140
  }[keyof TMutationsDefRecord]
140
141
  >
141
142
 
142
- export const makeMutationEventSchema = <TMutationsDefRecord extends MutationDefRecord>(
143
- mutationDefRecord: TMutationsDefRecord,
144
- ): MutationEventSchema<TMutationsDefRecord> =>
143
+ export const makeMutationEventSchema = <TSchema extends LiveStoreSchema>(
144
+ schema: TSchema,
145
+ ): MutationEventSchema<TSchema['_MutationDefMapType']> =>
145
146
  Schema.Union(
146
- ...Object.values(mutationDefRecord).map((def) =>
147
+ ...[...schema.mutations.values()].map((def) =>
147
148
  Schema.Struct({
148
149
  mutation: Schema.Literal(def.name),
149
150
  args: def.schema,
@@ -152,6 +153,8 @@ export const makeMutationEventSchema = <TMutationsDefRecord extends MutationDefR
152
153
  ),
153
154
  ).annotations({ title: 'MutationEventSchema' }) as any
154
155
 
156
+ export const makeMutationEventSchemaMemo = memoizeByRef(makeMutationEventSchema)
157
+
155
158
  export const mutationEventSchemaDecodedAny = Schema.Struct({
156
159
  mutation: Schema.String,
157
160
  args: Schema.Any,
@@ -86,8 +86,6 @@ export type TableOptions = {
86
86
  * @default false
87
87
  */
88
88
  isSingleton: boolean
89
- // TODO remove
90
- dynamicRegistration: boolean
91
89
  disableAutomaticIdColumn: boolean
92
90
  /**
93
91
  * Setting this to true will automatically derive insert, update and delete mutations for this table. Example:
@@ -139,7 +137,6 @@ export const table = <
139
137
 
140
138
  const options_: TableOptions = {
141
139
  isSingleton: options?.isSingleton ?? false,
142
- dynamicRegistration: options?.dynamicRegistration ?? false,
143
140
  disableAutomaticIdColumn: options?.disableAutomaticIdColumn ?? false,
144
141
  deriveMutations:
145
142
  options?.deriveMutations === true
@@ -239,7 +236,6 @@ type WithId<TColumns extends SqliteDsl.Columns, TOptions extends TableOptions> =
239
236
 
240
237
  type WithDefaults<TOptionsInput extends TableOptionsInput, TIsSingleColumn extends boolean> = {
241
238
  isSingleton: TOptionsInput['isSingleton'] extends true ? true : false
242
- dynamicRegistration: TOptionsInput['dynamicRegistration'] extends true ? true : false
243
239
  disableAutomaticIdColumn: TOptionsInput['disableAutomaticIdColumn'] extends true ? true : false
244
240
  deriveMutations: TOptionsInput['deriveMutations'] extends true
245
241
  ? { enabled: true; localOnly: boolean }