@livestore/livestore 0.0.54-dev.2 → 0.0.54-dev.22

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 (89) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/MainDatabaseWrapper.d.ts +6 -5
  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/QueryCache.d.ts +1 -1
  7. package/dist/QueryCache.d.ts.map +1 -1
  8. package/dist/QueryCache.js.map +1 -1
  9. package/dist/__tests__/react/fixture.d.ts +4 -4
  10. package/dist/__tests__/react/fixture.d.ts.map +1 -1
  11. package/dist/__tests__/react/fixture.js +10 -8
  12. package/dist/__tests__/react/fixture.js.map +1 -1
  13. package/dist/effect/LiveStore.d.ts +2 -3
  14. package/dist/effect/LiveStore.d.ts.map +1 -1
  15. package/dist/effect/LiveStore.js +4 -2
  16. package/dist/effect/LiveStore.js.map +1 -1
  17. package/dist/global-state.d.ts +1 -1
  18. package/dist/global-state.d.ts.map +1 -1
  19. package/dist/global-state.js +2 -2
  20. package/dist/global-state.js.map +1 -1
  21. package/dist/index.d.ts +3 -4
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -4
  24. package/dist/index.js.map +1 -1
  25. package/dist/react/LiveStoreProvider.d.ts +3 -4
  26. package/dist/react/LiveStoreProvider.d.ts.map +1 -1
  27. package/dist/react/LiveStoreProvider.js +8 -13
  28. package/dist/react/LiveStoreProvider.js.map +1 -1
  29. package/dist/react/useRow.d.ts +2 -2
  30. package/dist/react/useRow.d.ts.map +1 -1
  31. package/dist/react/useRow.js +3 -3
  32. package/dist/react/useRow.js.map +1 -1
  33. package/dist/react/useRow.test.js +21 -21
  34. package/dist/react/useRow.test.js.map +1 -1
  35. package/dist/react/useTemporaryQuery.js +1 -1
  36. package/dist/react/useTemporaryQuery.js.map +1 -1
  37. package/dist/reactive.js +1 -1
  38. package/dist/reactive.js.map +1 -1
  39. package/dist/reactiveQueries/base-class.d.ts +6 -6
  40. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  41. package/dist/reactiveQueries/base-class.js +3 -3
  42. package/dist/reactiveQueries/base-class.js.map +1 -1
  43. package/dist/reactiveQueries/graphql.d.ts +8 -8
  44. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  45. package/dist/reactiveQueries/graphql.js +10 -10
  46. package/dist/reactiveQueries/graphql.js.map +1 -1
  47. package/dist/reactiveQueries/js.d.ts +6 -6
  48. package/dist/reactiveQueries/js.d.ts.map +1 -1
  49. package/dist/reactiveQueries/js.js +8 -8
  50. package/dist/reactiveQueries/js.js.map +1 -1
  51. package/dist/reactiveQueries/sql.d.ts +9 -10
  52. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  53. package/dist/reactiveQueries/sql.js +12 -12
  54. package/dist/reactiveQueries/sql.js.map +1 -1
  55. package/dist/row-query.d.ts +2 -2
  56. package/dist/row-query.d.ts.map +1 -1
  57. package/dist/row-query.js +4 -4
  58. package/dist/row-query.js.map +1 -1
  59. package/dist/store.d.ts +23 -20
  60. package/dist/store.d.ts.map +1 -1
  61. package/dist/store.js +140 -144
  62. package/dist/store.js.map +1 -1
  63. package/package.json +5 -12
  64. package/src/MainDatabaseWrapper.ts +14 -8
  65. package/src/QueryCache.ts +1 -2
  66. package/src/__tests__/react/fixture.tsx +11 -9
  67. package/src/effect/LiveStore.ts +6 -5
  68. package/src/global-state.ts +2 -2
  69. package/src/index.ts +17 -4
  70. package/src/react/LiveStoreProvider.tsx +10 -18
  71. package/src/react/useRow.test.tsx +21 -21
  72. package/src/react/useRow.ts +5 -5
  73. package/src/react/useTemporaryQuery.ts +2 -2
  74. package/src/reactive.ts +3 -1
  75. package/src/reactiveQueries/base-class.ts +9 -9
  76. package/src/reactiveQueries/graphql.ts +19 -15
  77. package/src/reactiveQueries/js.ts +12 -12
  78. package/src/reactiveQueries/sql.ts +19 -21
  79. package/src/row-query.ts +8 -8
  80. package/src/store.ts +214 -179
  81. package/dist/utils/bounded-collections.d.ts +0 -34
  82. package/dist/utils/bounded-collections.d.ts.map +0 -1
  83. package/dist/utils/bounded-collections.js +0 -91
  84. package/dist/utils/bounded-collections.js.map +0 -1
  85. package/dist/utils/util.d.ts +0 -14
  86. package/dist/utils/util.d.ts.map +0 -1
  87. package/dist/utils/util.js +0 -19
  88. package/dist/utils/util.js.map +0 -1
  89. package/src/utils/util.ts +0 -31
