@livestore/livestore 0.0.0-snapshot-f2e552007b925ea180db22534afe9d5c076c470f → 0.0.0-snapshot-5f1d84c316c847e6ca72d4f5e4e4b1f1cedfe32c

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 (82) hide show
  1. package/README.md +3 -1
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/global-state.d.ts +1 -1
  4. package/dist/global-state.d.ts.map +1 -1
  5. package/dist/global-state.js +1 -1
  6. package/dist/global-state.js.map +1 -1
  7. package/dist/index.d.ts +6 -6
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +5 -5
  10. package/dist/index.js.map +1 -1
  11. package/dist/{reactiveQueries → live-queries}/base-class.d.ts +8 -4
  12. package/dist/live-queries/base-class.d.ts.map +1 -0
  13. package/dist/{reactiveQueries → live-queries}/base-class.js +2 -0
  14. package/dist/live-queries/base-class.js.map +1 -0
  15. package/dist/{reactiveQueries → live-queries}/computed.d.ts +4 -13
  16. package/dist/live-queries/computed.d.ts.map +1 -0
  17. package/dist/{reactiveQueries → live-queries}/computed.js +4 -23
  18. package/dist/live-queries/computed.js.map +1 -0
  19. package/dist/live-queries/db.d.ts +66 -0
  20. package/dist/live-queries/db.d.ts.map +1 -0
  21. package/dist/live-queries/db.js +199 -0
  22. package/dist/live-queries/db.js.map +1 -0
  23. package/dist/live-queries/db.test.d.ts +2 -0
  24. package/dist/live-queries/db.test.d.ts.map +1 -0
  25. package/dist/live-queries/db.test.js +117 -0
  26. package/dist/live-queries/db.test.js.map +1 -0
  27. package/dist/{reactiveQueries → live-queries}/graphql.d.ts +4 -8
  28. package/dist/live-queries/graphql.d.ts.map +1 -0
  29. package/dist/{reactiveQueries → live-queries}/graphql.js +2 -16
  30. package/dist/live-queries/graphql.js.map +1 -0
  31. package/dist/row-query-utils.d.ts +17 -0
  32. package/dist/row-query-utils.d.ts.map +1 -0
  33. package/dist/row-query-utils.js +30 -0
  34. package/dist/row-query-utils.js.map +1 -0
  35. package/dist/store/create-store.d.ts +1 -1
  36. package/dist/store/create-store.d.ts.map +1 -1
  37. package/dist/store/devtools.d.ts +1 -1
  38. package/dist/store/devtools.d.ts.map +1 -1
  39. package/dist/store/devtools.js.map +1 -1
  40. package/dist/store/store-types.d.ts +2 -2
  41. package/dist/store/store-types.d.ts.map +1 -1
  42. package/dist/store/store.d.ts +8 -3
  43. package/dist/store/store.d.ts.map +1 -1
  44. package/dist/store/store.js +32 -4
  45. package/dist/store/store.js.map +1 -1
  46. package/dist/utils/tests/fixture.d.ts +168 -132
  47. package/dist/utils/tests/fixture.d.ts.map +1 -1
  48. package/package.json +5 -5
  49. package/src/global-state.ts +1 -1
  50. package/src/index.ts +8 -5
  51. package/src/live-queries/__snapshots__/db.test.ts.snap +301 -0
  52. package/src/{reactiveQueries → live-queries}/base-class.ts +10 -5
  53. package/src/{reactiveQueries → live-queries}/computed.ts +5 -29
  54. package/src/live-queries/db.test.ts +153 -0
  55. package/src/live-queries/db.ts +350 -0
  56. package/src/{reactiveQueries → live-queries}/graphql.ts +6 -21
  57. package/src/row-query-utils.ts +65 -0
  58. package/src/store/create-store.ts +1 -1
  59. package/src/store/devtools.ts +1 -1
  60. package/src/store/store-types.ts +2 -2
  61. package/src/store/store.ts +44 -7
  62. package/dist/reactiveQueries/base-class.d.ts.map +0 -1
  63. package/dist/reactiveQueries/base-class.js.map +0 -1
  64. package/dist/reactiveQueries/computed.d.ts.map +0 -1
  65. package/dist/reactiveQueries/computed.js.map +0 -1
  66. package/dist/reactiveQueries/graphql.d.ts.map +0 -1
  67. package/dist/reactiveQueries/graphql.js.map +0 -1
  68. package/dist/reactiveQueries/sql.d.ts +0 -49
  69. package/dist/reactiveQueries/sql.d.ts.map +0 -1
  70. package/dist/reactiveQueries/sql.js +0 -130
  71. package/dist/reactiveQueries/sql.js.map +0 -1
  72. package/dist/reactiveQueries/sql.test.d.ts +0 -2
  73. package/dist/reactiveQueries/sql.test.d.ts.map +0 -1
  74. package/dist/reactiveQueries/sql.test.js +0 -284
  75. package/dist/reactiveQueries/sql.test.js.map +0 -1
  76. package/dist/row-query.d.ts +0 -33
  77. package/dist/row-query.d.ts.map +0 -1
  78. package/dist/row-query.js +0 -80
  79. package/dist/row-query.js.map +0 -1
  80. package/src/reactiveQueries/sql.test.ts +0 -308
  81. package/src/reactiveQueries/sql.ts +0 -226
  82. package/src/row-query.ts +0 -196
