@rocicorp/zero 1.10.0-canary.2 → 1.10.0-canary.20

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 (372) hide show
  1. package/out/replicache/src/btree/read.d.ts +12 -1
  2. package/out/replicache/src/btree/read.d.ts.map +1 -1
  3. package/out/replicache/src/btree/read.js +24 -12
  4. package/out/replicache/src/btree/read.js.map +1 -1
  5. package/out/replicache/src/db/write.js.map +1 -1
  6. package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
  7. package/out/replicache/src/kv/expo-sqlite/store.js +27 -4
  8. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  9. package/out/replicache/src/kv/sqlite-store.d.ts +11 -0
  10. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  11. package/out/replicache/src/kv/sqlite-store.js +43 -3
  12. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  13. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  14. package/out/replicache/src/replicache-impl.js +10 -0
  15. package/out/replicache/src/replicache-impl.js.map +1 -1
  16. package/out/replicache/src/transactions.d.ts.map +1 -1
  17. package/out/replicache/src/transactions.js +1 -1
  18. package/out/replicache/src/transactions.js.map +1 -1
  19. package/out/shared/src/error.d.ts +11 -0
  20. package/out/shared/src/error.d.ts.map +1 -1
  21. package/out/shared/src/error.js +35 -1
  22. package/out/shared/src/error.js.map +1 -1
  23. package/out/shared/src/hash.d.ts.map +1 -1
  24. package/out/shared/src/hash.js +105 -10
  25. package/out/shared/src/hash.js.map +1 -1
  26. package/out/shared/src/map.d.ts +7 -2
  27. package/out/shared/src/map.d.ts.map +1 -1
  28. package/out/shared/src/map.js.map +1 -1
  29. package/out/shared/src/promise-race.d.ts +14 -0
  30. package/out/shared/src/promise-race.d.ts.map +1 -1
  31. package/out/shared/src/promise-race.js +32 -1
  32. package/out/shared/src/promise-race.js.map +1 -1
  33. package/out/zero/package.js +2 -3
  34. package/out/zero/package.js.map +1 -1
  35. package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
  36. package/out/zero-cache/src/auth/write-authorizer.js +8 -1
  37. package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
  38. package/out/zero-cache/src/config/normalize.d.ts +5 -0
  39. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  40. package/out/zero-cache/src/config/normalize.js +16 -3
  41. package/out/zero-cache/src/config/normalize.js.map +1 -1
  42. package/out/zero-cache/src/config/zero-config.d.ts +36 -7
  43. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  44. package/out/zero-cache/src/config/zero-config.js +121 -46
  45. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  46. package/out/zero-cache/src/db/create.d.ts.map +1 -1
  47. package/out/zero-cache/src/db/create.js +4 -1
  48. package/out/zero-cache/src/db/create.js.map +1 -1
  49. package/out/zero-cache/src/db/index-predicate.d.ts +4 -0
  50. package/out/zero-cache/src/db/index-predicate.d.ts.map +1 -0
  51. package/out/zero-cache/src/db/index-predicate.js +46 -0
  52. package/out/zero-cache/src/db/index-predicate.js.map +1 -0
  53. package/out/zero-cache/src/db/lite-tables.d.ts +10 -2
  54. package/out/zero-cache/src/db/lite-tables.d.ts.map +1 -1
  55. package/out/zero-cache/src/db/lite-tables.js +8 -3
  56. package/out/zero-cache/src/db/lite-tables.js.map +1 -1
  57. package/out/zero-cache/src/db/migration-lite.d.ts.map +1 -1
  58. package/out/zero-cache/src/db/migration-lite.js +5 -3
  59. package/out/zero-cache/src/db/migration-lite.js.map +1 -1
  60. package/out/zero-cache/src/db/specs.d.ts +41 -0
  61. package/out/zero-cache/src/db/specs.d.ts.map +1 -1
  62. package/out/zero-cache/src/db/specs.js +30 -3
  63. package/out/zero-cache/src/db/specs.js.map +1 -1
  64. package/out/zero-cache/src/db/transaction-pool.d.ts +5 -15
  65. package/out/zero-cache/src/db/transaction-pool.d.ts.map +1 -1
  66. package/out/zero-cache/src/db/transaction-pool.js +39 -73
  67. package/out/zero-cache/src/db/transaction-pool.js.map +1 -1
  68. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  69. package/out/zero-cache/src/server/change-streamer.js +27 -15
  70. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  71. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  72. package/out/zero-cache/src/server/main.js +4 -4
  73. package/out/zero-cache/src/server/main.js.map +1 -1
  74. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  75. package/out/zero-cache/src/server/replicator.js +8 -4
  76. package/out/zero-cache/src/server/replicator.js.map +1 -1
  77. package/out/zero-cache/src/server/runner/run-worker.js +1 -1
  78. package/out/zero-cache/src/server/worker-urls.d.ts +0 -1
  79. package/out/zero-cache/src/server/worker-urls.d.ts.map +1 -1
  80. package/out/zero-cache/src/server/worker-urls.js +1 -2
  81. package/out/zero-cache/src/server/worker-urls.js.map +1 -1
  82. package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
  83. package/out/zero-cache/src/services/analyze.js +1 -0
  84. package/out/zero-cache/src/services/analyze.js.map +1 -1
  85. package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts +12 -2
  86. package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts.map +1 -1
  87. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +14 -3
  88. package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
  89. package/out/zero-cache/src/services/change-source/common/replica-schema.d.ts.map +1 -1
  90. package/out/zero-cache/src/services/change-source/common/replica-schema.js +10 -41
  91. package/out/zero-cache/src/services/change-source/common/replica-schema.js.map +1 -1
  92. package/out/zero-cache/src/services/change-source/pg/backfill-stream.d.ts +3 -2
  93. package/out/zero-cache/src/services/change-source/pg/backfill-stream.d.ts.map +1 -1
  94. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +42 -16
  95. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  96. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +34 -2
  97. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  98. package/out/zero-cache/src/services/change-source/pg/change-source.js +47 -19
  99. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  100. package/out/zero-cache/src/services/change-source/pg/decommission.d.ts.map +1 -1
  101. package/out/zero-cache/src/services/change-source/pg/decommission.js +20 -11
  102. package/out/zero-cache/src/services/change-source/pg/decommission.js.map +1 -1
  103. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +1 -0
  104. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  105. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +58 -80
  106. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  107. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts +20 -0
  108. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts.map +1 -1
  109. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +43 -3
  110. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js.map +1 -1
  111. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +24 -1
  112. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  113. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +35 -5
  114. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  115. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts +1513 -229
  116. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
  117. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +2 -2
  118. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
  119. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts +19 -0
  120. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -0
  121. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js +581 -0
  122. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -0
  123. package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts +1 -1
  124. package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts.map +1 -1
  125. package/out/zero-cache/src/services/change-source/pg/schema/init.js +15 -6
  126. package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
  127. package/out/zero-cache/src/services/change-source/pg/schema/published.d.ts +137 -20
  128. package/out/zero-cache/src/services/change-source/pg/schema/published.d.ts.map +1 -1
  129. package/out/zero-cache/src/services/change-source/pg/schema/published.js +82 -13
  130. package/out/zero-cache/src/services/change-source/pg/schema/published.js.map +1 -1
  131. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +128 -21
  132. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  133. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +5 -5
  134. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  135. package/out/zero-cache/src/services/change-source/protocol/current/data.d.ts +2 -0
  136. package/out/zero-cache/src/services/change-source/protocol/current/data.d.ts.map +1 -1
  137. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts +3 -0
  138. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts.map +1 -1
  139. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts +1 -4
  140. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts.map +1 -1
  141. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js +11 -18
  142. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js.map +1 -1
  143. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  144. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +8 -3
  145. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  146. package/out/zero-cache/src/services/change-streamer/broadcast.d.ts.map +1 -1
  147. package/out/zero-cache/src/services/change-streamer/broadcast.js +4 -1
  148. package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
  149. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.d.ts +34 -0
  150. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.d.ts.map +1 -0
  151. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.js +67 -0
  152. package/out/zero-cache/src/services/change-streamer/change-log-compare-digest.js.map +1 -0
  153. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +2 -4
  154. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  155. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +8 -2
  156. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  157. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +34 -13
  158. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  159. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +165 -36
  160. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  161. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +4 -9
  162. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  163. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  164. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts.map +1 -1
  165. package/out/zero-cache/src/services/change-streamer/forwarder.js +12 -3
  166. package/out/zero-cache/src/services/change-streamer/forwarder.js.map +1 -1
  167. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.d.ts.map +1 -1
  168. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js +11 -5
  169. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js.map +1 -1
  170. package/out/zero-cache/src/services/change-streamer/shard-sampling.d.ts +11 -0
  171. package/out/zero-cache/src/services/change-streamer/shard-sampling.d.ts.map +1 -0
  172. package/out/zero-cache/src/services/change-streamer/shard-sampling.js +19 -0
  173. package/out/zero-cache/src/services/change-streamer/shard-sampling.js.map +1 -0
  174. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.d.ts +11 -1
  175. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.d.ts.map +1 -1
  176. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js +38 -5
  177. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js.map +1 -1
  178. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.d.ts +25 -1
  179. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.d.ts.map +1 -1
  180. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js +95 -12
  181. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js.map +1 -1
  182. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.d.ts +83 -0
  183. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.d.ts.map +1 -0
  184. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.js +417 -0
  185. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-comparator.js.map +1 -0
  186. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.d.ts +73 -0
  187. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.d.ts.map +1 -0
  188. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js +157 -0
  189. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js.map +1 -0
  190. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.d.ts +1 -1
  191. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.d.ts.map +1 -1
  192. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.js +8 -3
  193. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-reader.js.map +1 -1
  194. package/out/zero-cache/src/services/change-streamer/storer.d.ts +63 -1
  195. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  196. package/out/zero-cache/src/services/change-streamer/storer.js +217 -49
  197. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  198. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +8 -1
  199. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  200. package/out/zero-cache/src/services/change-streamer/subscriber.js +10 -1
  201. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  202. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts +29 -0
  203. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts.map +1 -0
  204. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js +154 -0
  205. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js.map +1 -0
  206. package/out/zero-cache/src/services/http-service.d.ts +1 -0
  207. package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
  208. package/out/zero-cache/src/services/http-service.js +7 -3
  209. package/out/zero-cache/src/services/http-service.js.map +1 -1
  210. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  211. package/out/zero-cache/src/services/litestream/commands.js +16 -7
  212. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  213. package/out/zero-cache/src/services/litestream/config-v5.yml +15 -1
  214. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js +131 -0
  215. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js.map +1 -0
  216. package/out/zero-cache/src/services/litestream/litestream-controller.d.ts +70 -0
  217. package/out/zero-cache/src/services/litestream/litestream-controller.d.ts.map +1 -0
  218. package/out/zero-cache/src/services/litestream/litestream-controller.js +101 -0
  219. package/out/zero-cache/src/services/litestream/litestream-controller.js.map +1 -0
  220. package/out/zero-cache/src/services/litestream/metrics.d.ts +1 -0
  221. package/out/zero-cache/src/services/litestream/metrics.d.ts.map +1 -1
  222. package/out/zero-cache/src/services/litestream/metrics.js +4 -1
  223. package/out/zero-cache/src/services/litestream/metrics.js.map +1 -1
  224. package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
  225. package/out/zero-cache/src/services/mutagen/mutagen.js +1 -1
  226. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  227. package/out/zero-cache/src/services/replicator/change-log-db.d.ts +10 -0
  228. package/out/zero-cache/src/services/replicator/change-log-db.d.ts.map +1 -1
  229. package/out/zero-cache/src/services/replicator/change-log-db.js +15 -1
  230. package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
  231. package/out/zero-cache/src/services/replicator/change-processor.d.ts.map +1 -1
  232. package/out/zero-cache/src/services/replicator/change-processor.js +18 -21
  233. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  234. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +1 -1
  235. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  236. package/out/zero-cache/src/services/replicator/incremental-sync.js +2 -6
  237. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  238. package/out/zero-cache/src/services/replicator/replicator.d.ts +1 -1
  239. package/out/zero-cache/src/services/replicator/replicator.d.ts.map +1 -1
  240. package/out/zero-cache/src/services/replicator/replicator.js +2 -2
  241. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  242. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts +3 -2
  243. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts.map +1 -1
  244. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js +8 -6
  245. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js.map +1 -1
  246. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts +27 -2
  247. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts.map +1 -1
  248. package/out/zero-cache/src/services/replicator/write-worker-client.js +3 -2
  249. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  250. package/out/zero-cache/src/services/replicator/write-worker.js +24 -5
  251. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  252. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +3 -2
  253. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  254. package/out/zero-cache/src/services/view-syncer/client-handler.js +25 -17
  255. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  256. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts.map +1 -1
  257. package/out/zero-cache/src/services/view-syncer/cvr-store.js +14 -1
  258. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  259. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +3 -3
  260. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  261. package/out/zero-cache/src/services/view-syncer/view-syncer.js +6 -2
  262. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  263. package/out/zero-cache/src/types/downstream.d.ts +11 -0
  264. package/out/zero-cache/src/types/downstream.d.ts.map +1 -0
  265. package/out/zero-cache/src/types/pg.d.ts +15 -2
  266. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  267. package/out/zero-cache/src/types/pg.js +21 -8
  268. package/out/zero-cache/src/types/pg.js.map +1 -1
  269. package/out/zero-cache/src/types/poke-chunk.d.ts +18 -0
  270. package/out/zero-cache/src/types/poke-chunk.d.ts.map +1 -0
  271. package/out/zero-cache/src/types/poke-chunk.js +57 -0
  272. package/out/zero-cache/src/types/poke-chunk.js.map +1 -0
  273. package/out/zero-cache/src/types/processes.js +1 -4
  274. package/out/zero-cache/src/types/processes.js.map +1 -1
  275. package/out/zero-cache/src/types/streams.d.ts +4 -9
  276. package/out/zero-cache/src/types/streams.d.ts.map +1 -1
  277. package/out/zero-cache/src/types/streams.js.map +1 -1
  278. package/out/zero-cache/src/types/subscription.d.ts +2 -11
  279. package/out/zero-cache/src/types/subscription.d.ts.map +1 -1
  280. package/out/zero-cache/src/types/subscription.js +4 -25
  281. package/out/zero-cache/src/types/subscription.js.map +1 -1
  282. package/out/zero-cache/src/workers/connection.d.ts +22 -4
  283. package/out/zero-cache/src/workers/connection.d.ts.map +1 -1
  284. package/out/zero-cache/src/workers/connection.js +112 -14
  285. package/out/zero-cache/src/workers/connection.js.map +1 -1
  286. package/out/zero-cache/src/workers/replicator.d.ts +1 -0
  287. package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
  288. package/out/zero-cache/src/workers/replicator.js +4 -2
  289. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  290. package/out/zero-client/src/client/connection-manager.d.ts.map +1 -1
  291. package/out/zero-client/src/client/connection-manager.js +46 -0
  292. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  293. package/out/zero-client/src/client/context.d.ts +15 -0
  294. package/out/zero-client/src/client/context.d.ts.map +1 -1
  295. package/out/zero-client/src/client/context.js +44 -0
  296. package/out/zero-client/src/client/context.js.map +1 -1
  297. package/out/zero-client/src/client/inspector/lazy-inspector.d.ts.map +1 -1
  298. package/out/zero-client/src/client/inspector/lazy-inspector.js +19 -12
  299. package/out/zero-client/src/client/inspector/lazy-inspector.js.map +1 -1
  300. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  301. package/out/zero-client/src/client/version.js +1 -1
  302. package/out/zero-client/src/client/zero-poke-handler.d.ts +7 -2
  303. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  304. package/out/zero-client/src/client/zero-poke-handler.js +52 -2
  305. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  306. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  307. package/out/zero-client/src/client/zero-rep.js +2 -1
  308. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  309. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  310. package/out/zero-client/src/client/zero.js +98 -25
  311. package/out/zero-client/src/client/zero.js.map +1 -1
  312. package/out/zero-protocol/src/custom-queries.d.ts +1 -1
  313. package/out/zero-protocol/src/down.d.ts +1 -1
  314. package/out/zero-protocol/src/error.d.ts +4 -4
  315. package/out/zero-protocol/src/inspect-down.js +1 -1
  316. package/out/zero-protocol/src/mutate-server.d.ts +2 -2
  317. package/out/zero-protocol/src/poke.d.ts +89 -7
  318. package/out/zero-protocol/src/poke.d.ts.map +1 -1
  319. package/out/zero-protocol/src/poke.js +16 -8
  320. package/out/zero-protocol/src/poke.js.map +1 -1
  321. package/out/zero-protocol/src/protocol-version.d.ts +12 -7
  322. package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
  323. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  324. package/out/zero-protocol/src/push.d.ts +1 -1
  325. package/out/zero-protocol/src/query-server.d.ts +2 -2
  326. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  327. package/out/zql/src/ivm/array-view.js +5 -3
  328. package/out/zql/src/ivm/array-view.js.map +1 -1
  329. package/out/zql/src/ivm/data.d.ts.map +1 -1
  330. package/out/zql/src/ivm/data.js +8 -3
  331. package/out/zql/src/ivm/data.js.map +1 -1
  332. package/out/zql/src/ivm/deferred-input.d.ts +44 -0
  333. package/out/zql/src/ivm/deferred-input.d.ts.map +1 -0
  334. package/out/zql/src/ivm/deferred-input.js +88 -0
  335. package/out/zql/src/ivm/deferred-input.js.map +1 -0
  336. package/out/zql/src/ivm/memory-source.d.ts +10 -2
  337. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  338. package/out/zql/src/ivm/memory-source.js +19 -16
  339. package/out/zql/src/ivm/memory-source.js.map +1 -1
  340. package/out/zql/src/ivm/stream.d.ts +0 -1
  341. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  342. package/out/zql/src/ivm/stream.js +1 -7
  343. package/out/zql/src/ivm/stream.js.map +1 -1
  344. package/out/zql/src/ivm/union-fan-in.d.ts +1 -1
  345. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  346. package/out/zql/src/ivm/union-fan-in.js +11 -2
  347. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  348. package/out/zql/src/ivm/view-apply-change.js +6 -1
  349. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  350. package/out/zql/src/query/query-delegate-base.d.ts +6 -0
  351. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  352. package/out/zql/src/query/query-delegate-base.js +73 -10
  353. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  354. package/out/zql/src/query/query-delegate.d.ts +22 -0
  355. package/out/zql/src/query/query-delegate.d.ts.map +1 -1
  356. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  357. package/out/zqlite/src/query-builder.js +2 -1
  358. package/out/zqlite/src/query-builder.js.map +1 -1
  359. package/out/zqlite/src/sqlite-stat-fanout.d.ts.map +1 -1
  360. package/out/zqlite/src/sqlite-stat-fanout.js +1 -0
  361. package/out/zqlite/src/sqlite-stat-fanout.js.map +1 -1
  362. package/out/zqlite/src/table-source.d.ts +1 -1
  363. package/out/zqlite/src/table-source.d.ts.map +1 -1
  364. package/out/zqlite/src/table-source.js +3 -2
  365. package/out/zqlite/src/table-source.js.map +1 -1
  366. package/package.json +2 -3
  367. package/out/zero-cache/src/server/backup-watermark-poller.js +0 -31
  368. package/out/zero-cache/src/server/backup-watermark-poller.js.map +0 -1
  369. package/out/zero-cache/src/services/litestream/vfs-watermark-poller.d.ts +0 -57
  370. package/out/zero-cache/src/services/litestream/vfs-watermark-poller.d.ts.map +0 -1
  371. package/out/zero-cache/src/services/litestream/vfs-watermark-poller.js +0 -137
  372. package/out/zero-cache/src/services/litestream/vfs-watermark-poller.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"lite-tables.js","names":[],"sources":["../../../../../zero-cache/src/db/lite-tables.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../shared/src/must.ts';\nimport {difference} from '../../../shared/src/set-utils.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport {primaryKeySchema} from '../../../zero-protocol/src/primary-key.ts';\nimport type {Database} from '../../../zqlite/src/db.ts';\nimport {\n ColumnMetadataStore,\n metadataToLiteTypeString,\n} from '../services/replicator/schema/column-metadata.ts';\nimport {TableMetadataTracker} from '../services/replicator/schema/table-metadata.ts';\nimport {\n isArray,\n isEnum,\n liteTypeToZqlValueType,\n mapLiteDataTypeToZqlSchemaValue,\n nullableUpstream,\n} from '../types/lite.ts';\nimport * as PostgresTypeClass from './postgres-type-class-enum.ts';\nimport type {\n LiteAndZqlSpec,\n LiteIndexSpec,\n LiteTableSpec,\n MutableLiteIndexSpec,\n MutableLiteTableSpec,\n} from './specs.ts';\n\ntype ColumnInfo = {\n table: string;\n name: string;\n type: string;\n notNull: number;\n dflt: string | null;\n keyPos: number;\n};\n\nexport type LiteTableSpecWithReplicationStatus = LiteTableSpec & {\n readonly backfilling?: string[];\n readonly minRowVersion?: string | null;\n};\n\ntype MutableLiteTableSpecWithReplicationStatus = MutableLiteTableSpec & {\n backfilling: string[];\n minRowVersion: string | null;\n};\n\nexport function listTables(\n db: Database,\n useColumnMetadata = true,\n useTableMetadata = true,\n): LiteTableSpecWithReplicationStatus[] {\n const columns = db\n .prepare(\n `\n SELECT\n m.name as \"table\",\n p.name as name,\n p.type as type,\n p.\"notnull\" as \"notNull\",\n p.dflt_value as \"dflt\",\n p.pk as keyPos\n FROM sqlite_master as m\n LEFT JOIN pragma_table_info(m.name) as p\n WHERE m.type = 'table'\n AND m.name NOT LIKE 'sqlite_%'\n AND m.name NOT LIKE '_zero.%'\n AND m.name NOT LIKE '_litestream_%'\n `,\n )\n .all() as ColumnInfo[];\n\n const minRowVersions = useTableMetadata\n ? new TableMetadataTracker(db).getMinRowVersions()\n : new Map();\n const tables: LiteTableSpecWithReplicationStatus[] = [];\n let table: MutableLiteTableSpecWithReplicationStatus | undefined;\n\n columns.forEach(col => {\n if (col.table !== table?.name) {\n // New table\n table = {\n name: col.table,\n columns: {},\n backfilling: [],\n minRowVersion: minRowVersions.get(col.table) ?? null,\n };\n tables.push(table);\n }\n\n // Try to read from metadata table first, fall back to SQLite column type\n let dataType: string;\n let elemPgTypeClass:\n | typeof PostgresTypeClass.Base\n | typeof PostgresTypeClass.Enum\n | null;\n\n const metadata = useColumnMetadata\n ? ColumnMetadataStore.getInstance(db)?.getColumn(col.table, col.name)\n : undefined;\n if (metadata) {\n // Read from metadata table and convert to pipe notation\n dataType = metadataToLiteTypeString(metadata);\n elemPgTypeClass = metadata.isArray\n ? metadata.isEnum\n ? PostgresTypeClass.Enum\n : PostgresTypeClass.Base\n : null;\n } else {\n // Fall back to reading from SQLite column type (pipe notation)\n dataType = col.type;\n elemPgTypeClass = isArray(col.type)\n ? isEnum(col.type)\n ? PostgresTypeClass.Enum\n : PostgresTypeClass.Base\n : null;\n }\n\n table.columns[col.name] = {\n pos: Object.keys(table.columns).length + 1,\n dataType,\n characterMaximumLength: null,\n notNull: col.notNull !== 0,\n dflt: col.dflt,\n elemPgTypeClass,\n };\n if (metadata?.isBackfilling) {\n table.backfilling.push(col.name);\n }\n if (col.keyPos) {\n table.primaryKey ??= [];\n while (table.primaryKey.length < col.keyPos) {\n table.primaryKey.push('');\n }\n table.primaryKey[col.keyPos - 1] = col.name;\n }\n });\n\n return tables;\n}\n\nexport function listIndexes(db: Database): LiteIndexSpec[] {\n const indexes = db\n .prepare(\n `SELECT \n idx.name as indexName, \n idx.tbl_name as tableName, \n info.\"unique\" as \"unique\",\n col.name as column,\n CASE WHEN col.desc = 0 THEN 'ASC' ELSE 'DESC' END as dir\n FROM sqlite_master as idx\n JOIN pragma_index_list(idx.tbl_name) AS info ON info.name = idx.name\n JOIN pragma_index_xinfo(idx.name) as col\n WHERE idx.type = 'index' AND \n col.key = 1 AND\n idx.tbl_name NOT LIKE '_zero.%'\n ORDER BY idx.name, col.seqno ASC`,\n )\n .all() as {\n indexName: string;\n tableName: string;\n unique: number;\n column: string;\n dir: 'ASC' | 'DESC';\n }[];\n\n const ret: MutableLiteIndexSpec[] = [];\n for (const {indexName: name, tableName, unique, column, dir} of indexes) {\n if (ret.at(-1)?.name === name) {\n // Aggregate multiple column names into the array.\n must(ret.at(-1)).columns[column] = dir;\n } else {\n ret.push({\n tableName,\n name,\n columns: {[column]: dir},\n unique: unique !== 0,\n });\n }\n }\n\n return ret;\n}\n\nexport type ZqlSpecOptions = {\n /**\n * Controls whether to include backfilling columns in the computed\n * LiteAndZqlSpec. In general, backfilling columns should be include\n * in \"replication\" logic (copying data from upstream to the replica),\n * and excluded from \"sync\" logic (sending data from the replica to\n * clients).\n */\n includeBackfillingColumns: boolean;\n};\n\n/**\n * Computes a TableSpec \"view\" of the replicated data that is\n * suitable for processing / consumption for the client. This\n * includes:\n * * excluding tables without a PRIMARY KEY or UNIQUE INDEX\n * * excluding columns with types that are not supported by ZQL\n * * choosing columns to use as the primary key amongst those\n * in unique indexes\n *\n * @param tableSpecs an optional map to reset and populate\n * @param fullTables an optional map to receive the full table specs,\n * which may include tables and columns that are not synced to\n * the client because they lack a primary key or are of unsupported\n * data types.\n */\nexport function computeZqlSpecs(\n lc: LogContext,\n replica: Database,\n opts: ZqlSpecOptions,\n tableSpecs: Map<string, LiteAndZqlSpec> = new Map(),\n fullTables?: Map<string, LiteTableSpec>,\n): Map<string, LiteAndZqlSpec> {\n return computeZqlSpecsFromLiteSpecs(\n listTables(replica),\n listIndexes(replica),\n opts,\n tableSpecs,\n fullTables,\n lc,\n );\n}\n\nexport function computeZqlSpecsFromLiteSpecs(\n tables: LiteTableSpecWithReplicationStatus[],\n indexes: LiteIndexSpec[],\n {includeBackfillingColumns}: ZqlSpecOptions,\n tableSpecs: Map<string, LiteAndZqlSpec> = new Map(),\n fullTables?: Map<string, LiteTableSpec>,\n lc?: LogContext,\n): Map<string, LiteAndZqlSpec> {\n tableSpecs.clear();\n fullTables?.clear();\n\n const uniqueIndexColumns = new Map<string, string[][]>();\n for (const {tableName, columns} of indexes.filter(idx => idx.unique)) {\n if (!uniqueIndexColumns.has(tableName)) {\n uniqueIndexColumns.set(tableName, []);\n }\n uniqueIndexColumns.get(tableName)?.push(Object.keys(columns));\n }\n\n tables.forEach(fullTable => {\n fullTables?.set(fullTable.name, fullTable);\n\n const backfilling = new Set(fullTable.backfilling);\n // Only include columns that:\n // - have a defined ZQL Value\n // - aren't backfilling if `includeBackfillingColumns` is false\n const visibleColumns = Object.entries(fullTable.columns).filter(\n ([col, {dataType}]) =>\n liteTypeToZqlValueType(dataType) &&\n (includeBackfillingColumns || !backfilling.has(col)),\n );\n const notNullColumns = new Set(\n visibleColumns\n .filter(\n ([col, {dataType}]) =>\n !nullableUpstream(dataType) || fullTable.primaryKey?.includes(col),\n )\n .map(([col]) => col),\n );\n\n const uniqueKeys = uniqueIndexColumns.get(fullTable.name) ?? [];\n // Examine all column combinations that can serve as a primary key,\n // i.e. excluding indexes over nullable or unsynced columns.\n const keys = uniqueKeys.filter(\n key => difference(new Set(key), notNullColumns).size === 0,\n );\n if (keys.length === 0) {\n // Only include tables with a row key.\n //\n // Note that this will automatically exclude tables that are being\n // backfilled (when includeBackfillingColumns is `false`) since candidate\n // keys only include visible columns.\n lc?.debug?.(\n `not syncing table ${fullTable.name} because it ` +\n (backfilling.size ? 'is being backfilled' : 'has no primary key'),\n );\n return;\n }\n // Pick the \"best\" (i.e. shortest) key for default IVM operations.\n const primaryKey = keys.sort(keyCmp)[0];\n\n const tableSpec = {\n ...fullTable,\n columns: Object.fromEntries(visibleColumns),\n primaryKey: v.parse(primaryKey, primaryKeySchema),\n uniqueKeys: uniqueKeys.map(key => v.parse(key, primaryKeySchema)),\n allPotentialPrimaryKeys: keys.map(key => v.parse(key, primaryKeySchema)),\n minRowVersion: fullTable.minRowVersion ?? null,\n };\n\n tableSpecs.set(tableSpec.name, {\n tableSpec,\n zqlSpec: Object.fromEntries(\n Object.entries(tableSpec.columns).map(([name, {dataType}]) => [\n name,\n mapLiteDataTypeToZqlSchemaValue(dataType),\n ]),\n ),\n });\n });\n return tableSpecs;\n}\n\nexport function mustGetTableSpec(\n tableSpecs: Map<string, LiteAndZqlSpec>,\n tableName: string,\n): LiteAndZqlSpec {\n const tableSpec = tableSpecs.get(tableName);\n if (!tableSpec) {\n throw new Error(\n `table '${tableName}' is not one of: ${[...tableSpecs.keys()]\n .filter(t => !t.includes('.') && !t.startsWith('_litestream_'))\n .sort()}. ` +\n `Check the spelling and ensure that the table has a primary key.`,\n );\n }\n return tableSpec;\n}\n\n// Deterministic comparator for favoring shorter row keys.\nfunction keyCmp(a: string[], b: string[]) {\n if (a.length !== b.length) {\n return a.length - b.length; // Fewer columns are better.\n }\n for (let i = 0; i < a.length; i++) {\n if (a[i] < b[i]) {\n return -1;\n }\n if (a[i] > b[i]) {\n return 1;\n }\n }\n return 0;\n}\n"],"mappings":";;;;;;;;AA8CA,SAAgB,WACd,IACA,oBAAoB,MACpB,mBAAmB,MACmB;CACtC,MAAM,UAAU,GACb,QACC;;;;;;;;;;;;;;OAeF,EACC,IAAI;CAEP,MAAM,iBAAiB,mBACnB,IAAI,qBAAqB,EAAE,EAAE,kBAAkB,oBAC/C,IAAI,IAAI;CACZ,MAAM,SAA+C,CAAC;CACtD,IAAI;CAEJ,QAAQ,SAAQ,QAAO;EACrB,IAAI,IAAI,UAAU,OAAO,MAAM;GAE7B,QAAQ;IACN,MAAM,IAAI;IACV,SAAS,CAAC;IACV,aAAa,CAAC;IACd,eAAe,eAAe,IAAI,IAAI,KAAK,KAAK;GAClD;GACA,OAAO,KAAK,KAAK;EACnB;EAGA,IAAI;EACJ,IAAI;EAKJ,MAAM,WAAW,oBACb,oBAAoB,YAAY,EAAE,GAAG,UAAU,IAAI,OAAO,IAAI,IAAI,IAClE,KAAA;EACJ,IAAI,UAAU;GAEZ,WAAW,yBAAyB,QAAQ;GAC5C,kBAAkB,SAAS,UACvB,SAAS,SACP,MACA,MACF;EACN,OAAO;GAEL,WAAW,IAAI;GACf,kBAAkB,QAAQ,IAAI,IAAI,IAC9B,OAAO,IAAI,IAAI,IACb,MACA,MACF;EACN;EAEA,MAAM,QAAQ,IAAI,QAAQ;GACxB,KAAK,OAAO,KAAK,MAAM,OAAO,EAAE,SAAS;GACzC;GACA,wBAAwB;GACxB,SAAS,IAAI,YAA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U,UAAU,mBAAmB,CAAC,GAAG,WAAW,KAAK,CAAC,EACzD,QAAO,MAAK,CAAC,EAAE,SAAS,GAAG,KAAK,CAAC,EAAE,WAAW,cAAc,CAAC,EAC7D,KAAK,EAAE,kEAEZ;CAEF,OAAO;AACT;AAGA,SAAS,OAAO,GAAa,GAAa;CACxC,IAAI,EAAE,WAAW,EAAE,QACjB,OAAO,EAAE,SAAS,EAAE;CAEtB,KAAK,IAAI,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;EACjC,IAAI,EAAE,KAAK,EAAE,IACX,OAAO;EAET,IAAI,EAAE,KAAK,EAAE,IACX,OAAO;CAEX;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"lite-tables.js","names":[],"sources":["../../../../../zero-cache/src/db/lite-tables.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../shared/src/must.ts';\nimport {difference} from '../../../shared/src/set-utils.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport {primaryKeySchema} from '../../../zero-protocol/src/primary-key.ts';\nimport type {Database} from '../../../zqlite/src/db.ts';\nimport {\n ColumnMetadataStore,\n metadataToLiteTypeString,\n} from '../services/replicator/schema/column-metadata.ts';\nimport {TableMetadataTracker} from '../services/replicator/schema/table-metadata.ts';\nimport {\n isArray,\n isEnum,\n liteTypeToZqlValueType,\n mapLiteDataTypeToZqlSchemaValue,\n nullableUpstream,\n} from '../types/lite.ts';\nimport * as PostgresTypeClass from './postgres-type-class-enum.ts';\nimport type {\n LiteAndZqlSpec,\n LiteIndexSpec,\n LiteTableSpec,\n MutableLiteIndexSpec,\n MutableLiteTableSpec,\n} from './specs.ts';\n\ntype ColumnInfo = {\n table: string;\n name: string;\n type: string;\n notNull: number;\n dflt: string | null;\n keyPos: number;\n};\n\nexport type LiteTableSpecWithReplicationStatus = LiteTableSpec & {\n readonly backfilling?: string[];\n readonly minRowVersion?: string | null;\n};\n\ntype MutableLiteTableSpecWithReplicationStatus = MutableLiteTableSpec & {\n backfilling: string[];\n minRowVersion: string | null;\n};\n\nexport function listTables(\n db: Database,\n useColumnMetadata = true,\n useTableMetadata = true,\n): LiteTableSpecWithReplicationStatus[] {\n const columns = db\n .prepare(\n `\n SELECT\n m.name as \"table\",\n p.name as name,\n p.type as type,\n p.\"notnull\" as \"notNull\",\n p.dflt_value as \"dflt\",\n p.pk as keyPos\n FROM sqlite_master as m\n LEFT JOIN pragma_table_info(m.name) as p\n WHERE m.type = 'table'\n AND m.name NOT LIKE 'sqlite_%'\n AND m.name NOT LIKE '_zero.%'\n AND m.name NOT LIKE '_litestream_%'\n `,\n )\n .all() as ColumnInfo[];\n\n const minRowVersions = useTableMetadata\n ? new TableMetadataTracker(db).getMinRowVersions()\n : new Map();\n const tables: LiteTableSpecWithReplicationStatus[] = [];\n let table: MutableLiteTableSpecWithReplicationStatus | undefined;\n\n columns.forEach(col => {\n if (col.table !== table?.name) {\n // New table\n table = {\n name: col.table,\n columns: {},\n backfilling: [],\n minRowVersion: minRowVersions.get(col.table) ?? null,\n };\n tables.push(table);\n }\n\n // Try to read from metadata table first, fall back to SQLite column type\n let dataType: string;\n let elemPgTypeClass:\n | typeof PostgresTypeClass.Base\n | typeof PostgresTypeClass.Enum\n | null;\n\n const metadata = useColumnMetadata\n ? ColumnMetadataStore.getInstance(db)?.getColumn(col.table, col.name)\n : undefined;\n if (metadata) {\n // Read from metadata table and convert to pipe notation\n dataType = metadataToLiteTypeString(metadata);\n elemPgTypeClass = metadata.isArray\n ? metadata.isEnum\n ? PostgresTypeClass.Enum\n : PostgresTypeClass.Base\n : null;\n } else {\n // Fall back to reading from SQLite column type (pipe notation)\n dataType = col.type;\n elemPgTypeClass = isArray(col.type)\n ? isEnum(col.type)\n ? PostgresTypeClass.Enum\n : PostgresTypeClass.Base\n : null;\n }\n\n table.columns[col.name] = {\n pos: Object.keys(table.columns).length + 1,\n dataType,\n characterMaximumLength: null,\n notNull: col.notNull !== 0,\n dflt: col.dflt,\n elemPgTypeClass,\n };\n if (metadata?.isBackfilling) {\n table.backfilling.push(col.name);\n }\n if (col.keyPos) {\n table.primaryKey ??= [];\n while (table.primaryKey.length < col.keyPos) {\n table.primaryKey.push('');\n }\n table.primaryKey[col.keyPos - 1] = col.name;\n }\n });\n\n return tables;\n}\n\n/**\n * An index as it exists on the replica. SQLite retains only the DDL text of a\n * partial index, not a structured predicate, so replica indexes report a\n * `partial` flag in place of the {@link LiteIndexSpec.predicate}.\n */\nexport type ReplicaIndexSpec = Omit<LiteIndexSpec, 'predicate'> & {\n readonly partial?: boolean | undefined;\n};\n\ntype MutableReplicaIndexSpec = Omit<MutableLiteIndexSpec, 'predicate'> & {\n partial?: boolean | undefined;\n};\n\nexport function listIndexes(db: Database): ReplicaIndexSpec[] {\n const indexes = db\n .prepare(\n `SELECT \n idx.name as indexName, \n idx.tbl_name as tableName, \n info.\"unique\" as \"unique\",\n info.partial as partial,\n col.name as column,\n CASE WHEN col.desc = 0 THEN 'ASC' ELSE 'DESC' END as dir\n FROM sqlite_master as idx\n JOIN pragma_index_list(idx.tbl_name) AS info ON info.name = idx.name\n JOIN pragma_index_xinfo(idx.name) as col\n WHERE idx.type = 'index' AND \n col.key = 1 AND\n idx.tbl_name NOT LIKE '_zero.%'\n ORDER BY idx.name, col.seqno ASC`,\n )\n .all() as {\n indexName: string;\n tableName: string;\n unique: number;\n partial: number;\n column: string;\n dir: 'ASC' | 'DESC';\n }[];\n\n const ret: MutableReplicaIndexSpec[] = [];\n for (const {\n indexName: name,\n tableName,\n unique,\n partial,\n column,\n dir,\n } of indexes) {\n if (ret.at(-1)?.name === name) {\n // Aggregate multiple column names into the array.\n must(ret.at(-1)).columns[column] = dir;\n } else {\n ret.push({\n tableName,\n name,\n columns: {[column]: dir},\n unique: unique !== 0,\n ...(partial === 0 ? {} : {partial: true}),\n });\n }\n }\n\n return ret;\n}\n\nexport type ZqlSpecOptions = {\n /**\n * Controls whether to include backfilling columns in the computed\n * LiteAndZqlSpec. In general, backfilling columns should be include\n * in \"replication\" logic (copying data from upstream to the replica),\n * and excluded from \"sync\" logic (sending data from the replica to\n * clients).\n */\n includeBackfillingColumns: boolean;\n};\n\n/**\n * Computes a TableSpec \"view\" of the replicated data that is\n * suitable for processing / consumption for the client. This\n * includes:\n * * excluding tables without a PRIMARY KEY or UNIQUE INDEX\n * * excluding columns with types that are not supported by ZQL\n * * choosing columns to use as the primary key amongst those\n * in unique indexes\n *\n * @param tableSpecs an optional map to reset and populate\n * @param fullTables an optional map to receive the full table specs,\n * which may include tables and columns that are not synced to\n * the client because they lack a primary key or are of unsupported\n * data types.\n */\nexport function computeZqlSpecs(\n lc: LogContext,\n replica: Database,\n opts: ZqlSpecOptions,\n tableSpecs: Map<string, LiteAndZqlSpec> = new Map(),\n fullTables?: Map<string, LiteTableSpec>,\n): Map<string, LiteAndZqlSpec> {\n return computeZqlSpecsFromLiteSpecs(\n listTables(replica),\n listIndexes(replica),\n opts,\n tableSpecs,\n fullTables,\n lc,\n );\n}\n\nfunction isPartialIndex(idx: LiteIndexSpec | ReplicaIndexSpec): boolean {\n return (\n ('predicate' in idx && idx.predicate !== undefined) ||\n ('partial' in idx && idx.partial === true)\n );\n}\n\nexport function computeZqlSpecsFromLiteSpecs(\n tables: LiteTableSpecWithReplicationStatus[],\n indexes: readonly (LiteIndexSpec | ReplicaIndexSpec)[],\n {includeBackfillingColumns}: ZqlSpecOptions,\n tableSpecs: Map<string, LiteAndZqlSpec> = new Map(),\n fullTables?: Map<string, LiteTableSpec>,\n lc?: LogContext,\n): Map<string, LiteAndZqlSpec> {\n tableSpecs.clear();\n fullTables?.clear();\n\n const uniqueIndexColumns = new Map<string, string[][]>();\n // A partial unique index only constrains the rows matching its predicate,\n // so it cannot serve as a row key.\n for (const {tableName, columns} of indexes.filter(\n idx => idx.unique && !isPartialIndex(idx),\n )) {\n if (!uniqueIndexColumns.has(tableName)) {\n uniqueIndexColumns.set(tableName, []);\n }\n uniqueIndexColumns.get(tableName)?.push(Object.keys(columns));\n }\n\n tables.forEach(fullTable => {\n fullTables?.set(fullTable.name, fullTable);\n\n const backfilling = new Set(fullTable.backfilling);\n // Only include columns that:\n // - have a defined ZQL Value\n // - aren't backfilling if `includeBackfillingColumns` is false\n const visibleColumns = Object.entries(fullTable.columns).filter(\n ([col, {dataType}]) =>\n liteTypeToZqlValueType(dataType) &&\n (includeBackfillingColumns || !backfilling.has(col)),\n );\n const notNullColumns = new Set(\n visibleColumns\n .filter(\n ([col, {dataType}]) =>\n !nullableUpstream(dataType) || fullTable.primaryKey?.includes(col),\n )\n .map(([col]) => col),\n );\n\n const uniqueKeys = uniqueIndexColumns.get(fullTable.name) ?? [];\n // Examine all column combinations that can serve as a primary key,\n // i.e. excluding indexes over nullable or unsynced columns.\n const keys = uniqueKeys.filter(\n key => difference(new Set(key), notNullColumns).size === 0,\n );\n if (keys.length === 0) {\n // Only include tables with a row key.\n //\n // Note that this will automatically exclude tables that are being\n // backfilled (when includeBackfillingColumns is `false`) since candidate\n // keys only include visible columns.\n lc?.debug?.(\n `not syncing table ${fullTable.name} because it ` +\n (backfilling.size ? 'is being backfilled' : 'has no primary key'),\n );\n return;\n }\n // Pick the \"best\" (i.e. shortest) key for default IVM operations.\n const primaryKey = keys.sort(keyCmp)[0];\n\n const tableSpec = {\n ...fullTable,\n columns: Object.fromEntries(visibleColumns),\n primaryKey: v.parse(primaryKey, primaryKeySchema),\n uniqueKeys: uniqueKeys.map(key => v.parse(key, primaryKeySchema)),\n allPotentialPrimaryKeys: keys.map(key => v.parse(key, primaryKeySchema)),\n minRowVersion: fullTable.minRowVersion ?? null,\n };\n\n tableSpecs.set(tableSpec.name, {\n tableSpec,\n zqlSpec: Object.fromEntries(\n Object.entries(tableSpec.columns).map(([name, {dataType}]) => [\n name,\n mapLiteDataTypeToZqlSchemaValue(dataType),\n ]),\n ),\n });\n });\n return tableSpecs;\n}\n\nexport function mustGetTableSpec(\n tableSpecs: Map<string, LiteAndZqlSpec>,\n tableName: string,\n): LiteAndZqlSpec {\n const tableSpec = tableSpecs.get(tableName);\n if (!tableSpec) {\n throw new Error(\n `table '${tableName}' is not one of: ${[...tableSpecs.keys()]\n .filter(t => !t.includes('.') && !t.startsWith('_litestream_'))\n .sort()}. ` +\n `Check the spelling and ensure that the table has a primary key.`,\n );\n }\n return tableSpec;\n}\n\n// Deterministic comparator for favoring shorter row keys.\nfunction keyCmp(a: string[], b: string[]) {\n if (a.length !== b.length) {\n return a.length - b.length; // Fewer columns are better.\n }\n for (let i = 0; i < a.length; i++) {\n if (a[i] < b[i]) {\n return -1;\n }\n if (a[i] > b[i]) {\n return 1;\n }\n }\n return 0;\n}\n"],"mappings":";;;;;;;;AA8CA,SAAgB,WACd,IACA,oBAAoB,MACpB,mBAAmB,MACmB;CACtC,MAAM,UAAU,GACb,QACC;;;;;;;;;;;;;;OAeF,EACC,IAAI;CAEP,MAAM,iBAAiB,mBACnB,IAAI,qBAAqB,EAAE,EAAE,kBAAkB,oBAC/C,IAAI,IAAI;CACZ,MAAM,SAA+C,CAAC;CACtD,IAAI;CAEJ,QAAQ,SAAQ,QAAO;EACrB,IAAI,IAAI,UAAU,OAAO,MAAM;GAE7B,QAAQ;IACN,MAAM,IAAI;IACV,SAAS,CAAC;IACV,aAAa,CAAC;IACd,eAAe,eAAe,IAAI,IAAI,KAAK,KAAK;GAClD;GACA,OAAO,KAAK,KAAK;EACnB;EAGA,IAAI;EACJ,IAAI;EAKJ,MAAM,WAAW,oBACb,oBAAoB,YAAY,EAAE,GAAG,UAAU,IAAI,OAAO,IAAI,IAAI,IAClE,KAAA;EACJ,IAAI,UAAU;GAEZ,WAAW,yBAAyB,QAAQ;GAC5C,kBAAkB,SAAS,UACvB,SAAS,SACP,MACA,MACF;EACN,OAAO;GAEL,WAAW,IAAI;GACf,kBAAkB,QAAQ,IAAI,IAAI,IAC9B,OAAO,IAAI,IAAI,IACb,MACA,MACF;EACN;EAEA,MAAM,QAAQ,IAAI,QAAQ;GACxB,KAAK,OAAO,KAAK,MAAM,OAAO,EAA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ACpC,CAAC,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,UAAU,EACf,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,SAAS,EACzB,uBAAuB,EAAE,uBAAuB,GAC/C,OAAO,CAAC,IAAI,CAAC,CAiIf;AAaD,eAAO,MAAM,cAAc;IACzB;;;;;;OAMG;;IAGH;;;;;;OAMG;;IAGH;;;;OAIG;;aAEH,CAAC;AAGH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AAG5D,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,EAAE,GAAG,cAAc,CAmBxD"}
@@ -46,9 +46,11 @@ async function runSchemaMigrations(log, debugName, dbPath, setupMigration, incre
46
46
  return versions;
47
47
  });
48
48
  }
49
- db.exec("ANALYZE main");
50
- log.info?.("ANALYZE completed");
51
- } else db.pragma("optimize = 0x10002");
49
+ const start = performance.now();
50
+ db.pragma("optimize");
51
+ const elapsed = performance.now() - start;
52
+ log.info?.(`OPTIMIZE completed (${elapsed.toFixed(2)}ms)`);
53
+ }
52
54
  db.pragma("synchronous = NORMAL");
