@livestore/livestore 0.0.54-dev.19 → 0.0.54-dev.21

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 (73) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/MainDatabaseWrapper.d.ts +5 -3
  3. package/dist/MainDatabaseWrapper.d.ts.map +1 -1
  4. package/dist/MainDatabaseWrapper.js +3 -3
  5. package/dist/MainDatabaseWrapper.js.map +1 -1
  6. package/dist/__tests__/react/fixture.d.ts +3 -3
  7. package/dist/__tests__/react/fixture.d.ts.map +1 -1
  8. package/dist/__tests__/react/fixture.js +10 -8
  9. package/dist/__tests__/react/fixture.js.map +1 -1
  10. package/dist/effect/LiveStore.d.ts +2 -3
  11. package/dist/effect/LiveStore.d.ts.map +1 -1
  12. package/dist/effect/LiveStore.js +4 -2
  13. package/dist/effect/LiveStore.js.map +1 -1
  14. package/dist/global-state.d.ts +1 -1
  15. package/dist/global-state.d.ts.map +1 -1
  16. package/dist/global-state.js +2 -2
  17. package/dist/global-state.js.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +2 -2
  21. package/dist/index.js.map +1 -1
  22. package/dist/react/LiveStoreProvider.d.ts +3 -4
  23. package/dist/react/LiveStoreProvider.d.ts.map +1 -1
  24. package/dist/react/LiveStoreProvider.js +8 -13
  25. package/dist/react/LiveStoreProvider.js.map +1 -1
  26. package/dist/react/useRow.d.ts +2 -2
  27. package/dist/react/useRow.d.ts.map +1 -1
  28. package/dist/react/useRow.js +3 -3
  29. package/dist/react/useRow.js.map +1 -1
  30. package/dist/react/useRow.test.js +21 -21
  31. package/dist/react/useRow.test.js.map +1 -1
  32. package/dist/react/useTemporaryQuery.js +1 -1
  33. package/dist/react/useTemporaryQuery.js.map +1 -1
  34. package/dist/reactiveQueries/base-class.d.ts +6 -6
  35. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  36. package/dist/reactiveQueries/base-class.js +3 -3
  37. package/dist/reactiveQueries/base-class.js.map +1 -1
  38. package/dist/reactiveQueries/graphql.d.ts +8 -8
  39. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  40. package/dist/reactiveQueries/graphql.js +10 -10
  41. package/dist/reactiveQueries/graphql.js.map +1 -1
  42. package/dist/reactiveQueries/js.d.ts +6 -6
  43. package/dist/reactiveQueries/js.d.ts.map +1 -1
  44. package/dist/reactiveQueries/js.js +8 -8
  45. package/dist/reactiveQueries/js.js.map +1 -1
  46. package/dist/reactiveQueries/sql.d.ts +8 -8
  47. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  48. package/dist/reactiveQueries/sql.js +11 -11
  49. package/dist/reactiveQueries/sql.js.map +1 -1
  50. package/dist/row-query.d.ts +2 -2
  51. package/dist/row-query.d.ts.map +1 -1
  52. package/dist/row-query.js +2 -2
  53. package/dist/row-query.js.map +1 -1
  54. package/dist/store.d.ts +17 -16
  55. package/dist/store.d.ts.map +1 -1
  56. package/dist/store.js +46 -50
  57. package/dist/store.js.map +1 -1
  58. package/package.json +5 -12
  59. package/src/MainDatabaseWrapper.ts +7 -6
  60. package/src/__tests__/react/fixture.tsx +11 -9
  61. package/src/effect/LiveStore.ts +6 -5
  62. package/src/global-state.ts +2 -2
  63. package/src/index.ts +7 -2
  64. package/src/react/LiveStoreProvider.tsx +10 -18
  65. package/src/react/useRow.test.tsx +21 -21
  66. package/src/react/useRow.ts +5 -5
  67. package/src/react/useTemporaryQuery.ts +2 -2
  68. package/src/reactiveQueries/base-class.ts +9 -9
  69. package/src/reactiveQueries/graphql.ts +19 -15
  70. package/src/reactiveQueries/js.ts +12 -12
  71. package/src/reactiveQueries/sql.ts +18 -18
  72. package/src/row-query.ts +6 -6
  73. package/src/store.ts +75 -69
