@livestore/common 0.0.0-snapshot-2b8a9de3ec1a701aca891ebc2c98eb328274ae9e → 0.0.0-snapshot-2c861249e50661661613204300b1fc0d902c2e46

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 (287) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/__tests__/fixture.d.ts +83 -221
  3. package/dist/__tests__/fixture.d.ts.map +1 -1
  4. package/dist/__tests__/fixture.js +33 -11
  5. package/dist/__tests__/fixture.js.map +1 -1
  6. package/dist/adapter-types.d.ts +36 -22
  7. package/dist/adapter-types.d.ts.map +1 -1
  8. package/dist/adapter-types.js +20 -8
  9. package/dist/adapter-types.js.map +1 -1
  10. package/dist/debug-info.d.ts.map +1 -1
  11. package/dist/debug-info.js +1 -0
  12. package/dist/debug-info.js.map +1 -1
  13. package/dist/devtools/devtools-messages-client-session.d.ts +21 -21
  14. package/dist/devtools/devtools-messages-common.d.ts +13 -6
  15. package/dist/devtools/devtools-messages-common.d.ts.map +1 -1
  16. package/dist/devtools/devtools-messages-common.js +6 -0
  17. package/dist/devtools/devtools-messages-common.js.map +1 -1
  18. package/dist/devtools/devtools-messages-leader.d.ts +46 -46
  19. package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
  20. package/dist/devtools/devtools-messages-leader.js +12 -13
  21. package/dist/devtools/devtools-messages-leader.js.map +1 -1
  22. package/dist/index.d.ts +2 -5
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2 -5
  25. package/dist/index.js.map +1 -1
  26. package/dist/leader-thread/LeaderSyncProcessor.d.ts +34 -12
  27. package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
  28. package/dist/leader-thread/LeaderSyncProcessor.js +284 -226
  29. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
  30. package/dist/leader-thread/apply-event.d.ts +16 -0
  31. package/dist/leader-thread/apply-event.d.ts.map +1 -0
  32. package/dist/leader-thread/apply-event.js +122 -0
  33. package/dist/leader-thread/apply-event.js.map +1 -0
  34. package/dist/leader-thread/eventlog.d.ts +27 -0
  35. package/dist/leader-thread/eventlog.d.ts.map +1 -0
  36. package/dist/leader-thread/eventlog.js +123 -0
  37. package/dist/leader-thread/eventlog.js.map +1 -0
  38. package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
  39. package/dist/leader-thread/leader-worker-devtools.js +22 -23
  40. package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
  41. package/dist/leader-thread/make-leader-thread-layer.d.ts +16 -4
  42. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
  43. package/dist/leader-thread/make-leader-thread-layer.js +36 -41
  44. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
  45. package/dist/leader-thread/mod.d.ts +1 -1
  46. package/dist/leader-thread/mod.d.ts.map +1 -1
  47. package/dist/leader-thread/mod.js +1 -1
  48. package/dist/leader-thread/mod.js.map +1 -1
  49. package/dist/leader-thread/recreate-db.d.ts.map +1 -1
  50. package/dist/leader-thread/recreate-db.js +7 -7
  51. package/dist/leader-thread/recreate-db.js.map +1 -1
  52. package/dist/leader-thread/types.d.ts +40 -25
  53. package/dist/leader-thread/types.d.ts.map +1 -1
  54. package/dist/leader-thread/types.js.map +1 -1
  55. package/dist/materializer-helper.d.ts +23 -0
  56. package/dist/materializer-helper.d.ts.map +1 -0
  57. package/dist/materializer-helper.js +70 -0
  58. package/dist/materializer-helper.js.map +1 -0
  59. package/dist/query-builder/api.d.ts +55 -50
  60. package/dist/query-builder/api.d.ts.map +1 -1
  61. package/dist/query-builder/api.js +3 -5
  62. package/dist/query-builder/api.js.map +1 -1
  63. package/dist/query-builder/astToSql.d.ts.map +1 -1
  64. package/dist/query-builder/astToSql.js +59 -37
  65. package/dist/query-builder/astToSql.js.map +1 -1
  66. package/dist/query-builder/impl.d.ts +2 -3
  67. package/dist/query-builder/impl.d.ts.map +1 -1
  68. package/dist/query-builder/impl.js +47 -43
  69. package/dist/query-builder/impl.js.map +1 -1
  70. package/dist/query-builder/impl.test.d.ts +86 -1
  71. package/dist/query-builder/impl.test.d.ts.map +1 -1
  72. package/dist/query-builder/impl.test.js +223 -36
  73. package/dist/query-builder/impl.test.js.map +1 -1
  74. package/dist/rehydrate-from-eventlog.d.ts +15 -0
  75. package/dist/rehydrate-from-eventlog.d.ts.map +1 -0
  76. package/dist/{rehydrate-from-mutationlog.js → rehydrate-from-eventlog.js} +27 -28
  77. package/dist/rehydrate-from-eventlog.js.map +1 -0
  78. package/dist/schema/EventDef.d.ts +136 -0
  79. package/dist/schema/EventDef.d.ts.map +1 -0
  80. package/dist/schema/EventDef.js +58 -0
  81. package/dist/schema/EventDef.js.map +1 -0
  82. package/dist/schema/EventId.d.ts +10 -1
  83. package/dist/schema/EventId.d.ts.map +1 -1
  84. package/dist/schema/EventId.js +24 -3
  85. package/dist/schema/EventId.js.map +1 -1
  86. package/dist/schema/LiveStoreEvent.d.ts +255 -0
  87. package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
  88. package/dist/schema/LiveStoreEvent.js +118 -0
  89. package/dist/schema/LiveStoreEvent.js.map +1 -0
  90. package/dist/schema/client-document-def.d.ts +223 -0
  91. package/dist/schema/client-document-def.d.ts.map +1 -0
  92. package/dist/schema/client-document-def.js +164 -0
  93. package/dist/schema/client-document-def.js.map +1 -0
  94. package/dist/schema/client-document-def.test.d.ts +2 -0
  95. package/dist/schema/client-document-def.test.d.ts.map +1 -0
  96. package/dist/schema/client-document-def.test.js +161 -0
  97. package/dist/schema/client-document-def.test.js.map +1 -0
  98. package/dist/schema/db-schema/dsl/mod.d.ts.map +1 -1
  99. package/dist/schema/events.d.ts +2 -0
  100. package/dist/schema/events.d.ts.map +1 -0
  101. package/dist/schema/events.js +2 -0
  102. package/dist/schema/events.js.map +1 -0
  103. package/dist/schema/mod.d.ts +4 -3
  104. package/dist/schema/mod.d.ts.map +1 -1
  105. package/dist/schema/mod.js +4 -3
  106. package/dist/schema/mod.js.map +1 -1
  107. package/dist/schema/schema.d.ts +26 -22
  108. package/dist/schema/schema.d.ts.map +1 -1
  109. package/dist/schema/schema.js +45 -43
  110. package/dist/schema/schema.js.map +1 -1
  111. package/dist/schema/sqlite-state.d.ts +12 -0
  112. package/dist/schema/sqlite-state.d.ts.map +1 -0
  113. package/dist/schema/sqlite-state.js +36 -0
  114. package/dist/schema/sqlite-state.js.map +1 -0
  115. package/dist/schema/system-tables.d.ts +121 -85
  116. package/dist/schema/system-tables.d.ts.map +1 -1
  117. package/dist/schema/system-tables.js +68 -43
  118. package/dist/schema/system-tables.js.map +1 -1
  119. package/dist/schema/table-def.d.ts +26 -96
  120. package/dist/schema/table-def.d.ts.map +1 -1
  121. package/dist/schema/table-def.js +14 -64
  122. package/dist/schema/table-def.js.map +1 -1
  123. package/dist/schema/view.d.ts +3 -0
  124. package/dist/schema/view.d.ts.map +1 -0
  125. package/dist/schema/view.js +3 -0
  126. package/dist/schema/view.js.map +1 -0
  127. package/dist/schema-management/common.d.ts +4 -4
  128. package/dist/schema-management/common.d.ts.map +1 -1
  129. package/dist/schema-management/migrations.d.ts.map +1 -1
  130. package/dist/schema-management/migrations.js +6 -6
  131. package/dist/schema-management/migrations.js.map +1 -1
  132. package/dist/schema-management/validate-mutation-defs.d.ts +3 -3
  133. package/dist/schema-management/validate-mutation-defs.d.ts.map +1 -1
  134. package/dist/schema-management/validate-mutation-defs.js +17 -17
  135. package/dist/schema-management/validate-mutation-defs.js.map +1 -1
  136. package/dist/sync/ClientSessionSyncProcessor.d.ts +16 -8
  137. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
  138. package/dist/sync/ClientSessionSyncProcessor.js +50 -43
  139. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
  140. package/dist/sync/next/facts.d.ts +19 -19
  141. package/dist/sync/next/facts.d.ts.map +1 -1
  142. package/dist/sync/next/facts.js +2 -2
  143. package/dist/sync/next/facts.js.map +1 -1
  144. package/dist/sync/next/history-dag-common.d.ts +3 -3
  145. package/dist/sync/next/history-dag-common.d.ts.map +1 -1
  146. package/dist/sync/next/history-dag-common.js +1 -1
  147. package/dist/sync/next/history-dag-common.js.map +1 -1
  148. package/dist/sync/next/history-dag.js +1 -1
  149. package/dist/sync/next/history-dag.js.map +1 -1
  150. package/dist/sync/next/rebase-events.d.ts +7 -7
  151. package/dist/sync/next/rebase-events.d.ts.map +1 -1
  152. package/dist/sync/next/rebase-events.js +5 -5
  153. package/dist/sync/next/rebase-events.js.map +1 -1
  154. package/dist/sync/next/test/compact-events.calculator.test.js +38 -33
  155. package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
  156. package/dist/sync/next/test/compact-events.test.js +71 -71
  157. package/dist/sync/next/test/compact-events.test.js.map +1 -1
  158. package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +25 -25
  159. package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
  160. package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +60 -25
  161. package/dist/sync/next/test/event-fixtures.js.map +1 -0
  162. package/dist/sync/next/test/mod.d.ts +1 -1
  163. package/dist/sync/next/test/mod.d.ts.map +1 -1
  164. package/dist/sync/next/test/mod.js +1 -1
  165. package/dist/sync/next/test/mod.js.map +1 -1
  166. package/dist/sync/sync.d.ts +8 -7
  167. package/dist/sync/sync.d.ts.map +1 -1
  168. package/dist/sync/sync.js.map +1 -1
  169. package/dist/sync/syncstate.d.ts +69 -93
  170. package/dist/sync/syncstate.d.ts.map +1 -1
  171. package/dist/sync/syncstate.js +143 -146
  172. package/dist/sync/syncstate.js.map +1 -1
  173. package/dist/sync/syncstate.test.js +208 -289
  174. package/dist/sync/syncstate.test.js.map +1 -1
  175. package/dist/sync/validate-push-payload.d.ts +2 -2
  176. package/dist/sync/validate-push-payload.d.ts.map +1 -1
  177. package/dist/sync/validate-push-payload.js.map +1 -1
  178. package/dist/version.d.ts +1 -1
  179. package/dist/version.js +1 -1
  180. package/package.json +2 -2
  181. package/src/__tests__/fixture.ts +36 -15
  182. package/src/adapter-types.ts +34 -23
  183. package/src/debug-info.ts +1 -0
  184. package/src/devtools/devtools-messages-common.ts +9 -0
  185. package/src/devtools/devtools-messages-leader.ts +14 -15
  186. package/src/index.ts +2 -5
  187. package/src/leader-thread/LeaderSyncProcessor.ts +485 -389
  188. package/src/leader-thread/apply-event.ts +197 -0
  189. package/src/leader-thread/eventlog.ts +199 -0
  190. package/src/leader-thread/leader-worker-devtools.ts +23 -25
  191. package/src/leader-thread/make-leader-thread-layer.ts +68 -61
  192. package/src/leader-thread/mod.ts +1 -1
  193. package/src/leader-thread/recreate-db.ts +7 -8
  194. package/src/leader-thread/types.ts +39 -29
  195. package/src/materializer-helper.ts +110 -0
  196. package/src/query-builder/api.ts +76 -102
  197. package/src/query-builder/astToSql.ts +68 -39
  198. package/src/query-builder/impl.test.ts +239 -42
  199. package/src/query-builder/impl.ts +66 -54
  200. package/src/{rehydrate-from-mutationlog.ts → rehydrate-from-eventlog.ts} +37 -40
  201. package/src/schema/EventDef.ts +216 -0
  202. package/src/schema/EventId.ts +30 -4
  203. package/src/schema/LiveStoreEvent.ts +239 -0
  204. package/src/schema/client-document-def.test.ts +188 -0
  205. package/src/schema/client-document-def.ts +436 -0
  206. package/src/schema/db-schema/dsl/mod.ts +0 -1
  207. package/src/schema/events.ts +1 -0
  208. package/src/schema/mod.ts +4 -3
  209. package/src/schema/schema.ts +78 -68
  210. package/src/schema/sqlite-state.ts +62 -0
  211. package/src/schema/system-tables.ts +54 -46
  212. package/src/schema/table-def.ts +51 -209
  213. package/src/schema/view.ts +2 -0
  214. package/src/schema-management/common.ts +4 -4
  215. package/src/schema-management/migrations.ts +8 -9
  216. package/src/schema-management/validate-mutation-defs.ts +22 -24
  217. package/src/sync/ClientSessionSyncProcessor.ts +66 -53
  218. package/src/sync/next/facts.ts +31 -32
  219. package/src/sync/next/history-dag-common.ts +4 -4
  220. package/src/sync/next/history-dag.ts +1 -1
  221. package/src/sync/next/rebase-events.ts +13 -13
  222. package/src/sync/next/test/compact-events.calculator.test.ts +45 -45
  223. package/src/sync/next/test/compact-events.test.ts +73 -73
  224. package/src/sync/next/test/event-fixtures.ts +219 -0
  225. package/src/sync/next/test/mod.ts +1 -1
  226. package/src/sync/sync.ts +9 -12
  227. package/src/sync/syncstate.test.ts +236 -323
  228. package/src/sync/syncstate.ts +218 -203
  229. package/src/sync/validate-push-payload.ts +2 -2
  230. package/src/version.ts +1 -1
  231. package/tsconfig.json +1 -0
  232. package/dist/derived-mutations.d.ts +0 -109
  233. package/dist/derived-mutations.d.ts.map +0 -1
  234. package/dist/derived-mutations.js +0 -54
  235. package/dist/derived-mutations.js.map +0 -1
  236. package/dist/derived-mutations.test.d.ts +0 -2
  237. package/dist/derived-mutations.test.d.ts.map +0 -1
  238. package/dist/derived-mutations.test.js +0 -93
  239. package/dist/derived-mutations.test.js.map +0 -1
  240. package/dist/init-singleton-tables.d.ts +0 -4
  241. package/dist/init-singleton-tables.d.ts.map +0 -1
  242. package/dist/init-singleton-tables.js +0 -16
  243. package/dist/init-singleton-tables.js.map +0 -1
  244. package/dist/leader-thread/apply-mutation.d.ts +0 -11
  245. package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
  246. package/dist/leader-thread/apply-mutation.js +0 -115
  247. package/dist/leader-thread/apply-mutation.js.map +0 -1
  248. package/dist/leader-thread/mutationlog.d.ts +0 -11
  249. package/dist/leader-thread/mutationlog.d.ts.map +0 -1
  250. package/dist/leader-thread/mutationlog.js +0 -31
  251. package/dist/leader-thread/mutationlog.js.map +0 -1
  252. package/dist/leader-thread/pull-queue-set.d.ts +0 -7
  253. package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
  254. package/dist/leader-thread/pull-queue-set.js +0 -48
  255. package/dist/leader-thread/pull-queue-set.js.map +0 -1
  256. package/dist/mutation.d.ts +0 -20
  257. package/dist/mutation.d.ts.map +0 -1
  258. package/dist/mutation.js +0 -68
  259. package/dist/mutation.js.map +0 -1
  260. package/dist/query-info.d.ts +0 -41
  261. package/dist/query-info.d.ts.map +0 -1
  262. package/dist/query-info.js +0 -7
  263. package/dist/query-info.js.map +0 -1
  264. package/dist/rehydrate-from-mutationlog.d.ts +0 -14
  265. package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
  266. package/dist/rehydrate-from-mutationlog.js.map +0 -1
  267. package/dist/schema/MutationEvent.d.ts +0 -202
  268. package/dist/schema/MutationEvent.d.ts.map +0 -1
  269. package/dist/schema/MutationEvent.js +0 -105
  270. package/dist/schema/MutationEvent.js.map +0 -1
  271. package/dist/schema/mutations.d.ts +0 -115
  272. package/dist/schema/mutations.d.ts.map +0 -1
  273. package/dist/schema/mutations.js +0 -42
  274. package/dist/schema/mutations.js.map +0 -1
  275. package/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
  276. package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
  277. package/src/derived-mutations.test.ts +0 -101
  278. package/src/derived-mutations.ts +0 -170
  279. package/src/init-singleton-tables.ts +0 -24
  280. package/src/leader-thread/apply-mutation.ts +0 -187
  281. package/src/leader-thread/mutationlog.ts +0 -49
  282. package/src/leader-thread/pull-queue-set.ts +0 -67
  283. package/src/mutation.ts +0 -108
  284. package/src/query-info.ts +0 -83
  285. package/src/schema/MutationEvent.ts +0 -224
  286. package/src/schema/mutations.ts +0 -193
  287. package/src/sync/next/test/mutation-fixtures.ts +0 -228
