@livestore/common 0.4.0 → 0.5.0-dev.1

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 (501) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/ClientSessionLeaderThreadProxy.d.ts +1 -1
  3. package/dist/ClientSessionLeaderThreadProxy.d.ts.map +1 -1
  4. package/dist/MaterializationJournal.d.ts +44 -0
  5. package/dist/MaterializationJournal.d.ts.map +1 -0
  6. package/dist/MaterializationJournal.js +112 -0
  7. package/dist/MaterializationJournal.js.map +1 -0
  8. package/dist/StateHead.d.ts +26 -0
  9. package/dist/StateHead.d.ts.map +1 -0
  10. package/dist/StateHead.js +50 -0
  11. package/dist/StateHead.js.map +1 -0
  12. package/dist/WorkerTransportError.d.ts +3 -3
  13. package/dist/WorkerTransportError.d.ts.map +1 -1
  14. package/dist/WorkerTransportError.js +3 -3
  15. package/dist/WorkerTransportError.js.map +1 -1
  16. package/dist/__tests__/fixture.d.ts +8 -8
  17. package/dist/__tests__/fixture.d.ts.map +1 -1
  18. package/dist/__tests__/fixture.js +4 -4
  19. package/dist/__tests__/fixture.js.map +1 -1
  20. package/dist/adapter-types.d.ts +33 -57
  21. package/dist/adapter-types.d.ts.map +1 -1
  22. package/dist/adapter-types.js +24 -41
  23. package/dist/adapter-types.js.map +1 -1
  24. package/dist/bounded-collections.d.ts.map +1 -1
  25. package/dist/bounded-collections.js.map +1 -1
  26. package/dist/debug-info.d.ts +56 -56
  27. package/dist/debug-info.d.ts.map +1 -1
  28. package/dist/debug-info.js +32 -44
  29. package/dist/debug-info.js.map +1 -1
  30. package/dist/defs.d.ts +9 -9
  31. package/dist/defs.js +2 -2
  32. package/dist/devtools/devtools-compatibility.test.js +2 -1
  33. package/dist/devtools/devtools-compatibility.test.js.map +1 -1
  34. package/dist/devtools/devtools-messages-client-session.d.ts +522 -334
  35. package/dist/devtools/devtools-messages-client-session.d.ts.map +1 -1
  36. package/dist/devtools/devtools-messages-client-session.js +80 -78
  37. package/dist/devtools/devtools-messages-client-session.js.map +1 -1
  38. package/dist/devtools/devtools-messages-common.d.ts +27 -27
  39. package/dist/devtools/devtools-messages-common.d.ts.map +1 -1
  40. package/dist/devtools/devtools-messages-common.js +6 -6
  41. package/dist/devtools/devtools-messages-common.js.map +1 -1
  42. package/dist/devtools/devtools-messages-leader.d.ts +410 -348
  43. package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
  44. package/dist/devtools/devtools-messages-leader.js +105 -93
  45. package/dist/devtools/devtools-messages-leader.js.map +1 -1
  46. package/dist/devtools/devtools-messages.js +3 -3
  47. package/dist/devtools/devtools-sessioninfo.d.ts +18 -19
  48. package/dist/devtools/devtools-sessioninfo.d.ts.map +1 -1
  49. package/dist/devtools/devtools-sessioninfo.js +8 -5
  50. package/dist/devtools/devtools-sessioninfo.js.map +1 -1
  51. package/dist/devtools/mod.d.ts +4 -5
  52. package/dist/devtools/mod.d.ts.map +1 -1
  53. package/dist/devtools/mod.js +12 -8
  54. package/dist/devtools/mod.js.map +1 -1
  55. package/dist/errors.d.ts +45 -69
  56. package/dist/errors.d.ts.map +1 -1
  57. package/dist/errors.js +19 -17
  58. package/dist/errors.js.map +1 -1
  59. package/dist/index.d.ts +4 -1
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +22 -19
  62. package/dist/index.js.map +1 -1
  63. package/dist/leader-thread/LeaderSyncProcessor.d.ts +87 -18
  64. package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
  65. package/dist/leader-thread/LeaderSyncProcessor.js +548 -480
  66. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
  67. package/dist/leader-thread/RejectedPushError.d.ts +72 -41
  68. package/dist/leader-thread/RejectedPushError.d.ts.map +1 -1
  69. package/dist/leader-thread/RejectedPushError.js +25 -6
  70. package/dist/leader-thread/RejectedPushError.js.map +1 -1
  71. package/dist/leader-thread/connection.d.ts +1 -0
  72. package/dist/leader-thread/connection.d.ts.map +1 -1
  73. package/dist/leader-thread/connection.js +11 -10
  74. package/dist/leader-thread/connection.js.map +1 -1
  75. package/dist/leader-thread/eventlog.d.ts +15 -5
  76. package/dist/leader-thread/eventlog.d.ts.map +1 -1
  77. package/dist/leader-thread/eventlog.js +19 -19
  78. package/dist/leader-thread/eventlog.js.map +1 -1
  79. package/dist/leader-thread/leader-worker-devtools.d.ts +1 -1
  80. package/dist/leader-thread/leader-worker-devtools.d.ts.map +1 -1
  81. package/dist/leader-thread/leader-worker-devtools.js +36 -20
  82. package/dist/leader-thread/leader-worker-devtools.js.map +1 -1
  83. package/dist/leader-thread/make-leader-thread-layer.d.ts +15 -5
  84. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
  85. package/dist/leader-thread/make-leader-thread-layer.js +62 -58
  86. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
  87. package/dist/leader-thread/make-leader-thread-layer.test.js +9 -9
  88. package/dist/leader-thread/make-leader-thread-layer.test.js.map +1 -1
  89. package/dist/leader-thread/materialize-event.d.ts +2 -1
  90. package/dist/leader-thread/materialize-event.d.ts.map +1 -1
  91. package/dist/leader-thread/materialize-event.js +12 -8
  92. package/dist/leader-thread/materialize-event.js.map +1 -1
  93. package/dist/leader-thread/mod.js +9 -9
  94. package/dist/leader-thread/recreate-db.d.ts +5 -1
  95. package/dist/leader-thread/recreate-db.d.ts.map +1 -1
  96. package/dist/leader-thread/recreate-db.js +34 -59
  97. package/dist/leader-thread/recreate-db.js.map +1 -1
  98. package/dist/leader-thread/shutdown-channel.d.ts +2 -6
  99. package/dist/leader-thread/shutdown-channel.d.ts.map +1 -1
  100. package/dist/leader-thread/shutdown-channel.js +2 -3
  101. package/dist/leader-thread/shutdown-channel.js.map +1 -1
  102. package/dist/leader-thread/stream-events.d.ts +4 -7
  103. package/dist/leader-thread/stream-events.d.ts.map +1 -1
  104. package/dist/leader-thread/stream-events.js +16 -21
  105. package/dist/leader-thread/stream-events.js.map +1 -1
  106. package/dist/leader-thread/types.d.ts +38 -73
  107. package/dist/leader-thread/types.d.ts.map +1 -1
  108. package/dist/leader-thread/types.js +10 -6
  109. package/dist/leader-thread/types.js.map +1 -1
  110. package/dist/logging.d.ts +5 -30
  111. package/dist/logging.d.ts.map +1 -1
  112. package/dist/logging.js +1 -32
  113. package/dist/logging.js.map +1 -1
  114. package/dist/make-client-session.d.ts +2 -3
  115. package/dist/make-client-session.d.ts.map +1 -1
  116. package/dist/make-client-session.js +5 -5
  117. package/dist/make-client-session.js.map +1 -1
  118. package/dist/materializer-helper.d.ts +0 -1
  119. package/dist/materializer-helper.d.ts.map +1 -1
  120. package/dist/materializer-helper.js +9 -37
  121. package/dist/materializer-helper.js.map +1 -1
  122. package/dist/otel.d.ts +2 -3
  123. package/dist/otel.d.ts.map +1 -1
  124. package/dist/otel.js +2 -7
  125. package/dist/otel.js.map +1 -1
  126. package/dist/rematerialize-from-eventlog.d.ts +3 -1
  127. package/dist/rematerialize-from-eventlog.d.ts.map +1 -1
  128. package/dist/rematerialize-from-eventlog.js +42 -33
  129. package/dist/rematerialize-from-eventlog.js.map +1 -1
  130. package/dist/schema/EventDef/define.d.ts +6 -6
  131. package/dist/schema/EventDef/define.d.ts.map +1 -1
  132. package/dist/schema/EventDef/define.js +5 -5
  133. package/dist/schema/EventDef/define.js.map +1 -1
  134. package/dist/schema/EventDef/define.test.d.ts +2 -0
  135. package/dist/schema/EventDef/define.test.d.ts.map +1 -0
  136. package/dist/schema/EventDef/define.test.js +23 -0
  137. package/dist/schema/EventDef/define.test.js.map +1 -0
  138. package/dist/schema/EventDef/deprecated.d.ts +8 -13
  139. package/dist/schema/EventDef/deprecated.d.ts.map +1 -1
  140. package/dist/schema/EventDef/deprecated.js +12 -13
  141. package/dist/schema/EventDef/deprecated.js.map +1 -1
  142. package/dist/schema/EventDef/deprecated.test.js +3 -3
  143. package/dist/schema/EventDef/deprecated.test.js.map +1 -1
  144. package/dist/schema/EventDef/event-def.d.ts +1 -1
  145. package/dist/schema/EventDef/event-def.d.ts.map +1 -1
  146. package/dist/schema/EventDef/facts.d.ts.map +1 -1
  147. package/dist/schema/EventDef/materializer.d.ts +2 -2
  148. package/dist/schema/EventDef/materializer.d.ts.map +1 -1
  149. package/dist/schema/EventDef/mod.js +5 -5
  150. package/dist/schema/EventSequenceNumber/client.d.ts +12 -17
  151. package/dist/schema/EventSequenceNumber/client.d.ts.map +1 -1
  152. package/dist/schema/EventSequenceNumber/client.js +20 -24
  153. package/dist/schema/EventSequenceNumber/client.js.map +1 -1
  154. package/dist/schema/EventSequenceNumber/global.d.ts +2 -2
  155. package/dist/schema/EventSequenceNumber/global.d.ts.map +1 -1
  156. package/dist/schema/EventSequenceNumber/global.js +1 -1
  157. package/dist/schema/EventSequenceNumber/global.js.map +1 -1
  158. package/dist/schema/EventSequenceNumber/mod.js +2 -2
  159. package/dist/schema/EventSequenceNumber.test.js +6 -6
  160. package/dist/schema/EventSequenceNumber.test.js.map +1 -1
  161. package/dist/schema/LiveStoreEvent/client.d.ts +58 -187
  162. package/dist/schema/LiveStoreEvent/client.d.ts.map +1 -1
  163. package/dist/schema/LiveStoreEvent/client.js +33 -30
  164. package/dist/schema/LiveStoreEvent/client.js.map +1 -1
  165. package/dist/schema/LiveStoreEvent/client.test.js +13 -4
  166. package/dist/schema/LiveStoreEvent/client.test.js.map +1 -1
  167. package/dist/schema/LiveStoreEvent/for-event-def.d.ts +7 -7
  168. package/dist/schema/LiveStoreEvent/for-event-def.d.ts.map +1 -1
  169. package/dist/schema/LiveStoreEvent/global.d.ts +6 -6
  170. package/dist/schema/LiveStoreEvent/global.d.ts.map +1 -1
  171. package/dist/schema/LiveStoreEvent/global.js +2 -2
  172. package/dist/schema/LiveStoreEvent/global.js.map +1 -1
  173. package/dist/schema/LiveStoreEvent/input.d.ts +6 -6
  174. package/dist/schema/LiveStoreEvent/input.d.ts.map +1 -1
  175. package/dist/schema/LiveStoreEvent/input.js +3 -3
  176. package/dist/schema/LiveStoreEvent/input.js.map +1 -1
  177. package/dist/schema/LiveStoreEvent/mod.js +4 -4
  178. package/dist/schema/events.js +1 -1
  179. package/dist/schema/mod.d.ts +1 -0
  180. package/dist/schema/mod.d.ts.map +1 -1
  181. package/dist/schema/mod.js +11 -10
  182. package/dist/schema/mod.js.map +1 -1
  183. package/dist/schema/schema.d.ts +1 -1
  184. package/dist/schema/schema.d.ts.map +1 -1
  185. package/dist/schema/schema.js +6 -5
  186. package/dist/schema/schema.js.map +1 -1
  187. package/dist/schema/state/mod.js +2 -2
  188. package/dist/schema/state/sqlite/client-document-def.d.ts +13 -18
  189. package/dist/schema/state/sqlite/client-document-def.d.ts.map +1 -1
  190. package/dist/schema/state/sqlite/client-document-def.js +30 -27
  191. package/dist/schema/state/sqlite/client-document-def.js.map +1 -1
  192. package/dist/schema/state/sqlite/client-document-def.test.js +53 -18
  193. package/dist/schema/state/sqlite/client-document-def.test.js.map +1 -1
  194. package/dist/schema/state/sqlite/column-annotations.d.ts +13 -13
  195. package/dist/schema/state/sqlite/column-annotations.d.ts.map +1 -1
  196. package/dist/schema/state/sqlite/column-annotations.js +13 -15
  197. package/dist/schema/state/sqlite/column-annotations.js.map +1 -1
  198. package/dist/schema/state/sqlite/column-annotations.test.js +19 -43
  199. package/dist/schema/state/sqlite/column-annotations.test.js.map +1 -1
  200. package/dist/schema/state/sqlite/column-def.d.ts +1 -1
  201. package/dist/schema/state/sqlite/column-def.d.ts.map +1 -1
  202. package/dist/schema/state/sqlite/column-def.js +73 -33
  203. package/dist/schema/state/sqlite/column-def.js.map +1 -1
  204. package/dist/schema/state/sqlite/column-def.test.js +176 -75
  205. package/dist/schema/state/sqlite/column-def.test.js.map +1 -1
  206. package/dist/schema/state/sqlite/column-spec.d.ts.map +1 -1
  207. package/dist/schema/state/sqlite/column-spec.js +1 -1
  208. package/dist/schema/state/sqlite/column-spec.js.map +1 -1
  209. package/dist/schema/state/sqlite/column-spec.test.js +4 -4
  210. package/dist/schema/state/sqlite/column-spec.test.js.map +1 -1
  211. package/dist/schema/state/sqlite/db-schema/ast/fingerprint-digest.d.ts +3 -0
  212. package/dist/schema/state/sqlite/db-schema/ast/fingerprint-digest.d.ts.map +1 -0
  213. package/dist/schema/state/sqlite/db-schema/ast/fingerprint-digest.js +5 -0
  214. package/dist/schema/state/sqlite/db-schema/ast/fingerprint-digest.js.map +1 -0
  215. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.d.ts +5 -0
  216. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.d.ts.map +1 -0
  217. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.js +183 -0
  218. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.js.map +1 -0
  219. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.test.d.ts +2 -0
  220. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.test.d.ts.map +1 -0
  221. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.test.js +142 -0
  222. package/dist/schema/state/sqlite/db-schema/ast/fingerprint.test.js.map +1 -0
  223. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts +3 -8
  224. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts.map +1 -1
  225. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js +1 -73
  226. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js.map +1 -1
  227. package/dist/schema/state/sqlite/db-schema/ast/validate.d.ts.map +1 -1
  228. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts +9 -9
  229. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts.map +1 -1
  230. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js +9 -13
  231. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js.map +1 -1
  232. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js +28 -17
  233. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.test.js.map +1 -1
  234. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts +13 -11
  235. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts.map +1 -1
  236. package/dist/schema/state/sqlite/db-schema/dsl/mod.js +6 -4
  237. package/dist/schema/state/sqlite/db-schema/dsl/mod.js.map +1 -1
  238. package/dist/schema/state/sqlite/db-schema/has-json-string-encoding.d.ts +3 -0
  239. package/dist/schema/state/sqlite/db-schema/has-json-string-encoding.d.ts.map +1 -0
  240. package/dist/schema/state/sqlite/db-schema/has-json-string-encoding.js +6 -0
  241. package/dist/schema/state/sqlite/db-schema/has-json-string-encoding.js.map +1 -0
  242. package/dist/schema/state/sqlite/db-schema/mod.js +2 -2
  243. package/dist/schema/state/sqlite/mod.d.ts +1 -3
  244. package/dist/schema/state/sqlite/mod.d.ts.map +1 -1
  245. package/dist/schema/state/sqlite/mod.js +11 -10
  246. package/dist/schema/state/sqlite/mod.js.map +1 -1
  247. package/dist/schema/state/sqlite/persistence.d.ts +11 -0
  248. package/dist/schema/state/sqlite/persistence.d.ts.map +1 -0
  249. package/dist/schema/state/sqlite/persistence.js +10 -0
  250. package/dist/schema/state/sqlite/persistence.js.map +1 -0
  251. package/dist/schema/state/sqlite/query-builder/api.d.ts +7 -7
  252. package/dist/schema/state/sqlite/query-builder/api.d.ts.map +1 -1
  253. package/dist/schema/state/sqlite/query-builder/astToSql.d.ts.map +1 -1
  254. package/dist/schema/state/sqlite/query-builder/astToSql.js +22 -23
  255. package/dist/schema/state/sqlite/query-builder/astToSql.js.map +1 -1
  256. package/dist/schema/state/sqlite/query-builder/impl.d.ts +1 -1
  257. package/dist/schema/state/sqlite/query-builder/impl.d.ts.map +1 -1
  258. package/dist/schema/state/sqlite/query-builder/impl.js +18 -16
  259. package/dist/schema/state/sqlite/query-builder/impl.js.map +1 -1
  260. package/dist/schema/state/sqlite/query-builder/impl.test.js +364 -598
  261. package/dist/schema/state/sqlite/query-builder/impl.test.js.map +1 -1
  262. package/dist/schema/state/sqlite/query-builder/mod.js +2 -2
  263. package/dist/schema/state/sqlite/schema-helpers.d.ts.map +1 -1
  264. package/dist/schema/state/sqlite/schema-helpers.js +2 -2
  265. package/dist/schema/state/sqlite/schema-helpers.js.map +1 -1
  266. package/dist/schema/state/sqlite/schema-helpers.test.js +2 -2
  267. package/dist/schema/state/sqlite/system-tables/eventlog-tables.d.ts +102 -134
  268. package/dist/schema/state/sqlite/system-tables/eventlog-tables.d.ts.map +1 -1
  269. package/dist/schema/state/sqlite/system-tables/eventlog-tables.js +5 -5
  270. package/dist/schema/state/sqlite/system-tables/eventlog-tables.js.map +1 -1
  271. package/dist/schema/state/sqlite/system-tables/mod.js +2 -2
  272. package/dist/schema/state/sqlite/system-tables/state-tables.d.ts +380 -115
  273. package/dist/schema/state/sqlite/system-tables/state-tables.d.ts.map +1 -1
  274. package/dist/schema/state/sqlite/system-tables/state-tables.js +47 -6
  275. package/dist/schema/state/sqlite/system-tables/state-tables.js.map +1 -1
  276. package/dist/schema/state/sqlite/table-def.d.ts +17 -20
  277. package/dist/schema/state/sqlite/table-def.d.ts.map +1 -1
  278. package/dist/schema/state/sqlite/table-def.js +72 -6
  279. package/dist/schema/state/sqlite/table-def.js.map +1 -1
  280. package/dist/schema/state/sqlite/table-def.test.js +207 -37
  281. package/dist/schema/state/sqlite/table-def.test.js.map +1 -1
  282. package/dist/schema/unknown-events.d.ts.map +1 -1
  283. package/dist/schema/unknown-events.js +3 -2
  284. package/dist/schema/unknown-events.js.map +1 -1
  285. package/dist/schema-management/__tests__/migrations-autoincrement-quoting.test.js +28 -6
  286. package/dist/schema-management/__tests__/migrations-autoincrement-quoting.test.js.map +1 -1
  287. package/dist/schema-management/common.d.ts.map +1 -1
  288. package/dist/schema-management/common.js +2 -2
  289. package/dist/schema-management/migrations.d.ts +4 -4
  290. package/dist/schema-management/migrations.d.ts.map +1 -1
  291. package/dist/schema-management/migrations.js +9 -9
  292. package/dist/schema-management/migrations.js.map +1 -1
  293. package/dist/schema-management/validate-schema.d.ts.map +1 -1
  294. package/dist/schema-management/validate-schema.js +1 -1
  295. package/dist/session-id-symbol.d.ts +29 -0
  296. package/dist/session-id-symbol.d.ts.map +1 -0
  297. package/dist/session-id-symbol.js +44 -0
  298. package/dist/session-id-symbol.js.map +1 -0
  299. package/dist/sql-queries/index.js +3 -3
  300. package/dist/sql-queries/sql-queries.d.ts.map +1 -1
  301. package/dist/sql-queries/sql-queries.js +11 -13
  302. package/dist/sql-queries/sql-queries.js.map +1 -1
  303. package/dist/sql-queries/sql-query-builder.d.ts.map +1 -1
  304. package/dist/sql-queries/sql-query-builder.js +1 -1
  305. package/dist/sql-queries/types.d.ts +6 -7
  306. package/dist/sql-queries/types.d.ts.map +1 -1
  307. package/dist/sql-queries/types.js.map +1 -1
  308. package/dist/sqlite-db-helper.d.ts +23 -3
  309. package/dist/sqlite-db-helper.d.ts.map +1 -1
  310. package/dist/sqlite-db-helper.js +39 -4
  311. package/dist/sqlite-db-helper.js.map +1 -1
  312. package/dist/sqlite-types.d.ts +3 -6
  313. package/dist/sqlite-types.d.ts.map +1 -1
  314. package/dist/sqlite-types.js +3 -3
  315. package/dist/sqlite-types.js.map +1 -1
  316. package/dist/sync/ClientSessionSyncProcessor.d.ts +41 -19
  317. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
  318. package/dist/sync/ClientSessionSyncProcessor.js +200 -70
  319. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
  320. package/dist/sync/errors.d.ts +12 -18
  321. package/dist/sync/errors.d.ts.map +1 -1
  322. package/dist/sync/errors.js +3 -3
  323. package/dist/sync/errors.js.map +1 -1
  324. package/dist/sync/index.js +6 -6
  325. package/dist/sync/mock-sync-backend.d.ts +2 -3
  326. package/dist/sync/mock-sync-backend.d.ts.map +1 -1
  327. package/dist/sync/mock-sync-backend.js +58 -37
  328. package/dist/sync/mock-sync-backend.js.map +1 -1
  329. package/dist/sync/mock-sync-backend.test.d.ts +2 -0
  330. package/dist/sync/mock-sync-backend.test.d.ts.map +1 -0
  331. package/dist/sync/mock-sync-backend.test.js +49 -0
  332. package/dist/sync/mock-sync-backend.test.js.map +1 -0
  333. package/dist/sync/next/compact-events.d.ts.map +1 -1
  334. package/dist/sync/next/compact-events.js +3 -3
  335. package/dist/sync/next/facts.d.ts.map +1 -1
  336. package/dist/sync/next/facts.js +1 -1
  337. package/dist/sync/next/history-dag-common.d.ts +5 -1
  338. package/dist/sync/next/history-dag-common.d.ts.map +1 -1
  339. package/dist/sync/next/history-dag-common.js +53 -32
  340. package/dist/sync/next/history-dag-common.js.map +1 -1
  341. package/dist/sync/next/history-dag.d.ts.map +1 -1
  342. package/dist/sync/next/history-dag.js +5 -4
  343. package/dist/sync/next/history-dag.js.map +1 -1
  344. package/dist/sync/next/mod.js +5 -5
  345. package/dist/sync/next/rebase-events.d.ts.map +1 -1
  346. package/dist/sync/next/rebase-events.js +2 -2
  347. package/dist/sync/next/test/compact-events.calculator.test.js +6 -6
  348. package/dist/sync/next/test/compact-events.test.js +4 -4
  349. package/dist/sync/next/test/event-fixtures.d.ts +8 -8
  350. package/dist/sync/next/test/event-fixtures.d.ts.map +1 -1
  351. package/dist/sync/next/test/event-fixtures.js +6 -6
  352. package/dist/sync/next/test/event-fixtures.js.map +1 -1
  353. package/dist/sync/next/test/history-dag-common.test.d.ts +2 -0
  354. package/dist/sync/next/test/history-dag-common.test.d.ts.map +1 -0
  355. package/dist/sync/next/test/history-dag-common.test.js +73 -0
  356. package/dist/sync/next/test/history-dag-common.test.js.map +1 -0
  357. package/dist/sync/next/test/mod.js +1 -1
  358. package/dist/sync/sync-backend-kv.d.ts.map +1 -1
  359. package/dist/sync/sync-backend-kv.js +5 -3
  360. package/dist/sync/sync-backend-kv.js.map +1 -1
  361. package/dist/sync/sync-backend.d.ts +15 -15
  362. package/dist/sync/sync-backend.d.ts.map +1 -1
  363. package/dist/sync/sync-backend.js +10 -6
  364. package/dist/sync/sync-backend.js.map +1 -1
  365. package/dist/sync/sync.d.ts +1 -1
  366. package/dist/sync/sync.d.ts.map +1 -1
  367. package/dist/sync/sync.js +2 -2
  368. package/dist/sync/syncstate.d.ts +100 -176
  369. package/dist/sync/syncstate.d.ts.map +1 -1
  370. package/dist/sync/syncstate.js +36 -39
  371. package/dist/sync/syncstate.js.map +1 -1
  372. package/dist/sync/syncstate.test.js +103 -64
  373. package/dist/sync/syncstate.test.js.map +1 -1
  374. package/dist/sync/transport-chunking.d.ts +15 -16
  375. package/dist/sync/transport-chunking.d.ts.map +1 -1
  376. package/dist/sync/transport-chunking.js +23 -32
  377. package/dist/sync/transport-chunking.js.map +1 -1
  378. package/dist/sync/validate-push-payload.d.ts.map +1 -1
  379. package/dist/sync/validate-push-payload.js +2 -2
  380. package/dist/testing/event-factory.d.ts.map +1 -1
  381. package/dist/testing/event-factory.js +2 -2
  382. package/dist/testing/event-factory.js.map +1 -1
  383. package/dist/testing/mod.js +1 -1
  384. package/dist/util.d.ts +11 -4
  385. package/dist/util.d.ts.map +1 -1
  386. package/dist/util.js +20 -2
  387. package/dist/util.js.map +1 -1
  388. package/dist/version.d.ts.map +1 -1
  389. package/dist/version.js.map +1 -1
  390. package/package.json +40 -57
  391. package/src/ClientSessionLeaderThreadProxy.ts +1 -1
  392. package/src/MaterializationJournal.ts +184 -0
  393. package/src/StateHead.ts +80 -0
  394. package/src/WorkerTransportError.ts +4 -4
  395. package/src/__tests__/fixture.ts +3 -3
  396. package/src/adapter-types.ts +28 -61
  397. package/src/debug-info.ts +53 -70
  398. package/src/defs.ts +2 -2
  399. package/src/devtools/devtools-compatibility.test.ts +1 -0
  400. package/src/devtools/devtools-messages-client-session.ts +56 -65
  401. package/src/devtools/devtools-messages-common.ts +12 -11
  402. package/src/devtools/devtools-messages-leader.ts +71 -69
  403. package/src/devtools/devtools-sessioninfo.ts +13 -11
  404. package/src/devtools/mod.ts +4 -5
  405. package/src/errors.ts +46 -34
  406. package/src/index.ts +8 -1
  407. package/src/leader-thread/LeaderSyncProcessor.ts +777 -650
  408. package/src/leader-thread/RejectedPushError.ts +40 -27
  409. package/src/leader-thread/connection.ts +12 -8
  410. package/src/leader-thread/eventlog.ts +20 -12
  411. package/src/leader-thread/leader-worker-devtools.ts +38 -20
  412. package/src/leader-thread/make-leader-thread-layer.test.ts +7 -7
  413. package/src/leader-thread/make-leader-thread-layer.ts +100 -71
  414. package/src/leader-thread/materialize-event.ts +13 -8
  415. package/src/leader-thread/recreate-db.ts +41 -75
  416. package/src/leader-thread/shutdown-channel.ts +3 -14
  417. package/src/leader-thread/stream-events.ts +102 -101
  418. package/src/leader-thread/types.ts +29 -62
  419. package/src/logging.ts +5 -52
  420. package/src/make-client-session.ts +6 -6
  421. package/src/materializer-helper.ts +7 -36
  422. package/src/otel.ts +5 -14
  423. package/src/rematerialize-from-eventlog.ts +116 -98
  424. package/src/schema/EventDef/define.test.ts +28 -0
  425. package/src/schema/EventDef/define.ts +20 -19
  426. package/src/schema/EventDef/deprecated.test.ts +1 -8
  427. package/src/schema/EventDef/deprecated.ts +17 -20
  428. package/src/schema/EventDef/event-def.ts +2 -4
  429. package/src/schema/EventSequenceNumber/client.ts +20 -24
  430. package/src/schema/EventSequenceNumber/global.ts +1 -1
  431. package/src/schema/EventSequenceNumber.test.ts +5 -5
  432. package/src/schema/LiveStoreEvent/client.test.ts +17 -4
  433. package/src/schema/LiveStoreEvent/client.ts +36 -35
  434. package/src/schema/LiveStoreEvent/for-event-def.ts +7 -7
  435. package/src/schema/LiveStoreEvent/global.ts +1 -1
  436. package/src/schema/LiveStoreEvent/input.ts +6 -6
  437. package/src/schema/mod.ts +1 -0
  438. package/src/schema/schema.ts +4 -3
  439. package/src/schema/state/sqlite/client-document-def.test.ts +55 -20
  440. package/src/schema/state/sqlite/client-document-def.ts +95 -80
  441. package/src/schema/state/sqlite/column-annotations.test.ts +26 -45
  442. package/src/schema/state/sqlite/column-annotations.ts +26 -30
  443. package/src/schema/state/sqlite/column-def.test.ts +213 -77
  444. package/src/schema/state/sqlite/column-def.ts +87 -37
  445. package/src/schema/state/sqlite/column-spec.test.ts +2 -2
  446. package/src/schema/state/sqlite/column-spec.ts +3 -2
  447. package/src/schema/state/sqlite/db-schema/ast/fingerprint-digest.ts +6 -0
  448. package/src/schema/state/sqlite/db-schema/ast/fingerprint.test.ts +209 -0
  449. package/src/schema/state/sqlite/db-schema/ast/fingerprint.ts +225 -0
  450. package/src/schema/state/sqlite/db-schema/ast/sqlite.ts +2 -81
  451. package/src/schema/state/sqlite/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +123 -63
  452. package/src/schema/state/sqlite/db-schema/dsl/field-defs.test.ts +35 -16
  453. package/src/schema/state/sqlite/db-schema/dsl/field-defs.ts +26 -31
  454. package/src/schema/state/sqlite/db-schema/dsl/mod.ts +35 -21
  455. package/src/schema/state/sqlite/db-schema/has-json-string-encoding.ts +9 -0
  456. package/src/schema/state/sqlite/mod.ts +4 -5
  457. package/src/schema/state/sqlite/persistence.ts +11 -0
  458. package/src/schema/state/sqlite/query-builder/api.ts +9 -9
  459. package/src/schema/state/sqlite/query-builder/astToSql.ts +24 -26
  460. package/src/schema/state/sqlite/query-builder/impl.test.ts +433 -669
  461. package/src/schema/state/sqlite/query-builder/impl.ts +19 -15
  462. package/src/schema/state/sqlite/schema-helpers.ts +1 -1
  463. package/src/schema/state/sqlite/system-tables/eventlog-tables.ts +2 -2
  464. package/src/schema/state/sqlite/system-tables/state-tables.ts +54 -3
  465. package/src/schema/state/sqlite/table-def.test.ts +270 -66
  466. package/src/schema/state/sqlite/table-def.ts +112 -45
  467. package/src/schema/unknown-events.ts +3 -2
  468. package/src/schema-management/__tests__/migrations-autoincrement-quoting.test.ts +35 -4
  469. package/src/schema-management/migrations.ts +7 -6
  470. package/src/session-id-symbol.ts +59 -0
  471. package/src/sql-queries/sql-queries.ts +9 -13
  472. package/src/sql-queries/types.ts +6 -7
  473. package/src/sqlite-db-helper.ts +64 -2
  474. package/src/sqlite-types.ts +5 -8
  475. package/src/sync/ClientSessionSyncProcessor.ts +303 -119
  476. package/src/sync/errors.ts +14 -12
  477. package/src/sync/mock-sync-backend.test.ts +78 -0
  478. package/src/sync/mock-sync-backend.ts +93 -47
  479. package/src/sync/next/history-dag-common.ts +60 -34
  480. package/src/sync/next/history-dag.ts +4 -1
  481. package/src/sync/next/test/compact-events.calculator.test.ts +2 -2
  482. package/src/sync/next/test/event-fixtures.ts +1 -1
  483. package/src/sync/next/test/history-dag-common.test.ts +87 -0
  484. package/src/sync/sync-backend-kv.ts +4 -2
  485. package/src/sync/sync-backend.ts +15 -15
  486. package/src/sync/sync.ts +1 -1
  487. package/src/sync/syncstate.test.ts +191 -140
  488. package/src/sync/syncstate.ts +63 -53
  489. package/src/sync/transport-chunking.ts +34 -39
  490. package/src/util.ts +19 -9
  491. package/src/version.ts +1 -2
  492. package/dist/schema/state/sqlite/db-schema/hash.d.ts +0 -2
  493. package/dist/schema/state/sqlite/db-schema/hash.d.ts.map +0 -1
  494. package/dist/schema/state/sqlite/db-schema/hash.js +0 -15
  495. package/dist/schema/state/sqlite/db-schema/hash.js.map +0 -1
  496. package/dist/sql-queries/misc.d.ts +0 -2
  497. package/dist/sql-queries/misc.d.ts.map +0 -1
  498. package/dist/sql-queries/misc.js +0 -2
  499. package/dist/sql-queries/misc.js.map +0 -1
  500. package/src/schema/state/sqlite/db-schema/hash.ts +0 -13
  501. package/src/sql-queries/misc.ts +0 -2