package/src/row-query.ts DELETED
@@ -1,196 +0,0 @@
1
- import type { QueryInfoCol, QueryInfoNone, QueryInfoRow } from '@livestore/common'
2
- import { SessionIdSymbol, sql } from '@livestore/common'
3
- import { DbSchema } from '@livestore/common/schema'
4
- import type { SqliteDsl } from '@livestore/db-schema'
5
- import type { GetValForKey } from '@livestore/utils'
6
- import { shouldNeverHappen } from '@livestore/utils'
7
- import { Schema } from '@livestore/utils/effect'
8
- import type * as otel from '@opentelemetry/api'
9
-
10
- import type {
11
- GetAtomResult,
12
- LiveQuery,
13
- LiveQueryAny,
14
- QueryContext,
15
- ReactivityGraph,
16
- } from './reactiveQueries/base-class.js'
17
- import { computed } from './reactiveQueries/computed.js'
18
- import { LiveStoreSQLQuery } from './reactiveQueries/sql.js'
19
- import type { Store } from './store/store.js'
20
-
21
- export type RowQueryOptions<TTableDef extends DbSchema.TableDef, TResult = RowResult<TTableDef>> = {
22
- otelContext?: otel.Context
23
- skipInsertDefaultRow?: boolean
24
- reactivityGraph?: ReactivityGraph
25
- map?: (result: RowResult<TTableDef>) => TResult
26
- label?: string
27
- }
28
-
29
- export type RowQueryOptionsDefaulValues<TTableDef extends DbSchema.TableDef> = {
30
- defaultValues?: Partial<RowResult<TTableDef>>
31
- }
32
-
33
- export type MakeRowQuery = {
34
- <
35
- TTableDef extends DbSchema.TableDef<
36
- DbSchema.DefaultSqliteTableDef,
37
- boolean,
38
- DbSchema.TableOptions & { isSingleton: true }
39
- >,
40
- TResult = RowResult<TTableDef>,
41
- >(
42
- table: TTableDef,
43
- options?: RowQueryOptions<TTableDef, TResult>,
44
- ): LiveQuery<RowResult<TTableDef>, QueryInfoRow<TTableDef>>
45
- <
46
- TTableDef extends DbSchema.TableDef<
47
- DbSchema.DefaultSqliteTableDef,
48
- boolean,
49
- DbSchema.TableOptions & { isSingleton: false }
50
- >,
51
- TResult = RowResult<TTableDef>,
52
- >(
53
- table: TTableDef,
54
- // TODO adjust so it works with arbitrary primary keys or unique constraints
55
- id: string | SessionIdSymbol,
56
- options?: RowQueryOptions<TTableDef, TResult> & RowQueryOptionsDefaulValues<TTableDef>,
57
- ): LiveQuery<TResult, QueryInfoRow<TTableDef>>
58
- }
59
-
60
- // TODO also allow other where clauses and multiple rows
61
- export const rowQuery: MakeRowQuery = <TTableDef extends DbSchema.TableDef>(
62
- table: TTableDef,
63
- idOrOptions?: string | SessionIdSymbol | RowQueryOptions<TTableDef, any>,
64
- options_?: RowQueryOptions<TTableDef, any> & RowQueryOptionsDefaulValues<TTableDef>,
65
- ) => {
66
- const id = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? idOrOptions : undefined
67
- const options = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? options_ : idOrOptions
68
- const defaultValues: Partial<RowResult<TTableDef>> | undefined = (options as any)?.defaultValues ?? {}
69
-
70
- // Validate query args
71
- if (table.options.isSingleton === true && id !== undefined && id !== SessionIdSymbol) {
72
- shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} with id "${id}" as it is a singleton`)
73
- } else if (table.options.isSingleton !== true && id === undefined) {
74
- shouldNeverHappen(`Cannot query state table ${table.sqliteDef.name} without id`)
75
- }
76
-
77
- const tableSchema = table.sqliteDef
78
- const tableName = tableSchema.name
79
-
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)
87
-
88
- const rowSchema = table.isSingleColumn === true ? table.schema.pipe(Schema.pluck('value' as any)) : table.schema
89
-
90
- return new LiveStoreSQLQuery({
91
- label:
92
- options?.label ??
93
- `rowQuery:query:${tableSchema.name}${id === undefined ? '' : id === SessionIdSymbol ? `:sessionId` : `:${id}`}`,
94
- genQueryString,
95
- queriedTables: new Set([tableName]),
96
- reactivityGraph: options?.reactivityGraph,
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
98
- execBeforeFirstRun: makeExecBeforeFirstRun({
99
- otelContext: options?.otelContext,
100
- table,
101
- defaultValues,
102
- id,
103
- skipInsertDefaultRow: options?.skipInsertDefaultRow,
104
- }),
105
- schema: rowSchema.pipe(Schema.Array, Schema.headOrElse()),
106
- map: options?.map,
107
- queryInfo: { _tag: 'Row', table, id: id ?? 'singleton' },
108
- })
109
- }
110
-
111
- export type RowResult<TTableDef extends DbSchema.TableDef> = TTableDef['isSingleColumn'] extends true
112
- ? GetValForKey<SqliteDsl.FromColumns.RowDecoded<TTableDef['sqliteDef']['columns']>, 'value'>
113
- : SqliteDsl.FromColumns.RowDecoded<TTableDef['sqliteDef']['columns']>
114
-
115
- export type RowResultEncoded<TTableDef extends DbSchema.TableDef> = TTableDef['isSingleColumn'] extends true
116
- ? GetValForKey<SqliteDsl.FromColumns.RowEncoded<TTableDef['sqliteDef']['columns']>, 'value'>
117
- : SqliteDsl.FromColumns.RowEncoded<TTableDef['sqliteDef']['columns']>
118
-
119
- export const deriveColQuery: {
120
- <TQuery extends LiveQuery<any, QueryInfoNone>, TCol extends keyof TQuery['__result!'] & string>(
121
- query$: TQuery,
122
- colName: TCol,
123
- ): LiveQuery<TQuery['__result!'][TCol], QueryInfoNone>
124
- <TQuery extends LiveQuery<any, QueryInfoRow<any>>, TCol extends keyof TQuery['__result!'] & string>(
125
- query$: TQuery,
126
- colName: TCol,
127
- ): LiveQuery<TQuery['__result!'][TCol], QueryInfoCol<TQuery['queryInfo']['table'], TCol>>
128
- } = (query$: LiveQueryAny, colName: string) => {
129
- return computed((get) => get(query$)[colName], {
130
- label: `deriveColQuery:${query$.label}:${colName}`,
131
- queryInfo:
132
- query$.queryInfo._tag === 'Row'
133
- ? { _tag: 'Col', table: query$.queryInfo.table, column: colName, id: query$.queryInfo.id }
134
- : undefined,
135
- }) as any
136
- }
137
-
138
- const makeExecBeforeFirstRun =
139
- ({
140
- id,
141
- defaultValues,
142
- skipInsertDefaultRow,
143
- otelContext: otelContext_,
144
- table,
145
- }: {
146
- id?: string | SessionIdSymbol
147
- defaultValues?: any
148
- skipInsertDefaultRow?: boolean
149
- otelContext?: otel.Context
150
- table: DbSchema.TableDef
151
- }) =>
152
- ({ store }: QueryContext) => {
153
- const otelContext = otelContext_ ?? store.otel.queriesSpanContext
154
-
155
- if (skipInsertDefaultRow !== true && table.options.isSingleton === false) {
156
- insertRowWithDefaultValuesOrIgnore({
157
- store,
158
- id: id!,
159
- table,
160
- otelContext,
161
- explicitDefaultValues: defaultValues,
162
- })
163
- }
164
- }
165
-
166
- const insertRowWithDefaultValuesOrIgnore = ({
167
- store,
168
- id,
169
- table,
170
- otelContext,
171
- explicitDefaultValues,
172
- }: {
173
- store: Store
174
- id: string | SessionIdSymbol
175
- table: DbSchema.TableDef
176
- otelContext: otel.Context
177
- explicitDefaultValues: Partial<RowResult<DbSchema.TableDef>> | undefined
178
- }) => {
179
- const idStr = id === SessionIdSymbol ? store.sessionId : id
180
- const rowExists =
181
- store.syncDbWrapper.select(`select 1 from ${table.sqliteDef.name} where id = '${idStr}'`).length === 1
182
-
183
- if (rowExists) return
184
-
185
- // const mutationDef = deriveCreateMutationDef(table)
186
- if (DbSchema.tableHasDerivedMutations(table) === false) {
187
- return shouldNeverHappen(
188
- `Cannot insert row for table "${table.sqliteDef.name}" which does not have 'deriveMutations: true' set`,
189
- )
190
- }
191
- // NOTE It's important that we only mutate and don't refresh here, as this function is called during a render
192
- store.mutateWithoutRefresh(table.insert({ id, ...explicitDefaultValues }), {
193
- otelContext,
194
- coordinatorMode: 'default',
195
- })
196
- }