@livestore/livestore 0.0.39-dev.3 → 0.0.40

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 (215) hide show
  1. package/README.md +15 -24
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/__tests__/react/fixture.d.ts +210 -35
  4. package/dist/__tests__/react/fixture.d.ts.map +1 -1
  5. package/dist/__tests__/react/fixture.js +10 -29
  6. package/dist/__tests__/react/fixture.js.map +1 -1
  7. package/dist/cud.d.ts +28 -0
  8. package/dist/cud.d.ts.map +1 -0
  9. package/dist/cud.js +50 -0
  10. package/dist/cud.js.map +1 -0
  11. package/dist/cud.test.d.ts +2 -0
  12. package/dist/cud.test.d.ts.map +1 -0
  13. package/dist/cud.test.js +47 -0
  14. package/dist/cud.test.js.map +1 -0
  15. package/dist/inMemoryDatabase.d.ts +1 -1
  16. package/dist/inMemoryDatabase.d.ts.map +1 -1
  17. package/dist/inMemoryDatabase.js +1 -4
  18. package/dist/inMemoryDatabase.js.map +1 -1
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3 -3
  22. package/dist/index.js.map +1 -1
  23. package/dist/migrations.d.ts.map +1 -1
  24. package/dist/migrations.js +11 -7
  25. package/dist/migrations.js.map +1 -1
  26. package/dist/query-info.d.ts +49 -0
  27. package/dist/query-info.d.ts.map +1 -0
  28. package/dist/{update-path.js → query-info.js} +4 -3
  29. package/dist/query-info.js.map +1 -0
  30. package/dist/react/LiveStoreContext.d.ts +0 -6
  31. package/dist/react/LiveStoreContext.d.ts.map +1 -1
  32. package/dist/react/LiveStoreContext.js.map +1 -1
  33. package/dist/react/useAtom.d.ts +2 -2
  34. package/dist/react/useAtom.d.ts.map +1 -1
  35. package/dist/react/useAtom.js +3 -3
  36. package/dist/react/useAtom.js.map +1 -1
  37. package/dist/react/useQuery.d.ts.map +1 -1
  38. package/dist/react/useQuery.test.d.ts.map +1 -0
  39. package/dist/{__tests__/react → react}/useQuery.test.js +8 -11
  40. package/dist/react/useQuery.test.js.map +1 -0
  41. package/dist/react/useRow.d.ts +2 -2
  42. package/dist/react/useRow.d.ts.map +1 -1
  43. package/dist/react/useRow.js +5 -5
  44. package/dist/react/useRow.js.map +1 -1
  45. package/dist/react/useRow.test.d.ts.map +1 -0
  46. package/dist/{__tests__/react → react}/useRow.test.js +14 -38
  47. package/dist/react/useRow.test.js.map +1 -0
  48. package/dist/react/useTemporaryQuery.d.ts +2 -2
  49. package/dist/react/useTemporaryQuery.d.ts.map +1 -1
  50. package/dist/reactive.d.ts +2 -2
  51. package/dist/reactive.d.ts.map +1 -1
  52. package/dist/reactive.js +50 -15
  53. package/dist/reactive.js.map +1 -1
  54. package/dist/reactive.test.d.ts.map +1 -0
  55. package/dist/{__tests__/reactive.test.js → reactive.test.js} +1 -1
  56. package/dist/reactive.test.js.map +1 -0
  57. package/dist/reactiveQueries/base-class.d.ts +16 -8
  58. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  59. package/dist/reactiveQueries/base-class.js +2 -1
  60. package/dist/reactiveQueries/base-class.js.map +1 -1
  61. package/dist/reactiveQueries/graphql.d.ts +15 -9
  62. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  63. package/dist/reactiveQueries/graphql.js +29 -8
  64. package/dist/reactiveQueries/graphql.js.map +1 -1
  65. package/dist/reactiveQueries/js.d.ts +8 -8
  66. package/dist/reactiveQueries/js.d.ts.map +1 -1
  67. package/dist/reactiveQueries/js.js +5 -4
  68. package/dist/reactiveQueries/js.js.map +1 -1
  69. package/dist/reactiveQueries/sql.d.ts +11 -11
  70. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  71. package/dist/reactiveQueries/sql.js +7 -6
  72. package/dist/reactiveQueries/sql.js.map +1 -1
  73. package/dist/reactiveQueries/sql.test.d.ts.map +1 -0
  74. package/dist/{__tests__/reactiveQueries → reactiveQueries}/sql.test.js +44 -34
  75. package/dist/reactiveQueries/sql.test.js.map +1 -0
  76. package/dist/row-query.d.ts +5 -5
  77. package/dist/row-query.d.ts.map +1 -1
  78. package/dist/row-query.js +11 -9
  79. package/dist/row-query.js.map +1 -1
  80. package/dist/schema/index.d.ts +20 -7
  81. package/dist/schema/index.d.ts.map +1 -1
  82. package/dist/schema/index.js +18 -3
  83. package/dist/schema/index.js.map +1 -1
  84. package/dist/schema/mutations.d.ts +81 -0
  85. package/dist/schema/mutations.d.ts.map +1 -0
  86. package/dist/schema/mutations.js +29 -0
  87. package/dist/schema/mutations.js.map +1 -0
  88. package/dist/schema/parse-utils.d.ts +3 -6
  89. package/dist/schema/parse-utils.d.ts.map +1 -1
  90. package/dist/schema/parse-utils.js +2 -27
  91. package/dist/schema/parse-utils.js.map +1 -1
  92. package/dist/schema/system-tables.d.ts +8 -8
  93. package/dist/schema/table-def.d.ts +6 -6
  94. package/dist/schema/table-def.d.ts.map +1 -1
  95. package/dist/schema/table-def.js +2 -2
  96. package/dist/schema/table-def.js.map +1 -1
  97. package/dist/storage/in-memory/index.d.ts +4 -0
  98. package/dist/storage/in-memory/index.d.ts.map +1 -1
  99. package/dist/storage/in-memory/index.js +3 -0
  100. package/dist/storage/in-memory/index.js.map +1 -1
  101. package/dist/storage/index.d.ts +4 -0
  102. package/dist/storage/index.d.ts.map +1 -1
  103. package/dist/storage/tauri/index.d.ts +4 -0
  104. package/dist/storage/tauri/index.d.ts.map +1 -1
  105. package/dist/storage/tauri/index.js +6 -0
  106. package/dist/storage/tauri/index.js.map +1 -1
  107. package/dist/storage/utils/idb.d.ts +1 -0
  108. package/dist/storage/utils/idb.d.ts.map +1 -1
  109. package/dist/storage/utils/idb.js +11 -0
  110. package/dist/storage/utils/idb.js.map +1 -1
  111. package/dist/storage/web-worker/common.d.ts +11 -0
  112. package/dist/storage/web-worker/common.d.ts.map +1 -0
  113. package/dist/storage/web-worker/common.js +2 -0
  114. package/dist/storage/web-worker/common.js.map +1 -0
  115. package/dist/storage/web-worker/index.d.ts +14 -7
  116. package/dist/storage/web-worker/index.d.ts.map +1 -1
  117. package/dist/storage/web-worker/index.js +70 -14
  118. package/dist/storage/web-worker/index.js.map +1 -1
  119. package/dist/storage/web-worker/make-worker.d.ts +20 -0
  120. package/dist/storage/web-worker/make-worker.d.ts.map +1 -0
  121. package/dist/storage/web-worker/make-worker.js +155 -0
  122. package/dist/storage/web-worker/make-worker.js.map +1 -0
  123. package/dist/storage/web-worker/vite-dev-polyfill.d.ts +2 -0
  124. package/dist/storage/web-worker/vite-dev-polyfill.d.ts.map +1 -0
  125. package/dist/storage/web-worker/vite-dev-polyfill.js +35 -0
  126. package/dist/storage/web-worker/vite-dev-polyfill.js.map +1 -0
  127. package/dist/store.d.ts +32 -42
  128. package/dist/store.d.ts.map +1 -1
  129. package/dist/store.js +82 -131
  130. package/dist/store.js.map +1 -1
  131. package/dist/utils/dev.d.ts +3 -0
  132. package/dist/utils/dev.d.ts.map +1 -0
  133. package/dist/utils/dev.js +16 -0
  134. package/dist/utils/dev.js.map +1 -0
  135. package/dist/utils/util.d.ts +2 -0
  136. package/dist/utils/util.d.ts.map +1 -1
  137. package/dist/utils/util.js +2 -0
  138. package/dist/utils/util.js.map +1 -1
  139. package/package.json +26 -14
  140. package/src/__tests__/react/fixture.tsx +12 -30
  141. package/src/cud.test.ts +52 -0
  142. package/src/cud.ts +92 -0
  143. package/src/inMemoryDatabase.ts +2 -7
  144. package/src/index.ts +14 -8
  145. package/src/migrations.ts +10 -7
  146. package/src/{update-path.ts → query-info.ts} +18 -21
  147. package/src/react/LiveStoreContext.ts +0 -9
  148. package/src/react/useAtom.ts +6 -6
  149. package/src/{__tests__/react → react}/useQuery.test.tsx +11 -11
  150. package/src/react/useQuery.ts +1 -1
  151. package/src/{__tests__/react → react}/useRow.test.tsx +21 -39
  152. package/src/react/useRow.ts +10 -10
  153. package/src/{__tests__/reactive.test.ts → reactive.test.ts} +1 -1
  154. package/src/reactive.ts +60 -19
  155. package/src/reactiveQueries/base-class.ts +23 -9
  156. package/src/reactiveQueries/graphql.ts +49 -13
  157. package/src/reactiveQueries/js.ts +15 -13
  158. package/src/{__tests__/reactiveQueries → reactiveQueries}/sql.test.ts +44 -34
  159. package/src/reactiveQueries/sql.ts +24 -22
  160. package/src/row-query.ts +24 -22
  161. package/src/schema/index.ts +47 -11
  162. package/src/schema/mutations.ts +129 -0
  163. package/src/schema/parse-utils.ts +3 -45
  164. package/src/schema/table-def.ts +9 -2
  165. package/src/storage/in-memory/index.ts +7 -0
  166. package/src/storage/index.ts +8 -0
  167. package/src/storage/tauri/index.ts +10 -0
  168. package/src/storage/utils/idb.ts +14 -0
  169. package/src/storage/web-worker/common.ts +6 -0
  170. package/src/storage/web-worker/index.ts +86 -17
  171. package/src/storage/web-worker/make-worker.ts +214 -0
  172. package/src/storage/web-worker/vite-dev-polyfill.ts +33 -0
  173. package/src/store.ts +142 -212
  174. package/src/utils/dev.ts +23 -0
  175. package/src/utils/util.ts +4 -0
  176. package/dist/__tests__/mutations.test.d.ts +0 -2
  177. package/dist/__tests__/mutations.test.d.ts.map +0 -1
  178. package/dist/__tests__/mutations.test.js +0 -40
  179. package/dist/__tests__/mutations.test.js.map +0 -1
  180. package/dist/__tests__/react/useQuery.test.d.ts.map +0 -1
  181. package/dist/__tests__/react/useQuery.test.js.map +0 -1
  182. package/dist/__tests__/react/useRow.test.d.ts.map +0 -1
  183. package/dist/__tests__/react/useRow.test.js.map +0 -1
  184. package/dist/__tests__/reactive.test.d.ts.map +0 -1
  185. package/dist/__tests__/reactive.test.js.map +0 -1
  186. package/dist/__tests__/reactiveQueries/sql.test.d.ts.map +0 -1
  187. package/dist/__tests__/reactiveQueries/sql.test.js.map +0 -1
  188. package/dist/events.d.ts +0 -7
  189. package/dist/events.d.ts.map +0 -1
  190. package/dist/events.js +0 -2
  191. package/dist/events.js.map +0 -1
  192. package/dist/mutations.d.ts +0 -33
  193. package/dist/mutations.d.ts.map +0 -1
  194. package/dist/mutations.js +0 -38
  195. package/dist/mutations.js.map +0 -1
  196. package/dist/schema/action.d.ts +0 -30
  197. package/dist/schema/action.d.ts.map +0 -1
  198. package/dist/schema/action.js +0 -3
  199. package/dist/schema/action.js.map +0 -1
  200. package/dist/storage/web-worker/worker.d.ts +0 -13
  201. package/dist/storage/web-worker/worker.d.ts.map +0 -1
  202. package/dist/storage/web-worker/worker.js +0 -110
  203. package/dist/storage/web-worker/worker.js.map +0 -1
  204. package/dist/update-path.d.ts +0 -52
  205. package/dist/update-path.d.ts.map +0 -1
  206. package/dist/update-path.js.map +0 -1
  207. package/src/__tests__/mutations.test.ts +0 -43
  208. package/src/events.ts +0 -8
  209. package/src/mutations.ts +0 -79
  210. package/src/schema/action.ts +0 -41
  211. package/src/storage/web-worker/worker.ts +0 -141
  212. /package/dist/{__tests__/react → react}/useQuery.test.d.ts +0 -0
  213. /package/dist/{__tests__/react → react}/useRow.test.d.ts +0 -0
  214. /package/dist/{__tests__/reactive.test.d.ts → reactive.test.d.ts} +0 -0
  215. /package/dist/{__tests__/reactiveQueries → reactiveQueries}/sql.test.d.ts +0 -0
