@livestore/common 0.0.54-dev.0 → 0.0.54-dev.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.
Files changed (46) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/adapter-types.d.ts +8 -0
  3. package/dist/adapter-types.d.ts.map +1 -1
  4. package/dist/adapter-types.js +5 -1
  5. package/dist/adapter-types.js.map +1 -1
  6. package/dist/bounded-collections.d.ts +36 -0
  7. package/dist/bounded-collections.d.ts.map +1 -0
  8. package/dist/bounded-collections.js +98 -0
  9. package/dist/bounded-collections.js.map +1 -0
  10. package/dist/debug-info.d.ts +118 -0
  11. package/dist/debug-info.d.ts.map +1 -0
  12. package/dist/debug-info.js +50 -0
  13. package/dist/debug-info.js.map +1 -0
  14. package/dist/devtools/devtools-messages.d.ts +223 -0
  15. package/dist/devtools/devtools-messages.d.ts.map +1 -0
  16. package/dist/devtools/devtools-messages.js +148 -0
  17. package/dist/devtools/devtools-messages.js.map +1 -0
  18. package/dist/devtools/index copy.d.ts +214 -0
  19. package/dist/devtools/index copy.d.ts.map +1 -0
  20. package/dist/devtools/index copy.js +137 -0
  21. package/dist/devtools/index copy.js.map +1 -0
  22. package/dist/devtools/index.d.ts +1 -136
  23. package/dist/devtools/index.d.ts.map +1 -1
  24. package/dist/devtools/index.js +1 -114
  25. package/dist/devtools/index.js.map +1 -1
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +2 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/schema/index.d.ts +1 -45
  31. package/dist/schema/index.d.ts.map +1 -1
  32. package/dist/schema/index.js +3 -53
  33. package/dist/schema/index.js.map +1 -1
  34. package/dist/util.d.ts +4 -1
  35. package/dist/util.d.ts.map +1 -1
  36. package/dist/util.js +3 -0
  37. package/dist/util.js.map +1 -1
  38. package/package.json +3 -3
  39. package/src/adapter-types.ts +9 -0
  40. package/src/bounded-collections.ts +121 -0
  41. package/src/debug-info.ts +88 -0
  42. package/src/devtools/devtools-messages.ts +186 -0
  43. package/src/devtools/index.ts +1 -146
  44. package/src/index.ts +2 -0
  45. package/src/schema/index.ts +4 -66
  46. package/src/util.ts +10 -2