@@ -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
  }
@@ -1,15 +1,13 @@
1
- import type { QueryInfo, QueryInfoNone } from '@livestore/common'
1
+ import { type Bindable, prepareBindValues, type QueryInfo, type QueryInfoNone } from '@livestore/common'
2
2
  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 { Bindable } from '../utils/util.js'
11
- import { prepareBindValues } from '../utils/util.js'
12
- import type { DbContext, DbGraph, GetAtomResult, LiveQuery } from './base-class.js'
10
+ import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js'
13
11
  import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js'
14
12
 
15
13
  export type MapRows<TResult, TRaw = any> =
@@ -28,7 +26,7 @@ export const querySQL = <TResult, TRaw = any>(
28
26
  queriedTables?: Set<string>
29
27
  bindValues?: Bindable
30
28
  label?: string
31
- dbGraph?: DbGraph
29
+ reactivityGraph?: ReactivityGraph
32
30
  },
33
31
  ): LiveQuery<TResult, QueryInfoNone> =>
34
32
  new LiveStoreSQLQuery<TResult, QueryInfoNone>({
@@ -36,7 +34,7 @@ export const querySQL = <TResult, TRaw = any>(
36
34
  genQueryString: query,
37
35
  queriedTables: options?.queriedTables,
38
36
  bindValues: options?.bindValues,
39
- dbGraph: options?.dbGraph,
37
+ reactivityGraph: options?.reactivityGraph,
40
38
  map: options?.map,
41
39
  queryInfo: { _tag: 'None' },
42
40
  })
@@ -49,14 +47,14 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
49
47
  _tag: 'sql' = 'sql'
50
48
 
51
49
  /** A reactive thunk representing the query text */
52
- queryString$: Thunk<string, DbContext, RefreshReason> | undefined
50
+ queryString$: Thunk<string, QueryContext, RefreshReason> | undefined
53
51
 
54
52
  /** A reactive thunk representing the query results */
55
- results$: Thunk<TResult, DbContext, RefreshReason>
53
+ results$: Thunk<TResult, QueryContext, RefreshReason>
56
54
 
57
55
  label: string
58
56
 
59
- protected dbGraph
57
+ protected reactivityGraph
60
58
 
61
59
  /** Currently only used by `rowQuery` for lazy table migrations and eager default row insertion */
62
60
  private execBeforeFirstRun
@@ -70,7 +68,7 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
70
68
  queriedTables,
71
69
  bindValues,
72
70
  label: label_,
73
- dbGraph,
71
+ reactivityGraph,
74
72
  map,
75
73
  execBeforeFirstRun,
76
74
  queryInfo,
@@ -79,16 +77,16 @@ export class LiveStoreSQLQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo
79
77
  genQueryString: string | ((get: GetAtomResult) => string)
80
78
  queriedTables?: Set<string>
81
79
  bindValues?: Bindable
82
- dbGraph?: DbGraph
80
+ reactivityGraph?: ReactivityGraph
83
81
  map?: MapRows<TResult>
84
- execBeforeFirstRun?: (ctx: DbContext) => void
82
+ execBeforeFirstRun?: (ctx: QueryContext) => void
85
83
  queryInfo?: TQueryInfo
86
84
  }) {
87
85
  super()
88
86
 
89
87
  const label = label_ ?? genQueryString.toString()
90
88
  this.label = `sql(${label})`
91
- this.dbGraph = dbGraph ?? globalDbGraph
89
+ this.reactivityGraph = reactivityGraph ?? globalReactivityGraph
92
90
  this.execBeforeFirstRun = execBeforeFirstRun
93
91
  this.queryInfo = queryInfo ?? ({ _tag: 'None' } as TQueryInfo)
94
92
  this.mapRows =
@@ -126,9 +124,9 @@ Result:`,
126
124
  ? map
127
125
  : shouldNeverHappen(`Invalid map function ${map}`)
128
126
 
129
- let queryString$OrQueryString: string | Thunk<string, DbContext, RefreshReason>
127
+ let queryString$OrQueryString: string | Thunk<string, QueryContext, RefreshReason>
130
128
  if (typeof genQueryString === 'function') {
131
- queryString$OrQueryString = this.dbGraph.makeThunk(
129
+ queryString$OrQueryString = this.reactivityGraph.makeThunk(
132
130
  (get, setDebugInfo, { rootOtelContext }, otelContext) => {
133
131
  const startMs = performance.now()
134
132
  const queryString = genQueryString(makeGetAtomResult(get, otelContext ?? rootOtelContext))
@@ -148,7 +146,7 @@ Result:`,
148
146
 
149
147
  const queriedTablesRef = { current: queriedTables }
150
148
 
151
- const results$ = this.dbGraph.makeThunk<TResult>(
149
+ const results$ = this.reactivityGraph.makeThunk<TResult>(
152
150
  (get, setDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) =>
153
151
  otelTracer.startActiveSpan(
154
152
  'sql:...', // NOTE span name will be overridden further down
@@ -219,7 +217,7 @@ Result:`,
219
217
  // },
220
218
  // label: `${this.label}:js`,
221
219
  // onDestroy: () => this.destroy(),
222
- // dbGraph: this.dbGraph,
220
+ // reactivityGraph: this.reactivityGraph,
223
221
  // queryInfo: undefined,
224
222
  // })
225
223
 
@@ -237,14 +235,14 @@ Result:`,
237
235
  // },
238
236
  // label: `${this.label}:first`,
239
237
  // onDestroy: () => this.destroy(),
240
- // dbGraph: this.dbGraph,
238
+ // reactivityGraph: this.reactivityGraph,
241
239
  // })
242
240
 
243
241
  destroy = () => {
244
242
  if (this.queryString$ !== undefined) {
245
- this.dbGraph.destroyNode(this.queryString$)
243
+ this.reactivityGraph.destroyNode(this.queryString$)
246
244
  }
247
245
 
248
- this.dbGraph.destroyNode(this.results$)
246
+ this.reactivityGraph.destroyNode(this.results$)
249
247
  }
250
248
  }
package/src/row-query.ts CHANGED
@@ -3,13 +3,13 @@ import { migrateTable, sql } from '@livestore/common'
3
3
  import { DbSchema, SCHEMA_META_TABLE } from '@livestore/common/schema'
4
4
  import type { GetValForKey } from '@livestore/utils'
5
5
  import { shouldNeverHappen } from '@livestore/utils'
6
- import { Schema, TreeFormatter } from '@livestore/utils/effect'
6
+ import { Effect, Schema, TreeFormatter } from '@livestore/utils/effect'
7
7
  import type * as otel from '@opentelemetry/api'
8
8
  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
@@ -163,7 +163,7 @@ const makeExecBeforeFirstRun =
163
163
  ...mainStmt,
164
164
  execute: (bindValues) => {
165
165
  const getRowsChanged = mainStmt.execute(bindValues)
166
- store.adapter.coordinator.execute(query, bindValues, undefined)
166
+ store.adapter.coordinator.execute(query, bindValues).pipe(Effect.tapCauseLogPretty, Effect.runFork)
167
167
  return getRowsChanged
168
168
  },
169
169
  }
@@ -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
  }