@livestore/common 0.0.53 → 0.0.54-dev.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.
@@ -0,0 +1,146 @@
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')
package/src/index.ts CHANGED
@@ -8,3 +8,4 @@ export * from './rehydrate-from-mutationlog.js'
8
8
  export * from './query-info.js'
9
9
  export * from './derived-mutations.js'
10
10
  export * from './sync/index.js'
11
+ export * as Devtools from './devtools/index.js'
@@ -1,6 +1,7 @@
1
- import { isReadonlyArray, shouldNeverHappen } from '@livestore/utils'
1
+ import { isReadonlyArray, pick, shouldNeverHappen } from '@livestore/utils'
2
2
  import type { ReadonlyArray } from '@livestore/utils/effect'
3
- import { SqliteAst, type SqliteDsl } from 'effect-db-schema'
3
+ import { ReadonlyRecord, Schema } from '@livestore/utils/effect'
4
+ import { SqliteAst, SqliteDsl } from 'effect-db-schema'
4
5
 
5
6
  import type { MigrationOptions } from '../adapter-types.js'
6
7
  import { makeDerivedMutationDefsForTable } from '../derived-mutations.js'
@@ -12,7 +13,7 @@ import {
12
13
  rawSqlMutation,
13
14
  } from './mutations.js'
14
15
  import { systemTables } from './system-tables.js'
15
- import { type TableDef, tableHasDerivedMutations } from './table-def.js'
16
+ import { table, type TableDef, tableHasDerivedMutations } from './table-def.js'
16
17
 
17
18
  export * from './system-tables.js'
18
19
  export * as DbSchema from './table-def.js'
@@ -20,10 +21,14 @@ export * as ParseUtils from './parse-utils.js'
20
21
  export * from './mutations.js'
21
22
  export * from './schema-helpers.js'
22
23
 
24
+ export const LiveStoreSchemaSymbol = Symbol.for('livestore.LiveStoreSchema')
25
+ export type LiveStoreSchemaSymbol = typeof LiveStoreSchemaSymbol
26
+
23
27
  export type LiveStoreSchema<
24
28
  TDbSchema extends SqliteDsl.DbSchema = SqliteDsl.DbSchema,
25
29
  TMutationsDefRecord extends MutationDefRecord = MutationDefRecord,
26
30
  > = {
31
+ readonly _Type: LiveStoreSchemaSymbol
27
32
  /** Only used on type-level */
28
33
  readonly _DbSchemaType: TDbSchema
29
34
  /** Only used on type-level */
@@ -99,8 +104,9 @@ export const makeSchema = <TInputSchema extends InputSchema>(
99
104
  })
100
105
 
101
106
  return {
102
- _DbSchemaType: Symbol('livestore.DbSchemaType') as any,
103
- _MutationDefMapType: Symbol('livestore.MutationDefMapType') as any,
107
+ _Type: LiveStoreSchemaSymbol,
108
+ _DbSchemaType: Symbol.for('livestore.DbSchemaType') as any,
109
+ _MutationDefMapType: Symbol.for('livestore.MutationDefMapType') as any,
104
110
  tables,
105
111
  mutations,
106
112
  migrationOptions: inputSchema.migrations ?? { strategy: 'hard-reset' },
@@ -133,3 +139,65 @@ namespace FromInputSchema {
133
139
  ? { [K in keyof TMutations as TMutations[K]['name']]: TMutations[K] } & { 'livestore.RawSql': RawSqlMutation }
134
140
  : never
135
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
+ )