@livestore/livestore 0.0.0-snapshot-a39a3b63b870a2878f22f82ce2979a6989076053 → 0.0.0-snapshot-97ca7eac46b6a583b22d40189126d06a377ec1b0

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 (94) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/QueryCache.d.ts.map +1 -1
  3. package/dist/SqliteDbWrapper.d.ts.map +1 -1
  4. package/dist/SqliteDbWrapper.js +1 -2
  5. package/dist/SqliteDbWrapper.js.map +1 -1
  6. package/dist/effect/LiveStore.d.ts +6 -38
  7. package/dist/effect/LiveStore.d.ts.map +1 -1
  8. package/dist/effect/LiveStore.js +6 -18
  9. package/dist/effect/LiveStore.js.map +1 -1
  10. package/dist/effect/mod.d.ts +3 -0
  11. package/dist/effect/mod.d.ts.map +1 -0
  12. package/dist/effect/mod.js +3 -0
  13. package/dist/effect/mod.js.map +1 -0
  14. package/dist/internal/mod.d.ts +3 -0
  15. package/dist/internal/mod.d.ts.map +1 -0
  16. package/dist/internal/mod.js +3 -0
  17. package/dist/internal/mod.js.map +1 -0
  18. package/dist/live-queries/base-class.d.ts +10 -6
  19. package/dist/live-queries/base-class.d.ts.map +1 -1
  20. package/dist/live-queries/base-class.js.map +1 -1
  21. package/dist/live-queries/computed.d.ts.map +1 -1
  22. package/dist/live-queries/computed.js +1 -1
  23. package/dist/live-queries/computed.js.map +1 -1
  24. package/dist/live-queries/db-query.d.ts.map +1 -1
  25. package/dist/live-queries/db-query.js +1 -2
  26. package/dist/live-queries/db-query.js.map +1 -1
  27. package/dist/live-queries/db-query.test.js +4 -1
  28. package/dist/live-queries/db-query.test.js.map +1 -1
  29. package/dist/live-queries/make-ref.d.ts.map +1 -1
  30. package/dist/live-queries/mod.d.ts +5 -0
  31. package/dist/live-queries/mod.d.ts.map +1 -0
  32. package/dist/live-queries/mod.js +5 -0
  33. package/dist/live-queries/mod.js.map +1 -0
  34. package/dist/mod.d.ts +14 -0
  35. package/dist/mod.d.ts.map +1 -0
  36. package/dist/mod.js +11 -0
  37. package/dist/mod.js.map +1 -0
  38. package/dist/reactive.d.ts.map +1 -1
  39. package/dist/row-query-utils.d.ts.map +1 -1
  40. package/dist/store/create-store.d.ts +35 -9
  41. package/dist/store/create-store.d.ts.map +1 -1
  42. package/dist/store/create-store.js +12 -4
  43. package/dist/store/create-store.js.map +1 -1
  44. package/dist/store/devtools.d.ts.map +1 -1
  45. package/dist/store/store-types.d.ts +6 -29
  46. package/dist/store/store-types.d.ts.map +1 -1
  47. package/dist/store/store-types.js +1 -5
  48. package/dist/store/store-types.js.map +1 -1
  49. package/dist/store/store.d.ts +4 -6
  50. package/dist/store/store.d.ts.map +1 -1
  51. package/dist/store/store.js +6 -11
  52. package/dist/store/store.js.map +1 -1
  53. package/dist/utils/data-structures.d.ts.map +1 -1
  54. package/dist/utils/dev.d.ts.map +1 -1
  55. package/dist/utils/function-string.d.ts.map +1 -1
  56. package/dist/utils/stack-info.d.ts.map +1 -1
  57. package/dist/utils/tests/fixture.d.ts +1 -1
  58. package/dist/utils/tests/fixture.d.ts.map +1 -1
  59. package/dist/utils/tests/fixture.js.map +1 -1
  60. package/dist/utils/tests/otel.d.ts.map +1 -1
  61. package/package.json +20 -22
  62. package/src/SqliteDbWrapper.ts +8 -2
  63. package/src/effect/LiveStore.ts +22 -70
  64. package/src/effect/{index.ts → mod.ts} +2 -3
  65. package/src/internal/mod.ts +2 -0
  66. package/src/live-queries/base-class.ts +12 -7
  67. package/src/live-queries/computed.ts +1 -1
  68. package/src/live-queries/db-query.test.ts +4 -1
  69. package/src/live-queries/db-query.ts +1 -1
  70. package/src/live-queries/mod.ts +4 -0
  71. package/src/mod.ts +37 -0
  72. package/src/store/create-store.ts +56 -20
  73. package/src/store/store-types.ts +6 -30
  74. package/src/store/store.ts +11 -25
  75. package/src/utils/tests/fixture.ts +1 -1
  76. package/dist/effect/index.d.ts +0 -2
  77. package/dist/effect/index.d.ts.map +0 -1
  78. package/dist/effect/index.js +0 -2
  79. package/dist/effect/index.js.map +0 -1
  80. package/dist/index.d.ts +0 -19
  81. package/dist/index.d.ts.map +0 -1
  82. package/dist/index.js +0 -16
  83. package/dist/index.js.map +0 -1
  84. package/dist/live-queries/graphql.d.ts +0 -49
  85. package/dist/live-queries/graphql.d.ts.map +0 -1
  86. package/dist/live-queries/graphql.js +0 -147
  87. package/dist/live-queries/graphql.js.map +0 -1
  88. package/dist/utils/otel.d.ts +0 -4
  89. package/dist/utils/otel.d.ts.map +0 -1
  90. package/dist/utils/otel.js +0 -6
  91. package/dist/utils/otel.js.map +0 -1
  92. package/src/index.ts +0 -72
  93. package/src/live-queries/graphql.ts +0 -244
  94. package/src/utils/otel.ts +0 -9