53
55
  db.unsafeMode(false);
54
56
  assert(versions.dataVersion === codeVersion, () => `Final dataVersion (${versions.dataVersion}) does not match codeVersion (${codeVersion})`);
@@ -1 +1 @@
1
- {"version":3,"file":"migration-lite.js","names":[],"sources":["../../../../../zero-cache/src/db/migration-lite.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../shared/src/abort-error.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {randInt} from '../../../shared/src/rand.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport type {Database as Db} from '../../../zqlite/src/db.ts';\nimport {Database} from '../../../zqlite/src/db.ts';\n\ntype Operations = (log: LogContext, tx: Db) => Promise<void> | void;\n\n/**\n * Encapsulates the logic for setting up or upgrading to a new schema. After the\n * Migration code successfully completes, {@link runSchemaMigrations}\n * will update the schema version and commit the transaction.\n */\nexport type Migration = {\n /**\n * Perform database operations that create or alter table structure. This is\n * called at most once during lifetime of the application. If a `migrateData()`\n * operation is defined, that will be performed after `migrateSchema()` succeeds.\n */\n migrateSchema?: Operations;\n\n /**\n * Perform database operations to migrate data to the new schema. This is\n * called after `migrateSchema()` (if defined), and may be called again\n * to re-migrate data after the server was rolled back to an earlier version,\n * and rolled forward again.\n *\n * Consequently, the logic in `migrateData()` must be idempotent.\n */\n migrateData?: Operations;\n\n /**\n * Sets the `minSafeVersion` to the specified value, prohibiting running\n * any earlier code versions.\n */\n minSafeVersion?: number;\n};\n\n/**\n * Mapping of incremental migrations to move from the previous old code\n * version to next one. Versions must be non-zero.\n *\n * The schema resulting from performing incremental migrations should be\n * equivalent to that of the `setupMigration` on a blank database.\n *\n * The highest destinationVersion of this map denotes the current\n * \"code version\", and is also used as the destination version when\n * running the initial setup migration on a blank database.\n */\nexport type IncrementalMigrationMap = {\n [destinationVersion: number]: Migration;\n};\n\n/**\n * Ensures that the schema is compatible with the current code, updating and\n * migrating the schema if necessary.\n */\nexport async function runSchemaMigrations(\n log: LogContext,\n debugName: string,\n dbPath: string,\n setupMigration: Migration,\n incrementalMigrationMap: IncrementalMigrationMap,\n): Promise<void> {\n const start = Date.now();\n log = log.withContext(\n 'initSchema',\n randInt(0, Number.MAX_SAFE_INTEGER).toString(36),\n );\n const db = new Database(log, dbPath);\n\n try {\n const versionMigrations = sorted(incrementalMigrationMap);\n assert(\n versionMigrations.length,\n `Must specify a at least one version migration`,\n );\n assert(\n versionMigrations[0][0] > 0,\n `Versions must be non-zero positive numbers`,\n );\n // oxlint-disable-next-line typescript/no-non-null-assertion\n const codeVersion = versionMigrations.at(-1)![0];\n log.info?.(\n `Checking schema for compatibility with ${debugName} at schema v${codeVersion}`,\n );\n\n let versions = await runTransaction(log, db, tx => {\n const versions = getVersionHistory(tx);\n if (codeVersion < versions.minSafeVersion) {\n throw new Error(\n `Cannot run ${debugName} at schema v${codeVersion} because rollback limit is v${versions.minSafeVersion}`,\n );\n }\n\n if (versions.dataVersion > codeVersion) {\n log.info?.(\n `Data is at v${versions.dataVersion}. Resetting to v${codeVersion}`,\n );\n return updateVersionHistory(log, tx, versions, codeVersion);\n }\n return versions;\n });\n if (versions.dataVersion < codeVersion) {\n db.unsafeMode(true); // Enables journal_mode = OFF\n db.pragma('locking_mode = EXCLUSIVE');\n db.pragma('foreign_keys = OFF');\n db.pragma('journal_mode = OFF');\n db.pragma('synchronous = OFF');\n // Unfortunately, AUTO_VACUUM is not compatible with BEGIN CONCURRENT,\n // so it is not an option for the replica file.\n // https://sqlite.org/forum/forumpost/25f183416a\n // db.pragma('auto_vacuum = INCREMENTAL');\n\n const migrations =\n versions.dataVersion === 0\n ? // For the empty database v0, only run the setup migration.\n ([[codeVersion, setupMigration]] as const)\n : versionMigrations;\n\n for (const [dest, migration] of migrations) {\n if (versions.dataVersion < dest) {\n log.info?.(\n `Migrating schema from v${versions.dataVersion} to v${dest}`,\n );\n void log.flush(); // Flush logs before each migration to help debug crash-y migrations.\n\n versions = await runTransaction(log, db, async tx => {\n // Fetch meta from within the transaction to make the migration atomic.\n let versions = getVersionHistory(tx);\n if (versions.dataVersion < dest) {\n versions = await runMigration(log, tx, versions, dest, migration);\n assert(\n versions.dataVersion === dest,\n () =>\n `Migration did not reach target version: expected ${dest}, got ${versions.dataVersion}`,\n );\n }\n return versions;\n });\n }\n }\n\n db.exec('ANALYZE main');\n log.info?.('ANALYZE completed');\n } else {\n // Run optimize whenever opening an sqlite db file as recommended in\n // https://www.sqlite.org/pragma.html#pragma_optimize\n // It is important to run the same initialization steps as is done\n // in the view-syncer (i.e. when preparing database for serving\n // replication) so that any corruption detected in the view-syncer is\n // similarly detected in the change-streamer, facilitating an eventual\n // recovery by resyncing the replica anew.\n db.pragma('optimize = 0x10002');\n // Do not run `quick_check` here. It scans the full database and can\n // dominate startup time for large replicas or slower disks.\n }\n\n db.pragma('synchronous = NORMAL');\n db.unsafeMode(false);\n\n assert(\n versions.dataVersion === codeVersion,\n () =>\n `Final dataVersion (${versions.dataVersion}) does not match codeVersion (${codeVersion})`,\n );\n log.info?.(\n `Running ${debugName} at schema v${codeVersion} (${\n Date.now() - start\n } ms)`,\n );\n } catch (e) {\n if (e instanceof AbortError) {\n // AbortErrors (e.g. AutoResetSignal) are not failures. They are the\n // expected mechanism for signaling that the replica needs a reset (e.g.\n // corruption or an incompatible schema version) so that the caller can\n // resync anew. Logging them at `error` would trip error-based alerting\n // for a normal, self-healing path.\n log.warn?.('Replica requires reset; resync will follow', e);\n } else {\n log.error?.('Error in ensureSchemaMigrated', e);\n }\n throw e;\n } finally {\n db.close();\n void log.flush(); // Flush the logs but do not block server progress on it.\n }\n}\n\nfunction sorted(\n incrementalMigrationMap: IncrementalMigrationMap,\n): [number, Migration][] {\n const versionMigrations: [number, Migration][] = [];\n for (const [v, m] of Object.entries(incrementalMigrationMap)) {\n versionMigrations.push([Number(v), m]);\n }\n return versionMigrations.sort(([a], [b]) => a - b);\n}\n\n// Exposed for tests.\nexport const versionHistory = v.object({\n /**\n * The `schemaVersion` is highest code version that has ever been run\n * on the database, and is used to delineate the structure of the tables\n * in the database. A schemaVersion only moves forward; rolling back to\n * an earlier (safe) code version does not revert schema changes that\n * have already been applied.\n */\n schemaVersion: v.number(),\n\n /**\n * The data version is the code version of the latest server that ran.\n * Note that this may be less than the schemaVersion in the case that\n * a server is rolled back to an earlier version after a schema change.\n * In such a case, data (but not schema), may need to be re-migrated\n * when rolling forward again.\n */\n dataVersion: v.number(),\n\n /**\n * The minimum code version that is safe to run. This is used when\n * a schema migration is not backwards compatible with an older version\n * of the code.\n */\n minSafeVersion: v.number(),\n});\n\n// Exposed for tests.\nexport type VersionHistory = v.Infer<typeof versionHistory>;\n\n// Exposed for tests\nexport function getVersionHistory(db: Db): VersionHistory {\n // Note: The `lock` column transparently ensures that at most one row exists.\n db.prepare(\n `\n CREATE TABLE IF NOT EXISTS \"_zero.versionHistory\" (\n dataVersion INTEGER NOT NULL,\n schemaVersion INTEGER NOT NULL,\n minSafeVersion INTEGER NOT NULL,\n\n lock INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n `,\n ).run();\n const result = db\n .prepare(\n 'SELECT dataVersion, schemaVersion, minSafeVersion FROM \"_zero.versionHistory\"',\n )\n .get() as VersionHistory;\n return result ?? {dataVersion: 0, schemaVersion: 0, minSafeVersion: 0};\n}\n\nfunction updateVersionHistory(\n log: LogContext,\n db: Db,\n prev: VersionHistory,\n newVersion: number,\n minSafeVersion?: number,\n): VersionHistory {\n assert(newVersion > 0, 'newVersion must be positive');\n const meta = {\n ...prev,\n dataVersion: newVersion,\n // The schemaVersion never moves backwards.\n schemaVersion: Math.max(newVersion, prev.schemaVersion),\n minSafeVersion: getMinSafeVersion(log, prev, minSafeVersion),\n } satisfies VersionHistory;\n\n db.prepare(\n `\n INSERT INTO \"_zero.versionHistory\" (dataVersion, schemaVersion, minSafeVersion, lock)\n VALUES (@dataVersion, @schemaVersion, @minSafeVersion, 1)\n ON CONFLICT (lock) DO UPDATE\n SET dataVersion=@dataVersion,\n schemaVersion=@schemaVersion,\n minSafeVersion=@minSafeVersion\n `,\n ).run(meta);\n\n return meta;\n}\n\nasync function runMigration(\n log: LogContext,\n tx: Db,\n versions: VersionHistory,\n destinationVersion: number,\n migration: Migration,\n): Promise<VersionHistory> {\n if (versions.schemaVersion < destinationVersion) {\n await migration.migrateSchema?.(log, tx);\n }\n if (versions.dataVersion < destinationVersion) {\n await migration.migrateData?.(log, tx);\n }\n return updateVersionHistory(\n log,\n tx,\n versions,\n destinationVersion,\n migration.minSafeVersion,\n );\n}\n\n/**\n * Bumps the rollback limit [[toAtLeast]] the specified version.\n * Leaves the rollback limit unchanged if it is equal or greater.\n */\nfunction getMinSafeVersion(\n log: LogContext,\n current: VersionHistory,\n proposedSafeVersion?: number,\n): number {\n if (proposedSafeVersion === undefined) {\n return current.minSafeVersion;\n }\n if (current.minSafeVersion >= proposedSafeVersion) {\n // The rollback limit must never move backwards.\n log.debug?.(\n `rollback limit is already at ${current.minSafeVersion}, ` +\n `don't need to bump to ${proposedSafeVersion}`,\n );\n return current.minSafeVersion;\n }\n log.info?.(\n `bumping rollback limit from ${current.minSafeVersion} to ${proposedSafeVersion}`,\n );\n return proposedSafeVersion;\n}\n\n// Note: We use a custom transaction wrapper (instead of db.begin(...)) in order\n// to support async operations within the transaction.\nasync function runTransaction<T>(\n log: LogContext,\n db: Db,\n tx: (db: Db) => Promise<T> | T,\n): Promise<T> {\n db.prepare('BEGIN EXCLUSIVE').run();\n try {\n const result = await tx(db);\n db.prepare('COMMIT').run();\n return result;\n } catch (e) {\n if (e instanceof AbortError) {\n // AbortErrors (e.g. AutoResetSignal) are an expected, self-healing\n // control-flow signal, not a failure. Log at `warn` so the rollback is\n // still visible without tripping error-based alerting.\n log.warn?.('Aborted transaction for reset signal', e);\n } else {\n log.error?.('Aborted transaction due to error', e);\n }\n try {\n db.prepare('ROLLBACK').run();\n } catch (rollbackError) {\n log.error?.('Unable to rollback transaction', rollbackError);\n const combinedError = new Error(\n `Transaction failed and rollback also failed: operation error = ${String(\n e,\n )}; rollback error = ${String(rollbackError)}`,\n );\n combinedError.cause = e;\n throw combinedError;\n }\n throw e;\n }\n}\n"],"mappings":";;;;;;;;;;AA2DA,eAAsB,oBACpB,KACA,WACA,QACA,gBACA,yBACe;CACf,MAAM,QAAQ,KAAK,IAAI;CACvB,MAAM,IAAI,YACR,cACA,QAAQ,GAAG,OAAO,gBAAgB,EAAE,SAAS,EAAE,CACjD;CACA,MAAM,KAAK,IAAI,SAAS,KAAK,MAAM;CAEnC,IAAI;EACF,MAAM,oBAAoB,OAAO,uBAAuB;EACxD,OACE,kBAAkB,QAClB,+CACF;EACA,OACE,kBAAkB,GAAG,KAAK,GAC1B,4CACF;EAEA,MAAM,cAAc,kBAAk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;CAEvC,OAAO,qBACL,KACA,IACA,UACA,oBACA,UAAU,cACZ;AACF;;;;;AAMA,SAAS,kBACP,KACA,SACA,qBACQ;CACR,IAAI,wBAAwB,KAAA,GAC1B,OAAO,QAAQ;CAEjB,IAAI,QAAQ,kBAAkB,qBAAqB;EAEjD,IAAI,QACF,gCAAgC,QAAQ,eAAe,0BAC5B,qBAC7B;EACA,OAAO,QAAQ;CACjB;CACA,IAAI,OACF,+BAA+B,QAAQ,eAAe,MAAM,qBAC9D;CACA,OAAO;AACT;AAIA,eAAe,eACb,KACA,IACA,IACY;CACZ,GAAG,QAAQ,iBAAiB,EAAE,IAAI;CAClC,IAAI;EACF,MAAM,SAAS,MAAM,GAAG,EAAE;EAC1B,GAAG,QAAQ,QAAQ,EAAE,IAAI;EACzB,OAAO;CACT,SAAS,GAAG;EACV,IAAI,aAAa,YAIf,IAAI,OAAO,wCAAwC,CAAC;OAEpD,IAAI,QAAQ,oCAAoC,CAAC;EAEnD,IAAI;GACF,GAAG,QAAQ,UAAU,EAAE,IAAI;EAC7B,SAAS,eAAe;GACtB,IAAI,QAAQ,kCAAkC,aAAa;GAC3D,MAAM,gCAAgB,IAAI,MACxB,kEAAkE,OAChE,CACF,EAAE,qBAAqB,OAAO,aAAa,GAC7C;GACA,cAAc,QAAQ;GACtB,MAAM;EACR;EACA,MAAM;CACR;AACF"}
1
+ {"version":3,"file":"migration-lite.js","names":[],"sources":["../../../../../zero-cache/src/db/migration-lite.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {AbortError} from '../../../shared/src/abort-error.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {randInt} from '../../../shared/src/rand.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport type {Database as Db} from '../../../zqlite/src/db.ts';\nimport {Database} from '../../../zqlite/src/db.ts';\n\ntype Operations = (log: LogContext, tx: Db) => Promise<void> | void;\n\n/**\n * Encapsulates the logic for setting up or upgrading to a new schema. After the\n * Migration code successfully completes, {@link runSchemaMigrations}\n * will update the schema version and commit the transaction.\n */\nexport type Migration = {\n /**\n * Perform database operations that create or alter table structure. This is\n * called at most once during lifetime of the application. If a `migrateData()`\n * operation is defined, that will be performed after `migrateSchema()` succeeds.\n */\n migrateSchema?: Operations;\n\n /**\n * Perform database operations to migrate data to the new schema. This is\n * called after `migrateSchema()` (if defined), and may be called again\n * to re-migrate data after the server was rolled back to an earlier version,\n * and rolled forward again.\n *\n * Consequently, the logic in `migrateData()` must be idempotent.\n */\n migrateData?: Operations;\n\n /**\n * Sets the `minSafeVersion` to the specified value, prohibiting running\n * any earlier code versions.\n */\n minSafeVersion?: number;\n};\n\n/**\n * Mapping of incremental migrations to move from the previous old code\n * version to next one. Versions must be non-zero.\n *\n * The schema resulting from performing incremental migrations should be\n * equivalent to that of the `setupMigration` on a blank database.\n *\n * The highest destinationVersion of this map denotes the current\n * \"code version\", and is also used as the destination version when\n * running the initial setup migration on a blank database.\n */\nexport type IncrementalMigrationMap = {\n [destinationVersion: number]: Migration;\n};\n\n/**\n * Ensures that the schema is compatible with the current code, updating and\n * migrating the schema if necessary.\n */\nexport async function runSchemaMigrations(\n log: LogContext,\n debugName: string,\n dbPath: string,\n setupMigration: Migration,\n incrementalMigrationMap: IncrementalMigrationMap,\n): Promise<void> {\n const start = Date.now();\n log = log.withContext(\n 'initSchema',\n randInt(0, Number.MAX_SAFE_INTEGER).toString(36),\n );\n const db = new Database(log, dbPath);\n\n try {\n const versionMigrations = sorted(incrementalMigrationMap);\n assert(\n versionMigrations.length,\n `Must specify a at least one version migration`,\n );\n assert(\n versionMigrations[0][0] > 0,\n `Versions must be non-zero positive numbers`,\n );\n // oxlint-disable-next-line typescript/no-non-null-assertion\n const codeVersion = versionMigrations.at(-1)![0];\n log.info?.(\n `Checking schema for compatibility with ${debugName} at schema v${codeVersion}`,\n );\n\n let versions = await runTransaction(log, db, tx => {\n const versions = getVersionHistory(tx);\n if (codeVersion < versions.minSafeVersion) {\n throw new Error(\n `Cannot run ${debugName} at schema v${codeVersion} because rollback limit is v${versions.minSafeVersion}`,\n );\n }\n\n if (versions.dataVersion > codeVersion) {\n log.info?.(\n `Data is at v${versions.dataVersion}. Resetting to v${codeVersion}`,\n );\n return updateVersionHistory(log, tx, versions, codeVersion);\n }\n return versions;\n });\n if (versions.dataVersion < codeVersion) {\n db.unsafeMode(true); // Enables journal_mode = OFF\n db.pragma('locking_mode = EXCLUSIVE');\n db.pragma('foreign_keys = OFF');\n db.pragma('journal_mode = OFF');\n db.pragma('synchronous = OFF');\n // Unfortunately, AUTO_VACUUM is not compatible with BEGIN CONCURRENT,\n // so it is not an option for the replica file.\n // https://sqlite.org/forum/forumpost/25f183416a\n // db.pragma('auto_vacuum = INCREMENTAL');\n\n const migrations =\n versions.dataVersion === 0\n ? // For the empty database v0, only run the setup migration.\n ([[codeVersion, setupMigration]] as const)\n : versionMigrations;\n\n for (const [dest, migration] of migrations) {\n if (versions.dataVersion < dest) {\n log.info?.(\n `Migrating schema from v${versions.dataVersion} to v${dest}`,\n );\n void log.flush(); // Flush logs before each migration to help debug crash-y migrations.\n\n versions = await runTransaction(log, db, async tx => {\n // Fetch meta from within the transaction to make the migration atomic.\n let versions = getVersionHistory(tx);\n if (versions.dataVersion < dest) {\n versions = await runMigration(log, tx, versions, dest, migration);\n assert(\n versions.dataVersion === dest,\n () =>\n `Migration did not reach target version: expected ${dest}, got ${versions.dataVersion}`,\n );\n }\n return versions;\n });\n }\n }\n\n // Note: This used to be ANALYZE but that takes a long time for large\n // dbs. The SQLite docs generally recommend optimize instead.\n //\n // https://sqlite.org/pragma.html#pragma_analysis_limit\n //\n // Beginning with SQLite version 3.46.0 (2024-05-23), the recommended\n // way of running ANALYZE is with the PRAGMA optimize command.\n // The PRAGMA optimize will automatically set a reasonable, temporary\n // analysis limit that ensures that the PRAGMA optimize command will\n // finish quickly even on enormous databases. Applications that use\n // the PRAGMA optimize instead of running ANALYZE directly do not need\n // to set an analysis limit.\n const start = performance.now();\n db.pragma('optimize');\n const elapsed = performance.now() - start;\n log.info?.(`OPTIMIZE completed (${elapsed.toFixed(2)}ms)`);\n }\n\n // Do not run `quick_check` here. It scans the full database and can\n // dominate startup time for large replicas or slower disks.\n db.pragma('synchronous = NORMAL');\n db.unsafeMode(false);\n\n assert(\n versions.dataVersion === codeVersion,\n () =>\n `Final dataVersion (${versions.dataVersion}) does not match codeVersion (${codeVersion})`,\n );\n log.info?.(\n `Running ${debugName} at schema v${codeVersion} (${\n Date.now() - start\n } ms)`,\n );\n } catch (e) {\n if (e instanceof AbortError) {\n // AbortErrors (e.g. AutoResetSignal) are not failures. They are the\n // expected mechanism for signaling that the replica needs a reset (e.g.\n // corruption or an incompatible schema version) so that the caller can\n // resync anew. Logging them at `error` would trip error-based alerting\n // for a normal, self-healing path.\n log.warn?.('Replica requires reset; resync will follow', e);\n } else {\n log.error?.('Error in ensureSchemaMigrated', e);\n }\n throw e;\n } finally {\n db.close();\n void log.flush(); // Flush the logs but do not block server progress on it.\n }\n}\n\nfunction sorted(\n incrementalMigrationMap: IncrementalMigrationMap,\n): [number, Migration][] {\n const versionMigrations: [number, Migration][] = [];\n for (const [v, m] of Object.entries(incrementalMigrationMap)) {\n versionMigrations.push([Number(v), m]);\n }\n return versionMigrations.sort(([a], [b]) => a - b);\n}\n\n// Exposed for tests.\nexport const versionHistory = v.object({\n /**\n * The `schemaVersion` is highest code version that has ever been run\n * on the database, and is used to delineate the structure of the tables\n * in the database. A schemaVersion only moves forward; rolling back to\n * an earlier (safe) code version does not revert schema changes that\n * have already been applied.\n */\n schemaVersion: v.number(),\n\n /**\n * The data version is the code version of the latest server that ran.\n * Note that this may be less than the schemaVersion in the case that\n * a server is rolled back to an earlier version after a schema change.\n * In such a case, data (but not schema), may need to be re-migrated\n * when rolling forward again.\n */\n dataVersion: v.number(),\n\n /**\n * The minimum code version that is safe to run. This is used when\n * a schema migration is not backwards compatible with an older version\n * of the code.\n */\n minSafeVersion: v.number(),\n});\n\n// Exposed for tests.\nexport type VersionHistory = v.Infer<typeof versionHistory>;\n\n// Exposed for tests\nexport function getVersionHistory(db: Db): VersionHistory {\n // Note: The `lock` column transparently ensures that at most one row exists.\n db.prepare(\n `\n CREATE TABLE IF NOT EXISTS \"_zero.versionHistory\" (\n dataVersion INTEGER NOT NULL,\n schemaVersion INTEGER NOT NULL,\n minSafeVersion INTEGER NOT NULL,\n\n lock INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n `,\n ).run();\n const result = db\n .prepare(\n 'SELECT dataVersion, schemaVersion, minSafeVersion FROM \"_zero.versionHistory\"',\n )\n .get() as VersionHistory;\n return result ?? {dataVersion: 0, schemaVersion: 0, minSafeVersion: 0};\n}\n\nfunction updateVersionHistory(\n log: LogContext,\n db: Db,\n prev: VersionHistory,\n newVersion: number,\n minSafeVersion?: number,\n): VersionHistory {\n assert(newVersion > 0, 'newVersion must be positive');\n const meta = {\n ...prev,\n dataVersion: newVersion,\n // The schemaVersion never moves backwards.\n schemaVersion: Math.max(newVersion, prev.schemaVersion),\n minSafeVersion: getMinSafeVersion(log, prev, minSafeVersion),\n } satisfies VersionHistory;\n\n db.prepare(\n `\n INSERT INTO \"_zero.versionHistory\" (dataVersion, schemaVersion, minSafeVersion, lock)\n VALUES (@dataVersion, @schemaVersion, @minSafeVersion, 1)\n ON CONFLICT (lock) DO UPDATE\n SET dataVersion=@dataVersion,\n schemaVersion=@schemaVersion,\n minSafeVersion=@minSafeVersion\n `,\n ).run(meta);\n\n return meta;\n}\n\nasync function runMigration(\n log: LogContext,\n tx: Db,\n versions: VersionHistory,\n destinationVersion: number,\n migration: Migration,\n): Promise<VersionHistory> {\n if (versions.schemaVersion < destinationVersion) {\n await migration.migrateSchema?.(log, tx);\n }\n if (versions.dataVersion < destinationVersion) {\n await migration.migrateData?.(log, tx);\n }\n return updateVersionHistory(\n log,\n tx,\n versions,\n destinationVersion,\n migration.minSafeVersion,\n );\n}\n\n/**\n * Bumps the rollback limit [[toAtLeast]] the specified version.\n * Leaves the rollback limit unchanged if it is equal or greater.\n */\nfunction getMinSafeVersion(\n log: LogContext,\n current: VersionHistory,\n proposedSafeVersion?: number,\n): number {\n if (proposedSafeVersion === undefined) {\n return current.minSafeVersion;\n }\n if (current.minSafeVersion >= proposedSafeVersion) {\n // The rollback limit must never move backwards.\n log.debug?.(\n `rollback limit is already at ${current.minSafeVersion}, ` +\n `don't need to bump to ${proposedSafeVersion}`,\n );\n return current.minSafeVersion;\n }\n log.info?.(\n `bumping rollback limit from ${current.minSafeVersion} to ${proposedSafeVersion}`,\n );\n return proposedSafeVersion;\n}\n\n// Note: We use a custom transaction wrapper (instead of db.begin(...)) in order\n// to support async operations within the transaction.\nasync function runTransaction<T>(\n log: LogContext,\n db: Db,\n tx: (db: Db) => Promise<T> | T,\n): Promise<T> {\n db.prepare('BEGIN EXCLUSIVE').run();\n try {\n const result = await tx(db);\n db.prepare('COMMIT').run();\n return result;\n } catch (e) {\n if (e instanceof AbortError) {\n // AbortErrors (e.g. AutoResetSignal) are an expected, self-healing\n // control-flow signal, not a failure. Log at `warn` so the rollback is\n // still visible without tripping error-based alerting.\n log.warn?.('Aborted transaction for reset signal', e);\n } else {\n log.error?.('Aborted transaction due to error', e);\n }\n try {\n db.prepare('ROLLBACK').run();\n } catch (rollbackError) {\n log.error?.('Unable to rollback transaction', rollbackError);\n const combinedError = new Error(\n `Transaction failed and rollback also failed: operation error = ${String(\n e,\n )}; rollback error = ${String(rollbackError)}`,\n );\n combinedError.cause = e;\n throw combinedError;\n }\n throw e;\n }\n}\n"],"mappings":";;;;;;;;;;AA2DA,eAAsB,oBACpB,KACA,WACA,QACA,gBACA,yBACe;CACf,MAAM,QAAQ,KAAK,IAAI;CACvB,MAAM,IAAI,YACR,cACA,QAAQ,GAAG,OAAO,gBAAgB,EAAE,SAAS,EAAE,CACjD;CACA,MAAM,KAAK,IAAI,SAAS,KAAK,MAAM;CAEnC,IAAI;EACF,MAAM,oBAAoB,OAAO,uBAAuB;EACxD,OACE,kBAA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b,KACA,IACA,UACA,oBACA,WACyB;CACzB,IAAI,SAAS,gBAAgB,oBAC3B,MAAM,UAAU,gBAAgB,KAAK,EAAE;CAEzC,IAAI,SAAS,cAAc,oBACzB,MAAM,UAAU,cAAc,KAAK,EAAE;CAEvC,OAAO,qBACL,KACA,IACA,UACA,oBACA,UAAU,cACZ;AACF;;;;;AAMA,SAAS,kBACP,KACA,SACA,qBACQ;CACR,IAAI,wBAAwB,KAAA,GAC1B,OAAO,QAAQ;CAEjB,IAAI,QAAQ,kBAAkB,qBAAqB;EAEjD,IAAI,QACF,gCAAgC,QAAQ,eAAe,0BAC5B,qBAC7B;EACA,OAAO,QAAQ;CACjB;CACA,IAAI,OACF,+BAA+B,QAAQ,eAAe,MAAM,qBAC9D;CACA,OAAO;AACT;AAIA,eAAe,eACb,KACA,IACA,IACY;CACZ,GAAG,QAAQ,iBAAiB,EAAE,IAAI;CAClC,IAAI;EACF,MAAM,SAAS,MAAM,GAAG,EAAE;EAC1B,GAAG,QAAQ,QAAQ,EAAE,IAAI;EACzB,OAAO;CACT,SAAS,GAAG;EACV,IAAI,aAAa,YAIf,IAAI,OAAO,wCAAwC,CAAC;OAEpD,IAAI,QAAQ,oCAAoC,CAAC;EAEnD,IAAI;GACF,GAAG,QAAQ,UAAU,EAAE,IAAI;EAC7B,SAAS,eAAe;GACtB,IAAI,QAAQ,kCAAkC,aAAa;GAC3D,MAAM,gCAAgB,IAAI,MACxB,kEAAkE,OAChE,CACF,EAAE,qBAAqB,OAAO,aAAa,GAC7C;GACA,cAAc,QAAQ;GACtB,MAAM;EACR;EACA,MAAM;CACR;AACF"}
@@ -13,6 +13,17 @@ export declare const columnSpec: v.ObjectType<{
13
13
  dflt: v.Optional<string | null>;
14
14
  }, undefined>;
