@livestore/livestore 0.3.0-dev.5 → 0.3.0-dev.51

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 (170) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/QueryCache.d.ts.map +1 -1
  3. package/dist/SqliteDbWrapper.d.ts +60 -0
  4. package/dist/SqliteDbWrapper.d.ts.map +1 -0
  5. package/dist/{SynchronousDatabaseWrapper.js → SqliteDbWrapper.js} +69 -34
  6. package/dist/SqliteDbWrapper.js.map +1 -0
  7. package/dist/effect/LiveStore.d.ts +6 -34
  8. package/dist/effect/LiveStore.d.ts.map +1 -1
  9. package/dist/effect/LiveStore.js +10 -12
  10. package/dist/effect/LiveStore.js.map +1 -1
  11. package/dist/effect/mod.d.ts +3 -0
  12. package/dist/effect/mod.d.ts.map +1 -0
  13. package/dist/effect/mod.js +3 -0
  14. package/dist/effect/mod.js.map +1 -0
  15. package/dist/internal/mod.d.ts +3 -0
  16. package/dist/internal/mod.d.ts.map +1 -0
  17. package/dist/internal/mod.js +3 -0
  18. package/dist/internal/mod.js.map +1 -0
  19. package/dist/live-queries/base-class.d.ts +69 -29
  20. package/dist/live-queries/base-class.d.ts.map +1 -1
  21. package/dist/live-queries/base-class.js +60 -14
  22. package/dist/live-queries/base-class.js.map +1 -1
  23. package/dist/live-queries/client-document-get-query.d.ts +12 -0
  24. package/dist/live-queries/client-document-get-query.d.ts.map +1 -0
  25. package/dist/live-queries/client-document-get-query.js +18 -0
  26. package/dist/live-queries/client-document-get-query.js.map +1 -0
  27. package/dist/live-queries/computed.d.ts +13 -15
  28. package/dist/live-queries/computed.d.ts.map +1 -1
  29. package/dist/live-queries/computed.js +37 -15
  30. package/dist/live-queries/computed.js.map +1 -1
  31. package/dist/live-queries/db-query.d.ts +93 -0
  32. package/dist/live-queries/db-query.d.ts.map +1 -0
  33. package/dist/live-queries/{db.js → db-query.js} +113 -40
  34. package/dist/live-queries/db-query.js.map +1 -0
  35. package/dist/live-queries/db-query.test.d.ts +2 -0
  36. package/dist/live-queries/db-query.test.d.ts.map +1 -0
  37. package/dist/live-queries/db-query.test.js +133 -0
  38. package/dist/live-queries/db-query.test.js.map +1 -0
  39. package/dist/live-queries/mod.d.ts +5 -0
  40. package/dist/live-queries/mod.d.ts.map +1 -0
  41. package/dist/live-queries/mod.js +5 -0
  42. package/dist/live-queries/mod.js.map +1 -0
  43. package/dist/live-queries/signal.d.ts +25 -0
  44. package/dist/live-queries/signal.d.ts.map +1 -0
  45. package/dist/live-queries/signal.js +50 -0
  46. package/dist/live-queries/signal.js.map +1 -0
  47. package/dist/live-queries/signal.test.d.ts +2 -0
  48. package/dist/live-queries/signal.test.d.ts.map +1 -0
  49. package/dist/live-queries/signal.test.js +25 -0
  50. package/dist/live-queries/signal.test.js.map +1 -0
  51. package/dist/mod.d.ts +14 -0
  52. package/dist/mod.d.ts.map +1 -0
  53. package/dist/mod.js +13 -0
  54. package/dist/mod.js.map +1 -0
  55. package/dist/reactive.d.ts +23 -17
  56. package/dist/reactive.d.ts.map +1 -1
  57. package/dist/reactive.js +23 -19
  58. package/dist/reactive.js.map +1 -1
  59. package/dist/reactive.test.js +1 -1
  60. package/dist/reactive.test.js.map +1 -1
  61. package/dist/store/create-store.d.ts +70 -12
  62. package/dist/store/create-store.d.ts.map +1 -1
  63. package/dist/store/create-store.js +68 -19
  64. package/dist/store/create-store.js.map +1 -1
  65. package/dist/store/devtools.d.ts +5 -4
  66. package/dist/store/devtools.d.ts.map +1 -1
  67. package/dist/store/devtools.js +92 -40
  68. package/dist/store/devtools.js.map +1 -1
  69. package/dist/store/store-types.d.ts +54 -42
  70. package/dist/store/store-types.d.ts.map +1 -1
  71. package/dist/store/store-types.js +2 -5
  72. package/dist/store/store-types.js.map +1 -1
  73. package/dist/store/store.d.ts +141 -35
  74. package/dist/store/store.d.ts.map +1 -1
  75. package/dist/store/store.js +322 -154
  76. package/dist/store/store.js.map +1 -1
  77. package/dist/utils/data-structures.d.ts.map +1 -1
  78. package/dist/utils/dev.d.ts.map +1 -1
  79. package/dist/utils/dev.js +6 -1
  80. package/dist/utils/dev.js.map +1 -1
  81. package/dist/utils/function-string.d.ts +7 -0
  82. package/dist/utils/function-string.d.ts.map +1 -0
  83. package/dist/utils/function-string.js +9 -0
  84. package/dist/utils/function-string.js.map +1 -0
  85. package/dist/utils/stack-info.d.ts.map +1 -1
  86. package/dist/utils/stack-info.js +6 -1
  87. package/dist/utils/stack-info.js.map +1 -1
  88. package/dist/utils/stack-info.test.js +54 -1
  89. package/dist/utils/stack-info.test.js.map +1 -1
  90. package/dist/utils/tests/fixture.d.ts +59 -216
  91. package/dist/utils/tests/fixture.d.ts.map +1 -1
  92. package/dist/utils/tests/fixture.js +23 -18
  93. package/dist/utils/tests/fixture.js.map +1 -1
  94. package/dist/utils/tests/mod.d.ts +1 -0
  95. package/dist/utils/tests/mod.d.ts.map +1 -1
  96. package/dist/utils/tests/mod.js +1 -0
  97. package/dist/utils/tests/mod.js.map +1 -1
  98. package/dist/utils/tests/otel.d.ts.map +1 -1
  99. package/dist/utils/tests/otel.js +8 -3
  100. package/dist/utils/tests/otel.js.map +1 -1
  101. package/package.json +29 -26
  102. package/src/{SynchronousDatabaseWrapper.ts → SqliteDbWrapper.ts} +92 -42
  103. package/src/effect/LiveStore.ts +27 -64
  104. package/src/effect/{index.ts → mod.ts} +2 -3
  105. package/src/internal/mod.ts +2 -0
  106. package/src/live-queries/__snapshots__/{db.test.ts.snap → db-query.test.ts.snap} +241 -45
  107. package/src/live-queries/base-class.ts +170 -53
  108. package/src/live-queries/client-document-get-query.ts +52 -0
  109. package/src/live-queries/computed.ts +51 -33
  110. package/src/live-queries/db-query.test.ts +192 -0
  111. package/src/live-queries/{db.ts → db-query.ts} +171 -82
  112. package/src/live-queries/mod.ts +4 -0
  113. package/src/live-queries/signal.test.ts +40 -0
  114. package/src/live-queries/signal.ts +81 -0
  115. package/src/mod.ts +51 -0
  116. package/src/reactive.test.ts +1 -1
  117. package/src/reactive.ts +66 -43
  118. package/src/store/create-store.ts +188 -62
  119. package/src/store/devtools.ts +124 -46
  120. package/src/store/store-types.ts +54 -43
  121. package/src/store/store.ts +457 -237
  122. package/src/utils/dev.ts +6 -1
  123. package/src/utils/function-string.ts +12 -0
  124. package/src/utils/stack-info.test.ts +58 -1
  125. package/src/utils/stack-info.ts +6 -1
  126. package/src/utils/tests/fixture.ts +22 -31
  127. package/src/utils/tests/mod.ts +1 -0
  128. package/src/utils/tests/otel.ts +10 -3
  129. package/dist/SynchronousDatabaseWrapper.d.ts +0 -41
  130. package/dist/SynchronousDatabaseWrapper.d.ts.map +0 -1
  131. package/dist/SynchronousDatabaseWrapper.js.map +0 -1
  132. package/dist/effect/index.d.ts +0 -2
  133. package/dist/effect/index.d.ts.map +0 -1
  134. package/dist/effect/index.js +0 -2
  135. package/dist/effect/index.js.map +0 -1
  136. package/dist/global-state.d.ts +0 -14
  137. package/dist/global-state.d.ts.map +0 -1
  138. package/dist/global-state.js +0 -16
  139. package/dist/global-state.js.map +0 -1
  140. package/dist/index.d.ts +0 -20
  141. package/dist/index.d.ts.map +0 -1
  142. package/dist/index.js +0 -16
  143. package/dist/index.js.map +0 -1
  144. package/dist/live-queries/db.d.ts +0 -66
  145. package/dist/live-queries/db.d.ts.map +0 -1
  146. package/dist/live-queries/db.js.map +0 -1
  147. package/dist/live-queries/db.test.d.ts +0 -2
  148. package/dist/live-queries/db.test.d.ts.map +0 -1
  149. package/dist/live-queries/db.test.js +0 -118
  150. package/dist/live-queries/db.test.js.map +0 -1
  151. package/dist/live-queries/graphql.d.ts +0 -49
  152. package/dist/live-queries/graphql.d.ts.map +0 -1
  153. package/dist/live-queries/graphql.js +0 -122
  154. package/dist/live-queries/graphql.js.map +0 -1
  155. package/dist/row-query-utils.d.ts +0 -17
  156. package/dist/row-query-utils.d.ts.map +0 -1
  157. package/dist/row-query-utils.js +0 -30
  158. package/dist/row-query-utils.js.map +0 -1
  159. package/dist/utils/otel.d.ts +0 -4
  160. package/dist/utils/otel.d.ts.map +0 -1
  161. package/dist/utils/otel.js +0 -6
  162. package/dist/utils/otel.js.map +0 -1
  163. package/src/global-state.ts +0 -20
  164. package/src/index.ts +0 -66
  165. package/src/live-queries/db.test.ts +0 -154
  166. package/src/live-queries/graphql.ts +0 -219
  167. package/src/row-query-utils.ts +0 -65
  168. package/src/utils/otel.ts +0 -9
  169. package/tsconfig.json +0 -18
  170. package/vitest.config.js +0 -9
