@livestore/common 0.3.0-dev.5 → 0.3.0-dev.50

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 (491) 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 +120 -64
  7. package/dist/adapter-types.d.ts.map +1 -1
  8. package/dist/adapter-types.js +39 -8
  9. package/dist/adapter-types.js.map +1 -1
  10. package/dist/bounded-collections.d.ts.map +1 -1
  11. package/dist/debug-info.d.ts +1 -1
  12. package/dist/debug-info.d.ts.map +1 -1
  13. package/dist/debug-info.js +1 -0
  14. package/dist/debug-info.js.map +1 -1
  15. package/dist/devtools/devtools-messages-client-session.d.ts +390 -0
  16. package/dist/devtools/devtools-messages-client-session.d.ts.map +1 -0
  17. package/dist/devtools/devtools-messages-client-session.js +97 -0
  18. package/dist/devtools/devtools-messages-client-session.js.map +1 -0
  19. package/dist/devtools/devtools-messages-common.d.ts +68 -0
  20. package/dist/devtools/devtools-messages-common.d.ts.map +1 -0
  21. package/dist/devtools/devtools-messages-common.js +60 -0
  22. package/dist/devtools/devtools-messages-common.js.map +1 -0
  23. package/dist/devtools/devtools-messages-leader.d.ts +394 -0
  24. package/dist/devtools/devtools-messages-leader.d.ts.map +1 -0
  25. package/dist/devtools/devtools-messages-leader.js +147 -0
  26. package/dist/devtools/devtools-messages-leader.js.map +1 -0
  27. package/dist/devtools/devtools-messages.d.ts +3 -580
  28. package/dist/devtools/devtools-messages.d.ts.map +1 -1
  29. package/dist/devtools/devtools-messages.js +3 -174
  30. package/dist/devtools/devtools-messages.js.map +1 -1
  31. package/dist/devtools/devtools-sessioninfo.d.ts +32 -0
  32. package/dist/devtools/devtools-sessioninfo.d.ts.map +1 -0
  33. package/dist/devtools/devtools-sessioninfo.js +36 -0
  34. package/dist/devtools/devtools-sessioninfo.js.map +1 -0
  35. package/dist/devtools/mod.d.ts +55 -0
  36. package/dist/devtools/mod.d.ts.map +1 -0
  37. package/dist/devtools/mod.js +33 -0
  38. package/dist/devtools/mod.js.map +1 -0
  39. package/dist/index.d.ts +7 -13
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +7 -9
  42. package/dist/index.js.map +1 -1
  43. package/dist/leader-thread/LeaderSyncProcessor.d.ts +62 -0
  44. package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -0
  45. package/dist/leader-thread/LeaderSyncProcessor.js +595 -0
  46. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -0
  47. package/dist/leader-thread/connection.d.ts +34 -6
  48. package/dist/leader-thread/connection.d.ts.map +1 -1
  49. package/dist/leader-thread/connection.js +22 -7
  50. package/dist/leader-thread/connection.js.map +1 -1
  51. package/dist/leader-thread/eventlog.d.ts +27 -0
  52. package/dist/leader-thread/eventlog.d.ts.map +1 -0
  53. package/dist/leader-thread/eventlog.js +119 -0
  54. package/dist/leader-thread/eventlog.js.map +1 -0
  55. package/dist/leader-thread/leader-worker-devtools.d.ts +1 -1
  56. package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
  57. package/dist/leader-thread/leader-worker-devtools.js +155 -80
  58. package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
  59. package/dist/leader-thread/make-leader-thread-layer.d.ts +23 -11
  60. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
  61. package/dist/leader-thread/make-leader-thread-layer.js +72 -47
  62. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
  63. package/dist/leader-thread/materialize-event.d.ts +16 -0
  64. package/dist/leader-thread/materialize-event.d.ts.map +1 -0
  65. package/dist/leader-thread/materialize-event.js +109 -0
  66. package/dist/leader-thread/materialize-event.js.map +1 -0
  67. package/dist/leader-thread/mod.d.ts +1 -1
  68. package/dist/leader-thread/mod.d.ts.map +1 -1
  69. package/dist/leader-thread/mod.js +1 -1
  70. package/dist/leader-thread/mod.js.map +1 -1
  71. package/dist/leader-thread/recreate-db.d.ts +4 -2
  72. package/dist/leader-thread/recreate-db.d.ts.map +1 -1
  73. package/dist/leader-thread/recreate-db.js +33 -31
  74. package/dist/leader-thread/recreate-db.js.map +1 -1
  75. package/dist/leader-thread/shutdown-channel.d.ts +2 -5
  76. package/dist/leader-thread/shutdown-channel.d.ts.map +1 -1
  77. package/dist/leader-thread/shutdown-channel.js +2 -4
  78. package/dist/leader-thread/shutdown-channel.js.map +1 -1
  79. package/dist/leader-thread/types.d.ts +87 -40
  80. package/dist/leader-thread/types.d.ts.map +1 -1
  81. package/dist/leader-thread/types.js +1 -3
  82. package/dist/leader-thread/types.js.map +1 -1
  83. package/dist/make-client-session.d.ts +23 -0
  84. package/dist/make-client-session.d.ts.map +1 -0
  85. package/dist/make-client-session.js +57 -0
  86. package/dist/make-client-session.js.map +1 -0
  87. package/dist/materializer-helper.d.ts +23 -0
  88. package/dist/materializer-helper.d.ts.map +1 -0
  89. package/dist/materializer-helper.js +86 -0
  90. package/dist/materializer-helper.js.map +1 -0
  91. package/dist/otel.d.ts +2 -0
  92. package/dist/otel.d.ts.map +1 -1
  93. package/dist/otel.js +5 -0
  94. package/dist/otel.js.map +1 -1
  95. package/dist/rematerialize-from-eventlog.d.ts +14 -0
  96. package/dist/rematerialize-from-eventlog.d.ts.map +1 -0
  97. package/dist/rematerialize-from-eventlog.js +64 -0
  98. package/dist/rematerialize-from-eventlog.js.map +1 -0
  99. package/dist/schema/EventDef.d.ts +146 -0
  100. package/dist/schema/EventDef.d.ts.map +1 -0
  101. package/dist/schema/EventDef.js +58 -0
  102. package/dist/schema/EventDef.js.map +1 -0
  103. package/dist/schema/EventId.d.ts +43 -25
  104. package/dist/schema/EventId.d.ts.map +1 -1
  105. package/dist/schema/EventId.js +56 -18
  106. package/dist/schema/EventId.js.map +1 -1
  107. package/dist/schema/EventId.test.d.ts +2 -0
  108. package/dist/schema/EventId.test.d.ts.map +1 -0
  109. package/dist/schema/EventId.test.js +11 -0
  110. package/dist/schema/EventId.test.js.map +1 -0
  111. package/dist/schema/EventNumber.d.ts +57 -0
  112. package/dist/schema/EventNumber.d.ts.map +1 -0
  113. package/dist/schema/EventNumber.js +82 -0
  114. package/dist/schema/EventNumber.js.map +1 -0
  115. package/dist/schema/EventNumber.test.d.ts +2 -0
  116. package/dist/schema/EventNumber.test.d.ts.map +1 -0
  117. package/dist/schema/EventNumber.test.js +11 -0
  118. package/dist/schema/EventNumber.test.js.map +1 -0
  119. package/dist/schema/EventSequenceNumber.d.ts +57 -0
  120. package/dist/schema/EventSequenceNumber.d.ts.map +1 -0
  121. package/dist/schema/EventSequenceNumber.js +82 -0
  122. package/dist/schema/EventSequenceNumber.js.map +1 -0
  123. package/dist/schema/EventSequenceNumber.test.d.ts +2 -0
  124. package/dist/schema/EventSequenceNumber.test.d.ts.map +1 -0
  125. package/dist/schema/EventSequenceNumber.test.js +11 -0
  126. package/dist/schema/EventSequenceNumber.test.js.map +1 -0
  127. package/dist/schema/LiveStoreEvent.d.ts +257 -0
  128. package/dist/schema/LiveStoreEvent.d.ts.map +1 -0
  129. package/dist/schema/LiveStoreEvent.js +117 -0
  130. package/dist/schema/LiveStoreEvent.js.map +1 -0
  131. package/dist/schema/events.d.ts +2 -0
  132. package/dist/schema/events.d.ts.map +1 -0
  133. package/dist/schema/events.js +2 -0
  134. package/dist/schema/events.js.map +1 -0
  135. package/dist/schema/mod.d.ts +8 -6
  136. package/dist/schema/mod.d.ts.map +1 -1
  137. package/dist/schema/mod.js +8 -6
  138. package/dist/schema/mod.js.map +1 -1
  139. package/dist/schema/schema.d.ts +50 -32
  140. package/dist/schema/schema.d.ts.map +1 -1
  141. package/dist/schema/schema.js +36 -43
  142. package/dist/schema/schema.js.map +1 -1
  143. package/dist/schema/state/mod.d.ts +3 -0
  144. package/dist/schema/state/mod.d.ts.map +1 -0
  145. package/dist/schema/state/mod.js +3 -0
  146. package/dist/schema/state/mod.js.map +1 -0
  147. package/dist/schema/state/sqlite/client-document-def.d.ts +223 -0
  148. package/dist/schema/state/sqlite/client-document-def.d.ts.map +1 -0
  149. package/dist/schema/state/sqlite/client-document-def.js +170 -0
  150. package/dist/schema/state/sqlite/client-document-def.js.map +1 -0
  151. package/dist/schema/state/sqlite/client-document-def.test.d.ts +2 -0
  152. package/dist/schema/state/sqlite/client-document-def.test.d.ts.map +1 -0
  153. package/dist/schema/state/sqlite/client-document-def.test.js +201 -0
  154. package/dist/schema/state/sqlite/client-document-def.test.js.map +1 -0
  155. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts +69 -0
  156. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts.map +1 -0
  157. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js +71 -0
  158. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js.map +1 -0
  159. package/dist/schema/state/sqlite/db-schema/ast/validate.d.ts +3 -0
  160. package/dist/schema/state/sqlite/db-schema/ast/validate.d.ts.map +1 -0
  161. package/dist/schema/state/sqlite/db-schema/ast/validate.js +12 -0
  162. package/dist/schema/state/sqlite/db-schema/ast/validate.js.map +1 -0
  163. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts +90 -0
  164. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts.map +1 -0
  165. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js +87 -0
  166. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js.map +1 -0
  167. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.d.ts +2 -0
  168. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.d.ts.map +1 -0
  169. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js +29 -0
  170. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js.map +1 -0
  171. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts +90 -0
  172. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts.map +1 -0
  173. package/dist/schema/state/sqlite/db-schema/dsl/mod.js +41 -0
  174. package/dist/schema/state/sqlite/db-schema/dsl/mod.js.map +1 -0
  175. package/dist/schema/state/sqlite/db-schema/hash.d.ts +2 -0
  176. package/dist/schema/state/sqlite/db-schema/hash.d.ts.map +1 -0
  177. package/dist/schema/state/sqlite/db-schema/hash.js +14 -0
  178. package/dist/schema/state/sqlite/db-schema/hash.js.map +1 -0
  179. package/dist/schema/state/sqlite/db-schema/mod.d.ts +3 -0
  180. package/dist/schema/state/sqlite/db-schema/mod.d.ts.map +1 -0
  181. package/dist/schema/state/sqlite/db-schema/mod.js +3 -0
  182. package/dist/schema/state/sqlite/db-schema/mod.js.map +1 -0
  183. package/dist/schema/state/sqlite/mod.d.ts +17 -0
  184. package/dist/schema/state/sqlite/mod.d.ts.map +1 -0
  185. package/dist/schema/state/sqlite/mod.js +41 -0
  186. package/dist/schema/state/sqlite/mod.js.map +1 -0
  187. package/dist/schema/state/sqlite/query-builder/api.d.ts +294 -0
  188. package/dist/schema/state/sqlite/query-builder/api.d.ts.map +1 -0
  189. package/dist/schema/state/sqlite/query-builder/api.js +6 -0
  190. package/dist/schema/state/sqlite/query-builder/api.js.map +1 -0
  191. package/dist/schema/state/sqlite/query-builder/astToSql.d.ts +7 -0
  192. package/dist/schema/state/sqlite/query-builder/astToSql.d.ts.map +1 -0
  193. package/dist/schema/state/sqlite/query-builder/astToSql.js +190 -0
  194. package/dist/schema/state/sqlite/query-builder/astToSql.js.map +1 -0
  195. package/dist/schema/state/sqlite/query-builder/impl.d.ts +7 -0
  196. package/dist/schema/state/sqlite/query-builder/impl.d.ts.map +1 -0
  197. package/dist/schema/state/sqlite/query-builder/impl.js +286 -0
  198. package/dist/schema/state/sqlite/query-builder/impl.js.map +1 -0
  199. package/dist/schema/state/sqlite/query-builder/impl.test.d.ts +87 -0
  200. package/dist/schema/state/sqlite/query-builder/impl.test.d.ts.map +1 -0
  201. package/dist/schema/state/sqlite/query-builder/impl.test.js +563 -0
  202. package/dist/schema/state/sqlite/query-builder/impl.test.js.map +1 -0
  203. package/dist/{query-builder → schema/state/sqlite/query-builder}/mod.d.ts +7 -0
  204. package/dist/schema/state/sqlite/query-builder/mod.d.ts.map +1 -0
  205. package/dist/{query-builder → schema/state/sqlite/query-builder}/mod.js +7 -0
  206. package/dist/schema/state/sqlite/query-builder/mod.js.map +1 -0
  207. package/dist/schema/state/sqlite/schema-helpers.d.ts.map +1 -0
  208. package/dist/schema/{schema-helpers.js → state/sqlite/schema-helpers.js} +1 -1
  209. package/dist/schema/state/sqlite/schema-helpers.js.map +1 -0
  210. package/dist/schema/state/sqlite/system-tables.d.ts +574 -0
  211. package/dist/schema/state/sqlite/system-tables.d.ts.map +1 -0
  212. package/dist/schema/state/sqlite/system-tables.js +88 -0
  213. package/dist/schema/state/sqlite/system-tables.js.map +1 -0
  214. package/dist/schema/state/sqlite/table-def.d.ts +84 -0
  215. package/dist/schema/state/sqlite/table-def.d.ts.map +1 -0
  216. package/dist/schema/state/sqlite/table-def.js +36 -0
  217. package/dist/schema/state/sqlite/table-def.js.map +1 -0
  218. package/dist/schema-management/common.d.ts +7 -7
  219. package/dist/schema-management/common.d.ts.map +1 -1
  220. package/dist/schema-management/common.js.map +1 -1
  221. package/dist/schema-management/migrations.d.ts +6 -6
  222. package/dist/schema-management/migrations.d.ts.map +1 -1
  223. package/dist/schema-management/migrations.js +27 -18
  224. package/dist/schema-management/migrations.js.map +1 -1
  225. package/dist/schema-management/validate-schema.d.ts +8 -0
  226. package/dist/schema-management/validate-schema.d.ts.map +1 -0
  227. package/dist/schema-management/validate-schema.js +39 -0
  228. package/dist/schema-management/validate-schema.js.map +1 -0
  229. package/dist/sql-queries/misc.d.ts.map +1 -1
  230. package/dist/sql-queries/sql-queries.d.ts +1 -1
  231. package/dist/sql-queries/sql-queries.d.ts.map +1 -1
  232. package/dist/sql-queries/sql-queries.js.map +1 -1
  233. package/dist/sql-queries/sql-query-builder.d.ts +1 -1
  234. package/dist/sql-queries/sql-query-builder.d.ts.map +1 -1
  235. package/dist/sql-queries/sql-query-builder.js.map +1 -1
  236. package/dist/sql-queries/types.d.ts +2 -1
  237. package/dist/sql-queries/types.d.ts.map +1 -1
  238. package/dist/sql-queries/types.js.map +1 -1
  239. package/dist/sync/ClientSessionSyncProcessor.d.ts +66 -0
  240. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -0
  241. package/dist/sync/ClientSessionSyncProcessor.js +209 -0
  242. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -0
  243. package/dist/sync/index.d.ts +1 -1
  244. package/dist/sync/index.d.ts.map +1 -1
  245. package/dist/sync/index.js +1 -1
  246. package/dist/sync/index.js.map +1 -1
  247. package/dist/sync/next/compact-events.d.ts.map +1 -1
  248. package/dist/sync/next/compact-events.js +38 -35
  249. package/dist/sync/next/compact-events.js.map +1 -1
  250. package/dist/sync/next/facts.d.ts +21 -21
  251. package/dist/sync/next/facts.d.ts.map +1 -1
  252. package/dist/sync/next/facts.js +11 -11
  253. package/dist/sync/next/facts.js.map +1 -1
  254. package/dist/sync/next/history-dag-common.d.ts +9 -7
  255. package/dist/sync/next/history-dag-common.d.ts.map +1 -1
  256. package/dist/sync/next/history-dag-common.js +10 -5
  257. package/dist/sync/next/history-dag-common.js.map +1 -1
  258. package/dist/sync/next/history-dag.d.ts +0 -2
  259. package/dist/sync/next/history-dag.d.ts.map +1 -1
  260. package/dist/sync/next/history-dag.js +16 -14
  261. package/dist/sync/next/history-dag.js.map +1 -1
  262. package/dist/sync/next/rebase-events.d.ts +10 -8
  263. package/dist/sync/next/rebase-events.d.ts.map +1 -1
  264. package/dist/sync/next/rebase-events.js +18 -10
  265. package/dist/sync/next/rebase-events.js.map +1 -1
  266. package/dist/sync/next/test/compact-events.calculator.test.js +39 -34
  267. package/dist/sync/next/test/compact-events.calculator.test.js.map +1 -1
  268. package/dist/sync/next/test/compact-events.test.js +77 -77
  269. package/dist/sync/next/test/compact-events.test.js.map +1 -1
  270. package/dist/sync/next/test/{mutation-fixtures.d.ts → event-fixtures.d.ts} +38 -28
  271. package/dist/sync/next/test/event-fixtures.d.ts.map +1 -0
  272. package/dist/sync/next/test/{mutation-fixtures.js → event-fixtures.js} +81 -38
  273. package/dist/sync/next/test/event-fixtures.js.map +1 -0
  274. package/dist/sync/next/test/mod.d.ts +1 -1
  275. package/dist/sync/next/test/mod.d.ts.map +1 -1
  276. package/dist/sync/next/test/mod.js +1 -1
  277. package/dist/sync/next/test/mod.js.map +1 -1
  278. package/dist/sync/sync.d.ts +60 -25
  279. package/dist/sync/sync.d.ts.map +1 -1
  280. package/dist/sync/sync.js +10 -6
  281. package/dist/sync/sync.js.map +1 -1
  282. package/dist/sync/syncstate.d.ts +213 -82
  283. package/dist/sync/syncstate.d.ts.map +1 -1
  284. package/dist/sync/syncstate.js +337 -139
  285. package/dist/sync/syncstate.js.map +1 -1
  286. package/dist/sync/syncstate.test.js +310 -286
  287. package/dist/sync/syncstate.test.js.map +1 -1
  288. package/dist/sync/validate-push-payload.d.ts +2 -2
  289. package/dist/sync/validate-push-payload.d.ts.map +1 -1
  290. package/dist/sync/validate-push-payload.js +4 -4
  291. package/dist/sync/validate-push-payload.js.map +1 -1
  292. package/dist/util.d.ts +2 -2
  293. package/dist/util.d.ts.map +1 -1
  294. package/dist/version.d.ts +2 -2
  295. package/dist/version.d.ts.map +1 -1
  296. package/dist/version.js +2 -2
  297. package/dist/version.js.map +1 -1
  298. package/package.json +13 -6
  299. package/src/__tests__/fixture.ts +36 -15
  300. package/src/adapter-types.ts +107 -68
  301. package/src/debug-info.ts +1 -0
  302. package/src/devtools/devtools-messages-client-session.ts +142 -0
  303. package/src/devtools/devtools-messages-common.ts +115 -0
  304. package/src/devtools/devtools-messages-leader.ts +191 -0
  305. package/src/devtools/devtools-messages.ts +3 -246
  306. package/src/devtools/devtools-sessioninfo.ts +101 -0
  307. package/src/devtools/mod.ts +59 -0
  308. package/src/index.ts +7 -15
  309. package/src/leader-thread/LeaderSyncProcessor.ts +940 -0
  310. package/src/leader-thread/connection.ts +54 -9
  311. package/src/leader-thread/eventlog.ts +199 -0
  312. package/src/leader-thread/leader-worker-devtools.ts +227 -104
  313. package/src/leader-thread/make-leader-thread-layer.ts +128 -78
  314. package/src/leader-thread/materialize-event.ts +173 -0
  315. package/src/leader-thread/mod.ts +1 -1
  316. package/src/leader-thread/recreate-db.ts +38 -39
  317. package/src/leader-thread/shutdown-channel.ts +2 -4
  318. package/src/leader-thread/types.ts +96 -50
  319. package/src/make-client-session.ts +136 -0
  320. package/src/materializer-helper.ts +138 -0
  321. package/src/otel.ts +8 -0
  322. package/src/rematerialize-from-eventlog.ts +117 -0
  323. package/src/schema/EventDef.ts +227 -0
  324. package/src/schema/EventSequenceNumber.test.ts +12 -0
  325. package/src/schema/EventSequenceNumber.ts +121 -0
  326. package/src/schema/LiveStoreEvent.ts +240 -0
  327. package/src/schema/events.ts +1 -0
  328. package/src/schema/mod.ts +8 -6
  329. package/src/schema/schema.ts +88 -84
  330. package/src/schema/state/mod.ts +2 -0
  331. package/src/schema/state/sqlite/client-document-def.test.ts +238 -0
  332. package/src/schema/state/sqlite/client-document-def.ts +444 -0
  333. package/src/schema/state/sqlite/db-schema/ast/sqlite.ts +142 -0
  334. package/src/schema/state/sqlite/db-schema/ast/validate.ts +13 -0
  335. package/src/schema/state/sqlite/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +206 -0
  336. package/src/schema/state/sqlite/db-schema/dsl/field-defs.test.ts +35 -0
  337. package/src/schema/state/sqlite/db-schema/dsl/field-defs.ts +242 -0
  338. package/src/schema/state/sqlite/db-schema/dsl/mod.ts +222 -0
  339. package/src/schema/state/sqlite/db-schema/hash.ts +14 -0
  340. package/src/schema/state/sqlite/db-schema/mod.ts +2 -0
  341. package/src/schema/state/sqlite/mod.ts +73 -0
  342. package/src/schema/state/sqlite/query-builder/api.ts +440 -0
  343. package/src/schema/state/sqlite/query-builder/astToSql.ts +232 -0
  344. package/src/schema/state/sqlite/query-builder/impl.test.ts +617 -0
  345. package/src/schema/state/sqlite/query-builder/impl.ts +351 -0
  346. package/src/{query-builder → schema/state/sqlite/query-builder}/mod.ts +7 -0
  347. package/src/schema/{schema-helpers.ts → state/sqlite/schema-helpers.ts} +1 -1
  348. package/src/schema/state/sqlite/system-tables.ts +117 -0
  349. package/src/schema/state/sqlite/table-def.ts +197 -0
  350. package/src/schema-management/common.ts +7 -7
  351. package/src/schema-management/migrations.ts +37 -31
  352. package/src/schema-management/validate-schema.ts +61 -0
  353. package/src/sql-queries/sql-queries.ts +1 -1
  354. package/src/sql-queries/sql-query-builder.ts +1 -2
  355. package/src/sql-queries/types.ts +3 -1
  356. package/src/sync/ClientSessionSyncProcessor.ts +332 -0
  357. package/src/sync/index.ts +1 -1
  358. package/src/sync/next/compact-events.ts +38 -35
  359. package/src/sync/next/facts.ts +43 -41
  360. package/src/sync/next/history-dag-common.ts +17 -10
  361. package/src/sync/next/history-dag.ts +16 -17
  362. package/src/sync/next/rebase-events.ts +29 -17
  363. package/src/sync/next/test/compact-events.calculator.test.ts +46 -46
  364. package/src/sync/next/test/compact-events.test.ts +79 -79
  365. package/src/sync/next/test/event-fixtures.ts +226 -0
  366. package/src/sync/next/test/mod.ts +1 -1
  367. package/src/sync/sync.ts +60 -24
  368. package/src/sync/syncstate.test.ts +347 -320
  369. package/src/sync/syncstate.ts +422 -230
  370. package/src/sync/validate-push-payload.ts +6 -6
  371. package/src/version.ts +2 -2
  372. package/dist/derived-mutations.d.ts +0 -109
  373. package/dist/derived-mutations.d.ts.map +0 -1
  374. package/dist/derived-mutations.js +0 -54
  375. package/dist/derived-mutations.js.map +0 -1
  376. package/dist/derived-mutations.test.d.ts +0 -2
  377. package/dist/derived-mutations.test.d.ts.map +0 -1
  378. package/dist/derived-mutations.test.js +0 -93
  379. package/dist/derived-mutations.test.js.map +0 -1
  380. package/dist/devtools/devtools-bridge.d.ts +0 -13
  381. package/dist/devtools/devtools-bridge.d.ts.map +0 -1
  382. package/dist/devtools/devtools-bridge.js +0 -2
  383. package/dist/devtools/devtools-bridge.js.map +0 -1
  384. package/dist/devtools/devtools-window-message.d.ts +0 -29
  385. package/dist/devtools/devtools-window-message.d.ts.map +0 -1
  386. package/dist/devtools/devtools-window-message.js +0 -33
  387. package/dist/devtools/devtools-window-message.js.map +0 -1
  388. package/dist/devtools/index.d.ts +0 -42
  389. package/dist/devtools/index.d.ts.map +0 -1
  390. package/dist/devtools/index.js +0 -48
  391. package/dist/devtools/index.js.map +0 -1
  392. package/dist/init-singleton-tables.d.ts +0 -4
  393. package/dist/init-singleton-tables.d.ts.map +0 -1
  394. package/dist/init-singleton-tables.js +0 -16
  395. package/dist/init-singleton-tables.js.map +0 -1
  396. package/dist/leader-thread/apply-mutation.d.ts +0 -8
  397. package/dist/leader-thread/apply-mutation.d.ts.map +0 -1
  398. package/dist/leader-thread/apply-mutation.js +0 -95
  399. package/dist/leader-thread/apply-mutation.js.map +0 -1
  400. package/dist/leader-thread/leader-sync-processor.d.ts +0 -47
  401. package/dist/leader-thread/leader-sync-processor.d.ts.map +0 -1
  402. package/dist/leader-thread/leader-sync-processor.js +0 -425
  403. package/dist/leader-thread/leader-sync-processor.js.map +0 -1
  404. package/dist/leader-thread/mutationlog.d.ts +0 -10
  405. package/dist/leader-thread/mutationlog.d.ts.map +0 -1
  406. package/dist/leader-thread/mutationlog.js +0 -28
  407. package/dist/leader-thread/mutationlog.js.map +0 -1
  408. package/dist/leader-thread/pull-queue-set.d.ts +0 -7
  409. package/dist/leader-thread/pull-queue-set.d.ts.map +0 -1
  410. package/dist/leader-thread/pull-queue-set.js +0 -39
  411. package/dist/leader-thread/pull-queue-set.js.map +0 -1
  412. package/dist/mutation.d.ts +0 -13
  413. package/dist/mutation.d.ts.map +0 -1
  414. package/dist/mutation.js +0 -57
  415. package/dist/mutation.js.map +0 -1
  416. package/dist/query-builder/api.d.ts +0 -190
  417. package/dist/query-builder/api.d.ts.map +0 -1
  418. package/dist/query-builder/api.js +0 -8
  419. package/dist/query-builder/api.js.map +0 -1
  420. package/dist/query-builder/impl.d.ts +0 -12
  421. package/dist/query-builder/impl.d.ts.map +0 -1
  422. package/dist/query-builder/impl.js +0 -244
  423. package/dist/query-builder/impl.js.map +0 -1
  424. package/dist/query-builder/impl.test.d.ts +0 -2
  425. package/dist/query-builder/impl.test.d.ts.map +0 -1
  426. package/dist/query-builder/impl.test.js +0 -212
  427. package/dist/query-builder/impl.test.js.map +0 -1
  428. package/dist/query-builder/mod.d.ts.map +0 -1
  429. package/dist/query-builder/mod.js.map +0 -1
  430. package/dist/query-info.d.ts +0 -38
  431. package/dist/query-info.d.ts.map +0 -1
  432. package/dist/query-info.js +0 -7
  433. package/dist/query-info.js.map +0 -1
  434. package/dist/rehydrate-from-mutationlog.d.ts +0 -14
  435. package/dist/rehydrate-from-mutationlog.d.ts.map +0 -1
  436. package/dist/rehydrate-from-mutationlog.js +0 -72
  437. package/dist/rehydrate-from-mutationlog.js.map +0 -1
  438. package/dist/schema/MutationEvent.d.ts +0 -166
  439. package/dist/schema/MutationEvent.d.ts.map +0 -1
  440. package/dist/schema/MutationEvent.js +0 -72
  441. package/dist/schema/MutationEvent.js.map +0 -1
  442. package/dist/schema/mutations.d.ts +0 -107
  443. package/dist/schema/mutations.d.ts.map +0 -1
  444. package/dist/schema/mutations.js +0 -42
  445. package/dist/schema/mutations.js.map +0 -1
  446. package/dist/schema/schema-helpers.d.ts.map +0 -1
  447. package/dist/schema/schema-helpers.js.map +0 -1
  448. package/dist/schema/system-tables.d.ts +0 -399
  449. package/dist/schema/system-tables.d.ts.map +0 -1
  450. package/dist/schema/system-tables.js +0 -58
  451. package/dist/schema/system-tables.js.map +0 -1
  452. package/dist/schema/table-def.d.ts +0 -156
  453. package/dist/schema/table-def.d.ts.map +0 -1
  454. package/dist/schema/table-def.js +0 -79
  455. package/dist/schema/table-def.js.map +0 -1
  456. package/dist/schema-management/validate-mutation-defs.d.ts +0 -8
  457. package/dist/schema-management/validate-mutation-defs.d.ts.map +0 -1
  458. package/dist/schema-management/validate-mutation-defs.js +0 -39
  459. package/dist/schema-management/validate-mutation-defs.js.map +0 -1
  460. package/dist/sync/client-session-sync-processor.d.ts +0 -45
  461. package/dist/sync/client-session-sync-processor.d.ts.map +0 -1
  462. package/dist/sync/client-session-sync-processor.js +0 -131
  463. package/dist/sync/client-session-sync-processor.js.map +0 -1
  464. package/dist/sync/next/test/mutation-fixtures.d.ts.map +0 -1
  465. package/dist/sync/next/test/mutation-fixtures.js.map +0 -1
  466. package/src/derived-mutations.test.ts +0 -101
  467. package/src/derived-mutations.ts +0 -166
  468. package/src/devtools/devtools-bridge.ts +0 -14
  469. package/src/devtools/devtools-window-message.ts +0 -27
  470. package/src/devtools/index.ts +0 -48
  471. package/src/init-singleton-tables.ts +0 -24
  472. package/src/leader-thread/apply-mutation.ts +0 -143
  473. package/src/leader-thread/leader-sync-processor.ts +0 -670
  474. package/src/leader-thread/mutationlog.ts +0 -46
  475. package/src/leader-thread/pull-queue-set.ts +0 -58
  476. package/src/mutation.ts +0 -81
  477. package/src/query-builder/api.ts +0 -289
  478. package/src/query-builder/impl.test.ts +0 -239
  479. package/src/query-builder/impl.ts +0 -285
  480. package/src/query-info.ts +0 -78
  481. package/src/rehydrate-from-mutationlog.ts +0 -127
  482. package/src/schema/EventId.ts +0 -60
  483. package/src/schema/MutationEvent.ts +0 -180
  484. package/src/schema/mutations.ts +0 -192
  485. package/src/schema/system-tables.ts +0 -104
  486. package/src/schema/table-def.ts +0 -343
  487. package/src/schema-management/validate-mutation-defs.ts +0 -63
  488. package/src/sync/client-session-sync-processor.ts +0 -207
  489. package/src/sync/next/test/mutation-fixtures.ts +0 -224
  490. package/tsconfig.json +0 -11
  491. /package/dist/schema/{schema-helpers.d.ts → state/sqlite/schema-helpers.d.ts} +0 -0