package/src/mutation.ts DELETED
@@ -1,108 +0,0 @@
1
- import { Schema } from '@livestore/utils/effect'
2
-
3
- import { SessionIdSymbol } from './adapter-types.js'
4
- import type { QueryBuilder } from './query-builder/api.js'
5
- import { isQueryBuilder } from './query-builder/api.js'
6
- import type * as MutationEvent from './schema/MutationEvent.js'
7
- import type { MutationDef, MutationHandlerResult } from './schema/mutations.js'
8
- import type { BindValues } from './sql-queries/sql-queries.js'
9
- import type { PreparedBindValues } from './util.js'
10
- import { prepareBindValues } from './util.js'
11
-
12
- export const getExecArgsFromMutation = ({
13
- mutationDef,
14
- mutationEvent,
15
- }: {
16
- mutationDef: MutationDef.Any
17
- /** Both encoded and decoded mutation events are supported to reduce the number of times we need to decode/encode */
18
- mutationEvent:
19
- | {
20
- decoded: MutationEvent.AnyDecoded | MutationEvent.PartialAnyDecoded
21
- encoded: undefined
22
- }
23
- | {
24
- decoded: undefined
25
- encoded: MutationEvent.AnyEncoded | MutationEvent.PartialAnyEncoded
26
- }
27
- }): ReadonlyArray<{
28
- statementSql: string
29
- bindValues: PreparedBindValues
30
- writeTables: ReadonlySet<string> | undefined
31
- }> => {
32
- let statementRes: ReadonlyArray<
33
- string | { sql: string; bindValues: Record<string, unknown>; writeTables?: ReadonlySet<string> }
34
- >
35
-
36
- switch (typeof mutationDef.sql) {
37
- case 'function': {
38
- const mutationArgsDecoded =
39
- mutationEvent.decoded?.args ?? Schema.decodeUnknownSync(mutationDef.schema)(mutationEvent.encoded!.args)
40
-
41
- const res = mutationDef.sql(mutationArgsDecoded, {
42
- clientOnly: mutationDef.options.clientOnly,
43
- // TODO properly implement this
44
- currentFacts: new Map(),
45
- })
46
-
47
- statementRes = (Array.isArray(res) ? res : [res]).map((_: QueryBuilder.Any | MutationHandlerResult) => {
48
- if (isQueryBuilder(_)) {
49
- const { query, bindValues } = _.asSql()
50
- return { sql: query, bindValues: bindValues as BindValues }
51
- }
52
-
53
- return _
54
- })
55
-
56
- break
57
- }
58
- case 'string': {
59
- statementRes = [mutationDef.sql]
60
- break
61
- }
62
- default: {
63
- statementRes = mutationDef.sql
64
- break
65
- }
66
- }
67
-
68
- return statementRes.map((statementRes) => {
69
- const statementSql = typeof statementRes === 'string' ? statementRes : statementRes.sql
70
-
71
- const mutationArgsEncoded =
72
- mutationEvent.encoded?.args ?? Schema.encodeUnknownSync(mutationDef.schema)(mutationEvent.decoded!.args)
73
- const bindValues = typeof statementRes === 'string' ? mutationArgsEncoded : statementRes.bindValues
74
-
75
- const writeTables = typeof statementRes === 'string' ? undefined : statementRes.writeTables
76
-
77
- return { statementSql, bindValues: prepareBindValues(bindValues ?? {}, statementSql), writeTables }
78
- })
79
- }
80
-
81
- // NOTE we should explore whether there is a more elegant solution
82
- // e.g. by leveraging the schema to replace the sessionIdSymbol
83
- export const replaceSessionIdSymbol = (
84
- bindValues: Record<string, unknown> | ReadonlyArray<unknown>,
85
- sessionId: string,
86
- ) => {
87
- deepReplaceValue(bindValues, SessionIdSymbol, sessionId)
88
- }
89
-
90
- const deepReplaceValue = <S, R>(input: any, searchValue: S, replaceValue: R): void => {
91
- if (Array.isArray(input)) {
92
- for (const i in input) {
93
- if (input[i] === searchValue) {
94
- input[i] = replaceValue
95
- } else {
96
- deepReplaceValue(input[i], searchValue, replaceValue)
97
- }
98
- }
99
- } else if (typeof input === 'object' && input !== null) {
100
- for (const key in input) {
101
- if (input[key] === searchValue) {
102
- input[key] = replaceValue
103
- } else {
104
- deepReplaceValue(input[key], searchValue, replaceValue)
105
- }
106
- }
107
- }
108
- }
package/src/query-info.ts DELETED
@@ -1,83 +0,0 @@
1
- import type { SessionIdSymbol } from './adapter-types.js'
2
- import type { DbSchema } from './schema/mod.js'
3
-
4
- /**
5
- * Semantic information about a query with supported cases being:
6
- * - a whole row
7
- * - a single column value
8
- * - a sub value in a JSON column
9
- *
10
- * This information is currently only used for derived mutations.
11
- */
12
- export type QueryInfo = QueryInfo.None | QueryInfo.Row | QueryInfo.Col | QueryInfo.ColJsonValue | QueryInfo.Write
13
- // export type QueryInfo<TTableDef extends DbSchema.TableDefBase = DbSchema.TableDefBase> =
14
- // | QueryInfo.None
15
- // | QueryInfo.Row<TTableDef>
16
- // | QueryInfo.ColJsonValue<TTableDef, GetJsonColumn<TTableDef>>
17
- // | QueryInfo.Col<TTableDef, keyof TTableDef['sqliteDef']['columns']>
18
-
19
- export namespace QueryInfo {
20
- export type None = {
21
- _tag: 'None'
22
- }
23
-
24
- export type Row = {
25
- _tag: 'Row'
26
- table: DbSchema.TableDefBase
27
- id: string | SessionIdSymbol | number
28
- }
29
-
30
- export type Col = {
31
- _tag: 'Col'
32
- table: DbSchema.TableDefBase
33
- id: string | SessionIdSymbol | number
34
- column: string
35
- }
36
-
37
- export type ColJsonValue = {
38
- _tag: 'ColJsonValue'
39
- table: DbSchema.TableDefBase
40
- id: string | SessionIdSymbol | number
41
- column: string
42
- /**
43
- * example: `$.tabs[3].items[2]` (`$` referring to the column value)
44
- */
45
- jsonPath: string
46
- }
47
-
48
- // NOTE Not yet used but we might want to use this in order to avoid write queries in read-only situations
49
- export type Write = {
50
- _tag: 'Write'
51
- }
52
-
53
- // NOTE maybe we want to bring back type-params back like below
54
- // export type Row<TTableDef extends DbSchema.TableDefBase> = {
55
- // _tag: 'Row'
56
- // table: TTableDef
57
- // id: string | SessionIdSymbol
58
- // }
59
-
60
- // export type Col<TTableDef extends DbSchema.TableDefBase, TColName extends keyof TTableDef['sqliteDef']['columns']> = {
61
- // _tag: 'Col'
62
- // table: TTableDef
63
- // id: string | SessionIdSymbol
64
- // column: TColName
65
- // }
66
-
67
- // export type ColJsonValue<TTableDef extends DbSchema.TableDefBase, TColName extends GetJsonColumn<TTableDef>> = {
68
- // _tag: 'ColJsonValue'
69
- // table: TTableDef
70
- // id: string | SessionIdSymbol
71
- // column: TColName
72
- // /**
73
- // * example: `$.tabs[3].items[2]` (`$` referring to the column value)
74
- // */
75
- // jsonPath: string
76
- // }
77
- }
78
-
79
- // type GetJsonColumn<TTableDef extends DbSchema.TableDefBase> = keyof {
80
- // [ColName in keyof TTableDef['sqliteDef']['columns'] as TTableDef['sqliteDef']['columns'][ColName]['columnType'] extends 'text'
81
- // ? ColName
82
- // : never]: {}
83
- // }
@@ -1,224 +0,0 @@
1
- import { memoizeByRef } from '@livestore/utils'
2
- import { Schema } from '@livestore/utils/effect'
3
-
4
- import * as EventId from './EventId.js'
5
- import type { MutationDef, MutationDefRecord } from './mutations.js'
6
- import type { LiveStoreSchema } from './schema.js'
7
-
8
- export type MutationEventPartial<TMutationsDef extends MutationDef.Any> = {
9
- mutation: TMutationsDef['name']
10
- args: Schema.Schema.Type<TMutationsDef['schema']>
11
- }
12
-
13
- export type PartialEncoded<TMutationsDef extends MutationDef.Any> = {
14
- mutation: TMutationsDef['name']
15
- args: Schema.Schema.Encoded<TMutationsDef['schema']>
16
- }
17
-
18
- export type MutationEvent<TMutationsDef extends MutationDef.Any> = {
19
- mutation: TMutationsDef['name']
20
- args: Schema.Schema.Type<TMutationsDef['schema']>
21
- id: EventId.EventId
22
- parentId: EventId.EventId
23
- clientId: string
24
- sessionId: string
25
- }
26
-
27
- export type MutationEventEncoded<TMutationsDef extends MutationDef.Any> = {
28
- mutation: TMutationsDef['name']
29
- args: Schema.Schema.Encoded<TMutationsDef['schema']>
30
- id: EventId.EventId
31
- parentId: EventId.EventId
32
- clientId: string
33
- sessionId: string
34
- }
35
-
36
- export type AnyDecoded = MutationEvent<MutationDef.Any>
37
- export const AnyDecoded = Schema.Struct({
38
- mutation: Schema.String,
39
- args: Schema.Any,
40
- id: EventId.EventId,
41
- parentId: EventId.EventId,
42
- clientId: Schema.String,
43
- sessionId: Schema.String,
44
- }).annotations({ title: 'MutationEvent.AnyDecoded' })
45
-
46
- export type AnyEncoded = MutationEventEncoded<MutationDef.Any>
47
- export const AnyEncoded = Schema.Struct({
48
- mutation: Schema.String,
49
- args: Schema.Any,
50
- id: EventId.EventId,
51
- parentId: EventId.EventId,
52
- clientId: Schema.String,
53
- sessionId: Schema.String,
54
- }).annotations({ title: 'MutationEvent.AnyEncoded' })
55
-
56
- export const AnyEncodedGlobal = Schema.Struct({
57
- mutation: Schema.String,
58
- args: Schema.Any,
59
- id: EventId.GlobalEventId,
60
- parentId: EventId.GlobalEventId,
61
- clientId: Schema.String,
62
- sessionId: Schema.String,
63
- }).annotations({ title: 'MutationEvent.AnyEncodedGlobal' })
64
- export type AnyEncodedGlobal = typeof AnyEncodedGlobal.Type
65
-
66
- export type PartialAnyDecoded = MutationEventPartial<MutationDef.Any>
67
- export type PartialAnyEncoded = PartialEncoded<MutationDef.Any>
68
-
69
- export const PartialAnyEncoded = Schema.Struct({
70
- mutation: Schema.String,
71
- args: Schema.Any,
72
- })
73
-
74
- export type PartialForSchema<TSchema extends LiveStoreSchema> = {
75
- [K in keyof TSchema['_MutationDefMapType']]: MutationEventPartial<TSchema['_MutationDefMapType'][K]>
76
- }[keyof TSchema['_MutationDefMapType']]
77
-
78
- export type ForSchema<TSchema extends LiveStoreSchema> = {
79
- [K in keyof TSchema['_MutationDefMapType']]: MutationEvent<TSchema['_MutationDefMapType'][K]>
80
- }[keyof TSchema['_MutationDefMapType']]
81
-
82
- export const isPartialMutationEvent = (
83
- mutationEvent: AnyDecoded | PartialAnyDecoded,
84
- ): mutationEvent is PartialAnyDecoded => 'id' in mutationEvent === false && 'parentId' in mutationEvent === false
85
-
86
- export type ForMutationDefRecord<TMutationsDefRecord extends MutationDefRecord> = Schema.Schema<
87
- {
88
- [K in keyof TMutationsDefRecord]: {
89
- mutation: K
90
- args: Schema.Schema.Type<TMutationsDefRecord[K]['schema']>
91
- id: EventId.EventId
92
- parentId: EventId.EventId
93
- clientId: string
94
- sessionId: string
95
- }
96
- }[keyof TMutationsDefRecord],
97
- {
98
- [K in keyof TMutationsDefRecord]: {
99
- mutation: K
100
- args: Schema.Schema.Encoded<TMutationsDefRecord[K]['schema']>
101
- id: EventId.EventId
102
- parentId: EventId.EventId
103
- clientId: string
104
- sessionId: string
105
- }
106
- }[keyof TMutationsDefRecord]
107
- >
108
-
109
- export type MutationEventPartialSchema<TMutationsDefRecord extends MutationDefRecord> = Schema.Schema<
110
- {
111
- [K in keyof TMutationsDefRecord]: {
112
- mutation: K
113
- args: Schema.Schema.Type<TMutationsDefRecord[K]['schema']>
114
- }
115
- }[keyof TMutationsDefRecord],
116
- {
117
- [K in keyof TMutationsDefRecord]: {
118
- mutation: K
119
- args: Schema.Schema.Encoded<TMutationsDefRecord[K]['schema']>
120
- }
121
- }[keyof TMutationsDefRecord]
122
- >
123
-
124
- export const makeMutationEventSchema = <TSchema extends LiveStoreSchema>(
125
- schema: TSchema,
126
- ): ForMutationDefRecord<TSchema['_MutationDefMapType']> =>
127
- Schema.Union(
128
- ...[...schema.mutations.map.values()].map((def) =>
129
- Schema.Struct({
130
- mutation: Schema.Literal(def.name),
131
- args: def.schema,
132
- id: EventId.EventId,
133
- parentId: EventId.EventId,
134
- clientId: Schema.String,
135
- sessionId: Schema.String,
136
- }),
137
- ),
138
- ).annotations({ title: 'MutationEvent' }) as any
139
-
140
- export const makeMutationEventPartialSchema = <TSchema extends LiveStoreSchema>(
141
- schema: TSchema,
142
- ): MutationEventPartialSchema<TSchema['_MutationDefMapType']> =>
143
- Schema.Union(
144
- ...[...schema.mutations.map.values()].map((def) =>
145
- Schema.Struct({
146
- mutation: Schema.Literal(def.name),
147
- args: def.schema,
148
- }),
149
- ),
150
- ).annotations({ title: 'MutationEventPartial' }) as any
151
-
152
- export const makeMutationEventSchemaMemo = memoizeByRef(makeMutationEventSchema)
153
-
154
- /** Equivalent to AnyEncoded but with a meta field and some convenience methods */
155
- export class EncodedWithMeta extends Schema.Class<EncodedWithMeta>('MutationEvent.EncodedWithMeta')({
156
- mutation: Schema.String,
157
- args: Schema.Any,
158
- id: EventId.EventId,
159
- parentId: EventId.EventId,
160
- clientId: Schema.String,
161
- sessionId: Schema.String,
162
- // TODO get rid of `meta` again by cleaning up the usage implementations
163
- meta: Schema.optionalWith(
164
- Schema.Any as Schema.Schema<{
165
- sessionChangeset?: Uint8Array
166
- }>,
167
- { default: () => ({}) },
168
- ),
169
- }) {
170
- toJSON = (): any => {
171
- // Only used for logging/debugging
172
- // - More readable way to print the id + parentId
173
- // - not including `meta`, `clientId`, `sessionId`
174
- return {
175
- id: `(${this.id.global},${this.id.client}) → (${this.parentId.global},${this.parentId.client})`,
176
- mutation: this.mutation,
177
- args: this.args,
178
- }
179
- }
180
-
181
- /**
182
- * Example: (global event)
183
- * For event id (2,0) → (1,0) which should be rebased on event id (3,1) → (3,0)
184
- * the resulting event id will be (4,0) → (3,0)
185
- *
186
- * Example: (client event)
187
- * For event id (2,1) → (2,0) which should be rebased on event id (3,0) → (2,0)
188
- * the resulting event id will be (3,1) → (3,0)
189
- *
190
- * Syntax: (2,1) → (2,0)
191
- * ^ ^ ^ ^
192
- * | | | +- client parent id
193
- * | | +--- global parent id
194
- * | +-- client id
195
- * +---- global id
196
- */
197
- rebase = (parentId: EventId.EventId, isClient: boolean) =>
198
- new EncodedWithMeta({
199
- ...this,
200
- ...EventId.nextPair(parentId, isClient),
201
- })
202
-
203
- static fromGlobal = (mutationEvent: AnyEncodedGlobal) =>
204
- new EncodedWithMeta({
205
- ...mutationEvent,
206
- id: { global: mutationEvent.id, client: EventId.clientDefault },
207
- parentId: { global: mutationEvent.parentId, client: EventId.clientDefault },
208
- })
209
-
210
- toGlobal = (): AnyEncodedGlobal => ({
211
- ...this,
212
- id: this.id.global,
213
- parentId: this.parentId.global,
214
- })
215
- }
216
-
217
- export const isEqualEncoded = (a: AnyEncoded, b: AnyEncoded) =>
218
- a.id.global === b.id.global &&
219
- a.id.client === b.id.client &&
220
- a.mutation === b.mutation &&
221
- a.clientId === b.clientId &&
222
- a.sessionId === b.sessionId &&
223
- // TODO use schema equality here
224
- JSON.stringify(a.args) === JSON.stringify(b.args)
@@ -1,193 +0,0 @@
1
- import { Schema } from '@livestore/utils/effect'
2
-
3
- import type { QueryBuilder } from '../query-builder/mod.js'
4
- import type { BindValues } from '../sql-queries/sql-queries.js'
5
-
6
- export type MutationDefMap = {
7
- map: Map<string | 'livestore.RawSql', MutationDef.Any>
8
- wasProvided: boolean
9
- }
10
- export type MutationDefRecord = {
11
- 'livestore.RawSql': RawSqlMutation
12
- [name: string]: MutationDef.Any
13
- }
14
-
15
- export type InternalMutationSchema<TRecord extends MutationDefRecord = MutationDefRecord> = {
16
- _DefRecord: TRecord
17
-
18
- map: Map<keyof TRecord, TRecord[keyof TRecord]>
19
- schemaHashMap: Map<keyof TRecord, number>
20
- }
21
-
22
- export type MutationDefSqlResult<TTo> =
23
- | SingleOrReadonlyArray<string>
24
- | ((
25
- args: TTo,
26
- context: { currentFacts: MutationEventFacts; clientOnly: boolean },
27
- ) => SingleOrReadonlyArray<
28
- | string
29
- | {
30
- sql: string
31
- /** Note args need to be manually encoded to `BindValues` when returning this argument */
32
- bindValues: BindValues
33
- writeTables?: ReadonlySet<string>
34
- }
35
- | QueryBuilder.Any
36
- >)
37
-
38
- export type MutationHandlerResult = {
39
- sql: string
40
- bindValues: BindValues
41
- writeTables?: ReadonlySet<string>
42
- }
43
-
44
- export type SingleOrReadonlyArray<T> = T | ReadonlyArray<T>
45
-
46
- export type MutationDef<TName extends string, TFrom, TTo> = {
47
- name: TName
48
- schema: Schema.Schema<TTo, TFrom>
49
- sql: MutationDefSqlResult<NoInfer<TTo>>
50
- options: {
51
- /** Warning: This feature is not fully implemented yet */
52
- historyId: string
53
- /**
54
- * When set to true, the mutation won't be synced across clients but
55
- */
56
- clientOnly: boolean
57
- /** Warning: This feature is not fully implemented yet */
58
- facts: FactsCallback<TTo> | undefined
59
- }
60
-
61
- /** Helper function to construct a partial mutation event */
62
- (args: TTo): {
63
- mutation: TName
64
- args: TTo
65
- }
66
- }
67
-
68
- export type FactsCallback<TTo> = (
69
- args: TTo,
70
- currentFacts: MutationEventFacts,
71
- ) => {
72
- modify: {
73
- set: Iterable<MutationEventFactInput>
74
- unset: Iterable<MutationEventFactInput>
75
- }
76
- require: Iterable<MutationEventFactInput>
77
- }
78
-
79
- export namespace MutationDef {
80
- export type Any = MutationDef<string, any, any>
81
- }
82
-
83
- export type MutationEventKey = string
84
- export type MutationEventFact = string
85
- export type MutationEventFacts = ReadonlyMap<string, any>
86
-
87
- export type MutationEventFactsGroup = {
88
- modifySet: MutationEventFacts
89
- modifyUnset: MutationEventFacts
90
-
91
- /**
92
- * Events on independent "dependency" branches are commutative which can facilitate more prioritized syncing
93
- */
94
- depRequire: MutationEventFacts
95
- depRead: MutationEventFacts
96
- }
97
-
98
- export type MutationEventFactsSnapshot = Map<string, any>
99
-
100
- export type MutationEventFactInput = string | readonly [string, any]
101
-
102
- export const defineFacts = <
103
- TRecord extends Record<string, MutationEventFactInput | ((...args: any[]) => MutationEventFactInput)>,
104
- >(
105
- record: TRecord,
106
- ): TRecord => record
107
-
108
- export type DefineMutationOptions<TTo> = {
109
- // TODO actually implement this
110
- onError?: (error: any) => void
111
- historyId?: string
112
- /** Warning: This feature is not fully implemented yet */
113
- facts?: (
114
- args: TTo,
115
- currentFacts: MutationEventFacts,
116
- ) => {
117
- modify?: {
118
- set?: Iterable<MutationEventFactInput>
119
- unset?: Iterable<MutationEventFactInput>
120
- }
121
- /**
122
- * Two purposes: constrain history and constrain compaction
123
- */
124
- require?: Iterable<MutationEventFactInput>
125
- }
126
- /**
127
- * When set to true, the mutation won't be synced over the network
128
- */
129
- clientOnly?: boolean
130
- }
131
-
132
- // TODO possibly also allow for mutation event subsumption behaviour
133
- export const defineMutation = <TName extends string, TFrom, TTo>(
134
- name: TName,
135
- schema: Schema.Schema<TTo, TFrom>,
136
- sql: MutationDefSqlResult<NoInfer<TTo>>,
137
- options?: DefineMutationOptions<TTo>,
138
- ): MutationDef<TName, TFrom, TTo> => {
139
- const makePartialEvent = (args: TTo) => ({ mutation: name, args })
140
-
141
- Object.defineProperty(makePartialEvent, 'name', { value: name })
142
- Object.defineProperty(makePartialEvent, 'schema', { value: schema })
143
- Object.defineProperty(makePartialEvent, 'sql', { value: sql })
144
- Object.defineProperty(makePartialEvent, 'options', {
145
- value: {
146
- historyId: options?.historyId ?? 'main',
147
- clientOnly: options?.clientOnly ?? false,
148
- facts: options?.facts
149
- ? (args, currentFacts) => {
150
- const res = options.facts!(args, currentFacts)
151
- return {
152
- modify: {
153
- set: res.modify?.set ? new Set(res.modify.set) : new Set(),
154
- unset: res.modify?.unset ? new Set(res.modify.unset) : new Set(),
155
- },
156
- require: res.require ? new Set(res.require) : new Set(),
157
- }
158
- }
159
- : undefined,
160
- } satisfies MutationDef.Any['options'],
161
- })
162
-
163
- return makePartialEvent as MutationDef<TName, TFrom, TTo>
164
- }
165
-
166
- export const makeMutationDefRecord = <TInputRecord extends Record<string, MutationDef.Any>>(
167
- inputRecord: TInputRecord,
168
- ): {
169
- [K in TInputRecord[keyof TInputRecord]['name']]: Extract<TInputRecord[keyof TInputRecord], { name: K }>
170
- } => {
171
- const result: any = {}
172
-
173
- for (const [name, def] of Object.entries(inputRecord)) {
174
- result[name] = def
175
- }
176
-
177
- result['livestore.RawSql'] = rawSqlMutation
178
-
179
- return result
180
- }
181
-
182
- export const rawSqlMutation = defineMutation(
183
- 'livestore.RawSql',
184
- Schema.Struct({
185
- sql: Schema.String,
186
- bindValues: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
187
- writeTables: Schema.optional(Schema.ReadonlySet(Schema.String)),
188
- }),
189
- ({ sql, bindValues, writeTables }) => ({ sql, bindValues: bindValues ?? {}, writeTables }),
190
- )
191
-
192
- export type RawSqlMutation = typeof rawSqlMutation
193
- export type RawSqlMutationEvent = ReturnType<typeof rawSqlMutation>