@livestore/livestore 0.0.58-dev.1 → 0.0.58-dev.10

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 (157) hide show
  1. package/README.md +1 -117
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/effect/LiveStore.d.ts +3 -3
  4. package/dist/effect/LiveStore.d.ts.map +1 -1
  5. package/dist/effect/LiveStore.js +1 -1
  6. package/dist/effect/LiveStore.js.map +1 -1
  7. package/dist/global-state.d.ts.map +1 -1
  8. package/dist/global-state.js +2 -1
  9. package/dist/global-state.js.map +1 -1
  10. package/dist/index.d.ts +8 -6
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +4 -2
  13. package/dist/index.js.map +1 -1
  14. package/dist/reactiveQueries/base-class.d.ts +1 -1
  15. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  16. package/dist/reactiveQueries/base-class.js.map +1 -1
  17. package/dist/reactiveQueries/sql.d.ts +1 -1
  18. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  19. package/dist/reactiveQueries/sql.js +4 -4
  20. package/dist/reactiveQueries/sql.js.map +1 -1
  21. package/dist/reactiveQueries/sql.test.js +2 -2
  22. package/dist/reactiveQueries/sql.test.js.map +1 -1
  23. package/dist/row-query.d.ts +3 -2
  24. package/dist/row-query.d.ts.map +1 -1
  25. package/dist/row-query.js +18 -10
  26. package/dist/row-query.js.map +1 -1
  27. package/dist/store-devtools.d.ts +2 -2
  28. package/dist/store-devtools.d.ts.map +1 -1
  29. package/dist/store-devtools.js +3 -3
  30. package/dist/store-devtools.js.map +1 -1
  31. package/dist/store.d.ts +23 -19
  32. package/dist/store.d.ts.map +1 -1
  33. package/dist/store.js +90 -59
  34. package/dist/store.js.map +1 -1
  35. package/dist/utils/dev.d.ts.map +1 -1
  36. package/dist/utils/dev.js +1 -0
  37. package/dist/utils/dev.js.map +1 -1
  38. package/dist/{react/utils → utils}/stack-info.d.ts +1 -2
  39. package/dist/utils/stack-info.d.ts.map +1 -0
  40. package/dist/{react/utils → utils}/stack-info.js +1 -9
  41. package/dist/utils/stack-info.js.map +1 -0
  42. package/dist/utils/stack-info.test.d.ts.map +1 -0
  43. package/dist/{__tests__/react/utils → utils}/stack-info.test.js +1 -1
  44. package/dist/utils/stack-info.test.js.map +1 -0
  45. package/dist/utils/tests/fixture.d.ts +259 -0
  46. package/dist/utils/tests/fixture.d.ts.map +1 -0
  47. package/dist/utils/tests/fixture.js +33 -0
  48. package/dist/utils/tests/fixture.js.map +1 -0
  49. package/dist/utils/tests/mod.d.ts +3 -0
  50. package/dist/utils/tests/mod.d.ts.map +1 -0
  51. package/dist/utils/tests/mod.js +3 -0
  52. package/dist/utils/tests/mod.js.map +1 -0
  53. package/dist/utils/tests/otel.d.ts.map +1 -0
  54. package/dist/utils/tests/otel.js.map +1 -0
  55. package/package.json +17 -24
  56. package/src/ambient.d.ts +3 -0
  57. package/src/effect/LiveStore.ts +4 -4
  58. package/src/global-state.ts +5 -1
  59. package/src/index.ts +17 -4
  60. package/src/reactiveQueries/base-class.ts +1 -1
  61. package/src/reactiveQueries/sql.test.ts +2 -2
  62. package/src/reactiveQueries/sql.ts +5 -5
  63. package/src/row-query.ts +36 -16
  64. package/src/store-devtools.ts +5 -5
  65. package/src/store.ts +146 -78
  66. package/src/utils/dev.ts +1 -0
  67. package/src/{__tests__/react/utils → utils}/stack-info.test.ts +1 -1
  68. package/src/{react/utils → utils}/stack-info.ts +2 -12
  69. package/src/utils/tests/fixture.ts +77 -0
  70. package/src/utils/tests/mod.ts +2 -0
  71. package/tsconfig.json +1 -2
  72. package/vitest.config.js +0 -8
  73. package/dist/__tests__/react/fixture.d.ts +0 -461
  74. package/dist/__tests__/react/fixture.d.ts.map +0 -1
  75. package/dist/__tests__/react/fixture.js +0 -68
  76. package/dist/__tests__/react/fixture.js.map +0 -1
  77. package/dist/__tests__/react/utils/otel.d.ts.map +0 -1
  78. package/dist/__tests__/react/utils/otel.js.map +0 -1
  79. package/dist/__tests__/react/utils/stack-info.test.d.ts.map +0 -1
  80. package/dist/__tests__/react/utils/stack-info.test.js.map +0 -1
  81. package/dist/react/LiveStoreContext.d.ts +0 -7
  82. package/dist/react/LiveStoreContext.d.ts.map +0 -1
  83. package/dist/react/LiveStoreContext.js +0 -13
  84. package/dist/react/LiveStoreContext.js.map +0 -1
  85. package/dist/react/LiveStoreProvider.d.ts +0 -47
  86. package/dist/react/LiveStoreProvider.d.ts.map +0 -1
  87. package/dist/react/LiveStoreProvider.js +0 -169
  88. package/dist/react/LiveStoreProvider.js.map +0 -1
  89. package/dist/react/LiveStoreProvider.test.d.ts +0 -2
  90. package/dist/react/LiveStoreProvider.test.d.ts.map +0 -1
  91. package/dist/react/LiveStoreProvider.test.js +0 -62
  92. package/dist/react/LiveStoreProvider.test.js.map +0 -1
  93. package/dist/react/components/LiveList.d.ts +0 -21
  94. package/dist/react/components/LiveList.d.ts.map +0 -1
  95. package/dist/react/components/LiveList.js +0 -31
  96. package/dist/react/components/LiveList.js.map +0 -1
  97. package/dist/react/index.d.ts +0 -11
  98. package/dist/react/index.d.ts.map +0 -1
  99. package/dist/react/index.js +0 -10
  100. package/dist/react/index.js.map +0 -1
  101. package/dist/react/useAtom.d.ts +0 -10
  102. package/dist/react/useAtom.d.ts.map +0 -1
  103. package/dist/react/useAtom.js +0 -37
  104. package/dist/react/useAtom.js.map +0 -1
  105. package/dist/react/useLocalId.d.ts +0 -10
  106. package/dist/react/useLocalId.d.ts.map +0 -1
  107. package/dist/react/useLocalId.js +0 -21
  108. package/dist/react/useLocalId.js.map +0 -1
  109. package/dist/react/useQuery.d.ts +0 -9
  110. package/dist/react/useQuery.d.ts.map +0 -1
  111. package/dist/react/useQuery.js +0 -69
  112. package/dist/react/useQuery.js.map +0 -1
  113. package/dist/react/useQuery.test.d.ts +0 -2
  114. package/dist/react/useQuery.test.d.ts.map +0 -1
  115. package/dist/react/useQuery.test.js +0 -51
  116. package/dist/react/useQuery.test.js.map +0 -1
  117. package/dist/react/useRow.d.ts +0 -46
  118. package/dist/react/useRow.d.ts.map +0 -1
  119. package/dist/react/useRow.js +0 -94
  120. package/dist/react/useRow.js.map +0 -1
  121. package/dist/react/useRow.test.d.ts +0 -2
  122. package/dist/react/useRow.test.d.ts.map +0 -1
  123. package/dist/react/useRow.test.js +0 -562
  124. package/dist/react/useRow.test.js.map +0 -1
  125. package/dist/react/useTemporaryQuery.d.ts +0 -22
  126. package/dist/react/useTemporaryQuery.d.ts.map +0 -1
  127. package/dist/react/useTemporaryQuery.js +0 -70
  128. package/dist/react/useTemporaryQuery.js.map +0 -1
  129. package/dist/react/useTemporaryQuery.test.d.ts +0 -2
  130. package/dist/react/useTemporaryQuery.test.d.ts.map +0 -1
  131. package/dist/react/useTemporaryQuery.test.js +0 -37
  132. package/dist/react/useTemporaryQuery.test.js.map +0 -1
  133. package/dist/react/utils/stack-info.d.ts.map +0 -1
  134. package/dist/react/utils/stack-info.js.map +0 -1
  135. package/dist/react/utils/useStateRefWithReactiveInput.d.ts +0 -13
  136. package/dist/react/utils/useStateRefWithReactiveInput.d.ts.map +0 -1
  137. package/dist/react/utils/useStateRefWithReactiveInput.js +0 -38
  138. package/dist/react/utils/useStateRefWithReactiveInput.js.map +0 -1
  139. package/src/__tests__/react/fixture.tsx +0 -126
  140. package/src/react/LiveStoreContext.ts +0 -20
  141. package/src/react/LiveStoreProvider.test.tsx +0 -109
  142. package/src/react/LiveStoreProvider.tsx +0 -289
  143. package/src/react/components/LiveList.tsx +0 -84
  144. package/src/react/index.ts +0 -19
  145. package/src/react/useAtom.ts +0 -55
  146. package/src/react/useLocalId.ts +0 -33
  147. package/src/react/useQuery.test.tsx +0 -82
  148. package/src/react/useQuery.ts +0 -105
  149. package/src/react/useRow.test.tsx +0 -699
  150. package/src/react/useRow.ts +0 -180
  151. package/src/react/useTemporaryQuery.test.tsx +0 -56
  152. package/src/react/useTemporaryQuery.ts +0 -121
  153. package/src/react/utils/useStateRefWithReactiveInput.ts +0 -51
  154. /package/dist/{__tests__/react/utils → utils}/stack-info.test.d.ts +0 -0
  155. /package/dist/{__tests__/react/utils → utils/tests}/otel.d.ts +0 -0
  156. /package/dist/{__tests__/react/utils → utils/tests}/otel.js +0 -0
  157. /package/src/{__tests__/react/utils → utils/tests}/otel.ts +0 -0
