inlaycms 0.0.0-stage → 0.1.0

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 (364) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +462 -2
  3. package/assets/admin/admin.css +2 -0
  4. package/assets/admin/admin.js +1 -0
  5. package/assets/admin/editor.js +125 -0
  6. package/dist/adapters/admin/activity.d.ts +17 -0
  7. package/dist/adapters/admin/activity.js +59 -0
  8. package/dist/adapters/admin/assets.d.ts +14 -0
  9. package/dist/adapters/admin/assets.js +64 -0
  10. package/dist/adapters/admin/attention.d.ts +15 -0
  11. package/dist/adapters/admin/attention.js +34 -0
  12. package/dist/adapters/admin/context.d.ts +40 -0
  13. package/dist/adapters/admin/context.js +142 -0
  14. package/dist/adapters/admin/form/walk.d.ts +61 -0
  15. package/dist/adapters/admin/form/walk.js +565 -0
  16. package/dist/adapters/admin/pages.d.ts +38 -0
  17. package/dist/adapters/admin/pages.js +66 -0
  18. package/dist/adapters/admin/publish.d.ts +12 -0
  19. package/dist/adapters/admin/publish.js +195 -0
  20. package/dist/adapters/admin/routes.d.ts +47 -0
  21. package/dist/adapters/admin/routes.js +255 -0
  22. package/dist/adapters/admin/screens/backups.d.ts +11 -0
  23. package/dist/adapters/admin/screens/backups.js +223 -0
  24. package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
  25. package/dist/adapters/admin/screens/dashboard.js +160 -0
  26. package/dist/adapters/admin/screens/editor.d.ts +67 -0
  27. package/dist/adapters/admin/screens/editor.js +416 -0
  28. package/dist/adapters/admin/screens/history.d.ts +4 -0
  29. package/dist/adapters/admin/screens/history.js +105 -0
  30. package/dist/adapters/admin/screens/lists.d.ts +4 -0
  31. package/dist/adapters/admin/screens/lists.js +297 -0
  32. package/dist/adapters/admin/screens/media.d.ts +20 -0
  33. package/dist/adapters/admin/screens/media.js +186 -0
  34. package/dist/adapters/admin/screens/pickers.d.ts +29 -0
  35. package/dist/adapters/admin/screens/pickers.js +134 -0
  36. package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
  37. package/dist/adapters/admin/screens/preview-pane.js +74 -0
  38. package/dist/adapters/admin/screens/redirects.d.ts +4 -0
  39. package/dist/adapters/admin/screens/redirects.js +137 -0
  40. package/dist/adapters/admin/screens/releases.d.ts +7 -0
  41. package/dist/adapters/admin/screens/releases.js +287 -0
  42. package/dist/adapters/admin/screens/search.d.ts +22 -0
  43. package/dist/adapters/admin/screens/search.js +113 -0
  44. package/dist/adapters/admin/screens/security.d.ts +4 -0
  45. package/dist/adapters/admin/screens/security.js +85 -0
  46. package/dist/adapters/admin/screens/users.d.ts +4 -0
  47. package/dist/adapters/admin/screens/users.js +330 -0
  48. package/dist/adapters/admin/theme.d.ts +1 -0
  49. package/dist/adapters/admin/theme.js +1 -0
  50. package/dist/adapters/admin/translations.d.ts +16 -0
  51. package/dist/adapters/admin/translations.js +23 -0
  52. package/dist/adapters/admin/upload.d.ts +8 -0
  53. package/dist/adapters/admin/upload.js +116 -0
  54. package/dist/adapters/admin/views/data-table.d.ts +82 -0
  55. package/dist/adapters/admin/views/data-table.js +117 -0
  56. package/dist/adapters/admin/views/icons.d.ts +48 -0
  57. package/dist/adapters/admin/views/icons.js +47 -0
  58. package/dist/adapters/admin/views/layout.d.ts +51 -0
  59. package/dist/adapters/admin/views/layout.js +145 -0
  60. package/dist/adapters/admin/views/sheet.d.ts +15 -0
  61. package/dist/adapters/admin/views/sheet.js +6 -0
  62. package/dist/adapters/admin/views/status.d.ts +32 -0
  63. package/dist/adapters/admin/views/status.js +74 -0
  64. package/dist/adapters/auth/handler.d.ts +10 -0
  65. package/dist/adapters/auth/handler.js +63 -0
  66. package/dist/adapters/auth/request.d.ts +8 -0
  67. package/dist/adapters/auth/request.js +14 -0
  68. package/dist/adapters/cli/bin.d.ts +2 -0
  69. package/dist/adapters/cli/bin.js +42 -0
  70. package/dist/adapters/cli/main.d.ts +7 -0
  71. package/dist/adapters/cli/main.js +982 -0
  72. package/dist/adapters/cli/remote.d.ts +52 -0
  73. package/dist/adapters/cli/remote.js +161 -0
  74. package/dist/adapters/mcp/agent-refs.d.ts +9 -0
  75. package/dist/adapters/mcp/agent-refs.js +25 -0
  76. package/dist/adapters/mcp/documents.d.ts +15 -0
  77. package/dist/adapters/mcp/documents.js +26 -0
  78. package/dist/adapters/mcp/fetch-url.d.ts +19 -0
  79. package/dist/adapters/mcp/fetch-url.js +244 -0
  80. package/dist/adapters/mcp/request-state.d.ts +15 -0
  81. package/dist/adapters/mcp/request-state.js +38 -0
  82. package/dist/adapters/mcp/results.d.ts +20 -0
  83. package/dist/adapters/mcp/results.js +61 -0
  84. package/dist/adapters/mcp/route.d.ts +34 -0
  85. package/dist/adapters/mcp/route.js +52 -0
  86. package/dist/adapters/mcp/server.d.ts +44 -0
  87. package/dist/adapters/mcp/server.js +49 -0
  88. package/dist/adapters/mcp/tools/history.d.ts +5 -0
  89. package/dist/adapters/mcp/tools/history.js +84 -0
  90. package/dist/adapters/mcp/tools/media.d.ts +5 -0
  91. package/dist/adapters/mcp/tools/media.js +102 -0
  92. package/dist/adapters/mcp/tools/publish.d.ts +5 -0
  93. package/dist/adapters/mcp/tools/publish.js +157 -0
  94. package/dist/adapters/mcp/tools/read.d.ts +5 -0
  95. package/dist/adapters/mcp/tools/read.js +279 -0
  96. package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
  97. package/dist/adapters/mcp/tools/redirects.js +74 -0
  98. package/dist/adapters/mcp/tools/write.d.ts +5 -0
  99. package/dist/adapters/mcp/tools/write.js +304 -0
  100. package/dist/adapters/site/backup-schedule.d.ts +8 -0
  101. package/dist/adapters/site/backup-schedule.js +31 -0
  102. package/dist/adapters/site/cache.d.ts +29 -0
  103. package/dist/adapters/site/cache.js +63 -0
  104. package/dist/adapters/site/context.d.ts +29 -0
  105. package/dist/adapters/site/context.js +278 -0
  106. package/dist/adapters/site/head.d.ts +23 -0
  107. package/dist/adapters/site/head.js +44 -0
  108. package/dist/adapters/site/limiter.d.ts +17 -0
  109. package/dist/adapters/site/limiter.js +39 -0
  110. package/dist/adapters/site/lock.d.ts +30 -0
  111. package/dist/adapters/site/lock.js +143 -0
  112. package/dist/adapters/site/media-routes.d.ts +4 -0
  113. package/dist/adapters/site/media-routes.js +55 -0
  114. package/dist/adapters/site/plugin.d.ts +122 -0
  115. package/dist/adapters/site/plugin.js +436 -0
  116. package/dist/adapters/site/preview.d.ts +36 -0
  117. package/dist/adapters/site/preview.js +82 -0
  118. package/dist/adapters/site/public.d.ts +6 -0
  119. package/dist/adapters/site/public.js +87 -0
  120. package/dist/adapters/site/render.d.ts +45 -0
  121. package/dist/adapters/site/render.js +191 -0
  122. package/dist/adapters/site/routes.d.ts +17 -0
  123. package/dist/adapters/site/routes.js +125 -0
  124. package/dist/adapters/site/sitemap.d.ts +10 -0
  125. package/dist/adapters/site/sitemap.js +10 -0
  126. package/dist/adapters/web.d.ts +22 -0
  127. package/dist/adapters/web.js +75 -0
  128. package/dist/config.d.ts +21 -0
  129. package/dist/config.js +58 -0
  130. package/dist/core/agent/format.d.ts +62 -0
  131. package/dist/core/agent/format.js +211 -0
  132. package/dist/core/agent/schema.d.ts +6 -0
  133. package/dist/core/agent/schema.js +86 -0
  134. package/dist/core/archive.d.ts +12 -0
  135. package/dist/core/archive.js +72 -0
  136. package/dist/core/auth/auth.d.ts +4911 -0
  137. package/dist/core/auth/auth.js +61 -0
  138. package/dist/core/auth/roles.d.ts +10 -0
  139. package/dist/core/auth/roles.js +38 -0
  140. package/dist/core/auth/service.d.ts +118 -0
  141. package/dist/core/auth/service.js +424 -0
  142. package/dist/core/auth/upload-token.d.ts +8 -0
  143. package/dist/core/auth/upload-token.js +27 -0
  144. package/dist/core/backup/backup.d.ts +28 -0
  145. package/dist/core/backup/backup.js +126 -0
  146. package/dist/core/backup/restore.d.ts +41 -0
  147. package/dist/core/backup/restore.js +264 -0
  148. package/dist/core/backup/schedule.d.ts +12 -0
  149. package/dist/core/backup/schedule.js +29 -0
  150. package/dist/core/boot.d.ts +63 -0
  151. package/dist/core/boot.js +141 -0
  152. package/dist/core/check.d.ts +32 -0
  153. package/dist/core/check.js +111 -0
  154. package/dist/core/content/activity.d.ts +8 -0
  155. package/dist/core/content/activity.js +101 -0
  156. package/dist/core/content/delete.d.ts +5 -0
  157. package/dist/core/content/delete.js +100 -0
  158. package/dist/core/content/diff.d.ts +6 -0
  159. package/dist/core/content/diff.js +129 -0
  160. package/dist/core/content/dry-run.d.ts +24 -0
  161. package/dist/core/content/dry-run.js +69 -0
  162. package/dist/core/content/gaps.d.ts +25 -0
  163. package/dist/core/content/gaps.js +50 -0
  164. package/dist/core/content/hidden.d.ts +33 -0
  165. package/dist/core/content/hidden.js +103 -0
  166. package/dist/core/content/history.d.ts +15 -0
  167. package/dist/core/content/history.js +62 -0
  168. package/dist/core/content/patch.d.ts +40 -0
  169. package/dist/core/content/patch.js +287 -0
  170. package/dist/core/content/path.d.ts +6 -0
  171. package/dist/core/content/path.js +38 -0
  172. package/dist/core/content/publish.d.ts +26 -0
  173. package/dist/core/content/publish.js +211 -0
  174. package/dist/core/content/read.d.ts +99 -0
  175. package/dist/core/content/read.js +141 -0
  176. package/dist/core/content/reader.d.ts +37 -0
  177. package/dist/core/content/reader.js +195 -0
  178. package/dist/core/content/refs.d.ts +44 -0
  179. package/dist/core/content/refs.js +161 -0
  180. package/dist/core/content/release-migrations.d.ts +12 -0
  181. package/dist/core/content/release-migrations.js +154 -0
  182. package/dist/core/content/save.d.ts +25 -0
  183. package/dist/core/content/save.js +87 -0
  184. package/dist/core/content/schema.d.ts +22 -0
  185. package/dist/core/content/schema.js +127 -0
  186. package/dist/core/content/search.d.ts +8 -0
  187. package/dist/core/content/search.js +89 -0
  188. package/dist/core/content/shipped-input.d.ts +39 -0
  189. package/dist/core/content/shipped-input.js +221 -0
  190. package/dist/core/content/shipped.d.ts +112 -0
  191. package/dist/core/content/shipped.js +437 -0
  192. package/dist/core/content/slug.d.ts +23 -0
  193. package/dist/core/content/slug.js +182 -0
  194. package/dist/core/content/stats.d.ts +11 -0
  195. package/dist/core/content/stats.js +30 -0
  196. package/dist/core/content/translate.d.ts +3 -0
  197. package/dist/core/content/translate.js +68 -0
  198. package/dist/core/content/usages.d.ts +13 -0
  199. package/dist/core/content/usages.js +70 -0
  200. package/dist/core/content/validate.d.ts +21 -0
  201. package/dist/core/content/validate.js +124 -0
  202. package/dist/core/content/write.d.ts +10 -0
  203. package/dist/core/content/write.js +93 -0
  204. package/dist/core/core.d.ts +227 -0
  205. package/dist/core/core.js +251 -0
  206. package/dist/core/db/connection.d.ts +6 -0
  207. package/dist/core/db/connection.js +12 -0
  208. package/dist/core/db/migrate.d.ts +9 -0
  209. package/dist/core/db/migrate.js +42 -0
  210. package/dist/core/db/migrations/0001_content.d.ts +2 -0
  211. package/dist/core/db/migrations/0001_content.js +69 -0
  212. package/dist/core/db/migrations/0002_media.d.ts +2 -0
  213. package/dist/core/db/migrations/0002_media.js +26 -0
  214. package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
  215. package/dist/core/db/migrations/0003_redirects.js +19 -0
  216. package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
  217. package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
  218. package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
  219. package/dist/core/db/migrations/0005_site_lock.js +10 -0
  220. package/dist/core/db/migrations/0006_releases.d.ts +3 -0
  221. package/dist/core/db/migrations/0006_releases.js +34 -0
  222. package/dist/core/db/schema.d.ts +155 -0
  223. package/dist/core/db/schema.js +1 -0
  224. package/dist/core/errors.d.ts +27 -0
  225. package/dist/core/errors.js +15 -0
  226. package/dist/core/export/export.d.ts +17 -0
  227. package/dist/core/export/export.js +185 -0
  228. package/dist/core/export/format.d.ts +241 -0
  229. package/dist/core/export/format.js +106 -0
  230. package/dist/core/export/import.d.ts +54 -0
  231. package/dist/core/export/import.js +180 -0
  232. package/dist/core/export/read.d.ts +16 -0
  233. package/dist/core/export/read.js +234 -0
  234. package/dist/core/export/replace.d.ts +17 -0
  235. package/dist/core/export/replace.js +36 -0
  236. package/dist/core/export/report.d.ts +55 -0
  237. package/dist/core/export/report.js +50 -0
  238. package/dist/core/export/schema.d.ts +8 -0
  239. package/dist/core/export/schema.js +47 -0
  240. package/dist/core/export/stage.d.ts +25 -0
  241. package/dist/core/export/stage.js +192 -0
  242. package/dist/core/fields/builtins.d.ts +70 -0
  243. package/dist/core/fields/builtins.js +335 -0
  244. package/dist/core/fields/define.d.ts +86 -0
  245. package/dist/core/fields/define.js +34 -0
  246. package/dist/core/fields/walk.d.ts +49 -0
  247. package/dist/core/fields/walk.js +270 -0
  248. package/dist/core/fields/widgets.d.ts +27 -0
  249. package/dist/core/fields/widgets.js +330 -0
  250. package/dist/core/fields/zod-internals.d.ts +29 -0
  251. package/dist/core/fields/zod-internals.js +28 -0
  252. package/dist/core/media/detect.d.ts +5 -0
  253. package/dist/core/media/detect.js +66 -0
  254. package/dist/core/media/labels.d.ts +6 -0
  255. package/dist/core/media/labels.js +17 -0
  256. package/dist/core/media/library.d.ts +45 -0
  257. package/dist/core/media/library.js +239 -0
  258. package/dist/core/media/store.d.ts +11 -0
  259. package/dist/core/media/store.js +49 -0
  260. package/dist/core/media/variants.d.ts +10 -0
  261. package/dist/core/media/variants.js +59 -0
  262. package/dist/core/messages/en.d.ts +1278 -0
  263. package/dist/core/messages/en.js +1287 -0
  264. package/dist/core/messages/index.d.ts +7 -0
  265. package/dist/core/messages/index.js +14 -0
  266. package/dist/core/preview.d.ts +16 -0
  267. package/dist/core/preview.js +42 -0
  268. package/dist/core/redirects/collapse.d.ts +68 -0
  269. package/dist/core/redirects/collapse.js +153 -0
  270. package/dist/core/redirects/csv.d.ts +15 -0
  271. package/dist/core/redirects/csv.js +68 -0
  272. package/dist/core/redirects/redirects.d.ts +58 -0
  273. package/dist/core/redirects/redirects.js +545 -0
  274. package/dist/core/registry/content.d.ts +26 -0
  275. package/dist/core/registry/content.js +9 -0
  276. package/dist/core/registry/define.d.ts +145 -0
  277. package/dist/core/registry/define.js +41 -0
  278. package/dist/core/registry/migration.d.ts +44 -0
  279. package/dist/core/registry/migration.js +4 -0
  280. package/dist/core/registry/registry.d.ts +18 -0
  281. package/dist/core/registry/registry.js +255 -0
  282. package/dist/core/registry/render.d.ts +98 -0
  283. package/dist/core/registry/render.js +1 -0
  284. package/dist/core/registry/route.d.ts +42 -0
  285. package/dist/core/registry/route.js +3 -0
  286. package/dist/core/release/bundle.d.ts +21 -0
  287. package/dist/core/release/bundle.js +252 -0
  288. package/dist/core/release/keys.d.ts +11 -0
  289. package/dist/core/release/keys.js +60 -0
  290. package/dist/core/release/manifest.d.ts +103 -0
  291. package/dist/core/release/manifest.js +138 -0
  292. package/dist/core/release/pack.d.ts +20 -0
  293. package/dist/core/release/pack.js +97 -0
  294. package/dist/core/release/pointer.d.ts +19 -0
  295. package/dist/core/release/pointer.js +53 -0
  296. package/dist/core/release/read.d.ts +59 -0
  297. package/dist/core/release/read.js +256 -0
  298. package/dist/core/report.d.ts +31 -0
  299. package/dist/core/report.js +37 -0
  300. package/dist/core/richtext/html.d.ts +15 -0
  301. package/dist/core/richtext/html.js +81 -0
  302. package/dist/core/richtext/markdown.d.ts +19 -0
  303. package/dist/core/richtext/markdown.js +951 -0
  304. package/dist/core/richtext/schema.d.ts +98 -0
  305. package/dist/core/richtext/schema.js +275 -0
  306. package/dist/core/site/live.d.ts +56 -0
  307. package/dist/core/site/live.js +145 -0
  308. package/dist/core/site/paths.d.ts +15 -0
  309. package/dist/core/site/paths.js +64 -0
  310. package/dist/core/site/resolve.d.ts +9 -0
  311. package/dist/core/site/resolve.js +202 -0
  312. package/dist/core/site/sitemap.d.ts +11 -0
  313. package/dist/core/site/sitemap.js +24 -0
  314. package/dist/core/site-lock/site-lock.d.ts +21 -0
  315. package/dist/core/site-lock/site-lock.js +65 -0
  316. package/dist/core/site-lock/token.d.ts +7 -0
  317. package/dist/core/site-lock/token.js +24 -0
  318. package/dist/core/theme.d.ts +22 -0
  319. package/dist/core/theme.js +133 -0
  320. package/dist/core/types.d.ts +253 -0
  321. package/dist/core/types.js +1 -0
  322. package/dist/core/version.d.ts +1 -0
  323. package/dist/core/version.js +5 -0
  324. package/dist/host/activate.d.ts +53 -0
  325. package/dist/host/activate.js +162 -0
  326. package/dist/host/app.d.ts +46 -0
  327. package/dist/host/app.js +129 -0
  328. package/dist/host/bin.d.ts +5 -0
  329. package/dist/host/bin.js +8 -0
  330. package/dist/host/config.d.ts +36 -0
  331. package/dist/host/config.js +152 -0
  332. package/dist/host/dev.d.ts +10 -0
  333. package/dist/host/dev.js +293 -0
  334. package/dist/host/diff.d.ts +78 -0
  335. package/dist/host/diff.js +346 -0
  336. package/dist/host/hooks.d.ts +41 -0
  337. package/dist/host/hooks.js +49 -0
  338. package/dist/host/ipc.d.ts +123 -0
  339. package/dist/host/ipc.js +61 -0
  340. package/dist/host/preview-proxy.d.ts +24 -0
  341. package/dist/host/preview-proxy.js +154 -0
  342. package/dist/host/preview-worker.d.ts +42 -0
  343. package/dist/host/preview-worker.js +242 -0
  344. package/dist/host/primary.d.ts +38 -0
  345. package/dist/host/primary.js +357 -0
  346. package/dist/host/push.d.ts +125 -0
  347. package/dist/host/push.js +420 -0
  348. package/dist/host/serve.d.ts +16 -0
  349. package/dist/host/serve.js +150 -0
  350. package/dist/host/store.d.ts +48 -0
  351. package/dist/host/store.js +109 -0
  352. package/dist/host/worker.d.ts +67 -0
  353. package/dist/host/worker.js +310 -0
  354. package/dist/index.d.ts +47 -0
  355. package/dist/index.js +32 -0
  356. package/dist/jsx/html.d.ts +16 -0
  357. package/dist/jsx/html.js +65 -0
  358. package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
  359. package/dist/jsx/jsx-dev-runtime.js +5 -0
  360. package/dist/jsx/jsx-runtime.d.ts +17 -0
  361. package/dist/jsx/jsx-runtime.js +41 -0
  362. package/dist/site.d.ts +17 -0
  363. package/dist/site.js +14 -0
  364. package/package.json +111 -3