@@ -0,0 +1,52 @@
1
+ import { describe, expect, test } from 'vitest'
2
+
3
+ import { tables } from './__tests__/react/fixture.js'
4
+ import { makeCudMutations } from './cud.js'
5
+ import type { MutationEvent } from './index.js'
6
+
7
+ describe('cud mutations', () => {
8
+ const cud = makeCudMutations(tables)
9
+
10
+ test('basic', () => {
11
+ expect(patchId(cud.todos.insert({ id: 't1', completed: true, text: 'Task 1' }))).toMatchInlineSnapshot(`
12
+ {
13
+ "args": {
14
+ "bindValues": {
15
+ "completed": 1,
16
+ "id": "t1",
17
+ "text": "Task 1",
18
+ },
19
+ "sql": "INSERT INTO todos (id, text, completed) VALUES ($id, $text, $completed)",
20
+ "writeTables": Set {
21
+ "todos",
22
+ },
23
+ },
24
+ "id": "00000000-0000-0000-0000-000000000000",
25
+ "mutation": "livestore.RawSql",
26
+ }
27
+ `)
28
+
29
+ expect(patchId(cud.todos.update({ where: { id: 't1' }, values: { text: 'Task 1 - fixed' } })))
30
+ .toMatchInlineSnapshot(`
31
+ {
32
+ "args": {
33
+ "bindValues": {
34
+ "update_text": "Task 1 - fixed",
35
+ "where_id": "t1",
36
+ },
37
+ "sql": "UPDATE todos SET text = $update_text WHERE id = $where_id",
38
+ "writeTables": Set {
39
+ "todos",
40
+ },
41
+ },
42
+ "id": "00000000-0000-0000-0000-000000000000",
43
+ "mutation": "livestore.RawSql",
44
+ }
45
+ `)
46
+ })
47
+ })
48
+
49
+ const patchId = (muationEvent: MutationEvent.Any) => {
50
+ const id = `00000000-0000-0000-0000-000000000000`
51
+ return { ...muationEvent, id }
52
+ }
package/src/cud.ts ADDED
@@ -0,0 +1,92 @@
1
+ import * as SqlQueries from '@livestore/sql-queries'
2
+ import { pipe, ReadonlyRecord } from '@livestore/utils/effect'
3
+ import type { SqliteDsl } from 'effect-db-schema'
4
+
5
+ import type { RowResult } from './row-query.js'
6
+ import { rawSqlMutation, type RawSqlMutationEvent } from './schema/index.js'
7
+ import { getDefaultValuesEncoded, type TableDef } from './schema/table-def.js'
8
+ import { type GetValForKey, isIterable } from './utils/util.js'
9
+
10
+ export const makeCudMutations = <TTableDef extends TableDef>(
11
+ tables: Iterable<TTableDef> | Record<string, TTableDef>,
12
+ ): CudMutations<TTableDef> => {
13
+ const cudMutationRecord: CudMutations<TTableDef> = {} as any
14
+
15
+ const tables_ = isIterable(tables) ? tables : Object.values(tables)
16
+
17
+ for (const tableDef of tables_) {
18
+ const [tableName, cudMutation] = cudMutationsForTable(tableDef)
19
+ cudMutationRecord[tableName] = cudMutation as any
20
+ }
21
+
22
+ return cudMutationRecord
23
+ }
24
+
25
+ const cudMutationsForTable = <TTableDef extends TableDef>(
26
+ tableDef: TTableDef,
27
+ ): [TTableDef['sqliteDef']['name'], CudMutation<TTableDef>] => {
28
+ const table = tableDef.sqliteDef
29
+ const writeTables = new Set([table.name])
30
+ const api = {
31
+ insert: (values_: any) => {
32
+ const defaultValues = getDefaultValuesEncoded(tableDef)
33
+ const values = pipe(
34
+ tableDef.sqliteDef.columns,
35
+ ReadonlyRecord.map((_, columnName) => values_?.[columnName] ?? defaultValues[columnName]),
36
+ )
37
+
38
+ const [sql, bindValues] = SqlQueries.insertRow({
39
+ tableName: table.name,
40
+ columns: table.columns,
41
+ options: { orReplace: false },
42
+ values: values as any,
43
+ })
44
+ return rawSqlMutation({ sql, bindValues, writeTables })
45
+ },
46
+ update: ({ where, values }) => {
47
+ const [sql, bindValues] = SqlQueries.updateRows({
48
+ tableName: table.name,
49
+ columns: table.columns,
50
+ where: where,
51
+ updateValues: values,
52
+ })
53
+ return rawSqlMutation({ sql, bindValues, writeTables })
54
+ },
55
+ delete: ({ where }) => {
56
+ const [sql, bindValues] = SqlQueries.deleteRows({
57
+ tableName: table.name,
58
+ columns: table.columns,
59
+ where: where,
60
+ })
61
+ return rawSqlMutation({ sql, bindValues, writeTables })
62
+ },
63
+ } satisfies CudMutation<TTableDef>
64
+
65
+ return [tableDef.sqliteDef.name, api]
66
+ }
67
+
68
+ export type UpdateMutation<TTableDef extends TableDef> = (args: {
69
+ // TODO also allow `id` if present in `TTableDef`
70
+ where: Partial<RowResult<TTableDef>>
71
+ values: Partial<RowResult<TTableDef>>
72
+ }) => RawSqlMutationEvent
73
+
74
+ export type RowInsert<TTableDef extends TableDef> = TTableDef['isSingleColumn'] extends true
75
+ ? GetValForKey<SqliteDsl.FromColumns.InsertRowDecoded<TTableDef['sqliteDef']['columns']>, 'value'>
76
+ : SqliteDsl.FromColumns.InsertRowDecoded<TTableDef['sqliteDef']['columns']>
77
+
78
+ export type InsertMutation<TTableDef extends TableDef> = (values: RowInsert<TTableDef>) => RawSqlMutationEvent
79
+
80
+ export type DeleteMutation<TTableDef extends TableDef> = (args: {
81
+ where: Partial<RowResult<TTableDef>>
82
+ }) => RawSqlMutationEvent
83
+
84
+ export type CudMutation<TTableDef extends TableDef> = {
85
+ insert: InsertMutation<TTableDef>
86
+ update: UpdateMutation<TTableDef>
87
+ delete: DeleteMutation<TTableDef>
88
+ }
89
+
90
+ export type CudMutations<TTableDef extends TableDef> = {
91
+ [TTableName in TTableDef['sqliteDef']['name']]: CudMutation<Extract<TTableDef, { sqliteDef: { name: TTableName } }>>
92
+ }
@@ -138,7 +138,7 @@ export class InMemoryDatabase {
138
138
  execute(
139
139
  query: string,
140
140
  bindValues?: PreparedBindValues,
141
- writeTables?: ReadonlyArray<string>,
141
+ writeTables?: ReadonlySet<string>,
142
142
  options?: { hasNoEffects?: boolean; otelContext?: otel.Context },
143
143
  ): { durationMs: number } {
144
144
  // console.debug('in-memory-db:execute', query, bindValues)
@@ -170,12 +170,7 @@ export class InMemoryDatabase {
170
170
  stmt.reset() // Reset is needed for next execution
171
171
  }
172
172
  } catch (error) {
173
- shouldNeverHappen(
174
- `Error executing query: ${error} \n ${JSON.stringify({
175
- query,
176
- bindValues,
177
- })}`,
178
- )
173
+ shouldNeverHappen(`Error executing query: ${error} \n ${JSON.stringify({ query, bindValues })}`)
179
174
  }
180
175
 
181
176
  if (options?.hasNoEffects !== true && !this.resultCache.ignoreQuery(query)) {
package/src/index.ts CHANGED
@@ -13,23 +13,29 @@ export { LiveStoreSQLQuery, querySQL, type MapRows } from './reactiveQueries/sql
13
13
  export { LiveStoreGraphQLQuery, queryGraphQL } from './reactiveQueries/graphql.js'
14
14
  export { type GetAtomResult, type DbGraph, makeDbGraph, type LiveQuery } from './reactiveQueries/base-class.js'
15
15
 
16
- export { globalDbGraph } from './global-state.js'
16
+ export { globalDbGraph, dynamicallyRegisteredTables } from './global-state.js'
17
17
 
18
18
  export { type RowResult, type RowResultEncoded, rowQuery, deriveColQuery } from './row-query.js'
19
19
 
20
- export * from './mutations.js'
20
+ export * from './cud.js'
21
21
 
22
- export { defineAction, defineActions, makeSchema, DbSchema, ParseUtils } from './schema/index.js'
22
+ export {
23
+ makeSchema,
24
+ DbSchema,
25
+ ParseUtils,
26
+ defineMutation,
27
+ rawSqlMutation,
28
+ makeMutationEventSchema,
29
+ makeMutationDefRecord,
30
+ } from './schema/index.js'
23
31
 
24
32
  export type {
25
33
  LiveStoreSchema,
26
34
  InputSchema,
27
- GetActionArgs,
28
- GetApplyEventArgs,
29
- ActionDefinition,
30
- ActionDefinitions,
31
- SQLWriteStatement,
32
35
  SchemaMetaRow,
36
+ MutationDef,
37
+ MutationEvent,
38
+ MutationDefMap,
33
39
  } from './schema/index.js'
34
40
 
35
41
  export { SqliteAst, SqliteDsl } from 'effect-db-schema'
package/src/migrations.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Schema as EffectSchema } from '@livestore/utils/effect'
2
2
  import type * as otel from '@opentelemetry/api'
3
- import { SqliteAst } from 'effect-db-schema'
3
+ import { SqliteAst, SqliteDsl } from 'effect-db-schema'
4
4
  import { memoize } from 'lodash-es'
5
5
 
6
6
  import { dynamicallyRegisteredTables } from './global-state.js'
@@ -26,7 +26,7 @@ export const migrateDb = ({
26
26
  // TODO use schema migration definition from schema.ts instead
27
27
  sql`create table if not exists ${SCHEMA_META_TABLE} (tableName text primary key, schemaHash text, updatedAt text);`,
28
28
  undefined,
29
- [],
29
+ new Set(),
30
30
  { otelContext },
31
31
  )
32
32
 
@@ -80,11 +80,11 @@ export const migrateTable = ({
80
80
  const columnSpec = makeColumnSpec(tableAst)
81
81
 
82
82
  // TODO need to possibly handle cascading deletes due to foreign keys
83
- db.execute(sql`drop table if exists ${tableName}`, undefined, [], { otelContext })
84
- db.execute(sql`create table if not exists ${tableName} (${columnSpec});`, undefined, [], { otelContext })
83
+ db.execute(sql`drop table if exists ${tableName}`, undefined, new Set(), { otelContext })
84
+ db.execute(sql`create table if not exists ${tableName} (${columnSpec});`, undefined, new Set(), { otelContext })
85
85
 
86
86
  for (const index of tableAst.indexes) {
87
- db.execute(createIndexFromDefinition(tableName, index), undefined, [], { otelContext })
87
+ db.execute(createIndexFromDefinition(tableName, index), undefined, new Set(), { otelContext })
88
88
  }
89
89
 
90
90
  const updatedAt = getMemoizedTimestamp()
@@ -94,7 +94,7 @@ export const migrateTable = ({
94
94
  ON CONFLICT (tableName) DO UPDATE SET schemaHash = $schemaHash, updatedAt = $updatedAt;
95
95
  `,
96
96
  { $tableName: tableName, $schemaHash: schemaHash, $updatedAt: updatedAt } as unknown as PreparedBindValues,
97
- [],
97
+ new Set(),
98
98
  { otelContext },
99
99
  )
100
100
  }
@@ -121,10 +121,13 @@ const toSqliteColumnSpec = (column: SqliteAst.Column) => {
121
121
  const defaultValueStr = (() => {
122
122
  if (column.default._tag === 'None') return ''
123
123
 
124
+ if (SqliteDsl.isSqlDefaultValue(column.default.value)) return `default ${column.default.value.sql}`
125
+
124
126
  const encodeValue = EffectSchema.encodeSync(column.schema)
125
127
  const encodedDefaultValue = encodeValue(column.default.value)
126
128
 
127
- return columnTypeStr === 'text' ? `default '${encodedDefaultValue}'` : `default ${encodedDefaultValue}`
129
+ if (columnTypeStr === 'text') return `default '${encodedDefaultValue}'`
130
+ return `default ${encodedDefaultValue}`
128
131
  })()
129
132
 
130
133
  return `${column.name} ${columnTypeStr} ${nullableStr} ${defaultValueStr}`
@@ -1,38 +1,39 @@
1
1
  import { notYetImplemented, shouldNeverHappen } from '@livestore/utils'
2
2
  import { Schema } from '@livestore/utils/effect'
3
3
 
4
+ import { rawSqlMutation, type RawSqlMutationEvent } from './schema/mutations.js'
4
5
  import type { FromTable, TableDef } from './schema/table-def.js'
5
6
 
6
7
  /**
7
- * A description of a path to update a value either ...
8
- * - as a whole row
9
- * - or a single column value
10
- * - or a sub value in a JSON column
8
+ * Semantic information about a query with supported cases being:
9
+ * - a whole row
10
+ * - a single column value
11
+ * - a sub value in a JSON column
11
12
  */
12
- export type UpdatePathDesc<TTableDef extends TableDef = TableDef> =
13
- | UpdatePathDescNone
14
- | UpdatePathDescRow<TTableDef>
15
- | UpdatePathDescColJsonValue<TTableDef, GetJsonColumn<TTableDef>>
16
- | UpdatePathDescCol<TTableDef, keyof TTableDef['sqliteDef']['columns']>
13
+ export type QueryInfo<TTableDef extends TableDef = TableDef> =
14
+ | QueryInfoNone
15
+ | QueryInfoRow<TTableDef>
16
+ | QueryInfoColJsonValue<TTableDef, GetJsonColumn<TTableDef>>
17
+ | QueryInfoCol<TTableDef, keyof TTableDef['sqliteDef']['columns']>
17
18
 
18
- export type UpdatePathDescNone = {
19
+ export type QueryInfoNone = {
19
20
  _tag: 'None'
20
21
  }
21
22
 
22
- export type UpdatePathDescRow<TTableDef extends TableDef> = {
23
+ export type QueryInfoRow<TTableDef extends TableDef> = {
23
24
  _tag: 'Row'
24
25
  table: TTableDef
25
26
  id: string
26
27
  }
27
28
 
28
- export type UpdatePathDescCol<TTableDef extends TableDef, TColName extends keyof TTableDef['sqliteDef']['columns']> = {
29
+ export type QueryInfoCol<TTableDef extends TableDef, TColName extends keyof TTableDef['sqliteDef']['columns']> = {
29
30
  _tag: 'Col'
30
31
  table: TTableDef
31
32
  id: string
32
33
  column: TColName
33
34
  }
34
35
 
35
- export type UpdatePathDescColJsonValue<TTableDef extends TableDef, TColName extends GetJsonColumn<TTableDef>> = {
36
+ export type QueryInfoColJsonValue<TTableDef extends TableDef, TColName extends GetJsonColumn<TTableDef>> = {
36
37
  _tag: 'ColJsonValue'
37
38
  table: TTableDef
38
39
  id: string
@@ -49,9 +50,7 @@ type GetJsonColumn<TTableDef extends TableDef> = keyof {
49
50
  : never]: {}
50
51
  }
51
52
 
52
- // type GetObjValues<TObj extends {}> = TObj[keyof TObj]
53
-
54
- export type UpdateValueForPath<TPath extends UpdatePathDesc> = TPath extends { _tag: 'Row' }
53
+ export type UpdateValueForPath<TPath extends QueryInfo> = TPath extends { _tag: 'Row' }
55
54
  ? Partial<FromTable.RowDecodedAll<TPath['table']>>
56
55
  : TPath extends { _tag: 'Col' }
57
56
  ? Schema.Schema.To<TPath['table']['sqliteDef']['columns'][TPath['column']]['schema']>
@@ -59,10 +58,10 @@ export type UpdateValueForPath<TPath extends UpdatePathDesc> = TPath extends { _
59
58
  ? { TODO: true }
60
59
  : never
61
60
 
62
- export const storeEventForUpdatePath = <TPath extends UpdatePathDesc>(
61
+ export const mutationForQueryInfo = <const TPath extends QueryInfo>(
63
62
  updatePath: TPath,
64
63
  value: UpdateValueForPath<TPath>,
65
- ): StoreEvent => {
64
+ ): RawSqlMutationEvent => {
66
65
  if (updatePath._tag === 'ColJsonValue' || updatePath._tag === 'None') {
67
66
  return notYetImplemented('TODO')
68
67
  }
@@ -96,7 +95,5 @@ export const storeEventForUpdatePath = <TPath extends UpdatePathDesc>(
96
95
  const sql = `UPDATE ${sqliteTableDef.name} SET ${updateClause} ${whereClause}`
97
96
  const writeTables = new Set<string>([updatePath.table.sqliteDef.name])
98
97
 
99
- return { eventType: 'livestore.RawSql', args: { sql, bindValues, writeTables } }
98
+ return rawSqlMutation({ sql, bindValues, writeTables })
100
99
  }
101
-
102
- type StoreEvent = { eventType: string; args: any }
@@ -1,15 +1,6 @@
1
1
  import React, { useContext } from 'react'
2
2
 
3
3
  import type { LiveStoreContext as LiveStoreContext_ } from '../effect/LiveStore.js'
4
- import type { LiveQuery } from '../reactiveQueries/base-class.js'
5
-
6
- // TODO remove this?
7
- declare global {
8
- // NOTE Can be extended
9
- interface LiveStoreQueryTypes {
10
- [key: string]: LiveQuery<any>
11
- }
12
- }
13
4
 
14
5
  export const LiveStoreContext = React.createContext<LiveStoreContext_ | undefined>(undefined)
15
6
 
@@ -1,25 +1,25 @@
1
1
  import React from 'react'
2
2
 
3
+ import { mutationForQueryInfo, type QueryInfoCol, type QueryInfoRow } from '../query-info.js'
3
4
  import type { LiveQuery } from '../reactiveQueries/base-class.js'
4
- import { storeEventForUpdatePath, type UpdatePathDescCol, type UpdatePathDescRow } from '../update-path.js'
5
5
  import { useStore } from './LiveStoreContext.js'
6
6
  import { useQueryRef } from './useQuery.js'
7
7
  import type { Dispatch, SetStateAction } from './useRow.js'
8
8
 
9
- export const useAtom = <TQuery extends LiveQuery<any, UpdatePathDescRow<any> | UpdatePathDescCol<any, any>>>(
9
+ export const useAtom = <TQuery extends LiveQuery<any, QueryInfoRow<any> | QueryInfoCol<any, any>>>(
10
10
  query$: TQuery,
11
- ): [value: TQuery['result!'], setValue: Dispatch<SetStateAction<TQuery['result!']>>] => {
11
+ ): [value: TQuery['__result!'], setValue: Dispatch<SetStateAction<TQuery['__result!']>>] => {
12
12
  const query$Ref = useQueryRef(query$)
13
13
 
14
14
  const { store } = useStore()
15
15
 
16
- const setValue = React.useMemo<Dispatch<SetStateAction<TQuery['result!']>>>(() => {
16
+ const setValue = React.useMemo<Dispatch<SetStateAction<TQuery['__result!']>>>(() => {
17
17
  return (newValueOrFn: any) => {
18
18
  const newValue = typeof newValueOrFn === 'function' ? newValueOrFn(query$Ref.current) : newValueOrFn
19
19
 
20
- store.applyEvents([storeEventForUpdatePath(query$.updatePathDesc!, newValue)])
20
+ store.mutate(mutationForQueryInfo(query$.queryInfo!, newValue))
21
21
  }
22
- }, [query$.updatePathDesc, query$Ref, store])
22
+ }, [query$.queryInfo, query$Ref, store])
23
23
 
24
24
  return [query$Ref.current, setValue]
25
25
  }
@@ -2,15 +2,15 @@ import { act, renderHook } from '@testing-library/react'
2
2
  import React from 'react'
3
3
  import { describe, expect, it } from 'vitest'
4
4
 
5
- import * as LiveStoreReact from '../../react/index.js'
6
- import { querySQL } from '../../reactiveQueries/sql.js'
7
- import { makeTodoMvc, parseTodos } from './fixture.js'
5
+ import { makeTodoMvc, parseTodos } from '../__tests__/react/fixture.js'
6
+ import { querySQL } from '../reactiveQueries/sql.js'
7
+ import * as LiveStoreReact from './index.js'
8
8
 
9
9
  describe('useQuery', () => {
10
10
  it('simple', async () => {
11
11
  let renderCount = 0
12
12
 
13
- const { wrapper, store, mutations } = await makeTodoMvc()
13
+ const { wrapper, store, cud } = await makeTodoMvc()
14
14
 
15
15
  const allTodos$ = querySQL(`select * from todos`, { map: parseTodos })
16
16
 
@@ -26,7 +26,7 @@ describe('useQuery', () => {
26
26
  expect(result.current.length).toBe(0)
27
27
  expect(renderCount).toBe(1)
28
28
 
29
- act(() => store.applyEvents([mutations.todos.insert({ id: 't1', text: 'buy milk', completed: false })]))
29
+ act(() => store.mutate(cud.todos.insert({ id: 't1', text: 'buy milk', completed: false })))
30
30
 
31
31
  expect(result.current.length).toBe(1)
32
32
  expect(result.current[0]!.text).toBe('buy milk')
@@ -36,15 +36,15 @@ describe('useQuery', () => {
36
36
  it('same `useQuery` hook invoked with different queries', async () => {
37
37
  let renderCount = 0
38
38
 
39
- const { wrapper, store, mutations } = await makeTodoMvc()
39
+ const { wrapper, store, cud } = await makeTodoMvc()
40
40
 
41
41
  const todo1$ = querySQL(`select * from todos where id = 't1'`, { label: 'libraryTracksView1', map: parseTodos })
42
42
  const todo2$ = querySQL(`select * from todos where id = 't2'`, { label: 'libraryTracksView2', map: parseTodos })
43
43
 
44
- store.applyEvents([
45
- mutations.todos.insert({ id: 't1', text: 'buy milk', completed: false }),
46
- mutations.todos.insert({ id: 't2', text: 'buy eggs', completed: false }),
47
- ])
44
+ store.mutate(
45
+ cud.todos.insert({ id: 't1', text: 'buy milk', completed: false }),
46
+ cud.todos.insert({ id: 't2', text: 'buy eggs', completed: false }),
47
+ )
48
48
 
49
49
  const { result, rerender } = renderHook(
50
50
  (todoId: string) => {
@@ -60,7 +60,7 @@ describe('useQuery', () => {
60
60
  expect(result.current).toBe('buy milk')
61
61
  expect(renderCount).toBe(1)
62
62
 
63
- act(() => store.applyEvents([mutations.todos.update({ where: { id: 't1' }, values: { text: 'buy soy milk' } })]))
63
+ act(() => store.mutate(cud.todos.update({ where: { id: 't1' }, values: { text: 'buy soy milk' } })))
64
64
 
65
65
  expect(result.current).toBe('buy soy milk')
66
66
  expect(renderCount).toBe(2)
@@ -2,7 +2,7 @@ import * as otel from '@opentelemetry/api'
2
2
  import { isEqual } from 'lodash-es'
3
3
  import React from 'react'
4
4
 
5
- import type { GetResult, LiveQuery, LiveQueryAny } from '../reactiveQueries/base-class.js'
5
+ import type { GetResult, LiveQueryAny } from '../reactiveQueries/base-class.js'
6
6
  import { useStore } from './LiveStoreContext.js'
7
7
  import { extractStackInfoFromStackTrace, originalStackLimit } from './utils/stack-info.js'
8
8
  import { useStateRefWithReactiveInput } from './utils/useStateRefWithReactiveInput.js'
@@ -2,10 +2,11 @@ import { act, render, renderHook } from '@testing-library/react'
2
2
  import React from 'react'
3
3
  import { describe, expect, it } from 'vitest'
4
4
 
5
- import * as LiveStore from '../../index.js'
6
- import * as LiveStoreReact from '../../react/index.js'
7
- import type { Todo } from './fixture.js'
8
- import { makeTodoMvc, todos } from './fixture.js'
5
+ import type { Todo } from '../__tests__/react/fixture.js'
6
+ import { makeTodoMvc, todos } from '../__tests__/react/fixture.js'
7
+ import * as LiveStore from '../index.js'
8
+ import { mutationForQueryInfo } from '../query-info.js'
9
+ import * as LiveStoreReact from './index.js'
9
10
 
10
11
  describe('useRow', () => {
11
12
  it('should update the data based on component key', async () => {
@@ -27,9 +28,7 @@ describe('useRow', () => {
27
28
  expect(result.current.state.username).toBe('')
28
29
  expect(renderCount).toBe(1)
29
30
 
30
- act(() => {
31
- void store.execute(LiveStore.sql`INSERT INTO UserInfo (id, username) VALUES ('u2', 'username_u2');`)
32
- })
31
+ act(() => store.execute(LiveStore.sql`INSERT INTO UserInfo (id, username) VALUES ('u2', 'username_u2')`))
33
32
 
34
33
  rerender('u2')
35
34
 
@@ -85,9 +84,7 @@ describe('useRow', () => {
85
84
 
86
85
  act(() => result.current.setState.username('username_u1_hello'))
87
86
 
88
- act(() => {
89
- void store.execute(LiveStore.sql`UPDATE UserInfo SET username = 'username_u1_hello' WHERE id = 'u1';`)
90
- })
87
+ act(() => store.execute(LiveStore.sql`UPDATE UserInfo SET username = 'username_u1_hello' WHERE id = 'u1';`))
91
88
 
92
89
  expect(result.current.state.id).toBe('u1')
93
90
  expect(result.current.state.username).toBe('username_u1_hello')
@@ -149,10 +146,11 @@ describe('useRow', () => {
149
146
  expect(appRouterRenderCount).toBe(1)
150
147
 
151
148
  act(() =>
152
- store.applyEvent('livestore.RawSql', {
153
- sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0);`,
154
- writeTables: ['todos'],
155
- }),
149
+ store.mutate(
150
+ LiveStore.rawSqlMutation({
151
+ sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)`,
152
+ }),
153
+ ),
156
154
  )
157
155
 
158
156
  expect(appRouterRenderCount).toBe(1)
@@ -168,31 +166,15 @@ describe('useRow', () => {
168
166
  expect(renderResult.getByRole('current-id').innerHTML).toMatchInlineSnapshot('"Current Task Id: t1"')
169
167
 
170
168
  act(() =>
171
- store.applyEvents([
172
- {
173
- eventType: 'livestore.RawSql',
174
- args: {
175
- sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t2', 'buy eggs', 0);`,
176
- writeTables: ['todos'],
177
- },
178
- },
179
- {
180
- eventType: 'livestore.UpdateComponentState',
181
- args: {
182
- id: 'singleton',
183
- columnNames: ['currentTaskId'],
184
- tableName: AppRouterSchema.sqliteDef.name,
185
- bindValues: { currentTaskId: 't2' },
186
- },
187
- },
188
- {
189
- eventType: 'livestore.RawSql',
190
- args: {
191
- sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t3', 'buy bread', 0);`,
192
- writeTables: ['todos'],
193
- },
194
- },
195
- ]),
169
+ store.mutate(
170
+ LiveStore.rawSqlMutation({
171
+ sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t2', 'buy eggs', 0)`,
172
+ }),
173
+ mutationForQueryInfo({ _tag: 'Col', table: AppRouterSchema, column: 'currentTaskId', id: 'singleton' }, 't2'),
174
+ LiveStore.rawSqlMutation({
175
+ sql: LiveStore.sql`INSERT INTO todos (id, text, completed) VALUES ('t3', 'buy bread', 0)`,
176
+ }),
177
+ ),
196
178
  )
197
179
 
198
180
  expect(appRouterRenderCount).toBe(3)
@@ -4,18 +4,18 @@ import { mapValues } from 'lodash-es'
4
4
  import React from 'react'
5
5
 
6
6
  import type { DbGraph, LiveQuery } from '../index.js'
7
+ import type { QueryInfo } from '../query-info.js'
8
+ import { mutationForQueryInfo } from '../query-info.js'
7
9
  import type { RowResult } from '../row-query.js'
8
10
  import { rowQuery } from '../row-query.js'
9
11
  import { type DefaultSqliteTableDef, type TableDef, tableIsSingleton, type TableOptions } from '../schema/table-def.js'
10
- import type { UpdatePathDesc } from '../update-path.js'
11
- import { storeEventForUpdatePath } from '../update-path.js'
12
12
  import { useStore } from './LiveStoreContext.js'
13
13
  import { useQueryRef } from './useQuery.js'
14
14
 
15
15
  export type UseRowResult<TTableDef extends TableDef> = [
16
16
  row: RowResult<TTableDef>,
17
17
  setRow: StateSetters<TTableDef>,
18
- query$: LiveQuery<RowResult<TTableDef>, UpdatePathDesc>,
18
+ query$: LiveQuery<RowResult<TTableDef>, QueryInfo>,
19
19
  ]
20
20
 
21
21
  export type UseRowOptionsDefaulValues<TTableDef extends TableDef> = {
@@ -69,7 +69,7 @@ export const useRow: {
69
69
  cachedItem.span.addEvent('new-subscriber', { reactId })
70
70
 
71
71
  return {
72
- query$: cachedItem.query$ as LiveQuery<RowResult<TTableDef>, UpdatePathDesc>,
72
+ query$: cachedItem.query$ as LiveQuery<RowResult<TTableDef>, QueryInfo>,
73
73
  otelContext: cachedItem.otelContext,
74
74
  }
75
75
  }
@@ -83,12 +83,12 @@ export const useRow: {
83
83
  const otelContext = otel.trace.setSpan(otel.context.active(), span)
84
84
 
85
85
  const query$ = tableIsSingleton(table)
86
- ? (rowQuery(table, { otelContext, dbGraph }) as LiveQuery<RowResult<TTableDef>, UpdatePathDesc>)
86
+ ? (rowQuery(table, { otelContext, dbGraph }) as LiveQuery<RowResult<TTableDef>, QueryInfo>)
87
87
  : (rowQuery(table as TTableDef & { options: { isSingleton: false } }, id!, {
88
88
  otelContext,
89
89
  defaultValues: defaultValues!,
90
90
  dbGraph,
91
- }) as any as LiveQuery<RowResult<TTableDef>, UpdatePathDesc>)
91
+ }) as any as LiveQuery<RowResult<TTableDef>, QueryInfo>)
92
92
 