@@ -0,0 +1,186 @@
1
+ import { Schema } from '@livestore/utils/effect'
2
+ import { type SqliteDsl as __SqliteDsl } from 'effect-db-schema'
3
+
4
+ import { NetworkStatus } from '../adapter-types.js'
5
+ import { DebugInfo } from '../debug-info.js'
6
+ import { mutationEventSchemaEncodedAny } from '../schema/mutations.js'
7
+ import { PreparedBindValues } from '../util.js'
8
+
9
+ const requestId = Schema.String
10
+ const channelId = Schema.String
11
+
12
+ export class SnapshotReq extends Schema.TaggedStruct('LSD.SnapshotReq', {
13
+ requestId,
14
+ channelId,
15
+ }) {}
16
+
17
+ export class SnapshotRes extends Schema.TaggedStruct('LSD.SnapshotRes', {
18
+ requestId,
19
+ snapshot: Schema.Uint8Array,
20
+ }) {}
21
+
22
+ export class DebugInfoReq extends Schema.TaggedStruct('LSD.DebugInfoReq', {
23
+ requestId,
24
+ channelId,
25
+ }) {}
26
+
27
+ export class DebugInfoRes extends Schema.TaggedStruct('LSD.DebugInfoRes', {
28
+ requestId,
29
+ debugInfo: DebugInfo,
30
+ }) {}
31
+
32
+ export class DebugInfoResetReq extends Schema.TaggedStruct('LSD.DebugInfoResetReq', {
33
+ requestId,
34
+ channelId,
35
+ }) {}
36
+
37
+ export class DebugInfoResetRes extends Schema.TaggedStruct('LSD.DebugInfoResetRes', {
38
+ requestId,
39
+ }) {}
40
+
41
+ export class DebugInfoRerunQueryReq extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryReq', {
42
+ requestId,
43
+ channelId,
44
+ queryStr: Schema.String,
45
+ bindValues: Schema.UndefinedOr(PreparedBindValues),
46
+ queriedTables: Schema.ReadonlySet(Schema.String),
47
+ }) {}
48
+
49
+ export class DebugInfoRerunQueryRes extends Schema.TaggedStruct('LSD.DebugInfoRerunQueryRes', {
50
+ requestId,
51
+ }) {}
52
+
53
+ export class MutationBroadcast extends Schema.TaggedStruct('LSD.MutationBroadcast', {
54
+ requestId,
55
+ mutationEventEncoded: mutationEventSchemaEncodedAny,
56
+ persisted: Schema.Boolean,
57
+ }) {}
58
+
59
+ export class MutationLogReq extends Schema.TaggedStruct('LSD.MutationLogReq', {
60
+ requestId,
61
+ channelId,
62
+ }) {}
63
+
64
+ export class MutationLogRes extends Schema.TaggedStruct('LSD.MutationLogRes', {
65
+ requestId,
66
+ mutationLog: Schema.Uint8Array,
67
+ }) {}
68
+
69
+ export class SignalsSubscribe extends Schema.TaggedStruct('LSD.SignalsSubscribe', {
70
+ requestId,
71
+ channelId,
72
+ includeResults: Schema.Boolean,
73
+ }) {}
74
+
75
+ export class SignalsUnsubscribe extends Schema.TaggedStruct('LSD.SignalsUnsubscribe', {
76
+ requestId,
77
+ channelId,
78
+ }) {}
79
+
80
+ export class SignalsRes extends Schema.TaggedStruct('LSD.SignalsRes', {
81
+ requestId,
82
+ signals: Schema.Any,
83
+ }) {}
84
+
85
+ export class LiveQueriesSubscribe extends Schema.TaggedStruct('LSD.LiveQueriesSubscribe', {
86
+ requestId,
87
+ channelId,
88
+ }) {}
89
+
90
+ export class LiveQueriesUnsubscribe extends Schema.TaggedStruct('LSD.LiveQueriesUnsubscribe', {
91
+ requestId,
92
+ channelId,
93
+ }) {}
94
+
95
+ export class SerializedLiveQuery extends Schema.Struct({
96
+ _tag: Schema.Literal('js', 'sql', 'graphql'),
97
+ id: Schema.Number,
98
+ label: Schema.String,
99
+ runs: Schema.Number,
100
+ executionTimes: Schema.Array(Schema.Number),
101
+ lastestResult: Schema.Any,
102
+ activeSubscriptions: Schema.Array(
103
+ Schema.Struct({ frames: Schema.Array(Schema.Struct({ name: Schema.String, filePath: Schema.String })) }),
104
+ ),
105
+ }) {}
106
+
107
+ export class LiveQueriesRes extends Schema.TaggedStruct('LSD.LiveQueriesRes', {
108
+ requestId,
109
+ liveQueries: Schema.Array(SerializedLiveQuery),
110
+ }) {}
111
+
112
+ export class ResetAllDataReq extends Schema.TaggedStruct('LSD.ResetAllDataReq', {
113
+ requestId,
114
+ channelId,
115
+ mode: Schema.Literal('all-data', 'only-app-db'),
116
+ }) {}
117
+
118
+ export class ResetAllDataRes extends Schema.TaggedStruct('LSD.ResetAllDataRes', {
119
+ requestId,
120
+ }) {}
121
+
122
+ export class NetworkStatusBroadcast extends Schema.TaggedStruct('LSD.NetworkStatusBroadcast', {
123
+ channelId,
124
+ networkStatus: NetworkStatus,
125
+ }) {}
126
+
127
+ export class DevtoolsReadyBroadcast extends Schema.TaggedStruct('LSD.DevtoolsReadyBroadcast', {}) {}
128
+
129
+ export class DevtoolsConnected extends Schema.TaggedStruct('LSD.DevtoolsConnected', {
130
+ channelId,
131
+ }) {}
132
+
133
+ export class AppHostReadyBroadcast extends Schema.TaggedStruct('LSD.AppHostReadyBroadcast', {
134
+ channelId,
135
+ }) {}
136
+
137
+ export class Disconnect extends Schema.TaggedStruct('LSD.Disconnect', {
138
+ requestId,
139
+ channelId,
140
+ }) {}
141
+
142
+ // export class SchemaChanged extends Schema.TaggedStruct('LSD.SchemaChanged', {
143
+ // requestId,
144
+ // }) {}
145
+
146
+ export const MessageToAppHost = Schema.Union(
147
+ SnapshotReq,
148
+ MutationLogReq,
149
+ DebugInfoReq,
150
+ DebugInfoResetReq,
151
+ DebugInfoRerunQueryReq,
152
+ SignalsSubscribe,
153
+ SignalsUnsubscribe,
154
+ LiveQueriesSubscribe,
155
+ LiveQueriesUnsubscribe,
156
+ ResetAllDataReq,
157
+ DevtoolsReadyBroadcast,
158
+ Disconnect,
159
+ DevtoolsConnected,
160
+ )
161
+
162
+ export type MessageToAppHost = typeof MessageToAppHost.Type
163
+
164
+ export const MessageFromAppHost = Schema.Union(
165
+ SnapshotRes,
166
+ MutationLogRes,
167
+ DebugInfoRes,
168
+ DebugInfoResetRes,
169
+ DebugInfoRerunQueryRes,
170
+ SignalsRes,
171
+ LiveQueriesRes,
172
+ ResetAllDataRes,
173
+ Disconnect,
174
+ // SchemaChanged,
175
+ MutationBroadcast,
176
+ AppHostReadyBroadcast,
177
+ NetworkStatusBroadcast,
178
+ )
179
+
180
+ export type MessageFromAppHost = typeof MessageFromAppHost.Type
181
+
182
+ // TODO make specific over app key
183
+ export const makeBroadcastChannels = () => ({
184
+ fromAppHost: new BroadcastChannel(`livestore-devtools-from-app-host`),
185
+ toAppHost: new BroadcastChannel(`livestore-devtools-to-app-host`),
186
+ })
@@ -1,146 +1 @@
1
- import { Schema } from '@livestore/utils/effect'
2
- import { type SqliteDsl as __SqliteDsl } from 'effect-db-schema'
3
-
4
- import { LiveStoreSchemaSchema } from '../schema/index.js'
5
- import { mutationEventSchemaEncodedAny } from '../schema/mutations.js'
6
-
7
- const requestId = Schema.String
8
-
9
- export class SnapshotReq extends Schema.TaggedStruct('LSD.SnapshotReq', {
10
- requestId,
11
- }) {}
12
-
13
- export class SnapshotRes extends Schema.TaggedStruct('LSD.SnapshotRes', {
14
- requestId,
15
- snapshot: Schema.Uint8Array,
16
- }) {}
17
-
18
- export class SerializedSchemaReq extends Schema.TaggedStruct('LSD.SerializedSchemaReq', {
19
- requestId,
20
- }) {}
21
-
22
- export class SerializedSchemaRes extends Schema.TaggedStruct('LSD.SerializedSchemaRes', {
23
- requestId,
24
- schema: LiveStoreSchemaSchema,
25
- }) {}
26
-
27
- export class MutationBroadcast extends Schema.TaggedStruct('LSD.MutationBroadcast', {
28
- requestId,
29
- mutationEventEncoded: mutationEventSchemaEncodedAny,
30
- persisted: Schema.Boolean,
31
- }) {}
32
-
33
- export class MutationLogReq extends Schema.TaggedStruct('LSD.MutationLogReq', {
34
- requestId,
35
- }) {}
36
-
37
- export class MutationLogRes extends Schema.TaggedStruct('LSD.MutationLogRes', {
38
- requestId,
39
- mutationLog: Schema.Uint8Array,
40
- }) {}
41
-
42
- export class SubscribeSignalsReq extends Schema.TaggedStruct('LSD.SubscribeSignalsReq', {
43
- requestId,
44
- includeResults: Schema.Boolean,
45
- }) {}
46
-
47
- export class SubscribeSignalsRes extends Schema.TaggedStruct('LSD.SubscribeSignalsRes', {
48
- requestId,
49
- signals: Schema.Any,
50
- }) {}
51
-
52
- export class SubscribeLiveQueriesReq extends Schema.TaggedStruct('LSD.SubscribeLiveQueriesReq', {
53
- requestId,
54
- }) {}
55
-
56
- export class SerializedLiveQuery extends Schema.Struct({
57
- _tag: Schema.Literal('js', 'sql', 'graphql'),
58
- id: Schema.Number,
59
- label: Schema.String,
60
- runs: Schema.Number,
61
- executionTimes: Schema.Array(Schema.Number),
62
- lastestResult: Schema.Any,
63
- activeSubscriptions: Schema.Array(
64
- Schema.Struct({ frames: Schema.Array(Schema.Struct({ name: Schema.String, filePath: Schema.String })) }),
65
- ),
66
- }) {}
67
-
68
- export class SubscribeLiveQueriesRes extends Schema.TaggedStruct('LSD.SubscribeLiveQueriesRes', {
69
- requestId,
70
- liveQueries: Schema.Array(SerializedLiveQuery),
71
- }) {}
72
-
73
- export class ResetAllDataReq extends Schema.TaggedStruct('LSD.ResetAllDataReq', {
74
- requestId,
75
- mode: Schema.Literal('all-data', 'only-app-db'),
76
- }) {}
77
-
78
- export class ResetAllDataRes extends Schema.TaggedStruct('LSD.ResetAllDataRes', {
79
- requestId,
80
- }) {}
81
-
82
- export class AppHostReadyReq extends Schema.TaggedStruct('LSD.AppHostReadyReq', {
83
- requestId,
84
- }) {}
85
- export class AppHostReadyRes extends Schema.TaggedStruct('LSD.AppHostReadyRes', {
86
- requestId,
87
- }) {}
88
-
89
- export class Disconnect extends Schema.TaggedStruct('LSD.Disconnect', {
90
- requestId,
91
- }) {}
92
-
93
- export class SchemaChanged extends Schema.TaggedStruct('LSD.SchemaChanged', {
94
- requestId,
95
- }) {}
96
-
97
- // export const Message = Schema.Union(
98
- // SnapshotReq,
99
- // SnapshotRes,
100
- // SerializedSchemaReq,
101
- // SerializedSchemaRes,
102
- // MutationBroadcast,
103
- // MutationLogReq,
104
- // MutationLogRes,
105
- // SubscribeSignalsReq,
106
- // SubscribeSignalsRes,
107
- // SubscribeLiveQueriesReq,
108
- // SubscribeLiveQueriesRes,
109
- // ResetAllDataReq,
110
- // ResetAllDataRes,
111
- // Disconnect,
112
- // SchemaChanged,
113
- // AppHostReadyReq,
114
- // AppHostReadyRes,
115
- // )
116
-
117
- // export type Message = typeof Message.Type
118
-
119
- export const MessageToAppHost = Schema.Union(
120
- SnapshotReq,
121
- SerializedSchemaReq,
122
- MutationLogReq,
123
- SubscribeSignalsReq,
124
- SubscribeLiveQueriesReq,
125
- ResetAllDataReq,
126
- AppHostReadyReq,
127
- )
128
-
129
- export type MessageToAppHost = typeof MessageToAppHost.Type
130
-
131
- export const MessageFromAppHost = Schema.Union(
132
- SnapshotRes,
133
- SerializedSchemaRes,
134
- MutationLogRes,
135
- SubscribeSignalsRes,
136
- SubscribeLiveQueriesRes,
137
- ResetAllDataRes,
138
- AppHostReadyRes,
139
- Disconnect,
140
- SchemaChanged,
141
- MutationBroadcast,
142
- )
143
-
144
- export type MessageFromAppHost = typeof MessageFromAppHost.Type
145
-
146
- export const makeBc = () => new BroadcastChannel('livestore-devtools')
1
+ export * from './devtools-messages.js'
package/src/index.ts CHANGED
@@ -9,3 +9,5 @@ export * from './query-info.js'
9
9
  export * from './derived-mutations.js'