@@ -5,11 +5,11 @@ import { Schema, TreeFormatter } from '@livestore/utils/effect'
5
5
  import * as otel from '@opentelemetry/api'
6
6
  import * as graphql from 'graphql'
7
7
 
8
- import { globalDbGraph } from '../global-state.js'
8
+ import { globalReactivityGraph } from '../global-state.js'
9
9
  import { isThunk, type Thunk } from '../reactive.js'
10
10
  import type { BaseGraphQLContext, RefreshReason, Store } from '../store.js'
11
11
  import { getDurationMsFromSpan } from '../utils/otel.js'
12
- import type { DbContext, DbGraph, GetAtomResult, LiveQuery } from './base-class.js'
12
+ import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js'
13
13
  import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js'
14
14
 
15
15
  export type MapResult<To, From> = ((res: From, get: GetAtomResult) => To) | Schema.Schema<To, From>
@@ -21,9 +21,13 @@ export const queryGraphQL = <
21
21
  >(
22
22
  document: DocumentNode<TResult, TVariableValues>,
23
23
  genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues),
24
- { label, dbGraph, map }: { label?: string; dbGraph?: DbGraph; map?: MapResult<TResultMapped, TResult> } = {},
24
+ {
25
+ label,
26
+ reactivityGraph,
27
+ map,
28
+ }: { label?: string; reactivityGraph?: ReactivityGraph; map?: MapResult<TResultMapped, TResult> } = {},
25
29
  ): LiveQuery<TResultMapped, QueryInfoNone> =>
26
- new LiveStoreGraphQLQuery({ document, genVariableValues, label, dbGraph, map })
30
+ new LiveStoreGraphQLQuery({ document, genVariableValues, label, reactivityGraph, map })
27
31
 
28
32
  export class LiveStoreGraphQLQuery<
29
33
  TResult extends Record<string, any>,
@@ -37,13 +41,13 @@ export class LiveStoreGraphQLQuery<
37
41
  document: DocumentNode<TResult, TVariableValues>
38
42
 
39
43
  /** A reactive thunk representing the query results */
40
- results$: Thunk<TResultMapped, DbContext, RefreshReason>
44
+ results$: Thunk<TResultMapped, QueryContext, RefreshReason>
41
45
 
42
- variableValues$: Thunk<TVariableValues, DbContext, RefreshReason> | undefined
46
+ variableValues$: Thunk<TVariableValues, QueryContext, RefreshReason> | undefined
43
47
 
44
48
  label: string
45
49
 
46
- protected dbGraph: DbGraph
50
+ protected reactivityGraph: ReactivityGraph
47
51
 
48
52
  queryInfo: QueryInfoNone = { _tag: 'None' }
49
53
 
@@ -53,13 +57,13 @@ export class LiveStoreGraphQLQuery<
53
57
  document,
54
58
  label,
55
59
  genVariableValues,
56
- dbGraph,
60
+ reactivityGraph,
57
61
  map,
58
62
  }: {
59
63
  document: DocumentNode<TResult, TVariableValues>
60
64
  genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues)
61
65
  label?: string
62
- dbGraph?: DbGraph
66
+ reactivityGraph?: ReactivityGraph
63
67
  map?: MapResult<TResultMapped, TResult>