@@ -0,0 +1,184 @@
1
+ import { Context, Effect, Layer, Predicate, ReadonlyArray, Schema } from '@livestore/utils/effect'
2
+
3
+ import { SqliteError, type SqliteDb } from './adapter-types.ts'
4
+ import { execSql, execSqlPrepared } from './leader-thread/connection.ts'
5
+ import * as EventSequenceNumber from './schema/EventSequenceNumber/mod.ts'
6
+ import { SystemTables } from './schema/mod.ts'
7
+ import { findManyRows, insertRow } from './sql-queries/index.ts'
8
+ import * as SqliteDbHelper from './sqlite-db-helper.ts'
9
+ import { prepareBindValues, sql } from './util.ts'
10
+
11
+ export const TypeId = '~@livestore/common/MaterializationJournal' as const
12
+ export type TypeId = typeof TypeId
13
+
14
+ export const MaterializationJournalErrorTypeId = '~@livestore/common/MaterializationJournalError' as const
15
+
16
+ export const isMaterializationJournalError = (u: unknown): u is MaterializationJournalError =>
17
+ Predicate.hasProperty(u, MaterializationJournalErrorTypeId)
18
+
19
+ export class MaterializationJournalError extends Schema.TaggedError<MaterializationJournalError>(
20
+ MaterializationJournalErrorTypeId,
21
+ )('MaterializationJournalError', {
22
+ method: Schema.Literals(['record', 'rollback', 'discardUpTo']),
23
+ cause: Schema.Defect(),
24
+ }) {
25
+ readonly [MaterializationJournalErrorTypeId] = MaterializationJournalErrorTypeId
26
+ }
27
+
28
+ export type MaterializationChangeset = { _tag: 'changeset'; data: Uint8Array<ArrayBuffer> } | { _tag: 'no-op' }
29
+
30
+ export type MaterializationRecord = {
31
+ key: EventSequenceNumber.Client.Composite
32
+ changeset: MaterializationChangeset
33
+ }
34
+
35
+ export interface Service {
36
+ readonly [TypeId]: TypeId
37
+ /** Stores a materialization record, replacing any record at the same key. */
38
+ record: (record: MaterializationRecord) => Effect.Effect<void, MaterializationJournalError>
39
+ /** Reverts the keyed materializations in reverse order, then discards their records. */
40
+ rollback: (
41
+ keys: ReadonlyArray<EventSequenceNumber.Client.Composite>,
42
+ ) => Effect.Effect<void, MaterializationJournalError>
43
+ /** Discards records whose key is less than or equal to `key`, ignoring rebase generation. */
44
+ discardUpTo: (key: EventSequenceNumber.Client.Composite) => Effect.Effect<void, MaterializationJournalError>
45
+ }
46
+
47
+ export class MaterializationJournal extends Context.Service<MaterializationJournal, Service>()(
48
+ '@livestore/common/MaterializationJournal',
49
+ ) {}
50
+
51
+ interface Options {
52
+ readonly dbState: SqliteDb
53
+ }
54
+
55
+ export const make = ({ dbState }: Options) => {
56
+ const deleteByKeys = Effect.fnUntraced(function* (keys: ReadonlyArray<EventSequenceNumber.Client.Composite>) {
57
+ // Keep DELETE statements below SQLite's bound-parameter limit.
58
+ const keyChunks = ReadonlyArray.chunksOf(100)(keys)
59
+
60
+ for (const keyChunk of keyChunks) {
61
+ const placeholders = keyChunk.map(() => '(?, ?, ?)').join(', ')
62
+ const bindValues = keyChunk.flatMap((key) => [key.global, key.client, key.rebaseGeneration])
63
+ const statement = sql`DELETE FROM ${SystemTables.MATERIALIZATION_JOURNAL_META_TABLE}
64
+ WHERE (seqNumGlobal, seqNumClient, seqNumRebaseGeneration) IN (${placeholders})`
65
+
66
+ yield* execSqlPrepared(dbState, statement, prepareBindValues(bindValues, statement))
67
+ }
68
+ }, SqliteDbHelper.withSavepoint(dbState))
69
+
70
+ return MaterializationJournal.of({
71
+ [TypeId]: TypeId,
72
+ record: Effect.fnUntraced(
73
+ function* (record: MaterializationRecord) {
74
+ yield* deleteByKeys([record.key])
75
+
76
+ // Generate the parameterized INSERT statement
77
+ const [statement, bindValues] = insertRow({
78
+ tableName: SystemTables.MATERIALIZATION_JOURNAL_META_TABLE,
79
+ columns: SystemTables.materializationJournalMetaTable.sqliteDef.columns,
80
+ values: {
81
+ seqNumGlobal: record.key.global,
82
+ seqNumClient: record.key.client,
83
+ seqNumRebaseGeneration: record.key.rebaseGeneration,
84
+ changeset: record.changeset._tag === 'changeset' ? record.changeset.data : null,
85
+ // Legacy processors still expose this column for development diagnostics.
86
+ debug: null,
87
+ },
88
+ })
89
+
90
+ yield* execSqlPrepared(dbState, statement, prepareBindValues(bindValues, statement))
91
+ },
92
+ SqliteDbHelper.withSavepoint(dbState),
93
+ Effect.mapError((cause) => new MaterializationJournalError({ method: 'record', cause })),
94
+ ),
95
+ rollback: Effect.fnUntraced(
96
+ function* (keys: ReadonlyArray<EventSequenceNumber.Client.Composite>) {
97
+ const sortedKeys = keys.toSorted((a, b) => EventSequenceNumber.Client.compare(b, a))
98
+ const rollbackRecords = yield* Effect.forEach(
99
+ sortedKeys,
100
+ Effect.fnUntraced(function* (key) {
101
+ const [statement, bindValues] = findManyRows({
102
+ tableName: SystemTables.MATERIALIZATION_JOURNAL_META_TABLE,
103
+ columns: SystemTables.materializationJournalMetaTable.sqliteDef.columns,
104
+ where: {
105
+ seqNumGlobal: key.global,
106
+ seqNumClient: key.client,
107
+ seqNumRebaseGeneration: key.rebaseGeneration,
108
+ },
109
+ limit: 1,
110
+ })
111
+ const preparedBindValues = prepareBindValues(bindValues, statement)
112
+ const row = yield* Effect.try({
113
+ try: () => dbState.select<SystemTables.MaterializationJournalMetaRow>(statement, preparedBindValues)[0],
114
+ catch: (cause) => new SqliteError({ cause, query: { sql: statement, bindValues: preparedBindValues } }),
115
+ })
116
+
117
+ if (row === undefined) {
118
+ return yield* new MaterializationJournalError({
119
+ method: 'rollback',
120
+ cause: new Error(
121
+ `Missing materialization journal record for ${EventSequenceNumber.Client.toString(key)}`,
122
+ ),
123
+ })
124
+ }
125
+
126
+ return {
127
+ key,
128
+ changeset:
129
+ row.changeset === null
130
+ ? { _tag: 'no-op' as const }
131
+ : { _tag: 'changeset' as const, data: row.changeset },
132
+ }
133
+ }),
134
+ )
135
+
136
+ for (const record of rollbackRecords) {
137
+ if (record.changeset._tag === 'changeset') {
138
+ const data = record.changeset.data
139
+ yield* Effect.try({
140
+ try: () => dbState.makeChangeset(data).invert().apply(),
141
+ catch: (cause) => new SqliteError({ cause }),
142
+ })
143
+ }
144
+ }
145
+
146
+ yield* deleteByKeys(sortedKeys)
147
+ },
148
+ SqliteDbHelper.withSavepoint(dbState),
149
+ Effect.mapError((cause) =>
150
+ isMaterializationJournalError(cause) === true
151
+ ? cause
152
+ : new MaterializationJournalError({ method: 'rollback', cause }),
153
+ ),
154
+ ),
155
+ discardUpTo: Effect.fnUntraced(
156
+ function* (key: EventSequenceNumber.Client.Composite) {
157
+ yield* execSql(
158
+ dbState,
159
+ sql`DELETE FROM ${SystemTables.MATERIALIZATION_JOURNAL_META_TABLE}
160
+ WHERE seqNumGlobal < ${key.global}
161
+ OR (
162
+ seqNumGlobal = ${key.global}
163
+ AND seqNumClient <= ${key.client}
164
+ )`,
165
+ {},
166
+ )
167
+ },
168
+ SqliteDbHelper.withSavepoint(dbState),
169
+ Effect.mapError((cause) => new MaterializationJournalError({ method: 'discardUpTo', cause })),
170
+ ),
171
+ })
172
+ }
173
+
174
+ export const layer = (options: Options) => Layer.succeed(MaterializationJournal, make(options))
175
+
176
+ export const layerTest = Layer.succeed(
177
+ MaterializationJournal,
178
+ MaterializationJournal.of({
179
+ [TypeId]: TypeId,
180
+ record: () => Effect.void,
181
+ rollback: () => Effect.void,
182
+ discardUpTo: () => Effect.void,
183
+ }),
184
+ )
@@ -0,0 +1,80 @@
1
+ import { Context, Effect, Layer } from '@livestore/utils/effect'
2
+
3
+ import type { SqliteDb, SqliteError } from './adapter-types.ts'
4
+ import { execSql, selectSql } from './leader-thread/connection.ts'
5
+ import * as EventSequenceNumber from './schema/EventSequenceNumber/mod.ts'
6
+ import { SystemTables } from './schema/mod.ts'
7
+ import { findManyRows, insertRow } from './sql-queries/index.ts'
8
+
9
+ export const TypeId = '~@livestore/common/StateHead' as const
10
+ export type TypeId = typeof TypeId
11
+
12
+ const STATE_HEAD_ROW_ID = 1
13
+
14
+ /**
15
+ * Persists and reads the latest event sequence number reflected by the state database.
16
+ */
17
+ export interface Service {
18
+ readonly [TypeId]: TypeId
19
+ /** Persists the latest event sequence number reflected by the state database. */
20
+ set: (head: EventSequenceNumber.Client.Composite) => Effect.Effect<void, SqliteError>
21
+ /** Returns the persisted head, or the root when the current-schema table has no row. */
22
+ get: Effect.Effect<EventSequenceNumber.Client.Composite, SqliteError>
23
+ }
24
+
25
+ export class StateHead extends Context.Service<StateHead, Service>()('@livestore/common/StateHead') {}
26
+
27
+ interface Options {
28
+ readonly dbState: SqliteDb
29
+ }
30
+
31
+ export const make = ({ dbState }: Options) => {
32
+ return StateHead.of({
33
+ [TypeId]: TypeId,
34
+ set: (head) => {
35
+ const [statement, bindValues] = insertRow({
36
+ tableName: SystemTables.STATE_HEAD_META_TABLE,
37
+ columns: SystemTables.stateHeadMetaTable.sqliteDef.columns,
38
+ values: {
39
+ id: STATE_HEAD_ROW_ID,
40
+ seqNumGlobal: head.global,
41
+ seqNumClient: head.client,
42
+ seqNumRebaseGeneration: head.rebaseGeneration,
43
+ },
44
+ options: { orReplace: true },
45
+ })
46
+
47
+ return execSql(dbState, statement, bindValues)
48
+ },
49
+ get: Effect.suspend(() => {
50
+ const [statement, bindValues] = findManyRows({
51
+ tableName: SystemTables.STATE_HEAD_META_TABLE,
52
+ columns: SystemTables.stateHeadMetaTable.sqliteDef.columns,
53
+ where: { id: STATE_HEAD_ROW_ID },
54
+ limit: 1,
55
+ })
56
+ return selectSql<SystemTables.StateHeadMetaRow>(dbState, statement, bindValues).pipe(
57
+ Effect.map(([row]) =>
58
+ row === undefined
59
+ ? EventSequenceNumber.Client.ROOT
60
+ : EventSequenceNumber.Client.Composite.make({
61
+ global: row.seqNumGlobal,
62
+ client: row.seqNumClient,
63
+ rebaseGeneration: row.seqNumRebaseGeneration,
64
+ }),
65
+ ),
66
+ )
67
+ }),
68
+ })
69
+ }
70
+
71
+ export const layer = (options: Options) => Layer.succeed(StateHead, make(options))
72
+
73
+ export const layerTest = Layer.succeed(
74
+ StateHead,
75
+ StateHead.of({
76
+ [TypeId]: TypeId,
77
+ set: () => Effect.void,
78
+ get: Effect.succeed(EventSequenceNumber.Client.ROOT),
79
+ }),
80
+ )
@@ -1,12 +1,12 @@
1
- import { ParseResult, WorkerError } from '@livestore/utils/effect'
1
+ import { Schema, WorkerError } from '@livestore/utils/effect'
2
2
 