10
10
  export * from './sync/index.js'
11
11
  export * as Devtools from './devtools/index.js'
12
+ export * from './debug-info.js'
13
+ export * from './bounded-collections.js'
@@ -1,7 +1,7 @@
1
- import { isReadonlyArray, pick, shouldNeverHappen } from '@livestore/utils'
1
+ import { isReadonlyArray, shouldNeverHappen } from '@livestore/utils'
2
2
  import type { ReadonlyArray } from '@livestore/utils/effect'
3
- import { ReadonlyRecord, Schema } from '@livestore/utils/effect'
4
- import { SqliteAst, SqliteDsl } from 'effect-db-schema'
3
+ import type { SqliteDsl } from 'effect-db-schema'
4
+ import { SqliteAst } from 'effect-db-schema'
5
5
 
6
6
  import type { MigrationOptions } from '../adapter-types.js'
7
7
  import { makeDerivedMutationDefsForTable } from '../derived-mutations.js'
@@ -13,7 +13,7 @@ import {
13
13
  rawSqlMutation,
14
14
  } from './mutations.js'
15
15
  import { systemTables } from './system-tables.js'
16
- import { table, type TableDef, tableHasDerivedMutations } from './table-def.js'
16
+ import { type TableDef, tableHasDerivedMutations } from './table-def.js'
17
17
 