package/src/row-query.ts CHANGED
@@ -1,13 +1,19 @@
1
1
  import type { QueryInfoCol, QueryInfoNone, QueryInfoRow } from '@livestore/common'
2
- import { sql } from '@livestore/common'
2
+ import { SessionIdSymbol, sql } from '@livestore/common'
3
3
  import { DbSchema } from '@livestore/common/schema'
4
+ import type { SqliteDsl } from '@livestore/db-schema'
4
5
  import type { GetValForKey } from '@livestore/utils'
5
6
  import { shouldNeverHappen } from '@livestore/utils'
6
7
  import { Schema } from '@livestore/utils/effect'
7
8
  import type * as otel from '@opentelemetry/api'
8
- import type { SqliteDsl } from 'effect-db-schema'
9
9
 
10
- import type { LiveQuery, LiveQueryAny, QueryContext, ReactivityGraph } from './reactiveQueries/base-class.js'
10
+ import type {
11
+ GetAtomResult,
12
+ LiveQuery,
13
+ LiveQueryAny,
14
+ QueryContext,
15
+ ReactivityGraph,
16
+ } from './reactiveQueries/base-class.js'
11
17
  import { computed } from './reactiveQueries/js.js'
12
18
  import { LiveStoreSQLQuery } from './reactiveQueries/sql.js'