64
68
  }) {
65
69
  super()
@@ -69,7 +73,7 @@ export class LiveStoreGraphQLQuery<
69
73
  this.label = labelWithDefault
70
74
  this.document = document
71
75
 
72
- this.dbGraph = dbGraph ?? globalDbGraph
76
+ this.reactivityGraph = reactivityGraph ?? globalReactivityGraph
73
77
 
74
78
  this.mapResult =
75
79
  map === undefined
@@ -92,7 +96,7 @@ export class LiveStoreGraphQLQuery<
92
96
  let variableValues$OrvariableValues
93
97
 
94
98
  if (typeof genVariableValues === 'function') {
95
- variableValues$OrvariableValues = this.dbGraph.makeThunk(
99
+ variableValues$OrvariableValues = this.reactivityGraph.makeThunk(
96
100
  (get, _setDebugInfo, { rootOtelContext }, otelContext) => {
97
101
  return genVariableValues(makeGetAtomResult(get, otelContext ?? rootOtelContext))
98
102
  },
@@ -104,7 +108,7 @@ export class LiveStoreGraphQLQuery<
104
108
  }
105
109
 
106
110
  const resultsLabel = `${labelWithDefault}:results`
107
- this.results$ = this.dbGraph.makeThunk<TResultMapped>(
111
+ this.results$ = this.reactivityGraph.makeThunk<TResultMapped>(
108
112
  (get, setDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) => {
109
113
  const variableValues = isThunk(variableValues$OrvariableValues)
110
114
  ? (get(variableValues$OrvariableValues) as TVariableValues)
@@ -145,7 +149,7 @@ export class LiveStoreGraphQLQuery<
145
149
  // },
146
150
  // label: `${this.label}:js`,
147
151
  // onDestroy: () => this.destroy(),
148
- // dbGraph: this.dbGraph,
152
+ // reactivityGraph: this.reactivityGraph,
149
153
  // })
150
154
 
151
155
  queryOnce = ({
@@ -217,9 +221,9 @@ export class LiveStoreGraphQLQuery<
217
221
 
218
222
  destroy = () => {
219
223
  if (this.variableValues$ !== undefined) {
220
- this.dbGraph.destroyNode(this.variableValues$)
224
+ this.reactivityGraph.destroyNode(this.variableValues$)
221
225
  }
222
226
 
223
- this.dbGraph.destroyNode(this.results$)
227
+ this.reactivityGraph.destroyNode(this.results$)
224
228
  }
225
229
  }
@@ -1,25 +1,25 @@
1
1
  import type { QueryInfo, QueryInfoNone } from '@livestore/common'
2
2
  import * as otel from '@opentelemetry/api'
3
3
 
4
- import { globalDbGraph } from '../global-state.js'
4
+ import { globalReactivityGraph } from '../global-state.js'
5
5
  import type { Thunk } from '../reactive.js'
6
6
  import type { RefreshReason } from '../store.js'
7
7
  import { getDurationMsFromSpan } from '../utils/otel.js'
8
- import type { DbContext, DbGraph, GetAtomResult, LiveQuery } from './base-class.js'
8
+ import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js'
9
9
  import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js'
10
10
 
11
11
  export const computed = <TResult, TQueryInfo extends QueryInfo = QueryInfoNone>(
12
12
  fn: (get: GetAtomResult) => TResult,
13
13
  options?: {
14
14
  label: string
15
- dbGraph?: DbGraph
15
+ reactivityGraph?: ReactivityGraph
16
16
  queryInfo?: TQueryInfo
17
17
  },
18
18
  ): LiveQuery<TResult, TQueryInfo> =>
19
19
  new LiveStoreJSQuery<TResult, TQueryInfo>({
20
20
  fn,
21
21
  label: options?.label ?? fn.toString(),
22
- dbGraph: options?.dbGraph,
22
+ reactivityGraph: options?.reactivityGraph,
23
23
  queryInfo: options?.queryInfo,
24
24
  })
25
25
 
@@ -30,11 +30,11 @@ export class LiveStoreJSQuery<TResult, TQueryInfo extends QueryInfo = QueryInfoN
30
30
  _tag: 'js' = 'js'
31
31
 
32
32
  /** A reactive thunk representing the query results */
33
- results$: Thunk<TResult, DbContext, RefreshReason>
33
+ results$: Thunk<TResult, QueryContext, RefreshReason>
34
34
 
35
35
  label: string
36
36
 
37
- protected dbGraph: DbGraph
37
+ protected reactivityGraph: ReactivityGraph
38
38
 
39
39
  queryInfo: TQueryInfo
40
40
 
@@ -50,14 +50,14 @@ export class LiveStoreJSQuery<TResult, TQueryInfo extends QueryInfo = QueryInfoN
50
50
  fn,
51
51
  label,
52
52
  onDestroy,
53
- dbGraph,
53
+ reactivityGraph,
54
54
  queryInfo,
55
55
  }: {
56
56
  label: string
57
57
  fn: (get: GetAtomResult) => TResult
58
58
  /** Currently only used for "nested destruction" of piped queries */
59
59
  onDestroy?: () => void
60
- dbGraph?: DbGraph
60
+ reactivityGraph?: ReactivityGraph
61
61
  queryInfo?: TQueryInfo
62
62
  }) {
63
63
  super()
@@ -65,12 +65,12 @@ export class LiveStoreJSQuery<TResult, TQueryInfo extends QueryInfo = QueryInfoN
65
65
  this.onDestroy = onDestroy
66
66
  this.label = label
67
67
 
68
- this.dbGraph = dbGraph ?? globalDbGraph
68
+ this.reactivityGraph = reactivityGraph ?? globalReactivityGraph
69
69
  this.queryInfo = queryInfo ?? ({ _tag: 'None' } as TQueryInfo)
70
70
 
71
71
  const queryLabel = `${label}:results`
72
72
 
73
- this.results$ = this.dbGraph.makeThunk(
73
+ this.results$ = this.reactivityGraph.makeThunk(
74
74
  (get, setDebugInfo, { otelTracer, rootOtelContext }, otelContext) =>
75
75
  otelTracer.startActiveSpan(`js:${label}`, {}, otelContext ?? rootOtelContext, (span) => {
76
76
  const otelContext = otel.trace.setSpan(otel.context.active(), span)
@@ -98,11 +98,11 @@ export class LiveStoreJSQuery<TResult, TQueryInfo extends QueryInfo = QueryInfoN
98
98
  // },
99
99
  // label: `${this.label}:js`,
100
100
  // onDestroy: () => this.destroy(),
101
- // dbGraph: this.dbGraph,
101
+ // reactivityGraph: this.reactivityGraph,
102
102
  // })
103
103
 
104
104
  destroy = () => {
105
- this.dbGraph.destroyNode(this.results$)
105
+ this.reactivityGraph.destroyNode(this.results$)
106
106
  this.onDestroy?.()
107
107
  }
108
108
  }
@@ -3,11 +3,11 @@ import { shouldNeverHappen } from '@livestore/utils'
3
3
  import { Schema, TreeFormatter } from '@livestore/utils/effect'
4
4
  import * as otel from '@opentelemetry/api'
5
5
 
6
- import { globalDbGraph } from '../global-state.js'
6
+ import { globalReactivityGraph } from '../global-state.js'
7
7
  import type { Thunk } from '../reactive.js'
8
8
  import type { RefreshReason } from '../store.js'
9
9
  import { getDurationMsFromSpan } from '../utils/otel.js'
10
- import type { DbContext, DbGraph, GetAtomResult, LiveQuery } from './base-class.js'
10
+ import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js'
11
11
  import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js'
12
12
 
13
13
  export type MapRows<TResult, TRaw = any> =
@@ -26,7 +26,7 @@ export const querySQL = <TResult, TRaw = any>(
26
26
  queriedTables?: Set<string>
27
27
  bindValues?: Bindable
28
28
  label?: string
29
- dbGraph?: DbGraph
29
+ reactivityGraph?: ReactivityGraph
30
30
  },
31
31
  ): LiveQuery<TResult, QueryInfoNone> =>
32
32
  new LiveStoreSQLQuery<TResult, QueryInfoNone>({
@@ -34,7 +34,7 @@ export const querySQL = <TResult, TRaw = any>(
34
34
  genQueryString: query,
35
35
  queriedTables: options?.queriedTables,
36
36
  bindValues: options?.bindValues,
37
- dbGraph: options?.dbGraph,
37
+ reactivityGraph: options?.reactivityGraph,
38
38
  map: options?.map,
39
39
  queryInfo: { _tag: 'None' },
40
40
  })
@@ -47,14 +47,14 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
47
47
  _tag: 'sql' = 'sql'
48
48
 
49
49
  /** A reactive thunk representing the query text */
50
- queryString$: Thunk<string, DbContext, RefreshReason> | undefined
50
+ queryString$: Thunk<string, QueryContext, RefreshReason> | undefined
51
51
 
52
52
  /** A reactive thunk representing the query results */
53
- results$: Thunk<TResult, DbContext, RefreshReason>
53
+ results$: Thunk<TResult, QueryContext, RefreshReason>
54
54
 
55
55
  label: string
56
56
 
57
- protected dbGraph
57
+ protected reactivityGraph
58
58
 
59
59
  /** Currently only used by `rowQuery` for lazy table migrations and eager default row insertion */
60
60
  private execBeforeFirstRun
@@ -68,7 +68,7 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
68
68
  queriedTables,
69
69
  bindValues,
70
70
  label: label_,
71
- dbGraph,
71
+ reactivityGraph,
72
72
  map,
73
73
  execBeforeFirstRun,
74
74
  queryInfo,
@@ -77,16 +77,16 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
77
77
  genQueryString: string | ((get: GetAtomResult) => string)
78
78
  queriedTables?: Set<string>
79
79
  bindValues?: Bindable
80
- dbGraph?: DbGraph
80
+ reactivityGraph?: ReactivityGraph
81
81
  map?: MapRows<TResult>
82
- execBeforeFirstRun?: (ctx: DbContext) => void
82
+ execBeforeFirstRun?: (ctx: QueryContext) => void
83
83
  queryInfo?: TQueryInfo
84
84
  }) {
85
85
  super()
86
86
 
87
87
  const label = label_ ?? genQueryString.toString()
88
88
  this.label = `sql(${label})`
89
- this.dbGraph = dbGraph ?? globalDbGraph
89
+ this.reactivityGraph = reactivityGraph ?? globalReactivityGraph
90
90
  this.execBeforeFirstRun = execBeforeFirstRun
91
91
  this.queryInfo = queryInfo ?? ({ _tag: 'None' } as TQueryInfo)
92
92
  this.mapRows =
@@ -124,9 +124,9 @@ Result:`,
124
124
  ? map
125
125
  : shouldNeverHappen(`Invalid map function ${map}`)
126
126
 
127
- let queryString$OrQueryString: string | Thunk<string, DbContext, RefreshReason>
127
+ let queryString$OrQueryString: string | Thunk<string, QueryContext, RefreshReason>
128
128
  if (typeof genQueryString === 'function') {
129
- queryString$OrQueryString = this.dbGraph.makeThunk(
129
+ queryString$OrQueryString = this.reactivityGraph.makeThunk(
130
130
  (get, setDebugInfo, { rootOtelContext }, otelContext) => {
131
131
  const startMs = performance.now()
132
132
  const queryString = genQueryString(makeGetAtomResult(get, otelContext ?? rootOtelContext))
@@ -146,7 +146,7 @@ Result:`,
146
146
 
147
147
  const queriedTablesRef = { current: queriedTables }
148
148
 
149
- const results$ = this.dbGraph.makeThunk<TResult>(
149
+ const results$ = this.reactivityGraph.makeThunk<TResult>(
150
150
  (get, setDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) =>
151
151
  otelTracer.startActiveSpan(
152
152
  'sql:...', // NOTE span name will be overridden further down
@@ -217,7 +217,7 @@ Result:`,
217
217
  // },
218
218
  // label: `${this.label}:js`,
219
219
  // onDestroy: () => this.destroy(),
220
- // dbGraph: this.dbGraph,
220
+ // reactivityGraph: this.reactivityGraph,
221
221
  // queryInfo: undefined,
222
222
  // })
223
223
 
@@ -235,14 +235,14 @@ Result:`,
235
235
  // },
236
236
  // label: `${this.label}:first`,
237
237
  // onDestroy: () => this.destroy(),
238
- // dbGraph: this.dbGraph,
238
+ // reactivityGraph: this.reactivityGraph,
239
239
  // })
240
240
 
241
241
  destroy = () => {
242
242
  if (this.queryString$ !== undefined) {
243
- this.dbGraph.destroyNode(this.queryString$)
243
+ this.reactivityGraph.destroyNode(this.queryString$)
244
244
  }
245
245
 
246
- this.dbGraph.destroyNode(this.results$)
246
+ this.reactivityGraph.destroyNode(this.results$)
247
247
  }
248
248
  }
package/src/row-query.ts CHANGED
@@ -9,7 +9,7 @@ import type { SqliteDsl } from 'effect-db-schema'
9
9
  import { SqliteAst } from 'effect-db-schema'
10
10
 
11
11
  import type { Ref } from './reactive.js'
12
- import type { DbContext, DbGraph, LiveQuery, LiveQueryAny } from './reactiveQueries/base-class.js'
12
+ import type { LiveQuery, LiveQueryAny, QueryContext, ReactivityGraph } from './reactiveQueries/base-class.js'
13
13
  import { computed } from './reactiveQueries/js.js'
14
14
  import { LiveStoreSQLQuery } from './reactiveQueries/sql.js'
15
15
  import type { RefreshReason, Store } from './store.js'
@@ -17,7 +17,7 @@ import type { RefreshReason, Store } from './store.js'
17
17
  export type RowQueryOptions = {
18
18
  otelContext?: otel.Context
19
19
  skipInsertDefaultRow?: boolean
20
- dbGraph?: DbGraph
20
+ reactivityGraph?: ReactivityGraph
21
21
  }
22
22
 
23
23
  export type RowQueryOptionsDefaulValues<TTableDef extends DbSchema.TableDef> = {
@@ -76,7 +76,7 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
76
76
  label: `rowQuery:query:${tableSchema.name}${id === undefined ? '' : `:${id}`}`,
77
77
  genQueryString: queryStr,
78
78
  queriedTables: new Set([tableName]),
79
- dbGraph: options?.dbGraph,
79
+ reactivityGraph: options?.reactivityGraph,
80
80
  // While this code-path is not needed for singleton tables, it's still needed for `useRow` with non-existing rows for a given ID
81
81
  execBeforeFirstRun: makeExecBeforeFirstRun({
82
82
  otelContext: options?.otelContext,
@@ -145,7 +145,7 @@ const makeExecBeforeFirstRun =
145
145
  tableName: string
146
146
  table: DbSchema.TableDef
147
147
  }) =>
148
- ({ store }: DbContext) => {
148
+ ({ store }: QueryContext) => {
149
149
  const otelContext = otelContext_ ?? store.otel.queriesSpanContext
150
150
 
151
151
  // TODO we can remove this codepath again when Devtools v2 has landed
@@ -182,9 +182,9 @@ const makeExecBeforeFirstRun =
182
182
  const label = `tableRef:${tableName}`
183
183
 
184
184
  // TODO find a better implementation for this
185
- const existingTableRefFromGraph = Array.from(store.graph.atoms.values()).find(
185
+ const existingTableRefFromGraph = Array.from(store.reactivityGraph.atoms.values()).find(
186
186
  (_) => _._tag === 'ref' && _.label === label,
187
- ) as Ref<null, DbContext, RefreshReason> | undefined
187
+ ) as Ref<null, QueryContext, RefreshReason> | undefined
188
188
 
189
189
  store.tableRefs[tableName] = existingTableRefFromGraph ?? store.makeTableRef(tableName)
190
190
  }