15
15
  export type ColumnSpec = Readonly<v.Infer<typeof columnSpec>>;
16
+ export declare const publishedColumnSpec: v.ObjectType<Omit<{
17
+ pos: v.Type<number>;
18
+ dataType: v.Type<string>;
19
+ pgTypeClass: v.Optional<"e" | "d" | "b" | "c" | "p" | "r" | "m">;
20
+ elemPgTypeClass: v.Optional<"e" | "d" | "b" | "c" | "p" | "r" | "m" | null>;
21
+ characterMaximumLength: v.Optional<number | null>;
22
+ notNull: v.Optional<boolean | null>;
23
+ dflt: v.Optional<string | null>;
24
+ }, "typeOID"> & {
25
+ typeOID: v.Type<number>;
26
+ }, undefined>;
16
27
  export declare const liteTableSpec: v.ObjectType<{
17
28
  name: v.Type<string>;
18
29
  columns: v.Type<Record<string, {
@@ -106,11 +117,39 @@ export type LiteAndZqlSpec = {
106
117
  export type TableSpec = Readonly<v.Infer<typeof tableSpec>>;
107
118
  export type PublishedTableSpec = Readonly<v.Infer<typeof publishedTableSpec>>;
108
119
  export declare const directionSchema: v.Type<"ASC" | "DESC">;
120
+ export type IndexPredicateValue = {
121
+ readonly type: 'boolean';
122
+ readonly value: boolean;
123
+ } | {
124
+ readonly type: 'integer';
125
+ readonly value: string;
126
+ } | {
127
+ readonly type: 'string';
128
+ readonly value: string;
129
+ };
130
+ export type IndexPredicate = {
131
+ readonly type: 'and' | 'or';
132
+ readonly conditions: readonly IndexPredicate[];
133
+ } | {
134
+ readonly type: 'not';
135
+ readonly condition: IndexPredicate;
136
+ } | {
137
+ readonly type: 'null-test';
138
+ readonly column: string;
139
+ readonly op: 'IS NULL' | 'IS NOT NULL';
140
+ } | {
141
+ readonly type: 'comparison';
142
+ readonly column: string;
143
+ readonly op: '=' | '<>' | '<' | '<=' | '>' | '>=';
144
+ readonly value: IndexPredicateValue;
145
+ };
146
+ export declare const indexPredicateSchema: v.Type<IndexPredicate>;
109
147
  export declare const liteIndexSpec: v.ObjectType<{
110
148
  name: v.Type<string>;
111
149
  tableName: v.Type<string>;
112
150
  unique: v.Type<boolean>;
113
151
  columns: v.Type<Record<string, "ASC" | "DESC">>;
152
+ predicate: v.Optional<IndexPredicate>;
114
153
  }, undefined>;
115
154
  export type MutableLiteIndexSpec = v.Infer<typeof liteIndexSpec>;
116
155
  export type LiteIndexSpec = Readonly<MutableLiteIndexSpec>;
@@ -119,6 +158,7 @@ export declare const indexSpec: v.ObjectType<Omit<{
119
158
  tableName: v.Type<string>;
120
159
  unique: v.Type<boolean>;
121
160
  columns: v.Type<Record<string, "ASC" | "DESC">>;
161
+ predicate: v.Optional<IndexPredicate>;
122
162
  }, "schema"> & {
123
163
  schema: v.Type<string>;
124
164
  }, undefined>;
@@ -128,6 +168,7 @@ export declare const publishedIndexSpec: v.ObjectType<Omit<Omit<{
128
168
  tableName: v.Type<string>;
129
169
  unique: v.Type<boolean>;
130
170
  columns: v.Type<Record<string, "ASC" | "DESC">>;
171
+ predicate: v.Optional<IndexPredicate>;
131
172
  }, "schema"> & {
132
173
  schema: v.Type<string>;
133
174
  }, "isReplicaIdentity" | "isPrimaryKey" | "isImmediate"> & {
@@ -1 +1 @@
1
- {"version":3,"file":"specs.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/db/specs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,CAAC,MAAM,+BAA+B,CAAC;AACnD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,2CAA2C,CAAC;AAC1E,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0CAA0C,CAAC;AAI1E,eAAO,MAAM,iBAAiB,iDAQ7B,CAAC;AASF,eAAO,MAAM,UAAU;;;;;;;;aAarB,CAAC;AAEH,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC;AAM9D,eAAO,MAAM,aAAa;;;;;;;;;;;;aAIxB,CAAC;AAEH,eAAO,MAAM,SAAS;;;;;;;;;;;;;;aAEpB,CAAC;AAEH,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAQ7B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CA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AAI,CAAC;IAClD,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;CACrC,CAAC;AAQN,eAAO,MAAM,oBAAoB,EAAE,CAAC,CAAC,IAAI,CAAC,cAAc,CAsBvD,CAAC;AAEF,eAAO,MAAM,aAAa;;;;;;aAMxB,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAEjE,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,oBAAoB,CAAC,CAAC;AAE3D,eAAO,MAAM,SAAS;;;;;;;;aAEpB,CAAC;AAEH,MAAM,MAAM,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC;AAEhE,eAAO,MAAM,kBAAkB;;;;;;;;;;;;aAI7B,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,YAAY,CAC3C,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CACnC,CAAC"}
@@ -1,4 +1,4 @@
1
- import { literalUnion, valita_exports } from "../../../shared/src/valita.js";
1
+ import { literalUnion, readonlyArray, readonlyObject, valita_exports } from "../../../shared/src/valita.js";
2
2
  //#region ../zero-cache/src/db/specs.ts
3
3
  var pgTypeClassSchema = literalUnion("b", "c", "d", "e", "p", "r", "m");
4
4
  var pgReplicaIdentitySchema = literalUnion("d", "n", "f", "i");
@@ -25,11 +25,38 @@ var publishedTableSpec = tableSpec.extend({
25
25
  publications: valita_exports.record(valita_exports.object({ rowFilter: valita_exports.string().nullable() }))
26
26
  });
27
27
  var directionSchema = literalUnion("ASC", "DESC");
28
+ var indexPredicateValueSchema = valita_exports.union(readonlyObject({
29
+ type: valita_exports.literal("boolean"),
30
+ value: valita_exports.boolean()
31
+ }), readonlyObject({
32
+ type: valita_exports.literal("integer"),
33
+ value: valita_exports.string()
34
+ }), readonlyObject({
35
+ type: valita_exports.literal("string"),
36
+ value: valita_exports.string()
37
+ }));
38
+ var indexPredicateSchema = valita_exports.lazy(() => valita_exports.union(readonlyObject({
39
+ type: literalUnion("and", "or"),
40
+ conditions: readonlyArray(indexPredicateSchema)
41
+ }), readonlyObject({
42
+ type: valita_exports.literal("not"),
43
+ condition: indexPredicateSchema
44
+ }), readonlyObject({
45
+ type: valita_exports.literal("null-test"),
46
+ column: valita_exports.string(),
47
+ op: literalUnion("IS NULL", "IS NOT NULL")
48
+ }), readonlyObject({
49
+ type: valita_exports.literal("comparison"),
50
+ column: valita_exports.string(),
51
+ op: literalUnion("=", "<>", "<", "<=", ">", ">="),
52
+ value: indexPredicateValueSchema
53
+ })));
28
54
  var indexSpec = valita_exports.object({
29
55
  name: valita_exports.string(),
30
56
  tableName: valita_exports.string(),
31
57
  unique: valita_exports.boolean(),
32
- columns: valita_exports.record(directionSchema)
58
+ columns: valita_exports.record(directionSchema),
59
+ predicate: indexPredicateSchema.optional()
33
60
  }).extend({ schema: valita_exports.string() });
34
61
  var publishedIndexSpec = indexSpec.extend({
35
62
  isReplicaIdentity: valita_exports.boolean().optional(),
@@ -37,6 +64,6 @@ var publishedIndexSpec = indexSpec.extend({
37
64
  isImmediate: valita_exports.boolean().optional()
38
65
  });
39
66
  //#endregion
40
- export { columnSpec, indexSpec, publishedIndexSpec, publishedTableSpec, tableSpec };
67
+ export { columnSpec, indexSpec, publishedColumnSpec, publishedIndexSpec, publishedTableSpec, tableSpec };
41
68
 
42
69
  //# sourceMappingURL=specs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"specs.js","names":[],"sources":["../../../../../zero-cache/src/db/specs.ts"],"sourcesContent":["import type {DeepReadonly} from '../../../shared/src/json.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport type {SchemaValue} from '../../../zero-schema/src/table-schema.ts';\nimport * as PostgresReplicaIdentity from './postgres-replica-identity-enum.ts';\nimport * as PostgresTypeClass from './postgres-type-class-enum.ts';\n\nexport const pgTypeClassSchema = v.literalUnion(\n PostgresTypeClass.Base,\n PostgresTypeClass.Composite,\n PostgresTypeClass.Domain,\n PostgresTypeClass.Enum,\n PostgresTypeClass.Pseudo,\n PostgresTypeClass.Range,\n PostgresTypeClass.Multirange,\n);\n\nconst pgReplicaIdentitySchema = v.literalUnion(\n PostgresReplicaIdentity.Default,\n PostgresReplicaIdentity.Nothing,\n PostgresReplicaIdentity.Full,\n PostgresReplicaIdentity.Index,\n);\n\nexport const columnSpec = v.object({\n pos: v.number(),\n dataType: v.string(),\n pgTypeClass: pgTypeClassSchema.optional(),\n\n // If the column is an array, this will be the type of the\n // elements in the array. If the column is not an array,\n // this will be null.\n elemPgTypeClass: pgTypeClassSchema.nullable().optional(),\n\n characterMaximumLength: v.number().nullable().optional(),\n notNull: v.boolean().nullable().optional(),\n dflt: v.string().nullable().optional(),\n});\n\nexport type ColumnSpec = Readonly<v.Infer<typeof columnSpec>>;\n\nconst publishedColumnSpec = columnSpec.extend({\n typeOID: v.number(),\n});\n\nexport const liteTableSpec = v.object({\n name: v.string(),\n columns: v.record(columnSpec),\n primaryKey: v.array(v.string()).optional(),\n});\n\nexport const tableSpec = liteTableSpec.extend({\n schema: v.string(),\n});\n\nexport const publishedTableSpec = tableSpec.extend({\n oid: v.number(),\n // Always present for new instances (e.g. from DDL triggers), but\n // may from `initialSchema` object stored in the `replicas` table.\n schemaOID: v.number().optional(),\n columns: v.record(publishedColumnSpec),\n replicaIdentity: pgReplicaIdentitySchema.optional(),\n publications: v.record(v.object({rowFilter: v.string().nullable()})),\n});\n\nexport type MutableLiteTableSpec = v.Infer<typeof liteTableSpec>;\n\nexport type LiteTableSpec = Readonly<MutableLiteTableSpec>;\n\nexport type LiteTableSpecWithKeysAndVersion = Omit<\n LiteTableSpec,\n 'primaryKey'\n> & {\n /**\n * All keys associated with a unique index. Includes indexes with\n * nullable columns.\n */\n uniqueKeys: PrimaryKey[];\n\n /**\n * The key selected to act as the \"primary key\". Primary keys\n * are not explicitly set on the replica, but an appropriate\n * unique index is required.\n */\n primaryKey: PrimaryKey; // note: required\n\n /**\n * All keys associated with a unique index over non-null\n * columns, i.e. suitable as a primary key.\n */\n allPotentialPrimaryKeys: PrimaryKey[];\n\n /**\n * The minimum `_0_version` value for every row in the table. If this is\n * present, `_0_version` value in the row itself should only be used if\n * it is greater (i.e. later) than the `minRowVersion`.\n */\n minRowVersion: string | null;\n};\n\nexport type LiteAndZqlSpec = {\n tableSpec: LiteTableSpecWithKeysAndVersion;\n zqlSpec: Record<string, SchemaValue>;\n};\n\nexport type TableSpec = Readonly<v.Infer<typeof tableSpec>>;\n\nexport type PublishedTableSpec = Readonly<v.Infer<typeof publishedTableSpec>>;\n\nexport const directionSchema = v.literalUnion('ASC', 'DESC');\n\nexport const liteIndexSpec = v.object({\n name: v.string(),\n tableName: v.string(),\n unique: v.boolean(),\n columns: v.record(directionSchema),\n});\n\nexport type MutableLiteIndexSpec = v.Infer<typeof liteIndexSpec>;\n\nexport type LiteIndexSpec = Readonly<MutableLiteIndexSpec>;\n\nexport const indexSpec = liteIndexSpec.extend({\n schema: v.string(),\n});\n\nexport type IndexSpec = DeepReadonly<v.Infer<typeof indexSpec>>;\n\nexport const publishedIndexSpec = indexSpec.extend({\n isReplicaIdentity: v.boolean().optional(),\n isPrimaryKey: v.boolean().optional(),\n isImmediate: v.boolean().optional(),\n});\n\nexport type PublishedIndexSpec = DeepReadonly<\n v.Infer<typeof publishedIndexSpec>\n>;\n"],"mappings":";;AAOA,IAAa,oBAAoB,aAC/B,KACA,KACA,KACA,KACA,KACA,KACA,GACF;AAEA,IAAM,0BAA0B,aAC9B,KACA,KACA,KACA,GACF;AAEA,IAAa,aAAa,eAAE,OAAO;CACjC,KAAK,eAAE,OAAO;CACd,UAAU,eAAE,OAAO;CACnB,aAAa,kBAAkB,SAAS;CAKxC,iBAAiB,kBAAkB,SAAS,EAAE,SAAS;CAEvD,wBAAwB,eAAE,OAAO,EAAE,SAAS,EAAE,SAAS;CACvD,SAAS,eAAE,QAAQ,EAAE,SAAS,EAAE,SAAS;CACzC,MAAM,eAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AACvC,CAAC;AAID,IAAM,sBAAsB,WAAW,OAAO,EAC5C,SAAS,eAAE,OAAO,EACpB,CAAC;AAQD,IAAa,YANgB,eAAE,OAAO;CACpC,MAAM,eAAE,OAAO;CACf,SAAS,eAAE,OAAO,UAAU;CAC5B,YAAY,eAAE,MAAM,eAAE,OAAO,CAAC,EAAE,SAAS;AAC3C,CAEyB,EAAc,OAAO,EAC5C,QAAQ,eAAE,OAAO,EACnB,CAAC;AAED,IAAa,qBAAqB,UAAU,OAAO;CACjD,KAAK,eAAE,OAAO;CAGd,WAAW,eAAE,OAAO,EAAE,SAAS;CAC/B,SAAS,eAAE,OAAO,mBAAmB;CACrC,iBAAiB,wBAAwB,SAAS;CAClD,cAAc,eAAE,OAAO,eAAE,OAAO,EAAC,WAAW,eAAE,OAAO,EAAE,SAAS,EAAC,CAAC,CAAC;AACrE,CAAC;AA8CD,IAAa,kBAAkB,aAAe,OAAO,MAAM;AAa3D,IAAa,YAXgB,eAAE,OAAO;CACpC,MAAM,eAAE,OAAO;CACf,WAAW,eAAE,OAAO;CACpB,QAAQ,eAAE,QAAQ;CAClB,SAAS,eAAE,OAAO,eAAe;AACnC,CAMyB,EAAc,OAAO,EAC5C,QAAQ,eAAE,OAAO,EACnB,CAAC;AAID,IAAa,qBAAqB,UAAU,OAAO;CACjD,mBAAmB,eAAE,QAAQ,EAAE,SAAS;CACxC,cAAc,eAAE,QAAQ,EAAE,SAAS;CACnC,aAAa,eAAE,QAAQ,EAAE,SAAS;AACpC,CAAC"}
1
+ {"version":3,"file":"specs.js","names":[],"sources":["../../../../../zero-cache/src/db/specs.ts"],"sourcesContent":["import type {DeepReadonly} from '../../../shared/src/json.ts';\nimport * as v from '../../../shared/src/valita.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport type {SchemaValue} from '../../../zero-schema/src/table-schema.ts';\nimport * as PostgresReplicaIdentity from './postgres-replica-identity-enum.ts';\nimport * as PostgresTypeClass from './postgres-type-class-enum.ts';\n\nexport const pgTypeClassSchema = v.literalUnion(\n PostgresTypeClass.Base,\n PostgresTypeClass.Composite,\n PostgresTypeClass.Domain,\n PostgresTypeClass.Enum,\n PostgresTypeClass.Pseudo,\n PostgresTypeClass.Range,\n PostgresTypeClass.Multirange,\n);\n\nconst pgReplicaIdentitySchema = v.literalUnion(\n PostgresReplicaIdentity.Default,\n PostgresReplicaIdentity.Nothing,\n PostgresReplicaIdentity.Full,\n PostgresReplicaIdentity.Index,\n);\n\nexport const columnSpec = v.object({\n pos: v.number(),\n dataType: v.string(),\n pgTypeClass: pgTypeClassSchema.optional(),\n\n // If the column is an array, this will be the type of the\n // elements in the array. If the column is not an array,\n // this will be null.\n elemPgTypeClass: pgTypeClassSchema.nullable().optional(),\n\n characterMaximumLength: v.number().nullable().optional(),\n notNull: v.boolean().nullable().optional(),\n dflt: v.string().nullable().optional(),\n});\n\nexport type ColumnSpec = Readonly<v.Infer<typeof columnSpec>>;\n\nexport const publishedColumnSpec = columnSpec.extend({\n typeOID: v.number(),\n});\n\nexport const liteTableSpec = v.object({\n name: v.string(),\n columns: v.record(columnSpec),\n primaryKey: v.array(v.string()).optional(),\n});\n\nexport const tableSpec = liteTableSpec.extend({\n schema: v.string(),\n});\n\nexport const publishedTableSpec = tableSpec.extend({\n oid: v.number(),\n // Always present for new instances (e.g. from DDL triggers), but\n // may from `initialSchema` object stored in the `replicas` table.\n schemaOID: v.number().optional(),\n columns: v.record(publishedColumnSpec),\n replicaIdentity: pgReplicaIdentitySchema.optional(),\n publications: v.record(v.object({rowFilter: v.string().nullable()})),\n});\n\nexport type MutableLiteTableSpec = v.Infer<typeof liteTableSpec>;\n\nexport type LiteTableSpec = Readonly<MutableLiteTableSpec>;\n\nexport type LiteTableSpecWithKeysAndVersion = Omit<\n LiteTableSpec,\n 'primaryKey'\n> & {\n /**\n * All keys associated with a unique index. Includes indexes with\n * nullable columns.\n */\n uniqueKeys: PrimaryKey[];\n\n /**\n * The key selected to act as the \"primary key\". Primary keys\n * are not explicitly set on the replica, but an appropriate\n * unique index is required.\n */\n primaryKey: PrimaryKey; // note: required\n\n /**\n * All keys associated with a unique index over non-null\n * columns, i.e. suitable as a primary key.\n */\n allPotentialPrimaryKeys: PrimaryKey[];\n\n /**\n * The minimum `_0_version` value for every row in the table. If this is\n * present, `_0_version` value in the row itself should only be used if\n * it is greater (i.e. later) than the `minRowVersion`.\n */\n minRowVersion: string | null;\n};\n\nexport type LiteAndZqlSpec = {\n tableSpec: LiteTableSpecWithKeysAndVersion;\n zqlSpec: Record<string, SchemaValue>;\n};\n\nexport type TableSpec = Readonly<v.Infer<typeof tableSpec>>;\n\nexport type PublishedTableSpec = Readonly<v.Infer<typeof publishedTableSpec>>;\n\nexport const directionSchema = v.literalUnion('ASC', 'DESC');\n\nexport type IndexPredicateValue =\n | {readonly type: 'boolean'; readonly value: boolean}\n | {readonly type: 'integer'; readonly value: string}\n | {readonly type: 'string'; readonly value: string};\n\nexport type IndexPredicate =\n | {\n readonly type: 'and' | 'or';\n readonly conditions: readonly IndexPredicate[];\n }\n | {readonly type: 'not'; readonly condition: IndexPredicate}\n | {\n readonly type: 'null-test';\n readonly column: string;\n readonly op: 'IS NULL' | 'IS NOT NULL';\n }\n | {\n readonly type: 'comparison';\n readonly column: string;\n readonly op: '=' | '<>' | '<' | '<=' | '>' | '>=';\n readonly value: IndexPredicateValue;\n };\n\nconst indexPredicateValueSchema: v.Type<IndexPredicateValue> = v.union(\n v.readonlyObject({type: v.literal('boolean'), value: v.boolean()}),\n v.readonlyObject({type: v.literal('integer'), value: v.string()}),\n v.readonlyObject({type: v.literal('string'), value: v.string()}),\n);\n\nexport const indexPredicateSchema: v.Type<IndexPredicate> = v.lazy(() =>\n v.union(\n v.readonlyObject({\n type: v.literalUnion('and', 'or'),\n conditions: v.readonlyArray(indexPredicateSchema),\n }),\n v.readonlyObject({\n type: v.literal('not'),\n condition: indexPredicateSchema,\n }),\n v.readonlyObject({\n type: v.literal('null-test'),\n column: v.string(),\n op: v.literalUnion('IS NULL', 'IS NOT NULL'),\n }),\n v.readonlyObject({\n type: v.literal('comparison'),\n column: v.string(),\n op: v.literalUnion('=', '<>', '<', '<=', '>', '>='),\n value: indexPredicateValueSchema,\n }),\n ),\n);\n\nexport const liteIndexSpec = v.object({\n name: v.string(),\n tableName: v.string(),\n unique: v.boolean(),\n columns: v.record(directionSchema),\n predicate: indexPredicateSchema.optional(),\n});\n\nexport type MutableLiteIndexSpec = v.Infer<typeof liteIndexSpec>;\n\nexport type LiteIndexSpec = Readonly<MutableLiteIndexSpec>;\n\nexport const indexSpec = liteIndexSpec.extend({\n schema: v.string(),\n});\n\nexport type IndexSpec = DeepReadonly<v.Infer<typeof indexSpec>>;\n\nexport const publishedIndexSpec = indexSpec.extend({\n isReplicaIdentity: v.boolean().optional(),\n isPrimaryKey: v.boolean().optional(),\n isImmediate: v.boolean().optional(),\n});\n\nexport type PublishedIndexSpec = DeepReadonly<\n v.Infer<typeof publishedIndexSpec>\n>;\n"],"mappings":";;AAOA,IAAa,oBAAoB,aAC/B,KACA,KACA,KACA,KACA,KACA,KACA,GACF;AAEA,IAAM,0BAA0B,aAC9B,KACA,KACA,KACA,GACF;AAEA,IAAa,aAAa,eAAE,OAAO;CACjC,KAAK,eAAE,OAAO;CACd,UAAU,eAAE,OAAO;CACnB,aAAa,kBAAkB,SAAS;CAKxC,iBAAiB,kBAAkB,SAAS,EAAE,SAAS;CAEvD,wBAAwB,eAAE,OAAO,EAAE,SAAS,EAAE,SAAS;CACvD,SAAS,eAAE,QAAQ,EAAE,SAAS,EAAE,SAAS;CACzC,MAAM,eAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AACvC,CAAC;AAID,IAAa,sBAAsB,WAAW,OAAO,EACnD,SAAS,eAAE,OAAO,EACpB,CAAC;AAQD,IAAa,YANgB,eAAE,OAAO;CACpC,MAAM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M,eAAE,OAAO;CACf,WAAW,eAAE,OAAO;CACpB,QAAQ,eAAE,QAAQ;CAClB,SAAS,eAAE,OAAO,eAAe;CACjC,WAAW,qBAAqB,SAAS;AAC3C,CAMyB,EAAc,OAAO,EAC5C,QAAQ,eAAE,OAAO,EACnB,CAAC;AAID,IAAa,qBAAqB,UAAU,OAAO;CACjD,mBAAmB,eAAE,QAAQ,EAAE,SAAS;CACxC,cAAc,eAAE,QAAQ,EAAE,SAAS;CACnC,aAAa,eAAE,QAAQ,EAAE,SAAS;AACpC,CAAC"}
@@ -1,6 +1,7 @@
1
1
  import type { LogContext } from '@rocicorp/logger';
2
2
  import type postgres from 'postgres';
3
3
  import type { Enum } from '../../../shared/src/enum.ts';
4
+ import { Queue } from '../../../shared/src/queue.ts';
4
5
  import { type PostgresDB, type PostgresTransaction } from '../types/pg.ts';
5
6
  import type * as Mode from './mode-enum.ts';
6
7
  type Mode = Enum<typeof Mode>;
@@ -47,15 +48,6 @@ export type Options = {
47
48
  * workers will be shut down after an idle timeout of 5 seconds.
48
49
  */
49
50
  maxWorkers?: number;
50
- /**
51
- * Aborts the transaction if the response for a statement is not received
52
- * within the specified timeout. Note that this is different from a Postgres
53
- * `statement_timeout` or `lock_timeout` in that it is specifically intended
54
- * to detect situations in which Postgres either never received the
55
- * statement, or the application never got the response for the executed
56
- * statement.
57
- */
58
- statementResponseTimeout?: number;
59
51
  };
60
52
  /**
61
53
  * A TransactionPool is a pool of one or more {@link postgres.TransactionSql}
@@ -181,11 +173,13 @@ export declare function sharedSnapshot(): {
181
173
  snapshotID: Promise<string>;
182
174
  };
183
175
  /**
184
- * @returns An `init` Task for importing a snapshot from another transaction.
176
+ * @returns An `init` Task for importing a snapshot from another transaction,
177
+ * as well as an `imported` Queue which resolves or rejects when each
178
+ * worker is initialized.
185
179
  */
186
180
  export declare function importSnapshot(snapshotID: string): {
187
181
  init: Task;
188
- imported: Promise<void>;
182
+ imported: Queue<void>;
189
183
  };
190
184
  /**
191
185
  * A superclass of Errors used for control flow that is needed to handle
@@ -205,9 +199,5 @@ type TimeoutTasks = {
205
199
  forExtraWorkers: TimeoutTask;
206
200
  };
207
201
  export declare const TIMEOUT_TASKS: TimeoutTasks;
208
- export declare class ResponseTimeoutError extends Error {
209
- static readonly name = "ResponseTimeoutError";
210
- constructor(msg: string);
211
- }
212
202
  export {};
213
203
  //# sourceMappingURL=transaction-pool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transaction-pool.d.ts","sourceRoot":"","sources":["../../../../../zero-cache/src/db/transaction-pool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AAErC,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,6BAA6B,CAAC;AAGtD,OAAO,EAAC,KAAK,UAAU,EAAE,KAAK,mBAAmB,EAAC,MAAM,gBAAgB,CAAC;AACzE,OAAO,KAAK,KAAK,IAAI,MAAM,gBAAgB,CAAC;AAG5C,KAAK,IAAI,GAAG,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC;AAE9B,KAAK,YAAY,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEtC,MAAM,MAAM,SAAS,GACjB,QAAQ,CAAC,YAAY,CAAC,CAAC,QAAQ,CAAC,GAAG,GAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAChE,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;AAE1C;;;;;GAKG;AACH,MAAM,MAAM,IAAI,GAAG,CACjB,EAAE,EAAE,mBAAmB,EACvB,EAAE,EAAE,UAAU,KACX,YAAY,CAAC,SAAS,EAAE,CAAC,CAAC;AAE/B;;;;GAIG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CACxB,EAAE,EAAE,mBAAmB,EACvB,E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CAAC;IAClB,IAAI,EAAE,IAAI,GAAG,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,iBAAiB,EAAE,WAAW,CAAC;IAC/B,eAAe,EAAE,WAAW,CAAC;CAC9B,CAAC;AAGF,eAAO,MAAM,aAAa,EAAE,YAS3B,CAAC"}
@@ -22,16 +22,14 @@ var TransactionPool = class {
22
22
  #workers = [];
23
23
  #initialWorkers;
24
24
  #maxWorkers;
25
- #responseTimeout;
26
25
  #timeoutTask;
27
26
  #numWorkers;
28
27
  #numWorking = 0;
29
28
  #db;
30
29
  #done = false;
31
30
  #failure;
32
- #orphanedQueryCheckInterval;
33
31
  constructor(lc, opts, timeoutTasks = TIMEOUT_TASKS) {
34
- const { mode, init, cleanup, initialWorkers = 1, maxWorkers = initialWorkers, statementResponseTimeout } = opts;
32
+ const { mode, init, cleanup, initialWorkers = 1, maxWorkers = initialWorkers } = opts;
35
33
  assert(initialWorkers > 0, "initialWorkers must be positive");
36
34
  assert(maxWorkers >= initialWorkers, "maxWorkers must be >= initialWorkers");
37
35
  this.#lc = lc;
@@ -42,8 +40,6 @@ var TransactionPool = class {
42
40
  this.#numWorkers = initialWorkers;
43
41
  this.#maxWorkers = maxWorkers;
44
42
  this.#timeoutTask = timeoutTasks;
45
- this.#responseTimeout = statementResponseTimeout;
46
- this.#orphanedQueryCheckInterval = setInterval(this.#checkForOrphanedQueries, Math.min(5e3, statementResponseTimeout ?? 5e3));
47
43
  }
48
44
  /**
49
45
  * Starts the pool of workers to process Tasks with transactions opened from the
@@ -159,9 +155,6 @@ var TransactionPool = class {
159
155
  }
160
156
  #start = performance.now();
161
157
  #stmts = 0;
162
- #latestDoneIndex = 0;
163
- #latestDoneTime = 0;
164
- #pending = /* @__PURE__ */ new Map();
165
158
  /**
166
159
  * Implements the semantics specified in {@link process()}.
167
160
  *
@@ -188,69 +181,18 @@ var TransactionPool = class {
188
181
  r.resolve();
189
182
  return { pending: null };
190
183
  }
191
- const time = Date.now();
192
184
  return { pending: stmts.reduce((_, stmt) => {
193
185
  const query = stmt;
194
186
  const index = ++this.#stmts;
195
- const { promise: aborted, reject: abort } = resolver();
196
- aborted.catch(() => {});
197
- this.#pending.set(query, {
198
- index,
199
- time,
200
- abort
201
- });
202
- const responded = stmt.execute().then(() => {
187
+ return stmt.execute().then(() => {
203
188
  if (index % 1e3 === 0) lc[index % 1e4 === 0 ? "info" : "debug"]?.(`executed ${this.#stmts}th statement (${(performance.now() - this.#start).toFixed(3)} ms)`, { statement: query.string });
204
- }).catch((e) => this.fail(e)).finally(() => {
205
- this.#pending.delete(query);
206
- if (index > this.#latestDoneIndex) {
207
- this.#latestDoneIndex = index;
208
- this.#latestDoneTime = Date.now();
209
- }
210
- });
211
- const done = Promise.race([responded, aborted]);
212
- done.catch(() => {});
213
- return done;
189
+ }).catch((e) => this.fail(e));
214
190
  }, promiseVoid).finally(r.resolve) };
215
191
  },
216
192
  rejected: r.resolve
217
193
  };
218
194
  }
219
195
  /**
220
- * Periodically checks the map of `#pending` queries for which Promises
221
- * have yet to be resolved. Checks for pathological scenarios that have
222
- * been observed:
223
- *
224
- * * A promise does not resolve, even though the transaction is otherwise
225
- * healthy with keepalives being sent.
226
- * * A transaction never "finishes" at the postgres.js level after the
227
- * worker exits, implying that postgres.js is awaiting the `last` promise
228
- * returned. In this case Postgres disconnected the application with an
229
- * idle-in-transaction timeout, suggesting that all statements did in
230
- * fact complete.
231
- */
232
- #checkForOrphanedQueries = () => {
233
- if (this.#pending.size === 0) return;
234
- const now = Date.now();
235
- for (const [query, { index, time, abort }] of this.#pending.entries()) {
236
- const statement = query.string;
237
- const abortWith = (msg) => {
238
- this.#lc.warn?.(msg, { statement });
239
- const err = new ResponseTimeoutError(msg);
240
- abort(err);
241
- this.fail(err);
242
- this.#pending.delete(query);
243
- };
244
- if (index < this.#latestDoneIndex) {
245
- const elapsed = now - this.#latestDoneTime;
246
- if (this.#responseTimeout && elapsed > this.#responseTimeout) abortWith(`statement ${this.#latestDoneIndex} completed ${elapsed} ms ago, but statement ${index} has not.`);
247
- } else {
248
- const elapsed = now - time;
249
- if (this.#responseTimeout && elapsed > this.#responseTimeout) abortWith(`response for statement timed out after ${elapsed} ms`);
250
- }
251
- }
252
- };
253
- /**
254
196
  * Processes and returns the result of executing the {@link ReadTask} from
255
197
  * within the transaction. An error thrown by the task will result in
256
198
  * rejecting the returned Promise, but will not affect the transaction pool
@@ -317,7 +259,6 @@ var TransactionPool = class {
317
259
  assert(!this.#done, "already set done");
318
260
  this.#done = true;
319
261
  for (let i = 0; i < this.#numWorkers; i++) this.#tasks.enqueue("done");
320
- Promise.allSettled(this.#workers).then(() => clearInterval(this.#orphanedQueryCheckInterval));
321
262
  }
322
263
  isRunning() {
323
264
  return this.#db !== void 0 && !this.#done && this.#failure === void 0;
@@ -331,19 +272,50 @@ var TransactionPool = class {
331
272
  const level = this.#failure instanceof ControlFlowError ? "debug" : "info";
332
273
  this.#lc[level]?.(this.#failure);
333
274
  for (let i = 0; i < this.#numWorkers; i++) this.#tasks.enqueue(this.#failure);
334
- Promise.allSettled(this.#workers).then(() => clearInterval(this.#orphanedQueryCheckInterval));
335
275
  }
336
276
  }
337
277
  };
338
278
  /**
339
- * @returns An `init` Task for importing a snapshot from another transaction.
279
+ * Returns `init` and `cleanup` {@link Task}s for a TransactionPool that ensure its workers
280
+ * share a single view of the database. This is used for View Notifier and View Syncer logic
281
+ * that allows multiple entities to perform parallel reads on the same snapshot of the database.
282
+ */
283
+ function sharedSnapshot() {
284
+ const { promise: snapshotExported, resolve: exportSnapshot, reject: failExport } = resolver();
285
+ let firstWorkerRun = false;
286
+ let exporterLc;
287
+ let firstWorkerDone = false;
288
+ return {
289
+ init: (tx, lc) => {
290
+ if (!firstWorkerRun) {
291
+ firstWorkerRun = true;
292
+ exporterLc = lc;
293
+ const stmt = tx`SELECT pg_export_snapshot() AS snapshot;`.simple();
294
+ stmt.then((result) => exportSnapshot(result[0].snapshot), failExport);
295
+ return [stmt];
296
+ }
297
+ if (!firstWorkerDone) return snapshotExported.then((snapshotID) => [tx.unsafe(`SET TRANSACTION SNAPSHOT '${snapshotID}'`)]);
298
+ lc.debug?.("All work is done. No need to set snapshot");
299
+ return [];
300
+ },
301
+ cleanup: (_tx, lc) => {
302
+ if (lc === exporterLc) firstWorkerDone = true;
303
+ return [];
304
+ },
305
+ snapshotID: snapshotExported
306
+ };
307
+ }
308
+ /**
309
+ * @returns An `init` Task for importing a snapshot from another transaction,
310
+ * as well as an `imported` Queue which resolves or rejects when each
311
+ * worker is initialized.
340
312
  */
341
313
  function importSnapshot(snapshotID) {
342
- const { promise: imported, resolve, reject } = resolver();
314
+ const imported = new Queue();
343
315
  return {
344
316
  init: (tx) => {
345
317
  const stmt = tx.unsafe(`SET TRANSACTION SNAPSHOT '${snapshotID}'`);
346
- stmt.then(() => resolve(), reject);
318
+ stmt.then(() => imported.enqueue(), (err) => imported.enqueueRejection(err));
347
319
  return [stmt];
348
320
  },
349
321
  imported
@@ -397,13 +369,7 @@ var TIMEOUT_TASKS = {
397
369
  task: "done"
398
370
  }
399
371
  };
400
- var ResponseTimeoutError = class extends Error {
401
- static name = "ResponseTimeoutError";
402
- constructor(msg) {
403
- super(msg);
404
- }
405
- };
406
372
  //#endregion
407
- export { TransactionPool, importSnapshot };
373
+ export { TransactionPool, importSnapshot, sharedSnapshot };
408
374
 
409
375
  //# sourceMappingURL=transaction-pool.js.map