@@ -1,127 +0,0 @@
1
- import { isDevEnv, memoizeByRef, shouldNeverHappen } from '@livestore/utils'
2
- import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
3
-
4
- import { type MigrationOptionsFromMutationLog, type SynchronousDatabase, UnexpectedError } from './adapter-types.js'
5
- import { getExecArgsFromMutation } from './mutation.js'
6
- import type { LiveStoreSchema, MutationDef, MutationEvent, MutationLogMetaRow } from './schema/mod.js'
7
- import { EventId, MUTATION_LOG_META_TABLE } from './schema/mod.js'
8
- import type { PreparedBindValues } from './util.js'
9
- import { sql } from './util.js'
10
-
11
- export const rehydrateFromMutationLog = ({
12
- logDb,
13
- db,
14
- schema,
15
- migrationOptions,
16
- onProgress,
17
- }: {
18
- logDb: SynchronousDatabase
19
- db: SynchronousDatabase
20
- schema: LiveStoreSchema
21
- migrationOptions: MigrationOptionsFromMutationLog
22
- onProgress: (_: { done: number; total: number }) => Effect.Effect<void>
23
- }) =>
24
- Effect.gen(function* () {
25
- const mutationsCount = logDb.select<{ count: number }>(
26
- `SELECT COUNT(*) AS count FROM ${MUTATION_LOG_META_TABLE}`,
27
- )[0]!.count
28
-
29
- const hashMutation = memoizeByRef((mutation: MutationDef.Any) => Schema.hash(mutation.schema))
30
-
31
- const processMutation = (row: MutationLogMetaRow) =>
32
- Effect.gen(function* () {
33
- const mutationDef = schema.mutations.get(row.mutation) ?? shouldNeverHappen(`Unknown mutation ${row.mutation}`)
34
-
35
- if (migrationOptions.excludeMutations?.has(row.mutation) === true) return
36
-
37
- if (hashMutation(mutationDef) !== row.schemaHash) {
38
- yield* Effect.logWarning(
39
- `Schema hash mismatch for mutation ${row.mutation}. Trying to apply mutation anyway.`,
40
- )
41
- }
42
-
43
- const argsDecoded = yield* Schema.decodeUnknown(Schema.parseJson(mutationDef.schema))(row.argsJson).pipe(
44
- Effect.mapError((cause) =>
45
- UnexpectedError.make({
46
- cause,
47
- note: `\
48
- There was an error during rehydrating from the mutation log while decoding
49
- the persisted mutation event args for mutation "${row.mutation}".
50
- This likely means the schema has changed in an incompatible way.
51
- `,
52
- }),
53
- ),
54
- )
55
-
56
- const mutationEventDecoded = {
57
- id: { global: row.idGlobal, local: row.idLocal },
58
- parentId: { global: row.parentIdGlobal, local: row.parentIdLocal },
59
- mutation: row.mutation,
60
- args: argsDecoded,
61
- } satisfies MutationEvent.AnyDecoded
62
-
63
- const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
64
-
65
- const makeExecuteOptions = (statementSql: string, bindValues: any) => ({
66
- onRowsChanged: (rowsChanged: number) => {
67
- if (rowsChanged === 0 && migrationOptions.logging?.excludeAffectedRows?.(statementSql) !== true) {
68
- console.warn(`Mutation "${mutationDef.name}" did not affect any rows:`, statementSql, bindValues)
69
- }
70
- },
71
- })
72
-
73
- for (const { statementSql, bindValues } of execArgsArr) {
74
- // TODO cache prepared statements for mutations
75
- db.execute(statementSql, bindValues, isDevEnv() ? makeExecuteOptions(statementSql, bindValues) : undefined)
76
- // console.log(`Re-executed mutation ${mutationSql}`, bindValues)
77
- }
78
- }).pipe(Effect.withSpan(`@livestore/common:rehydrateFromMutationLog:processMutation`))
79
-
80
- const CHUNK_SIZE = 100
81
-
82
- const stmt = logDb.prepare(sql`\
83
- SELECT * FROM ${MUTATION_LOG_META_TABLE}
84
- WHERE idGlobal > $idGlobal OR (idGlobal = $idGlobal AND idLocal > $idLocal)
85
- ORDER BY idGlobal ASC, idLocal ASC
86
- LIMIT ${CHUNK_SIZE}
87
- `)
88
-
89
- let processedMutations = 0
90
-
91
- yield* Stream.unfoldChunk<Chunk.Chunk<MutationLogMetaRow> | { _tag: 'Initial ' }, MutationLogMetaRow>(
92
- { _tag: 'Initial ' },
93
- (item) => {
94
- // End stream if no more rows
95
- if (Chunk.isChunk(item) && item.length === 0) return Option.none()
96
-
97
- const lastId = Chunk.isChunk(item)
98
- ? Chunk.last(item).pipe(
99
- Option.map((_) => ({ global: _.idGlobal, local: _.idLocal })),
100
- Option.getOrElse(() => EventId.ROOT),
101
- )
102
- : EventId.ROOT
103
- const nextItem = Chunk.fromIterable(
104
- stmt.select<MutationLogMetaRow>({
105
- $idGlobal: lastId?.global,
106
- $idLocal: lastId?.local,
107
- } as any as PreparedBindValues),
108
- )
109
- const prevItem = Chunk.isChunk(item) ? item : Chunk.empty()
110
- return Option.some([prevItem, nextItem])
111
- },
112
- ).pipe(
113
- Stream.bufferChunks({ capacity: 2 }),
114
- Stream.tap((row) =>
115
- Effect.gen(function* () {
116
- yield* processMutation(row)
117
-
118
- processedMutations++
119
- yield* onProgress({ done: processedMutations, total: mutationsCount })
120
- }),
121
- ),
122
- Stream.runDrain,
123
- )
124
- }).pipe(
125
- Effect.withPerformanceMeasure('@livestore/common:rehydrateFromMutationLog'),
126
- Effect.withSpan('@livestore/common:rehydrateFromMutationLog'),
127
- )
@@ -1,60 +0,0 @@
1
- import { Brand, Schema } from '@livestore/utils/effect'
2
-
3
- export type LocalEventId = Brand.Branded<number, 'LocalEventId'>
4
- export const localEventId = Brand.nominal<LocalEventId>()
5
- export const LocalEventId = Schema.fromBrand(localEventId)(Schema.Int)
6
-
7
- export type GlobalEventId = Brand.Branded<number, 'GlobalEventId'>
8
- export const globalEventId = Brand.nominal<GlobalEventId>()
9
- export const GlobalEventId = Schema.fromBrand(globalEventId)(Schema.Int)
10
-
11
- export const localDefault = 0 as any as LocalEventId
12
-
13
- /**
14
- * LiveStore event id value consisting of a globally unique event sequence number
15
- * and a local sequence number.
16
- *
17
- * The local sequence number is only used for localOnly mutations and starts from 0 for each global sequence number.
18
- */
19
- export type EventId = { global: GlobalEventId; local: LocalEventId }
20
-
21
- export const EventId = Schema.Struct({
22
- global: GlobalEventId,
23
- local: LocalEventId,
24
- }).annotations({ title: 'LiveStore.EventId' })
25
-
26
- /**
27
- * Compare two event ids i.e. checks if the first event id is less than the second.
28
- */
29
- export const compare = (a: EventId, b: EventId) => {
30
- if (a.global !== b.global) {
31
- return a.global - b.global
32
- }
33
- return a.local - b.local
34
- }
35
-
36
- export const isEqual = (a: EventId, b: EventId) => a.global === b.global && a.local === b.local
37
-
38
- export type EventIdPair = { id: EventId; parentId: EventId }
39
-
40
- export const ROOT = { global: -1 as any as GlobalEventId, local: localDefault } satisfies EventId
41
-
42
- export const isGreaterThan = (a: EventId, b: EventId) => {
43
- return a.global > b.global || (a.global === b.global && a.local > b.local)
44
- }
45
-
46
- export const make = (id: EventId | typeof EventId.Encoded): EventId => {
47
- return Schema.is(EventId)(id) ? id : Schema.decodeSync(EventId)(id)
48
- }
49
-
50
- export const nextPair = (id: EventId, isLocal: boolean): EventIdPair => {
51
- if (isLocal) {
52
- return { id: { global: id.global, local: (id.local + 1) as any as LocalEventId }, parentId: id }
53
- }
54
-
55
- return {
56
- id: { global: (id.global + 1) as any as GlobalEventId, local: localDefault },
57
- // NOTE we always point to `local: 0` for non-localOnly mutations
58
- parentId: { global: id.global, local: localDefault },
59
- }
60
- }
@@ -1,180 +0,0 @@
1
- import { memoizeByRef } from '@livestore/utils'
2
- import type { Deferred } from '@livestore/utils/effect'
3
- import { Schema } from '@livestore/utils/effect'
4
-
5
- import * as EventId from './EventId.js'
6
- import type { MutationDef, MutationDefRecord } from './mutations.js'
7
- import type { LiveStoreSchema } from './schema.js'
8
-
9
- export type MutationEventPartial<TMutationsDef extends MutationDef.Any> = {
10
- mutation: TMutationsDef['name']
11
- args: Schema.Schema.Type<TMutationsDef['schema']>
12
- }
13
-
14
- export type MutationEventPartialEncoded<TMutationsDef extends MutationDef.Any> = {
15
- mutation: TMutationsDef['name']
16
- args: Schema.Schema.Encoded<TMutationsDef['schema']>
17
- }
18
-
19
- export type MutationEvent<TMutationsDef extends MutationDef.Any> = {
20
- mutation: TMutationsDef['name']
21
- args: Schema.Schema.Type<TMutationsDef['schema']>
22
- id: EventId.EventId
23
- parentId: EventId.EventId
24
- }
25
-
26
- export type MutationEventEncoded<TMutationsDef extends MutationDef.Any> = {
27
- mutation: TMutationsDef['name']
28
- args: Schema.Schema.Encoded<TMutationsDef['schema']>
29
- id: EventId.EventId
30
- parentId: EventId.EventId
31
- }
32
-
33
- export type AnyDecoded = MutationEvent<MutationDef.Any>
34
- export const AnyDecoded = Schema.Struct({
35
- mutation: Schema.String,
36
- args: Schema.Any,
37
- id: EventId.EventId,
38
- parentId: EventId.EventId,
39
- }).annotations({ title: 'MutationEvent.AnyDecoded' })
40
-
41
- export type AnyEncoded = MutationEventEncoded<MutationDef.Any>
42
- export const AnyEncoded = Schema.Struct({
43
- mutation: Schema.String,
44
- args: Schema.Any,
45
- id: EventId.EventId,
46
- parentId: EventId.EventId,
47
- }).annotations({ title: 'MutationEvent.AnyEncoded' })
48
-
49
- export const AnyEncodedGlobal = Schema.Struct({
50
- mutation: Schema.String,
51
- args: Schema.Any,
52
- id: EventId.GlobalEventId,
53
- parentId: EventId.GlobalEventId,
54
- }).annotations({ title: 'MutationEvent.AnyEncodedGlobal' })
55
- export type AnyEncodedGlobal = typeof AnyEncodedGlobal.Type
56
-
57
- export type PartialAny = MutationEventPartial<MutationDef.Any>
58
- export type PartialAnyEncoded = MutationEventPartialEncoded<MutationDef.Any>
59
-
60
- export type PartialForSchema<TSchema extends LiveStoreSchema> = {
61
- [K in keyof TSchema['_MutationDefMapType']]: MutationEventPartial<TSchema['_MutationDefMapType'][K]>
62
- }[keyof TSchema['_MutationDefMapType']]
63
-
64
- export type ForSchema<TSchema extends LiveStoreSchema> = {
65
- [K in keyof TSchema['_MutationDefMapType']]: MutationEvent<TSchema['_MutationDefMapType'][K]>
66
- }[keyof TSchema['_MutationDefMapType']]
67
-
68
- export const isPartialMutationEvent = (mutationEvent: AnyDecoded | PartialAny): mutationEvent is PartialAny =>
69
- 'id' in mutationEvent === false && 'parentId' in mutationEvent === false
70
-
71
- export type ForMutationDefRecord<TMutationsDefRecord extends MutationDefRecord> = Schema.Schema<
72
- {
73
- [K in keyof TMutationsDefRecord]: {
74
- mutation: K
75
- args: Schema.Schema.Type<TMutationsDefRecord[K]['schema']>
76
- id: EventId.EventId
77
- parentId: EventId.EventId
78
- }
79
- }[keyof TMutationsDefRecord],
80
- {
81
- [K in keyof TMutationsDefRecord]: {
82
- mutation: K
83
- args: Schema.Schema.Encoded<TMutationsDefRecord[K]['schema']>
84
- id: EventId.EventId
85
- parentId: EventId.EventId
86
- }
87
- }[keyof TMutationsDefRecord]
88
- >
89
-
90
- export type MutationEventPartialSchema<TMutationsDefRecord extends MutationDefRecord> = Schema.Schema<
91
- {
92
- [K in keyof TMutationsDefRecord]: {
93
- mutation: K
94
- args: Schema.Schema.Type<TMutationsDefRecord[K]['schema']>
95
- }
96
- }[keyof TMutationsDefRecord],
97
- {
98
- [K in keyof TMutationsDefRecord]: {
99
- mutation: K
100
- args: Schema.Schema.Encoded<TMutationsDefRecord[K]['schema']>
101
- }
102
- }[keyof TMutationsDefRecord]
103
- >
104
-
105
- export const makeMutationEventSchema = <TSchema extends LiveStoreSchema>(
106
- schema: TSchema,
107
- ): ForMutationDefRecord<TSchema['_MutationDefMapType']> =>
108
- Schema.Union(
109
- ...[...schema.mutations.values()].map((def) =>
110
- Schema.Struct({
111
- mutation: Schema.Literal(def.name),
112
- args: def.schema,
113
- id: EventId.EventId,
114
- parentId: EventId.EventId,
115
- }),
116
- ),
117
- ).annotations({ title: 'MutationEvent' }) as any
118
-
119
- export const makeMutationEventPartialSchema = <TSchema extends LiveStoreSchema>(
120
- schema: TSchema,
121
- ): MutationEventPartialSchema<TSchema['_MutationDefMapType']> =>
122
- Schema.Union(
123
- ...[...schema.mutations.values()].map((def) =>
124
- Schema.Struct({
125
- mutation: Schema.Literal(def.name),
126
- args: def.schema,
127
- }),
128
- ),
129
- ).annotations({ title: 'MutationEventSchemaPartial' }) as any
130
-
131
- export const makeMutationEventSchemaMemo = memoizeByRef(makeMutationEventSchema)
132
-
133
- /** Equivalent to AnyEncoded but with a meta field and some convenience methods */
134
- export class EncodedWithMeta extends Schema.Class<EncodedWithMeta>('MutationEvent.EncodedWithMeta')({
135
- mutation: Schema.String,
136
- args: Schema.Any,
137
- id: EventId.EventId,
138
- parentId: EventId.EventId,
139
- meta: Schema.optionalWith(
140
- Schema.Any as Schema.Schema<{ deferred?: Deferred.Deferred<void>; sessionChangeset?: Uint8Array }>,
141
- { default: () => ({}) },
142
- ),
143
- }) {
144
- toJSON = (): any => {
145
- // Only used for logging/debugging
146
- // - More readable way to print the id + parentId
147
- // - not including `meta`
148
- return {
149
- id: `(${this.id.global},${this.id.local}) → (${this.parentId.global},${this.parentId.local})`,
150
- mutation: this.mutation,
151
- args: this.args,
152
- }
153
- }
154
-
155
- rebase = (parentId: EventId.EventId, isLocal: boolean) =>
156
- new EncodedWithMeta({
157
- ...this,
158
- ...EventId.nextPair(this.id, isLocal),
159
- })
160
-
161
- static fromGlobal = (mutationEvent: AnyEncodedGlobal) =>
162
- new EncodedWithMeta({
163
- ...mutationEvent,
164
- id: { global: mutationEvent.id, local: EventId.localDefault },
165
- parentId: { global: mutationEvent.parentId, local: EventId.localDefault },
166
- })
167
-
168
- toGlobal = (): AnyEncodedGlobal => ({
169
- ...this,
170
- id: this.id.global,
171
- parentId: this.parentId.global,
172
- })
173
- }
174
-
175
- export const isEqualEncoded = (a: AnyEncoded, b: AnyEncoded) =>
176
- a.id.global === b.id.global &&
177
- a.id.local === b.id.local &&
178
- a.mutation === b.mutation &&
179
- // TODO use schema equality here
180
- JSON.stringify(a.args) === JSON.stringify(b.args)
@@ -1,192 +0,0 @@
1
- import { Schema } from '@livestore/utils/effect'
2
-
3
- import type { BindValues } from '../sql-queries/sql-queries.js'
4
- import type * as EventId from './EventId.js'
5
-
6
- export type MutationDefMap = Map<string | 'livestore.RawSql', MutationDef.Any>
7
- export type MutationDefRecord = {
8
- 'livestore.RawSql': RawSqlMutation
9
- [name: string]: MutationDef.Any
10
- }
11
-
12
- export type InternalMutationSchema<TRecord extends MutationDefRecord = MutationDefRecord> = {
13
- _DefRecord: TRecord
14
-
15
- map: Map<keyof TRecord, TRecord[keyof TRecord]>
16
- schemaHashMap: Map<keyof TRecord, number>
17
- }
18
-
19
- export type MutationDefSqlResult<TTo> =
20
- | SingleOrReadonlyArray<string>
21
- | ((args: TTo) => SingleOrReadonlyArray<
22
- | string
23
- | {
24
- sql: string
25
- /** Note args need to be manually encoded to `BindValues` when returning this argument */
26
- bindValues: BindValues
27
- writeTables?: ReadonlySet<string>
28
- }
29
- >)
30
-
31
- export type SingleOrReadonlyArray<T> = T | ReadonlyArray<T>
32
-
33
- export type MutationDef<TName extends string, TFrom, TTo> = {
34
- name: TName
35
- schema: Schema.Schema<TTo, TFrom>
36
- sql: MutationDefSqlResult<NoInfer<TTo>>
37
- options: {
38
- /** Warning: This feature is not fully implemented yet */
39
- historyId: string
40
- /**
41
- * When set to true, the mutation won't be synced over the network
42
- */
43
- localOnly: boolean
44
- /** Warning: This feature is not fully implemented yet */
45
- facts: FactsCallback<TTo> | undefined
46
- }
47
-
48
- /** Helper function to construct a partial mutation event */
49
- (
50
- args: TTo,
51
- options?: {
52
- id?: number
53
- },
54
- ): {
55
- mutation: TName
56
- args: TTo
57
- // TODO remove/clean up after sync-next is fully implemented
58
- id?: EventId.EventId
59
- }
60
- }
61
-
62
- export type FactsCallback<TTo> = (
63
- args: TTo,
64
- currentFacts: MutationEventFacts,
65
- ) => {
66
- modify: {
67
- set: Iterable<MutationEventFactInput>
68
- unset: Iterable<MutationEventFactInput>
69
- }
70
- require: Iterable<MutationEventFactInput>
71
- }
72
-
73
- export namespace MutationDef {
74
- export type Any = MutationDef<string, any, any>
75
- }
76
-
77
- export type MutationEventKey = string
78
- export type MutationEventFact = string
79
- export type MutationEventFacts = ReadonlyMap<string, any>
80
-
81
- export type MutationEventFactsGroup = {
82
- modifySet: MutationEventFacts
83
- modifyUnset: MutationEventFacts
84
-
85
- /**
86
- * Events on independent "dependency" branches are commutative which can facilitate more prioritized syncing
87
- */
88
- depRequire: MutationEventFacts
89
- depRead: MutationEventFacts
90
- }
91
-
92
- export type MutationEventFactsSnapshot = Map<string, any>
93
-
94
- export type MutationEventFactInput = string | readonly [string, any]
95
-
96
- export const defineFacts = <
97
- TRecord extends Record<string, MutationEventFactInput | ((...args: any[]) => MutationEventFactInput)>,
98
- >(
99
- record: TRecord,
100
- ): TRecord => record
101
-
102
- export type DefineMutationOptions<TTo> = {
103
- // TODO actually implement this
104
- onError?: (error: any) => void
105
- historyId?: string
106
- /** Warning: This feature is not fully implemented yet */
107
- facts?: (
108
- args: TTo,
109
- currentFacts: MutationEventFacts,
110
- ) => {
111
- modify?: {
112
- set?: Iterable<MutationEventFactInput>
113
- unset?: Iterable<MutationEventFactInput>
114
- }
115
- /**
116
- * Two purposes: constrain history and constrain compaction
117
- */
118
- require?: Iterable<MutationEventFactInput>
119
- }
120
- /**
121
- * When set to true, the mutation won't be synced over the network
122
- */
123
- localOnly?: boolean
124
- }
125
-
126
- // TODO possibly also allow for mutation event subsumption behaviour
127
- export const defineMutation = <TName extends string, TFrom, TTo>(
128
- name: TName,
129
- schema: Schema.Schema<TTo, TFrom>,
130
- sql: MutationDefSqlResult<NoInfer<TTo>>,
131
- options?: DefineMutationOptions<TTo>,
132
- ): MutationDef<TName, TFrom, TTo> => {
133
- const makePartialEvent = (
134
- args: TTo,
135
- options?: {
136
- id?: EventId.EventId
137
- },
138
- ) => ({ mutation: name, args, ...options })
139
-
140
- Object.defineProperty(makePartialEvent, 'name', { value: name })
141
- Object.defineProperty(makePartialEvent, 'schema', { value: schema })
142
- Object.defineProperty(makePartialEvent, 'sql', { value: sql })
143
- Object.defineProperty(makePartialEvent, 'options', {
144
- value: {
145
- historyId: options?.historyId ?? 'main',
146
- localOnly: options?.localOnly ?? false,
147
- facts: options?.facts
148
- ? (args, currentFacts) => {
149
- const res = options.facts!(args, currentFacts)
150
- return {
151
- modify: {
152
- set: res.modify?.set ? new Set(res.modify.set) : new Set(),
153
- unset: res.modify?.unset ? new Set(res.modify.unset) : new Set(),
154
- },
155
- require: res.require ? new Set(res.require) : new Set(),
156
- }
157
- }
158
- : undefined,
159
- } satisfies MutationDef.Any['options'],
160
- })
161
-
162
- return makePartialEvent as MutationDef<TName, TFrom, TTo>
163
- }
164
-
165
- export const makeMutationDefRecord = <TInputRecord extends Record<string, MutationDef.Any>>(
166
- inputRecord: TInputRecord,
167
- ): {
168
- [K in TInputRecord[keyof TInputRecord]['name']]: Extract<TInputRecord[keyof TInputRecord], { name: K }>
169
- } => {
170
- const result: any = {}
171
-
172
- for (const [name, def] of Object.entries(inputRecord)) {
173
- result[name] = def
174
- }
175
-
176
- result['livestore.RawSql'] = rawSqlMutation
177
-
178
- return result
179
- }
180
-
181
- export const rawSqlMutation = defineMutation(
182
- 'livestore.RawSql',
183
- Schema.Struct({
184
- sql: Schema.String,
185
- bindValues: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
186
- writeTables: Schema.optional(Schema.ReadonlySet(Schema.String)),
187
- }),
188
- ({ sql, bindValues, writeTables }) => ({ sql, bindValues: bindValues ?? {}, writeTables }),
189
- )
190
-
191
- export type RawSqlMutation = typeof rawSqlMutation
192
- export type RawSqlMutationEvent = ReturnType<typeof rawSqlMutation>
@@ -1,104 +0,0 @@
1
- import { type SqliteAst as __SqliteAst, SqliteDsl } from '@livestore/db-schema'
2
- import { Schema } from '@livestore/utils/effect'
3
-
4
- import * as EventId from './EventId.js'
5
- import type { FromTable } from './table-def.js'
6
- import { table } from './table-def.js'
7
-
8
- /// App DB
9
-
10
- export const SCHEMA_META_TABLE = '__livestore_schema'
11
-
12
- export const schemaMetaTable = table(
13
- SCHEMA_META_TABLE,
14
- {
15
- tableName: SqliteDsl.text({ primaryKey: true }),
16
- schemaHash: SqliteDsl.integer({ nullable: false }),
17
- /** ISO date format */
18
- updatedAt: SqliteDsl.text({ nullable: false }),
19
- },
20
- { disableAutomaticIdColumn: true },
21
- )
22
-
23
- export type SchemaMetaRow = FromTable.RowDecoded<typeof schemaMetaTable>
24
-
25
- export const SCHEMA_MUTATIONS_META_TABLE = '__livestore_schema_mutations'
26
-
27
- export const schemaMutationsMetaTable = table(
28
- SCHEMA_MUTATIONS_META_TABLE,
29
- {
30
- mutationName: SqliteDsl.text({ primaryKey: true }),
31
- schemaHash: SqliteDsl.integer({ nullable: false }),
32
- /** ISO date format */
33
- updatedAt: SqliteDsl.text({ nullable: false }),
34
- },
35
- { disableAutomaticIdColumn: true },
36
- )
37
-
38
- export type SchemaMutationsMetaRow = FromTable.RowDecoded<typeof schemaMutationsMetaTable>
39
-
40
- /**
41
- * Table which stores SQLite changeset blobs which is used for rolling back
42
- * read-model state during rebasing.
43
- */
44
- export const SESSION_CHANGESET_META_TABLE = '__livestore_session_changeset'
45
-
46
- export const sessionChangesetMetaTable = table(
47
- SESSION_CHANGESET_META_TABLE,
48
- {
49
- // TODO bring back primary key
50
- idGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
51
- idLocal: SqliteDsl.integer({ schema: EventId.LocalEventId }),
52
- // idGlobal: SqliteDsl.integer({ primaryKey: true }),
53
- // idLocal: SqliteDsl.integer({ primaryKey: true }),
54
- changeset: SqliteDsl.blob({}),
55
- debug: SqliteDsl.json({ nullable: true }),
56
- },
57
- { disableAutomaticIdColumn: true },
58
- )
59
-
60
- export type SessionChangesetMetaRow = FromTable.RowDecoded<typeof sessionChangesetMetaTable>
61
-
62
- export const systemTables = [schemaMetaTable, schemaMutationsMetaTable, sessionChangesetMetaTable]
63
-
64
- /// Mutation log DB
65
-
66
- export const SyncStatus = Schema.Literal('synced', 'pending', 'error', 'localOnly')
67
- export type SyncStatus = typeof SyncStatus.Type
68
-
69
- export const MUTATION_LOG_META_TABLE = 'mutation_log'
70
-
71
- export const mutationLogMetaTable = table(
72
- MUTATION_LOG_META_TABLE,
73
- {
74
- idGlobal: SqliteDsl.integer({ primaryKey: true, schema: EventId.GlobalEventId }),
75
- idLocal: SqliteDsl.integer({ primaryKey: true, schema: EventId.LocalEventId }),
76
- parentIdGlobal: SqliteDsl.integer({ schema: EventId.GlobalEventId }),
77
- parentIdLocal: SqliteDsl.integer({ schema: EventId.LocalEventId }),
78
- mutation: SqliteDsl.text({}),
79
- argsJson: SqliteDsl.text({ schema: Schema.parseJson(Schema.Any) }),
80
- schemaHash: SqliteDsl.integer({}),
81
- syncMetadataJson: SqliteDsl.text({ schema: Schema.parseJson(Schema.Option(Schema.JsonValue)) }),
82
- },
83
- {
84
- disableAutomaticIdColumn: true,
85
- indexes: [
86
- { columns: ['idGlobal'], name: 'idx_idGlobal' },
87
- { columns: ['idGlobal', 'idLocal'], name: 'idx_idGlobal_idLocal' },
88
- ],
89
- },
90
- )
91
-
92
- export type MutationLogMetaRow = FromTable.RowDecoded<typeof mutationLogMetaTable>
93
-
94
- export const SYNC_STATUS_TABLE = '__livestore_sync_status'
95
-
96
- export const syncStatusTable = table(
97
- SYNC_STATUS_TABLE,
98
- {
99
- head: SqliteDsl.integer({ primaryKey: true }),
100
- },
101
- { disableAutomaticIdColumn: true },
102
- )
103
-
104
- export type SyncStatusRow = FromTable.RowDecoded<typeof syncStatusTable>