18
18
  export * from './system-tables.js'
19
19
  export * as DbSchema from './table-def.js'
@@ -139,65 +139,3 @@ namespace FromInputSchema {
139
139
  ? { [K in keyof TMutations as TMutations[K]['name']]: TMutations[K] } & { 'livestore.RawSql': RawSqlMutation }
140
140
  : never
141
141
  }
142
-
143
- export const LiveStoreSchemaFromSelf: Schema.Schema<LiveStoreSchema> = Schema.declare(
144
- (_): _ is LiveStoreSchema =>
145
- (_ as any)._DbSchemaType === Symbol.for('livestore.DbSchemaType') &&
146
- (_ as any)._MutationDefMapType === Symbol.for('livestore.MutationDefMapType'),
147
- {
148
- identifier: 'LiveStoreSchemaFromSelf',
149
- pretty: () => (_) => `LiveStoreSchema(hash: ${_.hash})`,
150
- arbitrary: () => (fc) => fc.anything as any,
151
- equivalence: () => (a, b) => a.hash === b.hash,
152
- },
153
- )
154
-
155
- export const TableDefColumnSchema = Schema.Struct({
156
- columnType: Schema.Literal('blob', 'integer', 'text', 'real'),
157
- nullable: Schema.Boolean,
158
- primaryKey: Schema.Boolean,
159
- })
160
-
161
- export const TableDefSqliteSchema = Schema.Struct({
162
- name: Schema.String,
163
- columns: Schema.Record(Schema.String, TableDefColumnSchema),
164
- ast: Schema.Any,
165
- })
166
-
167
- export const TableDefSchema = Schema.Struct({
168
- sqliteDef: TableDefSqliteSchema,
169
- isSingleColumn: Schema.Boolean,
170
- options: Schema.Any,
171
- })
172
-
173
- export const LiveStoreSchemaSchema = Schema.transform(
174
- Schema.Struct({
175
- tables: Schema.Record(Schema.String, TableDefSchema),
176
- // mutations: Schema.Record(Schema.String, MutationDef.AnySchema),
177
- // migrationOptions: Schema.Struct({ strategy: Schema.Literal('hard-reset') }),
178
- }),
179
- LiveStoreSchemaFromSelf,
180
- {
181
- encode: (schema) => ({
182
- tables: ReadonlyRecord.map(Object.fromEntries(schema.tables.entries()), (t) => ({
183
- sqliteDef: {
184
- name: t.sqliteDef.name,
185
- columns: ReadonlyRecord.map(t.sqliteDef.columns, (c) => pick(c!, ['columnType', 'nullable', 'primaryKey'])),
186
- ast: JSON.parse(JSON.stringify(t.sqliteDef.ast)),
187
- },
188
- isSingleColumn: t.isSingleColumn,
189
- options: t.options,
190
- })),
191
- }),
192
- decode: (schema) => {
193
- const tables = ReadonlyRecord.map(schema.tables, (t) => {
194
- const columns = ReadonlyRecord.map(t.sqliteDef.columns, (c) => {
195
- const makeCol = SqliteDsl[c.columnType] as any
196
- return makeCol({ nullable: c.nullable, primaryKey: c.primaryKey })
197
- })
198
- return table(t.sqliteDef.name, columns, t.options)
199
- })
200
- return makeSchema({ tables })
201
- },
202
- },
203
- )
package/src/util.ts CHANGED
@@ -1,11 +1,19 @@
1
1
  /// <reference lib="es2022" />