@@ -1,244 +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 { isThunk, type Thunk } from '../reactive.js'
9
- import type { Store } from '../store/store.js'
10
- import type { BaseGraphQLContext, RefreshReason } from '../store/store-types.js'
11
- import { getDurationMsFromSpan } from '../utils/otel.js'
12
- import type { DepKey, GetAtomResult, LiveQueryDef, ReactivityGraph, ReactivityGraphContext } from './base-class.js'
13
- import { depsToString, LiveStoreQueryBase, makeGetAtomResult, withRCMap } from './base-class.js'
14
-
15
- export type MapResult<To, From> = ((res: From, get: GetAtomResult) => To) | Schema.Schema<To, From>
16
-
17
- export const queryGraphQL = <
18
- TResult extends Record<string, any>,
19
- TVariableValues extends Record<string, any>,
20
- TResultMapped extends Record<string, any> = TResult,
21
- >(
22
- document: DocumentNode<TResult, TVariableValues>,
23
- genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues),
24
- options: {
25
- label?: string
26
- // reactivityGraph?: ReactivityGraph
27
- map?: MapResult<TResultMapped, TResult>
28
- deps?: DepKey
29
- } = {},
30
- ): LiveQueryDef<TResultMapped, QueryInfo.None> => {
31
- const documentName = graphql.getOperationAST(document)?.name?.value
32
- const hash = options.deps
33
- ? depsToString(options.deps)
34
- : (documentName ?? shouldNeverHappen('No document name found and no deps provided'))
35
- const label = options.label ?? documentName ?? 'graphql'
36
- const map = options.map
37
-
38
- return {
39
- _tag: 'def',
40
- make: withRCMap(hash, (ctx, _otelContext) => {
41
- return new LiveStoreGraphQLQuery({
42
- document,
43
- genVariableValues,
44
- label,
45
- map,
46
- reactivityGraph: ctx.reactivityGraph.deref()!,
47
- })
48
- }),
49
- label,
50
- hash,
51
- queryInfo: { _tag: 'None' },
52
- }
53
- }
54
-
55
- export class LiveStoreGraphQLQuery<
56
- TResult extends Record<string, any>,
57
- TVariableValues extends Record<string, any>,
58
- TContext extends BaseGraphQLContext,
59
- TResultMapped extends Record<string, any> = TResult,
60
- > extends LiveStoreQueryBase<TResultMapped, QueryInfo.None> {
61
- _tag: 'graphql' = 'graphql'
62
-
63
- /** The abstract GraphQL query */
64
- document: DocumentNode<TResult, TVariableValues>
65
-
66
- /** A reactive thunk representing the query results */
67
- results$: Thunk<TResultMapped, ReactivityGraphContext, RefreshReason>
68
-
69
- variableValues$: Thunk<TVariableValues, ReactivityGraphContext, RefreshReason> | undefined
70
-
71
- label: string
72
-
73
- reactivityGraph: ReactivityGraph
74
-
75
- queryInfo: QueryInfo.None = { _tag: 'None' }
76
-
77
- private mapResult
78
-
79
- constructor({
80
- document,
81
- label,
82
- genVariableValues,
83
- reactivityGraph,
84
- map,
85
- }: {
86
- document: DocumentNode<TResult, TVariableValues>
87
- genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues)
88
- label?: string
89
- reactivityGraph: ReactivityGraph
90
- map?: MapResult<TResultMapped, TResult>
91
- }) {
92
- super()
93
-
94
- const labelWithDefault = label ?? graphql.getOperationAST(document)?.name?.value ?? 'graphql'
95
-
96
- this.label = labelWithDefault
97
- this.document = document
98
-
99
- this.reactivityGraph = reactivityGraph
100
-
101
- this.mapResult =
102
- map === undefined
103
- ? (res: TResult) => res as any as TResultMapped
104
- : Schema.isSchema(map)
105
- ? (res: TResult) => {
106
- const parseResult = Schema.decodeEither(map as Schema.Schema<TResultMapped, TResult>)(res)
107
- if (parseResult._tag === 'Left') {
108
- console.error(`Error parsing GraphQL query result: ${TreeFormatter.formatErrorSync(parseResult.left)}`)
109
- return shouldNeverHappen(`Error parsing SQL query result: ${parseResult.left}`)
110
- } else {
111
- return parseResult.right as TResultMapped
112
- }
113
- }
114
- : typeof map === 'function'
115
- ? map
116
- : shouldNeverHappen(`Invalid map function ${map}`)
117
-
118
- // TODO don't even create a thunk if variables are static
119
- let variableValues$OrvariableValues
120
-
121
- if (typeof genVariableValues === 'function') {
122
- variableValues$OrvariableValues = this.reactivityGraph.makeThunk(
123
- (get, _setDebugInfo, ctx, otelContext) => {
124
- return genVariableValues(
125
- makeGetAtomResult(get, ctx, otelContext ?? ctx.rootOtelContext, this.dependencyQueriesRef),
126
- )
127
- },
128
- { label: `${labelWithDefault}:variableValues`, meta: { liveStoreThunkType: 'graphql.variables' } },
129
- )
130
- this.variableValues$ = variableValues$OrvariableValues
131
- } else {
132
- variableValues$OrvariableValues = genVariableValues
133
- }
134
-
135
- const resultsLabel = `${labelWithDefault}:results`
136
- this.results$ = this.reactivityGraph.makeThunk<TResultMapped>(
137
- (get, setDebugInfo, ctx, otelContext, debugRefreshReason) => {
138
- const { store, otelTracer, rootOtelContext } = ctx
139
- const variableValues = isThunk(variableValues$OrvariableValues)
140
- ? (get(variableValues$OrvariableValues, otelContext, debugRefreshReason) as TVariableValues)
141
- : (variableValues$OrvariableValues as TVariableValues)
142
- const { result, queriedTables, durationMs } = this.queryOnce({
143
- document,
144
- variableValues,
145
- otelContext: otelContext ?? rootOtelContext,
146
- otelTracer,
147
- store: store as Store<TContext>,
148
- get: makeGetAtomResult(get, ctx, otelContext ?? rootOtelContext, this.dependencyQueriesRef),
149
- })
150
-
151
- // Add dependencies on any tables that were used
152
- for (const tableName of queriedTables) {
153
- const tableRef = store.tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`)
154
- get(tableRef)
155
- }
156
-
157
- setDebugInfo({ _tag: 'graphql', label: resultsLabel, query: graphql.print(document), durationMs })
158
-
159
- return result
160
- },
161
- { label: resultsLabel, meta: { liveStoreThunkType: 'graphql.result' } },
162
- // otelContext,
163
- )
164
- }
165
-
166
- queryOnce = ({
167
- document,
168
- otelContext,
169
- otelTracer,
170
- variableValues,
171
- store,
172
- get,
173
- }: {
174
- document: graphql.DocumentNode
175
- otelContext: otel.Context
176
- otelTracer: otel.Tracer
177
- variableValues: TVariableValues
178
- store: Store<TContext>
179
- get: GetAtomResult
180
- }) => {
181
- const schema =
182
- store.graphQLSchema ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL schema")
183
- const context =
184
- store.graphQLContext ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL context")
185
-
186
- const operationName = graphql.getOperationAST(document)?.name?.value
187
-
188
- return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
189
- span.setAttribute('graphql.variables', JSON.stringify(variableValues))
190
- span.setAttribute('graphql.query', graphql.print(document))
191
-
192
- context.queriedTables.clear()
193
-
194
- context.otelContext = otel.trace.setSpan(otel.context.active(), span)
195
-
196
- const res = graphql.executeSync({
197
- document,
198
- contextValue: context,
199
- schema: schema,
200
- variableValues,
201
- })
202
-
203
- // TODO track number of nested SQL queries via Otel + debug info
204
-
205
- if (res.errors) {
206
- span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' })
207
- span.setAttribute('graphql.error', res.errors.join('\n'))
208
- span.setAttribute('graphql.error-detail', JSON.stringify(res.errors))
209
- console.error(`graphql error (${operationName}) - ${res.errors.length} errors`)
210
- for (const error of res.errors) {
211
- console.error(error)
212
- }
213
- debugger
214
- shouldNeverHappen(`GraphQL error: ${res.errors.join('\n')}`)
215
- }
216
-
217
- span.end()
218
-
219
- const result = this.mapResult(res.data as unknown as TResult, get)
220
-
221
- const durationMs = getDurationMsFromSpan(span)
222
-
223
- this.executionTimes.push(durationMs)
224
-
225
- return {
226
- result,
227
- queriedTables: Array.from(context.queriedTables.values()),
228
- durationMs,
229
- }
230
- })
231
- }
232
-
233
- destroy = () => {
234
- if (this.variableValues$ !== undefined) {
235
- this.reactivityGraph.destroyNode(this.variableValues$)
236
- }
237
-
238
- this.reactivityGraph.destroyNode(this.results$)
239
-
240
- for (const query of this.dependencyQueriesRef) {
241
- query.deref()
242
- }
243
- }
244
- }
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