13
19
  import type { Store } from './store.js'
@@ -46,7 +52,7 @@ export type MakeRowQuery = {
46
52
  >(
47
53
  table: TTableDef,
48
54
  // TODO adjust so it works with arbitrary primary keys or unique constraints
49
- id: string,
55
+ id: string | SessionIdSymbol,
50
56
  options?: RowQueryOptions<TTableDef, TResult> & RowQueryOptionsDefaulValues<TTableDef>,
51
57
  ): LiveQuery<TResult, QueryInfoRow<TTableDef>>
52
58
  }
@@ -54,15 +60,15 @@ export type MakeRowQuery = {
54
60
  // TODO also allow other where clauses and multiple rows
55
61
  export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
56
62
  table: TTableDef,
57
- idOrOptions?: string | RowQueryOptions<TTableDef, any>,
63
+ idOrOptions?: string | SessionIdSymbol | RowQueryOptions<TTableDef, any>,
58
64
  options_?: RowQueryOptions<TTableDef, any> & RowQueryOptionsDefaulValues<TTableDef>,
59
65
  ) => {
60
- const id = typeof idOrOptions === 'string' ? idOrOptions : undefined
61
- const options = typeof idOrOptions === 'string' ? options_ : idOrOptions
66
+ const id = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? idOrOptions : undefined
67
+ const options = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? options_ : idOrOptions
62
68
  const defaultValues: Partial<RowResult<TTableDef>> | undefined = (options as any)?.defaultValues ?? {}
63
69
 
64
70
  // Validate query args
65
- if (table.options.isSingleton === true && id !== undefined) {
71
+ if (table.options.isSingleton === true && id !== undefined && id !== SessionIdSymbol) {
66
72
  shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} with id "${id}" as it is a singleton`)
67
73
  } else if (table.options.isSingleton !== true && id === undefined) {
68
74
  shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} without id`)
@@ -71,14 +77,21 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
71
77
  const tableSchema = table.sqliteDef
72
78
  const tableName = tableSchema.name
73
79
 
74
- const whereClause = id === undefined ? '' : `where id = '${id}'`
75
- const queryStr = sql`select * from ${tableName} ${whereClause} limit 1`
80
+ const makeQueryString = (id: string | undefined) =>
81
+ sql`select * from ${tableName} ${id === undefined ? '' : `where id = '${id}'`} limit 1`
82
+
83
+ const genQueryString =
84
+ id === SessionIdSymbol
85
+ ? (_: GetAtomResult, ctx: QueryContext) => makeQueryString(ctx.store.sessionId)
86
+ : makeQueryString(id)
76
87
 
77
88
  const rowSchema = table.isSingleColumn === true ? table.schema.pipe(Schema.pluck('value' as any)) : table.schema
78
89
 
79
90
  return new LiveStoreSQLQuery({
80
- label: options?.label ?? `rowQuery:query:${tableSchema.name}${id === undefined ? '' : `:${id}`}`,
81
- genQueryString: queryStr,
91
+ label:
92
+ options?.label ??
93
+ `rowQuery:query:${tableSchema.name}${id === undefined ? '' : id === SessionIdSymbol ? `:sessionId` : `:${id}`}`,
94
+ genQueryString,
82
95
  queriedTables: new Set([tableName]),
83
96
  reactivityGraph: options?.reactivityGraph,
84
97
  // While this code-path is not needed for singleton tables, it's still needed for `useRow` with non-existing rows for a given ID
@@ -91,7 +104,11 @@ export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
91
104
  }),
92
105
  schema: rowSchema.pipe(Schema.Array, Schema.headOrElse()),
93
106
  map: options?.map,
94
- queryInfo: { _tag: 'Row', table, id: id ?? 'singleton' },
107
+ queryInfo: {
108
+ _tag: 'Row',
109
+ table,
110
+ id: id === SessionIdSymbol ? 'sessionId' : (id ?? 'singleton'),
111
+ },
95
112
  })