@@ -1,219 +0,0 @@
1
- import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
2
- import type { QueryInfo } from '@livestore/common'
3
- import { shouldNeverHappen } from '@livestore/utils'
4
- import { Schema, TreeFormatter } from '@livestore/utils/effect'
5
- import * as otel from '@opentelemetry/api'
6
- import * as graphql from 'graphql'
7
-
8
- import { globalReactivityGraph } from '../global-state.js'
9
- import { isThunk, type Thunk } from '../reactive.js'
10
- import type { Store } from '../store/store.js'
11
- import type { BaseGraphQLContext, RefreshReason } from '../store/store-types.js'
12
- import { getDurationMsFromSpan } from '../utils/otel.js'
13
- import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js'
14
- import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js'
15
-
16
- export type MapResult<To, From> = ((res: From, get: GetAtomResult) => To) | Schema.Schema<To, From>
17
-
18
- export const queryGraphQL = <
19
- TResult extends Record<string, any>,
20
- TVariableValues extends Record<string, any>,
21
- TResultMapped extends Record<string, any> = TResult,
22
- >(
23
- document: DocumentNode<TResult, TVariableValues>,
24
- genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues),
25
- {
26
- label,
27
- reactivityGraph,
28
- map,
29
- }: {
30
- label?: string
31
- reactivityGraph?: ReactivityGraph
32
- map?: MapResult<TResultMapped, TResult>
33
- } = {},
34
- ): LiveQuery<TResultMapped, QueryInfo.None> =>
35
- new LiveStoreGraphQLQuery({ document, genVariableValues, label, reactivityGraph, map })
36
-
37
- export class LiveStoreGraphQLQuery<
38
- TResult extends Record<string, any>,
39
- TVariableValues extends Record<string, any>,
40
- TContext extends BaseGraphQLContext,
41
- TResultMapped extends Record<string, any> = TResult,
42
- > extends LiveStoreQueryBase<TResultMapped, QueryInfo.None> {
43
- _tag: 'graphql' = 'graphql'
44
-
45
- /** The abstract GraphQL query */
46
- document: DocumentNode<TResult, TVariableValues>
47
-
48
- /** A reactive thunk representing the query results */
49
- results$: Thunk<TResultMapped, QueryContext, RefreshReason>
50
-
51
- variableValues$: Thunk<TVariableValues, QueryContext, RefreshReason> | undefined
52
-
53
- label: string
54
-
55
- protected reactivityGraph: ReactivityGraph
56
-
57
- queryInfo: QueryInfo.None = { _tag: 'None' }
58
-
59
- private mapResult
60
-
61
- constructor({
62
- document,
63
- label,
64
- genVariableValues,
65
- reactivityGraph,
66
- map,
67
- }: {
68
- document: DocumentNode<TResult, TVariableValues>
69
- genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues)
70
- label?: string
71
- reactivityGraph?: ReactivityGraph
72
- map?: MapResult<TResultMapped, TResult>
73
- }) {
74
- super()
75
-
76
- const labelWithDefault = label ?? graphql.getOperationAST(document)?.name?.value ?? 'graphql'
77
-
78
- this.label = labelWithDefault
79
- this.document = document
80
-
81
- this.reactivityGraph = reactivityGraph ?? globalReactivityGraph
82
-
83
- this.mapResult =
84
- map === undefined
85
- ? (res: TResult) => res as any as TResultMapped
86
- : Schema.isSchema(map)
87
- ? (res: TResult) => {
88
- const parseResult = Schema.decodeEither(map as Schema.Schema<TResultMapped, TResult>)(res)
89
- if (parseResult._tag === 'Left') {
90
- console.error(`Error parsing GraphQL query result: ${TreeFormatter.formatErrorSync(parseResult.left)}`)
91
- return shouldNeverHappen(`Error parsing SQL query result: ${parseResult.left}`)
92
- } else {
93
- return parseResult.right as TResultMapped
94
- }
95
- }
96
- : typeof map === 'function'
97
- ? map
98
- : shouldNeverHappen(`Invalid map function ${map}`)
99
-
100
- // TODO don't even create a thunk if variables are static
101
- let variableValues$OrvariableValues
102
-
103
- if (typeof genVariableValues === 'function') {
104
- variableValues$OrvariableValues = this.reactivityGraph.makeThunk(
105
- (get, _setDebugInfo, { rootOtelContext }, otelContext) => {
106
- return genVariableValues(makeGetAtomResult(get, otelContext ?? rootOtelContext))
107
- },
108
- { label: `${labelWithDefault}:variableValues`, meta: { liveStoreThunkType: 'graphql.variables' } },
109
- )
110
- this.variableValues$ = variableValues$OrvariableValues
111
- } else {
112
- variableValues$OrvariableValues = genVariableValues
113
- }
114
-
115
- const resultsLabel = `${labelWithDefault}:results`
116
- this.results$ = this.reactivityGraph.makeThunk<TResultMapped>(
117
- (get, setDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) => {
118
- const variableValues = isThunk(variableValues$OrvariableValues)
119
- ? (get(variableValues$OrvariableValues) as TVariableValues)
120
- : (variableValues$OrvariableValues as TVariableValues)
121
- const { result, queriedTables, durationMs } = this.queryOnce({
122
- document,
123
- variableValues,
124
- otelContext: otelContext ?? rootOtelContext,
125
- otelTracer,
126
- store: store as Store<TContext>,
127
- get: makeGetAtomResult(get, otelContext ?? rootOtelContext),
128
- })
129
-
130
- // Add dependencies on any tables that were used
131
- for (const tableName of queriedTables) {
132
- const tableRef = store.tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`)
133
- get(tableRef)
134
- }
135
-
136
- setDebugInfo({ _tag: 'graphql', label: resultsLabel, query: graphql.print(document), durationMs })
137
-
138
- return result
139
- },
140
- { label: resultsLabel, meta: { liveStoreThunkType: 'graphql.result' } },
141
- // otelContext,
142
- )
143
- }
144
-
145
- queryOnce = ({
146
- document,
147
- otelContext,
148
- otelTracer,
149
- variableValues,
150
- store,
151
- get,
152
- }: {
153
- document: graphql.DocumentNode
154
- otelContext: otel.Context
155
- otelTracer: otel.Tracer
156
- variableValues: TVariableValues
157
- store: Store<TContext>
158
- get: GetAtomResult
159
- }) => {
160
- const schema =
161
- store.graphQLSchema ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL schema")
162
- const context =
163
- store.graphQLContext ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL context")
164
-
165
- const operationName = graphql.getOperationAST(document)?.name?.value
166
-
167
- return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
168
- span.setAttribute('graphql.variables', JSON.stringify(variableValues))
169
- span.setAttribute('graphql.query', graphql.print(document))
170
-
171
- context.queriedTables.clear()
172
-
173
- context.otelContext = otel.trace.setSpan(otel.context.active(), span)
174
-
175
- const res = graphql.executeSync({
176
- document,
177
- contextValue: context,
178
- schema: schema,
179
- variableValues,
180
- })
181
-
182
- // TODO track number of nested SQL queries via Otel + debug info
183
-
184
- if (res.errors) {
185
- span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' })
186
- span.setAttribute('graphql.error', res.errors.join('\n'))
187
- span.setAttribute('graphql.error-detail', JSON.stringify(res.errors))
188
- console.error(`graphql error (${operationName}) - ${res.errors.length} errors`)
189
- for (const error of res.errors) {
190
- console.error(error)
191
- }
192
- debugger
193
- shouldNeverHappen(`GraphQL error: ${res.errors.join('\n')}`)
194
- }
195
-
196
- span.end()
197
-
198
- const result = this.mapResult(res.data as unknown as TResult, get)
199
-
200
- const durationMs = getDurationMsFromSpan(span)
201
-
202
- this.executionTimes.push(durationMs)
203
-
204
- return {
205
- result,
206
- queriedTables: Array.from(context.queriedTables.values()),
207
- durationMs,
208
- }
209
- })
210
- }
211
-
212
- destroy = () => {
213
- if (this.variableValues$ !== undefined) {
214
- this.reactivityGraph.destroyNode(this.variableValues$)
215
- }
216
-
217
- this.reactivityGraph.destroyNode(this.results$)
218
- }
219
- }
@@ -1,65 +0,0 @@
1
- import type { PreparedBindValues, QueryInfo } from '@livestore/common'
2
- import { SessionIdSymbol } from '@livestore/common'
3
- import { DbSchema } from '@livestore/common/schema'
4
- import { shouldNeverHappen } from '@livestore/utils'
5
- import type * as otel from '@opentelemetry/api'
6
-
7
- import type { LiveQuery, LiveQueryAny, QueryContext } from './live-queries/base-class.js'
8
- import { computed } from './live-queries/computed.js'
9
-
10
- export const rowQueryLabel = (table: DbSchema.TableDefBase, id: string | SessionIdSymbol | undefined) =>
11
- `row:${table.sqliteDef.name}${id === undefined ? '' : id === SessionIdSymbol ? `:sessionId` : `:${id}`}`
12
-
13
- export const deriveColQuery: {
14
- <TQuery extends LiveQuery<any, QueryInfo.None>, TCol extends keyof TQuery['__result!'] & string>(
15
- query$: TQuery,
16
- colName: TCol,
17
- ): LiveQuery<TQuery['__result!'][TCol], QueryInfo.None>
18
- <TQuery extends LiveQuery<any, QueryInfo.Row>, TCol extends keyof TQuery['__result!'] & string>(
19
- query$: TQuery,
20
- colName: TCol,
21
- ): LiveQuery<TQuery['__result!'][TCol], QueryInfo.Col>
22
- } = (query$: LiveQueryAny, colName: string) => {
23
- return computed((get) => get(query$)[colName], {
24
- label: `deriveColQuery:${query$.label}:${colName}`,
25
- queryInfo:
26
- query$.queryInfo._tag === 'Row'
27
- ? { _tag: 'Col', table: query$.queryInfo.table, column: colName, id: query$.queryInfo.id }
28
- : undefined,
29
- }) as any
30
- }
31
-
32
- export const makeExecBeforeFirstRun =
33
- ({
34
- id,
35
- insertValues,
36
- table,
37
- otelContext: otelContext_,
38
- }: {
39
- id?: string | SessionIdSymbol
40
- insertValues?: any
41
- table: DbSchema.TableDefBase
42
- otelContext: otel.Context | undefined
43
- }) =>
44
- ({ store }: QueryContext) => {
45
- const otelContext = otelContext_ ?? store.otel.queriesSpanContext
46
-
47
- if (table.options.isSingleton === false) {
48
- const idStr = id === SessionIdSymbol ? store.sessionId : id!
49
- const rowExists =
50
- store.syncDbWrapper.select(`SELECT 1 FROM '${table.sqliteDef.name}' WHERE id = ?`, {
51
- bindValues: [idStr] as any as PreparedBindValues,
52
- }).length === 1
53
-
54
- if (rowExists) return
55
-
56
- if (DbSchema.tableHasDerivedMutations(table) === false) {
57
- return shouldNeverHappen(
58
- `Cannot insert row for table "${table.sqliteDef.name}" which does not have 'deriveMutations: true' set`,
59
- )
60
- }
61
-
62
- // NOTE It's important that we only mutate and don't refresh here, as this function is called during a render
63
- store.mutate({ otelContext, skipRefresh: true }, table.insert({ id, ...insertValues }))
64
- }
65
- }
package/src/utils/otel.ts DELETED
@@ -1,9 +0,0 @@
1
- import type * as otel from '@opentelemetry/api'
2
-
3
- export const getDurationMsFromSpan = (span: otel.Span): number => {
4
- const durationHr: [seconds: number, nanos: number] = (span as any)._duration
5
- return durationHr[0] * 1000 + durationHr[1] / 1_000_000
6
- }
7
-
8
- export const getStartTimeHighResFromSpan = (span: otel.Span): DOMHighResTimeStamp =>
9
- (span as any)._performanceStartTime as DOMHighResTimeStamp
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "skipLibCheck": true,
7
- "resolveJsonModule": true,
8
- // "jsx": "preserve",
9
- "tsBuildInfoFile": "./dist/.tsbuildinfo"
10
- },
11
- "include": ["./src"],
12
- "references": [
13
- { "path": "../db-schema" },
14
- { "path": "../common" },
15
- { "path": "../web" },
16
- { "path": "../utils" }
17
- ]
18
- }
package/vitest.config.js DELETED
@@ -1,9 +0,0 @@
1
- import { defineConfig } from 'vite'
2
-
3
- export default defineConfig({
4
- resolve: {
5
- alias: {
6
- '@livestore/wa-sqlite/dist/wa-sqlite.mjs': '@livestore/wa-sqlite/dist/wa-sqlite.node.mjs',
7
- },
8
- },
9
- })