2
2
 
3
3
  import type { Brand } from '@livestore/utils/effect'
4
+ import { Schema } from '@livestore/utils/effect'
4
5
 
5
6
  export type ParamsObject = Record<string, SqlValue>
6
7
  export type SqlValue = string | number | Uint8Array | null
7
8
 
8
- export type Bindable = SqlValue[] | ParamsObject
9
+ export type Bindable = ReadonlyArray<SqlValue> | ParamsObject
10
+
11
+ export const SqlValueSchema = Schema.Union(Schema.String, Schema.Number, Schema.Uint8Array, Schema.Null)
12
+
13
+ export const PreparedBindValues = Schema.Union(
14
+ Schema.Array(SqlValueSchema),
15
+ Schema.Record(Schema.String, SqlValueSchema),
16
+ ).pipe(Schema.brand('PreparedBindValues'))
9
17
 
10
18
  export type PreparedBindValues = Brand.Branded<Bindable, 'PreparedBindValues'>
11
19
 
@@ -33,7 +41,7 @@ export const sql = (template: TemplateStringsArray, ...args: unknown[]): string
33
41
  /* TODO: Search for unused params via proper parsing, not string search
34
42
  **/
35
43
  export const prepareBindValues = (values: Bindable, statement: string): PreparedBindValues => {
36
- if (Array.isArray(values)) return values as PreparedBindValues
44
+ if (Array.isArray(values)) return values as any as PreparedBindValues
37
45
 
38
46
  const result: ParamsObject = {}
39
47
  for (const [key, value] of Object.entries(values)) {