93
93
  rcCache.set(table, id ?? 'singleton', query$, reactId, otelContext, span)
94
94
 
@@ -117,7 +117,7 @@ export const useRow: {
117
117
  const newValue = typeof newValueOrFn === 'function' ? newValueOrFn(query$Ref.current) : newValueOrFn
118
118
  if (query$Ref.current === newValue) return
119
119
 
120
- store.applyEvents([storeEventForUpdatePath(query$.updatePathDesc!, { value: newValue })])
120
+ store.mutate(mutationForQueryInfo(query$.queryInfo!, { value: newValue }))
121
121
  }
122
122
  } else {
123
123
  const setState = // TODO: do we have a better type for the values that can go in SQLite?
@@ -130,7 +130,7 @@ export const useRow: {
130
130
  // @ts-expect-error TODO fix typing
131
131
  if (query$Ref.current[columnName] === newValue) return
132
132
 
133
- store.applyEvents([storeEventForUpdatePath(query$.updatePathDesc!, { [columnName]: newValue })])
133
+ store.mutate(mutationForQueryInfo(query$.queryInfo!, { [columnName]: newValue }))
134
134
  })
135
135
 
136
136
  setState.setMany = (columnValuesOrFn: Partial<TComponentState>) => {
@@ -147,12 +147,12 @@ export const useRow: {
147
147
  return
148
148
  }
149
149
 
150
- store.applyEvents([storeEventForUpdatePath(query$.updatePathDesc!, columnValues)])
150
+ store.mutate(mutationForQueryInfo(query$.queryInfo!, columnValues))
151
151
  }
152
152
 
153
153
  return setState as any
154
154
  }
155
- }, [query$.updatePathDesc, query$Ref, sqliteTableDef.columns, store, table.isSingleColumn])
155
+ }, [query$.queryInfo, query$Ref, sqliteTableDef.columns, store, table.isSingleColumn])
156
156
 
157
157
  return [query$Ref.current, setState, query$]
158
158
  }
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
 
3
- import { ReactiveGraph } from '../reactive.js'
3
+ import { ReactiveGraph } from './reactive.js'
4
4
 
5
5
  describe('a trivial graph', () => {
6
6
  const makeGraph = () => {