96
113
  }
97
114
 
@@ -130,7 +147,7 @@ const makeExecBeforeFirstRun =
130
147
  otelContext: otelContext_,
131
148
  table,
132
149
  }: {
133
- id?: string
150
+ id?: string | SessionIdSymbol
134
151
  defaultValues?: any
135
152
  skipInsertDefaultRow?: boolean
136
153
  otelContext?: otel.Context
@@ -158,12 +175,15 @@ const insertRowWithDefaultValuesOrIgnore = ({
158
175
  explicitDefaultValues,
159
176
  }: {
160
177
  store: Store
161
- id: string
178
+ id: string | SessionIdSymbol
162
179
  table: DbSchema.TableDef
163
180
  otelContext: otel.Context
164
181
  explicitDefaultValues: Partial<RowResult<DbSchema.TableDef>> | undefined
165
182
  }) => {
166
- const rowExists = store.syncDbWrapper.select(`select 1 from ${table.sqliteDef.name} where id = '${id}'`).length === 1
183
+ const idStr = id === SessionIdSymbol ? store.sessionId : id
184
+ const rowExists =
185
+ store.syncDbWrapper.select(`select 1 from ${table.sqliteDef.name} where id = '${idStr}'`).length === 1
186
+
167
187
  if (rowExists) return
168
188
 
169
189
  // const mutationDef = deriveCreateMutationDef(table)
@@ -1,4 +1,4 @@
1
- import type { DebugInfo, StoreAdapter } from '@livestore/common'
1
+ import type { ClientSession, DebugInfo } from '@livestore/common'
2
2
  import { Devtools, liveStoreVersion, UnexpectedError } from '@livestore/common'
3
3
  import { throttle } from '@livestore/utils'
4
4
  import type { WebChannel } from '@livestore/utils/effect'
@@ -11,7 +11,7 @@ import { emptyDebugInfo as makeEmptyDebugInfo } from './SynchronousDatabaseWrapp
11
11
  import type { ReferenceCountedSet } from './utils/data-structures.js'
12
12
 
13
13
  type IStore = {
14
- adapter: StoreAdapter
14
+ clientSession: ClientSession
15
15
  reactivityGraph: ReactivityGraph
16
16
  syncDbWrapper: SynchronousDatabaseWrapper
17
17
  activeQueries: ReferenceCountedSet<LiveQuery<any>>
@@ -29,7 +29,7 @@ export const connectDevtoolsToStore = ({
29
29
  store: IStore
30
30
  }) =>
31
31
  Effect.gen(function* () {
32
- const appHostId = store.adapter.coordinator.devtools.appHostId
32
+ const appHostId = store.clientSession.coordinator.devtools.appHostId
33
33
 
34
34
  const reactivityGraphSubcriptions: SubMap = new Map()
35
35
  const liveQueriesSubscriptions: SubMap = new Map()
@@ -49,14 +49,14 @@ export const connectDevtoolsToStore = ({
49
49
  const onMessage = (decodedMessage: typeof Devtools.MessageToAppHostStore.Type) => {
50
50
  // console.log('storeMessagePort message', decodedMessage)
51
51
 
52
- if (decodedMessage.appHostId !== store.adapter.coordinator.devtools.appHostId) {
52
+ if (decodedMessage.appHostId !== store.clientSession.coordinator.devtools.appHostId) {
53
53
  // console.log(`Unknown message`, event)
54
54
  return
55
55
  }
56
56
 
57
57
  const requestId = decodedMessage.requestId
58
58
 
59
- const requestIdleCallback = window.requestIdleCallback ?? ((cb: Function) => cb())
59
+ const requestIdleCallback = globalThis.requestIdleCallback ?? ((cb: () => void) => cb())
60
60
 
61
61
  switch (decodedMessage._tag) {
62
62
  case 'LSD.ReactivityGraphSubscribe': {