3
3
  /**
4
4
  * Type guard that identifies infrastructure errors from the Effect Worker transport layer.
5
5
  *
6
6
  * @remarks
7
7
  *
8
- * These errors ({@link WorkerError.WorkerError}, {@link ParseResult.ParseError}) represent communication failures between
8
+ * These errors ({@link WorkerError.WorkerError}, {@link Schema.SchemaError}) represent communication failures between
9
9
  * threads/processes — not application-level failures.
10
10
  */
11
- export const isWorkerTransportError = (e: unknown): e is WorkerError.WorkerError | ParseResult.ParseError =>
12
- ParseResult.isParseError(e) || WorkerError.isWorkerError(e)
11
+ export const isWorkerTransportError = (e: unknown): e is WorkerError.WorkerError | Schema.SchemaError =>
12
+ Schema.isSchemaError(e) || WorkerError.isWorkerError(e)
@@ -1,7 +1,7 @@
1
1
  import { Schema } from '@livestore/utils/effect'
2
2
 
3
- import { SessionIdSymbol } from '../adapter-types.ts'
4
3
  import { makeSchema, State } from '../schema/mod.ts'
4
+ import { SessionIdSymbol } from '../session-id-symbol.ts'
5
5
 
6
6
  export const UiState = State.SQLite.clientDocument({
7
7
  name: 'UiState',
@@ -14,8 +14,8 @@ export const UiState = State.SQLite.clientDocument({
14
14
  })
15
15
 
16
16
  const Config = Schema.Struct({
17
- fontSize: Schema.Number,
18
- theme: Schema.Literal('light', 'dark'),
17
+ fontSize: Schema.Finite,
18
+ theme: Schema.Literals(['light', 'dark']),
19
19
  })
20
20
 
21
21
  export const appConfig = State.SQLite.clientDocument({
@@ -2,6 +2,7 @@ import {
2
2
  type Effect,
3
3
  type Exit,
4
4
  type Queue,
5
+ type Latch,
5
6
  Schema,
6
7
  type Scope,
7
8
  type SubscriptionRef,
@@ -28,16 +29,13 @@ export * from './sqlite-types.ts'
28
29
  export interface ClientSession {
29
30
  /** SQLite database with synchronous API running in the same thread (usually in-memory) */
30
31
  sqliteDb: SqliteDb
31
- devtools: { enabled: false } | { enabled: true; pullLatch: Effect.Latch; pushLatch: Effect.Latch }
32
+ devtools: { enabled: false } | { enabled: true; pullLatch: Latch.Latch; pushLatch: Latch.Latch }
32
33
  clientId: string
33
34
  sessionId: string
34
35
  /** Status info whether current session is leader or not */
35
36
  lockStatus: SubscriptionRef.SubscriptionRef<LockStatus>
36
37
  shutdown: (
37
- cause: Exit.Exit<
38
- IntentionalShutdownCause,
39
- UnknownError | MaterializeError | BackendIdMismatchError
40
- >,
38
+ cause: Exit.Exit<IntentionalShutdownCause, UnknownError | MaterializeError | BackendIdMismatchError>,
41
39
  ) => Effect.Effect<void>
42
40
  /** A proxy API to communicate with the leader thread */
43
41
  leaderThread: ClientSessionLeaderThreadProxy
@@ -48,8 +46,8 @@ export interface ClientSession {
48
46
  export type ResetMode = 'all-data' | 'only-app-db'
49
47
 
50
48
  export const BootStateProgress = Schema.Struct({
51
- done: Schema.Number,
52
- total: Schema.Number,
49
+ done: Schema.Finite,
50
+ total: Schema.Finite,
53
51
  })
54
52
 
55
53
  /**
@@ -60,7 +58,7 @@ export const BootStateProgress = Schema.Struct({
60
58
  * - `storage-unavailable`: OPFS access denied for other reasons (permissions, quota)
61
59
  * - `unknown`: Unexpected error during storage initialization
62
60
  */
63
- export const BootWarningReason = Schema.Literal('private-browsing', 'storage-unavailable', 'unknown')
61
+ export const BootWarningReason = Schema.Literals(['private-browsing', 'storage-unavailable', 'unknown'])
64
62
  export type BootWarningReason = typeof BootWarningReason.Type
65
63
 
66
64
  /**
@@ -70,10 +68,10 @@ export type BootWarningReason = typeof BootWarningReason.Type
70
68
  * - `persisted`: Data is persisted to disk (e.g., via OPFS)
71
69
  * - `in-memory`: Data is only stored in memory and will be lost on page refresh
72
70
  */
73
- export const StorageMode = Schema.Literal('persisted', 'in-memory')
71
+ export const StorageMode = Schema.Literals(['persisted', 'in-memory'])
74
72
  export type StorageMode = typeof StorageMode.Type
75
73
 
76
- export const BootStatus = Schema.Union(
74
+ export const BootStatus = Schema.Union([
77
75
  Schema.Struct({ stage: Schema.Literal('loading') }),
78
76
  Schema.Struct({ stage: Schema.Literal('migrating'), progress: BootStateProgress }),
79
77
  Schema.Struct({ stage: Schema.Literal('rehydrating'), progress: BootStateProgress }),
@@ -88,67 +86,35 @@ export const BootStatus = Schema.Union(
88
86
  reason: BootWarningReason,
89
87
  message: Schema.String,
90
88
  }),
91
- ).annotations({ title: 'BootStatus' })
89
+ ]).annotate({ title: 'BootStatus' })
92
90
 
93
91
  export type BootStatus = typeof BootStatus.Type
94
92
 
95
- /**
96
- * Can be used in queries to refer to the current session id.
97
- * Will be replaced with the actual session id at runtime
98
- *
99
- * In client document table:
100
- * ```ts
101
- * const uiState = State.SQLite.clientDocument({
102
- * name: 'ui_state',
103
- * schema: Schema.Struct({
104
- * theme: Schema.Literal('dark', 'light', 'system'),
105
- * user: Schema.String,
106
- * showToolbar: Schema.Boolean,
107
- * }),
108
- * default: { value: defaultFrontendState, id: SessionIdSymbol },
109
- * })
110
- * ```
111
- *
112
- * Or in a client document query:
113
- * ```ts
114
- * const query$ = queryDb(tables.uiState.get(SessionIdSymbol))
115
- * ```
116
- */
117
- export const SessionIdSymbol = Symbol.for('@livestore/session-id')
118
- export type SessionIdSymbol = typeof SessionIdSymbol
119
-
120
93
  export type LockStatus = 'has-lock' | 'no-lock'
121
94
 
122
- // TODO possibly allow a combination of these options
123
95
  // TODO allow a way to stream the migration progress back to the app
124
- export type MigrationOptions =
125
- | {
126
- strategy: 'auto'
127
- hooks?: Partial<MigrationHooks>
128
- logging?: {
129
- excludeAffectedRows?: (sqlStmt: string) => boolean
130
- }
131
- }
132
- | {
133
- strategy: 'manual'
134
- migrate: (
135
- oldDb: Uint8Array<ArrayBuffer>,
136
- ) => Uint8Array<ArrayBuffer> | Promise<Uint8Array<ArrayBuffer>> | Effect.Effect<Uint8Array<ArrayBuffer>, unknown>
137
- }
96
+ export type MigrationOptions = {
97
+ hooks?: Partial<MigrationHooks>
98
+ logging?: {
99
+ excludeAffectedRows?: (sqlStmt: string) => boolean
100
+ }
101
+ }
138
102
 
139
103
  export type MigrationHooks = {
140
104
  /** Runs on the empty in-memory database with no database schemas applied yet */
141
105
  init: MigrationHook
142
- /** Runs before applying the migration strategy but after table schemas have been applied and singleton rows have been created */
106
+ /** Runs after table schemas and singleton rows are created, but before rematerializing state from the eventlog */
143
107
  pre: MigrationHook
144
- /** Runs after applying the migration strategy before creating export snapshot and closing the database */
108
+ /** Runs after rematerializing state from the eventlog */
145
109
  post: MigrationHook
146
110
  }
147
111
 
148
112
  export type MigrationHook = (db: SqliteDb) => void | Promise<void> | Effect.Effect<void, unknown>
149
113
 
150
- export interface ClientSessionDevtoolsChannel
151
- extends WebChannel.WebChannel<Devtools.ClientSession.MessageToApp, Devtools.ClientSession.MessageFromApp> {}
114
+ export interface ClientSessionDevtoolsChannel extends WebChannel.WebChannel<
115
+ Devtools.ClientSession.MessageToApp,
116
+ Devtools.ClientSession.MessageFromApp
117
+ > {}
152
118
 
153
119
  export type ConnectDevtoolsToStore = (
154
120
  storeDevtoolsChannel: ClientSessionDevtoolsChannel,
@@ -159,14 +125,15 @@ export type Adapter = (args: AdapterArgs) => Effect.Effect<ClientSession, Unknow
159
125
  export interface AdapterArgs {
160
126
  schema: LiveStoreSchema
161
127
  storeId: string
128
+ /** Runtime parameters resolved by `createStore` before the adapter is invoked. */
129
+ params: {
130
+ stateRebuildBatchSize: number
131
+ }
162
132
  devtoolsEnabled: boolean
163
133
  debugInstanceId: string
164
134
  bootStatusQueue: Queue.Queue<BootStatus>
165
135
  shutdown: (
166
- exit: Exit.Exit<
167
- IntentionalShutdownCause,
168
- UnknownError | MaterializeError | BackendIdMismatchError
169
- >,
136
+ exit: Exit.Exit<IntentionalShutdownCause, UnknownError | MaterializeError | BackendIdMismatchError>,
170
137
  ) => Effect.Effect<void>
171
138
  connectDevtoolsToStore: ConnectDevtoolsToStore
172
139
  /**
@@ -174,7 +141,7 @@ export interface AdapterArgs {
174
141
  *
175
142
  * @default undefined
176
143
  */
177
- syncPayloadSchema: Schema.Schema<any> | undefined
144
+ syncPayloadSchema: Schema.Decoder<Schema.Json> | undefined
178
145
  /** Encoded representation of the sync payload matching `syncPayloadSchema`. */
179
- syncPayloadEncoded: Schema.JsonValue | undefined
146
+ syncPayloadEncoded: Schema.Json | undefined
180
147
  }
package/src/debug-info.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference lib="dom" />
2
- import { ParseResult, Schema } from '@livestore/utils/effect'
2
+ import { Schema, SchemaGetter, Struct } from '@livestore/utils/effect'
3
3
 
4
4
  import { BoundArray } from './bounded-collections.ts'
5
5
  import { PreparedBindValues } from './util.ts'
@@ -16,93 +16,76 @@ export type SlowQueryInfo = {
16
16
  export const SlowQueryInfo = Schema.Struct({
17
17
  queryStr: Schema.String,
18
18
  bindValues: Schema.UndefinedOr(PreparedBindValues),
19
- durationMs: Schema.Number,
20
- rowsCount: Schema.UndefinedOr(Schema.Number),
19
+ durationMs: Schema.Finite,
20
+ rowsCount: Schema.UndefinedOr(Schema.Finite),
21
21
  queriedTables: Schema.ReadonlySet(Schema.String),
22
- startTimePerfNow: Schema.Number,
22
+ startTimePerfNow: Schema.Finite,
23
23
  })
24
24
 
25
- const getSizeLimit = (value: unknown): number =>
26
- typeof (value as { sizeLimit?: number }).sizeLimit === 'number'
27
- ? (value as { sizeLimit: number }).sizeLimit
28
- : Number.POSITIVE_INFINITY
25
+ const isBoundArrayLike = <A>(value: unknown): value is BoundArray<A> => value instanceof BoundArray
29
26
 
30
- const isBoundArrayLike = (value: unknown): value is BoundArray<unknown> =>
31
- value instanceof BoundArray ||
32
- (value !== null && typeof value === 'object' && typeof (value as { sizeLimit?: number }).sizeLimit === 'number')
33
-
34
- const BoundArraySchemaFromSelf = <A, I, R>(
35
- item: Schema.Schema<A, I, R>,
36
- ): Schema.Schema<BoundArray<A>, BoundArray<I>, R> =>
37
- Schema.declare(
38
- [item],
39
- {
40
- decode: (item) => (input, parseOptions, ast) => {
41
- if (isBoundArrayLike(input) === true) {
42
- const elements = ParseResult.decodeUnknown(Schema.Array(item))([...input], parseOptions)
43
- return ParseResult.map(elements, (as): BoundArray<A> => BoundArray.make(getSizeLimit(input), as))
27
+ const BoundArraySchemaFromSelf = <A, I, RD, RE>(
28
+ item: Schema.Codec<A, I, RD, RE>,
29
+ ): Schema.Codec<BoundArray<A>, BoundArray<A>> =>
30
+ Schema.declare<BoundArray<A>>(isBoundArrayLike, {
31
+ identifier: 'BoundArray',
32
+ expected: 'BoundArray',
33
+ description: 'Bounded array',
34
+ toFormatter: () => (_) => `BoundArray(${_.length})`,
35
+ toCodecArbitrary: () =>
36
+ Schema.link<BoundArray<A>>()(
37
+ Schema.Struct({
38
+ size: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 100 })),
39
+ items: Schema.Array(item).check(Schema.isMaxLength(100)),
40
+ }),
41
+ {
42
+ decode: SchemaGetter.transform(({ size, items }) => BoundArray.make(size, items)),
43
+ encode: SchemaGetter.forbiddenEncoding,
44
+ },
45
+ ),
46
+ toEquivalence: () => {
47
+ const elementEquivalence = Schema.toEquivalence(item)
48
+ return (a, b) => {
49
+ if (a === b) {
50
+ return true
44
51
  }
45
- return ParseResult.fail(new ParseResult.Type(ast, input))
46
- },
47
- encode: (item) => (input, parseOptions, ast) => {
48
- if (isBoundArrayLike(input) === true) {
49
- const items = [...input]
50
- const elements = ParseResult.encodeUnknown(Schema.Array(item))(items, parseOptions)
51
- return ParseResult.map(elements, (is): BoundArray<I> => BoundArray.make(getSizeLimit(input), is))
52
+ if (isBoundArrayLike(a) === false || isBoundArrayLike(b) === false) {
53
+ return false
52
54
  }
53
- return ParseResult.fail(new ParseResult.Type(ast, input))
54
- },
55
- },
56
- {
57
- description: `BoundArray<${Schema.format(item)}>`,
58
- pretty: () => (_) => `BoundArray(${_.length})`,
59
- arbitrary: () => (fc) => fc.anything() as any,
60
- equivalence: () => {
61
- const elementEquivalence = Schema.equivalence(item)
62
- return (a: unknown, b: unknown) => {
63
- if (a === b) {
64
- return true
65
- }
66
- if (isBoundArrayLike(a) === false || isBoundArrayLike(b) === false) {
67
- return false
68
- }
69
- if (getSizeLimit(a) !== getSizeLimit(b) || a.length !== b.length) {
55
+ if (a.sizeLimit !== b.sizeLimit || a.length !== b.length) {
56
+ return false
57
+ }
58
+ const itemsA = [...a]
59
+ const itemsB = [...b]
60
+ for (let i = 0; i < itemsA.length; i++) {
61
+ if (elementEquivalence(itemsA[i]!, itemsB[i]!) === false) {
70
62
  return false
71
63
  }
72
- const itemsA = [...a]
73
- const itemsB = [...b]
74
- for (let i = 0; i < itemsA.length; i++) {
75
- if (elementEquivalence(itemsA[i] as any, itemsB[i] as any) === false) {
76
- return false
77
- }
78
- }
79
- return true
80
64
  }
81
- },
65
+ return true
66
+ }
82
67
  },
83
- )
68
+ })
84
69
 
85
- export const BoundArraySchema = <ItemDecoded, ItemEncoded>(elSchema: Schema.Schema<ItemDecoded, ItemEncoded>) =>
86
- Schema.transform(
87
- Schema.Struct({
88
- size: Schema.Number,
89
- items: Schema.Array(elSchema),
70
+ export const BoundArraySchema = <ItemDecoded, ItemEncoded>(elSchema: Schema.Codec<ItemDecoded, ItemEncoded>) =>
71
+ Schema.Struct({
72
+ size: Schema.Finite,
73
+ items: Schema.Array(elSchema),
74
+ }).pipe(
75
+ Schema.decodeTo(BoundArraySchemaFromSelf(Schema.toType(elSchema)), {
76
+ decode: SchemaGetter.transform((_) => BoundArray.make(_.size, _.items)),
77
+ encode: SchemaGetter.transform((_) => ({ size: _.sizeLimit, items: [..._] })),
90
78
  }),
91
- BoundArraySchemaFromSelf(Schema.typeSchema(elSchema)),
92
- {
93
- encode: (_) => ({ size: _.sizeLimit, items: [..._] }),
94
- decode: (_) => BoundArray.make(_.size, _.items),
95
- },
96
79
  )
97
80
 
98
81
  export const DebugInfo = Schema.Struct({
99
82
  slowQueries: BoundArraySchema(SlowQueryInfo),
100
- queryFrameDuration: Schema.Number,
101
- queryFrameCount: Schema.Number,
102
- events: BoundArraySchema(Schema.Tuple(Schema.String, Schema.Any)),
83
+ queryFrameDuration: Schema.Finite,
84
+ queryFrameCount: Schema.Finite,
85
+ events: BoundArraySchema(Schema.Tuple([Schema.String, Schema.Any])),
103
86
  })
104
87
 
105
88
  export type DebugInfo = typeof DebugInfo.Type
106
89
 
107
- export const MutableDebugInfo = Schema.mutable(DebugInfo)
90
+ export const MutableDebugInfo = DebugInfo.mapFields(Struct.map(Schema.mutableKey))
108
91
  export type MutableDebugInfo = typeof MutableDebugInfo.Type
package/src/defs.ts CHANGED
@@ -3,8 +3,8 @@ import { Schema } from '@livestore/utils/effect'
3
3
  export const MigrationsReportEntry = Schema.Struct({
4
4
  tableName: Schema.String,
5
5
  hashes: Schema.Struct({
6
- expected: Schema.Number,
7
- actual: Schema.optional(Schema.Number),
6
+ expected: Schema.String,
7
+ actual: Schema.optional(Schema.String),
8
8
  }),
9
9
  })
10
10
 
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
2
2
 
3
3
  import { isDevtoolsProtocolVersionSupported, resolveDevtoolsProtocolVersion } from '../version.ts'
4
4
 
5
+ /** Verifies: LS.SYS.DT-R02, LS.SYS.VER.PROTO-R01 */
5
6
  describe('DevTools protocol compatibility', () => {
6
7
  it('treats legacy pings without a protocol version as protocol 1', () => {
7
8
  expect(resolveDevtoolsProtocolVersion(undefined)).toBe(1)