@@ -0,0 +1,141 @@
1
+ import { expressionBuilder } from 'kysely';
2
+ import { t } from '../messages/index.js';
3
+ import { readData } from './reader.js';
4
+ /** Stored revision JSON read against the current schema: defaults filled, unfit values absent, unknown keys carried. */
5
+ export function parseData(deps, type, json) {
6
+ return readData(deps.registry, type, JSON.parse(json)).data;
7
+ }
8
+ /** Singleton/global → type label; entry → title field if non-blank, else slug, else "Untitled". */
9
+ export function documentTitle(deps, doc, data, slug) {
10
+ const def = deps.registry.getType(doc.type);
11
+ if (def.kind !== 'collection')
12
+ return def.label;
13
+ const title = data.fields[def.titleField];
14
+ if (typeof title === 'string' && title.trim() !== '')
15
+ return title;
16
+ return slug ?? t('label.untitled');
17
+ }
18
+ export function revisionMeta(rev) {
19
+ return {
20
+ id: rev.id,
21
+ createdAt: rev.created_at,
22
+ authorId: rev.author_id,
23
+ authorName: rev.author_name,
24
+ source: rev.source,
25
+ message: rev.message,
26
+ };
27
+ }
28
+ function summary(doc, title, updatedAt) {
29
+ return {
30
+ id: doc.id,
31
+ groupId: doc.group_id,
32
+ kind: doc.kind,
33
+ type: doc.type,
34
+ locale: doc.locale,
35
+ title,
36
+ headRev: doc.head_rev,
37
+ publishedRev: doc.published_rev,
38
+ deletedAt: doc.deleted_at,
39
+ updatedAt,
40
+ };
41
+ }
42
+ /**
43
+ * Query modifier (for `$call`) keeping only visible documents: those whose
44
+ * type is in the site and whose locale is one of its locales. Documents of
45
+ * removed types or locales stay in the database but are hidden.
46
+ */
47
+ export function whereVisible(registry) {
48
+ const eb = expressionBuilder();
49
+ const types = [...registry.types.keys()];
50
+ const visible = types.length === 0
51
+ ? eb.or([])
52
+ : eb.and([eb('documents.type', 'in', types), eb('documents.locale', 'in', [...registry.locales])]);
53
+ return (qb) => qb.where(visible);
54
+ }
55
+ /** Documents (hidden ones included) joined with their head revision's data and time. */
56
+ export function headQuery(deps) {
57
+ return deps.db
58
+ .selectFrom('documents')
59
+ .innerJoin('revisions', 'revisions.id', 'documents.head_rev')
60
+ .selectAll('documents')
61
+ .select(['revisions.data as head_data', 'revisions.created_at as head_created_at']);
62
+ }
63
+ /** Visible documents joined with their head revision's data and time. */
64
+ export function summaryQuery(deps) {
65
+ return headQuery(deps).$call(whereVisible(deps.registry));
66
+ }
67
+ export function toSummary(deps, row) {
68
+ const title = documentTitle(deps, row, parseData(deps, row.type, row.head_data), row.head_slug);
69
+ return summary(row, title, row.head_created_at);
70
+ }
71
+ /** Visible documents joined with their published revision's data and time (documents never published are left out). */
72
+ export function summaryBase(deps) {
73
+ return deps.db
74
+ .selectFrom('documents')
75
+ .innerJoin('revisions', 'revisions.id', 'documents.published_rev')
76
+ .selectAll('documents')
77
+ .select(['revisions.data as published_data', 'revisions.created_at as published_created_at'])
78
+ .$call(whereVisible(deps.registry));
79
+ }
80
+ /** A summary of `doc` titled from the given revision JSON (its slug too, else the head's); `updatedAt` is that revision's time. */
81
+ export function summarizeRevision(deps, doc, json, updatedAt) {
82
+ const data = parseData(deps, doc.type, json);
83
+ return summary(doc, documentTitle(deps, doc, data, data.slug ?? doc.head_slug), updatedAt);
84
+ }
85
+ export async function getDocument(deps, id, opts = {}) {
86
+ const state = opts.state ?? 'head';
87
+ const doc = await deps.db
88
+ .selectFrom('documents')
89
+ .selectAll()
90
+ .where('id', '=', id)
91
+ .$call(whereVisible(deps.registry))
92
+ .executeTakeFirst();
93
+ if (!doc || doc.head_rev === null)
94
+ return null;
95
+ const revId = state === 'head' ? doc.head_rev : doc.published_rev;
96
+ if (revId === null)
97
+ return null;
98
+ const ids = [...new Set([doc.head_rev, revId])];
99
+ const revs = await deps.db.selectFrom('revisions').selectAll().where('id', 'in', ids).execute();
100
+ const head = revs.find((r) => r.id === doc.head_rev);
101
+ const rev = revs.find((r) => r.id === revId);
102
+ if (!head || !rev)
103
+ return null;
104
+ const data = parseData(deps, doc.type, rev.data);
105
+ const slug = state === 'head' ? doc.head_slug : doc.published_slug;
106
+ return {
107
+ ...summary(doc, documentTitle(deps, doc, data, slug), head.created_at),
108
+ state,
109
+ revision: revisionMeta(rev),
110
+ data,
111
+ };
112
+ }
113
+ /** The document of a singleton or global in `locale` (default locale if omitted). */
114
+ export async function findSingleton(deps, type, locale) {
115
+ deps.registry.getType(type);
116
+ const row = await summaryQuery(deps)
117
+ .where('documents.type', '=', type)
118
+ .where('documents.kind', 'in', ['singleton', 'global'])
119
+ .where('documents.locale', '=', locale ?? deps.registry.defaultLocale)
120
+ .executeTakeFirst();
121
+ return row ? toSummary(deps, row) : null;
122
+ }
123
+ export async function findTranslation(deps, groupId, locale) {
124
+ const row = await summaryQuery(deps)
125
+ .where('documents.group_id', '=', groupId)
126
+ .where('documents.locale', '=', locale)
127
+ .executeTakeFirst();
128
+ return row ? toSummary(deps, row) : null;
129
+ }
130
+ /** Entries of collection `type` in `locale` (default locale if omitted), newest head first. */
131
+ export async function listEntries(deps, type, opts = {}) {
132
+ deps.registry.getType(type);
133
+ let query = summaryQuery(deps)
134
+ .where('documents.type', '=', type)
135
+ .where('documents.kind', '=', 'entry')
136
+ .where('documents.locale', '=', opts.locale ?? deps.registry.defaultLocale);
137
+ if (!opts.includeDeleted)
138
+ query = query.where('documents.deleted_at', 'is', null);
139
+ const rows = await query.orderBy('documents.head_rev', 'desc').execute();
140
+ return rows.map((row) => toSummary(deps, row));
141
+ }
@@ -0,0 +1,37 @@
1
+ import { z } from 'zod';
2
+ import type { Registry } from '../registry/registry.js';
3
+ import type { DocumentData, PathSegment } from '../types.js';
4
+ export type ReadResult = {
5
+ data: DocumentData;
6
+ gaps: PathSegment[][];
7
+ };
8
+ type ReadOpts = {
9
+ fill: boolean;
10
+ };
11
+ /**
12
+ * Reads `value` against `schema`, keeping what fits (spec §6.1). A required
13
+ * value that doesn't fit is recorded in `gaps` at `path` and is absent, or the
14
+ * field's empty value when `fill` is set; an optional one is just absent. A
15
+ * required object that is missing is read as `{}`. Unknown object keys are
16
+ * carried through unchanged, except inside list items.
17
+ */
18
+ export declare function readValue(schema: z.ZodType, value: unknown, opts: ReadOpts, path: PathSegment[], gaps: PathSegment[][]): unknown;
19
+ /**
20
+ * Stored content of `type` read against the current schema, with the paths of
21
+ * required values that are missing or don't fit. `fill` gives those the
22
+ * field's empty value, for rendering only. Unknown fields and slots are kept;
23
+ * a switched-off slot is read but its gaps don't count.
24
+ */
25
+ export declare function readData(registry: Registry, type: string, raw: unknown, opts?: {
26
+ fill?: boolean;
27
+ }): ReadResult;
28
+ /**
29
+ * `to` with what the stored head `raw` keeps that `to` doesn't supply
30
+ * (spec §6.1): unknown keys, and known keys whose stored value didn't fit and
31
+ * so is absent from `read` (the head as `readData` returned it). This happens
32
+ * at the fields object, each slot's data object, each group reached through
33
+ * the schema, and the slots map, but never inside list items. A known key the
34
+ * editor saw and cleared stays cleared. Pure.
35
+ */
36
+ export declare function carryUnknown(registry: Registry, type: string, raw: unknown, read: DocumentData, to: DocumentData): DocumentData;
37
+ export {};
@@ -0,0 +1,195 @@
1
+ import { z } from 'zod';
2
+ import { fieldMeta } from '../fields/define.js';
3
+ import { isLeaf, ownMeta } from '../fields/walk.js';
4
+ import { defOf, innerOf, isRecord, unwrap } from '../fields/zod-internals.js';
5
+ /** Wrappers the reader looks through. Pipes and catches are read as a whole, like leaf fields. */
6
+ const OPTIONAL = new Set(['optional', 'nullable']);
7
+ const DEFAULTED = new Set(['default', 'prefault']);
8
+ /** An unfit or missing value: absent if the field may be left out, otherwise a gap (and the empty value when filling). */
9
+ function unfit(empty, required, opts, path, gaps) {
10
+ if (!required)
11
+ return undefined;
12
+ gaps.push(path);
13
+ return opts.fill && empty !== undefined ? structuredClone(empty) : undefined;
14
+ }
15
+ function readNode(s, value, opts, path, gaps, required, inList) {
16
+ const def = defOf(s);
17
+ const inner = innerOf(s);
18
+ const leaf = isLeaf(ownMeta(s));
19
+ if (!leaf && inner) {
20
+ if (def.type === 'nullable' && value === null)
21
+ return null;
22
+ if (def.type === 'optional' && value === undefined)
23
+ return undefined;
24
+ if (OPTIONAL.has(def.type))
25
+ return readNode(inner, value, opts, path, gaps, false, inList);
26
+ if (DEFAULTED.has(def.type)) {
27
+ const read = value === undefined ? undefined : readNode(inner, value, opts, path, gaps, false, inList);
28
+ if (read !== undefined)
29
+ return read;
30
+ const fallback = s.safeParse(undefined);
31
+ // A default that doesn't fit its own schema is read as if there were none.
32
+ return fallback.success ? fallback.data : readNode(inner, value, opts, path, gaps, required, inList);
33
+ }
34
+ if (def.type === 'readonly')
35
+ return readNode(inner, value, opts, path, gaps, required, inList);
36
+ }
37
+ if (!leaf && def.type === 'object' && def.shape) {
38
+ // A required object that is missing or isn't one is read as if `{}` were stored.
39
+ if (!isRecord(value) && !required)
40
+ return undefined;
41
+ const stored = isRecord(value) ? value : {};
42
+ const entries = [];
43
+ for (const [key, child] of Object.entries(def.shape)) {
44
+ const read = readNode(child, stored[key], opts, [...path, key], gaps, true, inList);
45
+ if (read !== undefined)
46
+ entries.push([key, read]);
47
+ }
48
+ // List items are rebuilt on every save, so unknown keys inside them aren't kept.
49
+ if (!inList)
50
+ for (const [key, v] of Object.entries(stored))
51
+ if (!Object.hasOwn(def.shape, key))
52
+ entries.push([key, v]);
53
+ return Object.fromEntries(entries);
54
+ }
55
+ if (!leaf && def.type === 'array' && def.element) {
56
+ if (!Array.isArray(value))
57
+ return unfit([], required, opts, path, gaps);
58
+ const element = def.element;
59
+ const wantsObject = defOf(unwrap(element)).type === 'object';
60
+ const out = [];
61
+ for (const item of value) {
62
+ if (wantsObject && !isRecord(item))
63
+ continue;
64
+ // An item that doesn't fit as a whole is dropped; gaps inside a kept item still count.
65
+ const itemPath = [...path, out.length];
66
+ const itemGaps = [];
67
+ const read = readNode(element, item, opts, itemPath, itemGaps, wantsObject, true);
68
+ if (read === undefined || itemGaps.some((g) => g.length === itemPath.length))
69
+ continue;
70
+ gaps.push(...itemGaps);
71
+ out.push(read);
72
+ }
73
+ return listChecks(s, def).safeParse(out).success ? out : unfit([], required, opts, path, gaps);
74
+ }
75
+ const result = s.safeParse(value);
76
+ if (result.success)
77
+ return result.data;
78
+ if (OPTIONAL.has(def.type))
79
+ return undefined;
80
+ if (DEFAULTED.has(def.type)) {
81
+ const fallback = s.safeParse(undefined);
82
+ if (fallback.success)
83
+ return fallback.data;
84
+ }
85
+ return unfit(fieldMeta(s)?.empty, required, opts, path, gaps);
86
+ }
87
+ const listCache = new WeakMap();
88
+ /** The array's own checks (such as min and max) over items that were already read. */
89
+ function listChecks(s, def) {
90
+ let checked = listCache.get(s);
91
+ if (!checked) {
92
+ checked = z.array(z.unknown()).check(...(def.checks ?? []));
93
+ listCache.set(s, checked);
94
+ }
95
+ return checked;
96
+ }
97
+ /**
98
+ * Reads `value` against `schema`, keeping what fits (spec §6.1). A required
99
+ * value that doesn't fit is recorded in `gaps` at `path` and is absent, or the
100
+ * field's empty value when `fill` is set; an optional one is just absent. A
101
+ * required object that is missing is read as `{}`. Unknown object keys are
102
+ * carried through unchanged, except inside list items.
103
+ */
104
+ export function readValue(schema, value, opts, path, gaps) {
105
+ return readNode(schema, value, opts, path, gaps, true, false);
106
+ }
107
+ /**
108
+ * Stored content of `type` read against the current schema, with the paths of
109
+ * required values that are missing or don't fit. `fill` gives those the
110
+ * field's empty value, for rendering only. Unknown fields and slots are kept;
111
+ * a switched-off slot is read but its gaps don't count.
112
+ */
113
+ export function readData(registry, type, raw, opts = {}) {
114
+ const { template } = registry.getType(type);
115
+ const read = { fill: opts.fill === true };
116
+ const doc = isRecord(raw) ? raw : {};
117
+ const gaps = [];
118
+ const fields = (readValue(template.fields, doc.fields, read, ['fields'], gaps) ?? {});
119
+ const stored = isRecord(doc.slots) ? doc.slots : {};
120
+ const entries = [];
121
+ for (const [key, slot] of Object.entries(template.slots)) {
122
+ const value = stored[key];
123
+ const enabled = slot.optional ? isRecord(value) && value.enabled === true : true;
124
+ const storedData = isRecord(value) ? value.data : undefined;
125
+ const data = readValue(slot.block.schema, storedData, read, ['slots', key, 'data'], enabled ? gaps : []);
126
+ entries.push([key, { enabled, data: data ?? {} }]);
127
+ }
128
+ for (const [key, v] of Object.entries(stored))
129
+ if (!Object.hasOwn(template.slots, key))
130
+ entries.push([key, v]);
131
+ return {
132
+ data: {
133
+ ...(typeof doc.slug === 'string' && { slug: doc.slug }),
134
+ ...(typeof doc.path === 'string' && { path: doc.path }),
135
+ fields,
136
+ slots: Object.fromEntries(entries),
137
+ },
138
+ gaps,
139
+ };
140
+ }
141
+ /**
142
+ * Copies into `to` each key of `raw` that `to` lacks and that is either
143
+ * unknown to the schema or known but missing from `read` (it didn't fit, so
144
+ * the editor never saw it), at every object the schema reaches. Stops at
145
+ * leaves and lists.
146
+ */
147
+ function carryObjects(s, raw, read, to) {
148
+ if (!isRecord(raw) || !isRecord(to))
149
+ return;
150
+ if (isLeaf(ownMeta(s)))
151
+ return;
152
+ const inner = innerOf(s);
153
+ if (inner)
154
+ return carryObjects(inner, raw, read, to);
155
+ const def = defOf(s);
156
+ if (def.type !== 'object' || !def.shape)
157
+ return;
158
+ const seen = isRecord(read) ? read : {};
159
+ for (const [key, child] of Object.entries(def.shape))
160
+ carryObjects(child, raw[key], seen[key], to[key]);
161
+ for (const [key, v] of Object.entries(raw)) {
162
+ if (Object.hasOwn(to, key))
163
+ continue;
164
+ if (!Object.hasOwn(def.shape, key) || !Object.hasOwn(seen, key))
165
+ to[key] = structuredClone(v);
166
+ }
167
+ }
168
+ /**
169
+ * `to` with what the stored head `raw` keeps that `to` doesn't supply
170
+ * (spec §6.1): unknown keys, and known keys whose stored value didn't fit and
171
+ * so is absent from `read` (the head as `readData` returned it). This happens
172
+ * at the fields object, each slot's data object, each group reached through
173
+ * the schema, and the slots map, but never inside list items. A known key the
174
+ * editor saw and cleared stays cleared. Pure.
175
+ */
176
+ export function carryUnknown(registry, type, raw, read, to) {
177
+ const { template } = registry.getType(type);
178
+ const doc = isRecord(raw) ? raw : {};
179
+ const out = structuredClone(to);
180
+ carryObjects(template.fields, doc.fields, read.fields, out.fields);
181
+ const rawSlots = isRecord(doc.slots) ? doc.slots : {};
182
+ const slots = isRecord(out.slots) ? out.slots : {};
183
+ for (const [key, slot] of Object.entries(template.slots)) {
184
+ const a = rawSlots[key];
185
+ const b = slots[key];
186
+ if (isRecord(a) && isRecord(b))
187
+ carryObjects(slot.block.schema, a.data, read.slots[key]?.data, b.data);
188
+ }
189
+ for (const [key, v] of Object.entries(rawSlots)) {
190
+ if (!Object.hasOwn(template.slots, key) && !Object.hasOwn(slots, key))
191
+ slots[key] = structuredClone(v);
192
+ }
193
+ out.slots = slots;
194
+ return out;
195
+ }
@@ -0,0 +1,44 @@
1
+ import type { FoundRef } from '../fields/walk.js';
2
+ import type { ContentIssue, CoreDeps, DocumentData, DocumentKind } from '../types.js';
3
+ /** Document refs (from any field with `refs`) with their document paths. Media refs are not included. */
4
+ export declare function documentRefPaths(deps: CoreDeps, type: string, data: DocumentData): FoundRef[];
5
+ /** Media refs (from any field with `refs`) with their document paths. */
6
+ export declare function mediaRefPaths(deps: CoreDeps, type: string, data: DocumentData): FoundRef[];
7
+ /** A document ref's identity: where it sits and what it points at. */
8
+ export declare function refKey(ref: FoundRef): string;
9
+ /**
10
+ * Problems with the document references in `data`, from any field: targets
11
+ * that don't exist (`not_found`), `ref`s outside their target collection
12
+ * (`wrong_collection`), links (link fields and rich text) to content
13
+ * without a page (`not_routable`).
14
+ * Hidden targets (removed types or locales) count as not found, except refs
15
+ * whose `refKey` is in `kept`. Deleted targets are allowed here; publishing
16
+ * blocks them.
17
+ * Media refs: an id the library has never had is `not_found` (unless kept or
18
+ * in `opts.media`, the id -> mime of files about to be imported); media in an
19
+ * image field must be an image (`wrong_media_type`). Deleted media is allowed.
20
+ * `opts.pending` holds groups about to be created in the same transaction,
21
+ * which count as existing, visible targets.
22
+ * `deps.db` may be a transaction.
23
+ */
24
+ export declare function checkReferences(deps: CoreDeps, type: string, data: DocumentData, opts?: {
25
+ kept?: ReadonlySet<string>;
26
+ media?: ReadonlyMap<string, string>;
27
+ pending?: ReadonlyMap<string, {
28
+ group_id: string;
29
+ kind: DocumentKind;
30
+ type: string;
31
+ }>;
32
+ }): Promise<ContentIssue[]>;
33
+ /**
34
+ * Makes `refs` hold exactly the refs of the document's current head and
35
+ * published revisions as the reader sees them (none if it is deleted). Inside
36
+ * a transaction pass `{ ...deps, db: trx }`.
37
+ */
38
+ export declare function syncRefs(deps: CoreDeps, documentId: string): Promise<void>;
39
+ /**
40
+ * Re-creates the refs of visible, live documents that have none (for
41
+ * example documents of a removed type that an import brought back). Returns
42
+ * how many documents it synced.
43
+ */
44
+ export declare function repairRefs(deps: CoreDeps): Promise<number>;
@@ -0,0 +1,161 @@
1
+ import { whereVisible } from './read.js';
2
+ import { readData } from './reader.js';
3
+ import { docLabels, documentRefs, documentRefsWithPaths } from './schema.js';
4
+ import { contentIssue } from './validate.js';
5
+ import { withTransaction } from './dry-run.js';
6
+ /** Document refs (from any field with `refs`) with their document paths. Media refs are not included. */
7
+ export function documentRefPaths(deps, type, data) {
8
+ return documentRefsWithPaths(deps.registry, type, data).filter((r) => r.ref.kind === 'document');
9
+ }
10
+ /** Media refs (from any field with `refs`) with their document paths. */
11
+ export function mediaRefPaths(deps, type, data) {
12
+ return documentRefsWithPaths(deps.registry, type, data).filter((r) => r.ref.kind === 'media');
13
+ }
14
+ /** A document ref's identity: where it sits and what it points at. */
15
+ export function refKey(ref) {
16
+ return JSON.stringify([ref.path, ref.ref.id]);
17
+ }
18
+ /**
19
+ * Problems with the document references in `data`, from any field: targets
20
+ * that don't exist (`not_found`), `ref`s outside their target collection
21
+ * (`wrong_collection`), links (link fields and rich text) to content
22
+ * without a page (`not_routable`).
23
+ * Hidden targets (removed types or locales) count as not found, except refs
24
+ * whose `refKey` is in `kept`. Deleted targets are allowed here; publishing
25
+ * blocks them.
26
+ * Media refs: an id the library has never had is `not_found` (unless kept or
27
+ * in `opts.media`, the id -> mime of files about to be imported); media in an
28
+ * image field must be an image (`wrong_media_type`). Deleted media is allowed.
29
+ * `opts.pending` holds groups about to be created in the same transaction,
30
+ * which count as existing, visible targets.
31
+ * `deps.db` may be a transaction.
32
+ */
33
+ export async function checkReferences(deps, type, data, opts = {}) {
34
+ const kept = opts.kept ?? new Set();
35
+ const found = documentRefPaths(deps, type, data);
36
+ const mediaFound = mediaRefPaths(deps, type, data);
37
+ if (found.length === 0 && mediaFound.length === 0)
38
+ return [];
39
+ const targets = new Map();
40
+ if (found.length > 0) {
41
+ const rows = await deps.db
42
+ .selectFrom('documents')
43
+ .select(['group_id', 'kind', 'type'])
44
+ .where('group_id', 'in', [...new Set(found.map((r) => r.ref.id))])
45
+ .$call(whereVisible(deps.registry))
46
+ .execute();
47
+ for (const r of rows)
48
+ targets.set(r.group_id, r);
49
+ for (const ref of found) {
50
+ const pending = opts.pending?.get(ref.ref.id);
51
+ if (pending && !targets.has(ref.ref.id))
52
+ targets.set(ref.ref.id, pending);
53
+ }
54
+ }
55
+ const mediaMime = new Map(opts.media ?? []);
56
+ const deleted = new Set();
57
+ if (mediaFound.length > 0) {
58
+ const ids = [...new Set(mediaFound.map((r) => r.ref.id))];
59
+ const present = await deps.db.selectFrom('media').select(['id', 'mime']).where('id', 'in', ids).execute();
60
+ for (const m of present)
61
+ mediaMime.set(m.id, m.mime);
62
+ const gone = await deps.db.selectFrom('media_deletions').select('media_id').where('media_id', 'in', ids).execute();
63
+ for (const g of gone)
64
+ deleted.add(g.media_id);
65
+ }
66
+ const issues = [];
67
+ const issue = (ref, code, messageId, params = {}) => {
68
+ const labels = docLabels(deps.registry, type, ref.path) ?? ref.path.map(String);
69
+ issues.push(contentIssue(ref.path, labels, code, messageId, params));
70
+ };
71
+ for (const ref of found) {
72
+ const target = targets.get(ref.ref.id);
73
+ if (!target) {
74
+ if (!kept.has(refKey(ref)))
75
+ issue(ref, 'not_found', 'issue.notFound');
76
+ }
77
+ else if (ref.meta.kind === 'ref') {
78
+ if (target.kind !== 'entry' || target.type !== ref.meta.target) {
79
+ const collection = deps.registry.types.get(ref.meta.target ?? '')?.label ?? ref.meta.target ?? '';
80
+ issue(ref, 'wrong_collection', 'issue.wrongCollection', { collection });
81
+ }
82
+ }
83
+ else if ((ref.meta.kind === 'link' || ref.meta.kind === 'richText') && !isRoutable(deps, target)) {
84
+ issue(ref, 'not_routable', 'issue.notRoutable');
85
+ }
86
+ }
87
+ for (const ref of mediaFound) {
88
+ const mime = mediaMime.get(ref.ref.id);
89
+ if (mime === undefined) {
90
+ if (!deleted.has(ref.ref.id) && !kept.has(refKey(ref)))
91
+ issue(ref, 'not_found', 'issue.mediaNotFound');
92
+ }
93
+ else if (ref.meta.kind !== 'file' && !mime.startsWith('image/')) {
94
+ issue(ref, 'wrong_media_type', 'issue.notAnImage');
95
+ }
96
+ }
97
+ return issues;
98
+ }
99
+ function isRoutable(deps, target) {
100
+ const def = deps.registry.types.get(target.type);
101
+ if (!def)
102
+ return false;
103
+ if (target.kind === 'singleton')
104
+ return def.kind === 'singleton' && def.path !== null;
105
+ if (target.kind === 'entry')
106
+ return def.kind === 'collection' && def.route !== null;
107
+ return false;
108
+ }
109
+ /**
110
+ * Makes `refs` hold exactly the refs of the document's current head and
111
+ * published revisions as the reader sees them (none if it is deleted). Inside
112
+ * a transaction pass `{ ...deps, db: trx }`.
113
+ */
114
+ export async function syncRefs(deps, documentId) {
115
+ const { db, registry } = deps;
116
+ await db.deleteFrom('refs').where('from_document_id', '=', documentId).execute();
117
+ const doc = await db
118
+ .selectFrom('documents')
119
+ .select(['type', 'head_rev', 'published_rev', 'deleted_at'])
120
+ .where('id', '=', documentId)
121
+ .executeTakeFirst();
122
+ if (!doc || doc.deleted_at !== null)
123
+ return;
124
+ const revIds = [...new Set([doc.head_rev, doc.published_rev])].filter((id) => id !== null);
125
+ if (revIds.length === 0)
126
+ return;
127
+ const revisions = await db
128
+ .selectFrom('revisions')
129
+ .select(['id', 'data'])
130
+ .where('id', 'in', revIds)
131
+ .execute();
132
+ const rows = revisions.flatMap((rev) => documentRefs(registry, doc.type, readData(registry, doc.type, JSON.parse(rev.data)).data).map((ref) => ({
133
+ from_document_id: documentId,
134
+ from_revision_id: rev.id,
135
+ to_kind: ref.kind,
136
+ to_id: ref.id,
137
+ })));
138
+ if (rows.length > 0)
139
+ await db.insertInto('refs').values(rows).execute();
140
+ }
141
+ /**
142
+ * Re-creates the refs of visible, live documents that have none (for
143
+ * example documents of a removed type that an import brought back). Returns
144
+ * how many documents it synced.
145
+ */
146
+ export async function repairRefs(deps) {
147
+ return withTransaction(deps, async (tdeps) => {
148
+ const trx = tdeps.db;
149
+ const ids = await trx
150
+ .selectFrom('documents')
151
+ .select('documents.id')
152
+ .$call(whereVisible(deps.registry))
153
+ .where('documents.deleted_at', 'is', null)
154
+ .where('documents.head_rev', 'is not', null)
155
+ .where((eb) => eb.not(eb.exists(eb.selectFrom('refs').select('refs.from_document_id').whereRef('refs.from_document_id', '=', 'documents.id'))))
156
+ .execute();
157
+ for (const { id } of ids)
158
+ await syncRefs(tdeps, id);
159
+ return ids.length;
160
+ });
161
+ }
@@ -0,0 +1,12 @@
1
+ import type { ReleaseMigration } from '../registry/migration.js';
2
+ import type { CoreDeps, Ctx } from '../types.js';
3
+ /**
4
+ * Runs the release's migration functions not yet recorded for it, in order,
5
+ * and records each in `release_migrations`. `deps.db` must be the caller's
6
+ * transaction: a throw propagates and the caller rolls everything back. Each
7
+ * content call a function makes runs in its own savepoint, one at a time; a
8
+ * failed call the function never awaited or handled fails it too.
9
+ * Writes are revisions with `ctx.source` and the release id. Returns the
10
+ * names run.
11
+ */
12
+ export declare function runReleaseMigrations(deps: CoreDeps, releaseId: string, migrations: readonly ReleaseMigration[], ctx: Ctx): Promise<string[]>;