tinyzoo 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (392) hide show
  1. package/LICENSE +10 -0
  2. package/README.md +46 -2
  3. package/app/app.config.ts +35 -0
  4. package/app/assets/brand/characters/bright/bird.svg +1 -0
  5. package/app/assets/brand/characters/bright/bunny.svg +1 -0
  6. package/app/assets/brand/characters/bright/elephant.svg +1 -0
  7. package/app/assets/brand/characters/bright/fox.svg +1 -0
  8. package/app/assets/brand/characters/bright/frog.svg +1 -0
  9. package/app/assets/brand/characters/bright/octopus.svg +1 -0
  10. package/app/assets/brand/characters/muted/bee.svg +1 -0
  11. package/app/assets/brand/characters/muted/bird.svg +1 -0
  12. package/app/assets/brand/characters/muted/butterfly.svg +1 -0
  13. package/app/assets/brand/characters/muted/chick.svg +1 -0
  14. package/app/assets/brand/characters/muted/cow.svg +1 -0
  15. package/app/assets/brand/characters/muted/deer.svg +1 -0
  16. package/app/assets/brand/characters/muted/duck.svg +1 -0
  17. package/app/assets/brand/characters/muted/elephant.svg +1 -0
  18. package/app/assets/brand/characters/muted/fox.svg +1 -0
  19. package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
  20. package/app/assets/brand/characters/muted/koala.svg +1 -0
  21. package/app/assets/brand/characters/muted/ladybug.svg +1 -0
  22. package/app/assets/brand/characters/muted/lion.svg +1 -0
  23. package/app/assets/brand/characters/muted/mouse.svg +1 -0
  24. package/app/assets/brand/characters/muted/octopus.svg +1 -0
  25. package/app/assets/brand/characters/muted/owl.svg +1 -0
  26. package/app/assets/brand/characters/muted/panda.svg +1 -0
  27. package/app/assets/brand/characters/muted/penguin.svg +1 -0
  28. package/app/assets/brand/characters/muted/pig.svg +1 -0
  29. package/app/assets/brand/characters/muted/poodle.svg +1 -0
  30. package/app/assets/brand/characters/muted/raccoon.svg +1 -0
  31. package/app/assets/brand/characters/muted/seal.svg +1 -0
  32. package/app/assets/brand/characters/muted/sheep.svg +1 -0
  33. package/app/assets/brand/characters/muted/snail.svg +1 -0
  34. package/app/assets/brand/characters/muted/turtle.svg +1 -0
  35. package/app/assets/brand/characters/muted/whale.svg +1 -0
  36. package/app/assets/brand/logo/favicon.svg +1 -0
  37. package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
  38. package/app/assets/brand/logo/logo-primary.svg +1 -0
  39. package/app/assets/brand/logo/mascot-reversed.svg +1 -0
  40. package/app/assets/brand/logo/mascot.svg +1 -0
  41. package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
  42. package/app/assets/brand/logo/wordmark.svg +1 -0
  43. package/app/assets/css/cms-admin.css +607 -0
  44. package/app/components/CmsBlocks.vue +30 -0
  45. package/app/components/CmsCell.vue +16 -0
  46. package/app/components/CmsCellBadge.vue +18 -0
  47. package/app/components/CmsCellBlocks.vue +9 -0
  48. package/app/components/CmsCellBoolean.vue +12 -0
  49. package/app/components/CmsCellCode.vue +7 -0
  50. package/app/components/CmsCellColor.vue +13 -0
  51. package/app/components/CmsCellDate.vue +10 -0
  52. package/app/components/CmsCellExcerpt.vue +21 -0
  53. package/app/components/CmsCellGallery.vue +10 -0
  54. package/app/components/CmsCellGroup.vue +23 -0
  55. package/app/components/CmsCellNumber.vue +7 -0
  56. package/app/components/CmsCellRelation.vue +8 -0
  57. package/app/components/CmsCellText.vue +12 -0
  58. package/app/components/CmsCellThumb.vue +18 -0
  59. package/app/components/CmsConfirmDialog.vue +55 -0
  60. package/app/components/CmsContent.vue +103 -0
  61. package/app/components/CmsDayPicker.vue +44 -0
  62. package/app/components/CmsEmptyState.vue +35 -0
  63. package/app/components/CmsField.vue +111 -0
  64. package/app/components/CmsFieldBlocks.vue +353 -0
  65. package/app/components/CmsFieldBoolean.vue +8 -0
  66. package/app/components/CmsFieldColor.vue +42 -0
  67. package/app/components/CmsFieldDate.vue +131 -0
  68. package/app/components/CmsFieldGallery.vue +114 -0
  69. package/app/components/CmsFieldGroup.vue +53 -0
  70. package/app/components/CmsFieldMedia.vue +219 -0
  71. package/app/components/CmsFieldNumber.vue +11 -0
  72. package/app/components/CmsFieldRelationMany.vue +54 -0
  73. package/app/components/CmsFieldRelationOne.vue +47 -0
  74. package/app/components/CmsFieldRichText.vue +45 -0
  75. package/app/components/CmsFieldSelect.vue +41 -0
  76. package/app/components/CmsFieldSlug.vue +46 -0
  77. package/app/components/CmsFieldText.vue +75 -0
  78. package/app/components/CmsFormSections.vue +150 -0
  79. package/app/components/CmsHighlight.vue +22 -0
  80. package/app/components/CmsImage.vue +39 -0
  81. package/app/components/CmsLinkPicker.vue +122 -0
  82. package/app/components/CmsLinkTarget.vue +35 -0
  83. package/app/components/CmsListFilters.vue +107 -0
  84. package/app/components/CmsLiveNotice.vue +64 -0
  85. package/app/components/CmsLoadError.vue +24 -0
  86. package/app/components/CmsMascot.vue +10 -0
  87. package/app/components/CmsMediaLibrary.vue +114 -0
  88. package/app/components/CmsMediaPreview.vue +34 -0
  89. package/app/components/CmsMediaThumb.vue +22 -0
  90. package/app/components/CmsMoment.vue +42 -0
  91. package/app/components/CmsNav.vue +39 -0
  92. package/app/components/CmsPage.vue +86 -0
  93. package/app/components/CmsPasswordInput.vue +22 -0
  94. package/app/components/CmsPresence.vue +29 -0
  95. package/app/components/CmsPreviewPanel.vue +131 -0
  96. package/app/components/CmsPublishReview.vue +102 -0
  97. package/app/components/CmsRevisions.vue +195 -0
  98. package/app/components/CmsRichTextEditor.vue +290 -0
  99. package/app/components/CmsSearch.vue +99 -0
  100. package/app/components/CmsSection.vue +20 -0
  101. package/app/components/CmsSingletonForm.vue +283 -0
  102. package/app/components/CmsStatusBadge.vue +21 -0
  103. package/app/components/CmsTime.vue +16 -0
  104. package/app/components/CmsTranslationRef.vue +27 -0
  105. package/app/components/CmsTreeList.vue +453 -0
  106. package/app/components/CmsUnsavedText.vue +34 -0
  107. package/app/components/CmsWordmark.vue +10 -0
  108. package/app/composables/useCmsAdminHead.ts +18 -0
  109. package/app/composables/useCmsCollab.ts +165 -0
  110. package/app/composables/useCmsConfirm.ts +39 -0
  111. package/app/composables/useCmsEntryActions.ts +75 -0
  112. package/app/composables/useCmsFocus.ts +13 -0
  113. package/app/composables/useCmsForm.ts +273 -0
  114. package/app/composables/useCmsI18n.ts +185 -0
  115. package/app/composables/useCmsLinkTargets.ts +84 -0
  116. package/app/composables/useCmsLive.ts +199 -0
  117. package/app/composables/useCmsSearch.ts +62 -0
  118. package/app/composables/useCmsSeo.ts +99 -0
  119. package/app/composables/useCmsSignedIn.ts +24 -0
  120. package/app/composables/useCmsUiLocale.ts +24 -0
  121. package/app/i18n/cs.ts +781 -0
  122. package/app/i18n/de.ts +781 -0
  123. package/app/i18n/en.ts +782 -0
  124. package/app/i18n/types.ts +22 -0
  125. package/app/layouts/admin.vue +224 -0
  126. package/app/layouts/cms-auth.vue +37 -0
  127. package/app/middleware/cms-api-not-a-page.global.ts +10 -0
  128. package/app/middleware/cms-auth.global.ts +16 -0
  129. package/app/pages/admin/[entity]/[id].vue +1109 -0
  130. package/app/pages/admin/[entity]/index.vue +733 -0
  131. package/app/pages/admin/account.vue +117 -0
  132. package/app/pages/admin/forgot.vue +50 -0
  133. package/app/pages/admin/index.vue +184 -0
  134. package/app/pages/admin/invite/[token].vue +73 -0
  135. package/app/pages/admin/login.vue +84 -0
  136. package/app/pages/admin/media.vue +494 -0
  137. package/app/pages/admin/privacy.vue +193 -0
  138. package/app/pages/admin/reset/[token].vue +52 -0
  139. package/app/pages/admin/setup.vue +87 -0
  140. package/app/pages/admin/styleguide.vue +261 -0
  141. package/app/pages/admin/team.vue +258 -0
  142. package/app/plugins/cmsLivePreview.client.ts +20 -0
  143. package/app/plugins/cmsSignedOut.client.ts +31 -0
  144. package/app/utils/cmsBlockType.ts +11 -0
  145. package/app/utils/cmsBrand.ts +38 -0
  146. package/app/utils/cmsDiff.ts +83 -0
  147. package/app/utils/cmsFetch.ts +7 -0
  148. package/app/utils/cmsFocusInvalid.ts +22 -0
  149. package/app/utils/cmsIssues.ts +128 -0
  150. package/app/utils/cmsJson.ts +28 -0
  151. package/app/utils/cmsMedia.ts +63 -0
  152. package/app/utils/cmsSections.ts +154 -0
  153. package/app/utils/cmsTime.ts +41 -0
  154. package/cli/backup.ts +713 -0
  155. package/cli/breaking.ts +174 -0
  156. package/cli/content-migrate.ts +239 -0
  157. package/cli/db.ts +86 -0
  158. package/cli/export.ts +336 -0
  159. package/cli/generate.ts +146 -0
  160. package/cli/import.ts +643 -0
  161. package/cli/index.ts +140 -0
  162. package/cli/links-convert.ts +291 -0
  163. package/cli/seed-engine.ts +705 -0
  164. package/cli/seed.ts +339 -0
  165. package/cli/status.ts +49 -0
  166. package/cli/tar.ts +304 -0
  167. package/cli/transfer.ts +677 -0
  168. package/cli/up.ts +27 -0
  169. package/cli/upgrade.ts +60 -0
  170. package/cli/versions.ts +60 -0
  171. package/forms/components/CmsForm.vue +186 -0
  172. package/forms/index.ts +203 -0
  173. package/forms/messages.ts +104 -0
  174. package/forms/retention.ts +48 -0
  175. package/forms/server/form.get.ts +12 -0
  176. package/forms/server/form.post.ts +109 -0
  177. package/forms/server/formSettings.ts +12 -0
  178. package/forms/server/forms.ts +127 -0
  179. package/module.ts +373 -0
  180. package/nuxt.config.ts +43 -0
  181. package/package.json +98 -3
  182. package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
  183. package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
  184. package/server/api/cms/[entity]/[id].delete.ts +52 -0
  185. package/server/api/cms/[entity]/[id].get.ts +21 -0
  186. package/server/api/cms/[entity]/[id].patch.ts +116 -0
  187. package/server/api/cms/[entity]/_export.get.ts +69 -0
  188. package/server/api/cms/[entity]/_move.post.ts +81 -0
  189. package/server/api/cms/[entity]/_order.post.ts +49 -0
  190. package/server/api/cms/[entity]/_tree.get.ts +41 -0
  191. package/server/api/cms/[entity]/index.get.ts +62 -0
  192. package/server/api/cms/[entity]/index.post.ts +38 -0
  193. package/server/api/cms/[entity]/options.get.ts +55 -0
  194. package/server/api/cms/_auth/accept.post.ts +33 -0
  195. package/server/api/cms/_auth/forgot.post.ts +39 -0
  196. package/server/api/cms/_auth/invite/[token].get.ts +18 -0
  197. package/server/api/cms/_auth/login.post.ts +42 -0
  198. package/server/api/cms/_auth/logout.post.ts +7 -0
  199. package/server/api/cms/_auth/me.get.ts +5 -0
  200. package/server/api/cms/_auth/me.patch.ts +32 -0
  201. package/server/api/cms/_auth/password.post.ts +29 -0
  202. package/server/api/cms/_auth/reset.post.ts +34 -0
  203. package/server/api/cms/_auth/setup.post.ts +46 -0
  204. package/server/api/cms/_auth/status.get.ts +18 -0
  205. package/server/api/cms/_collab/session.get.ts +30 -0
  206. package/server/api/cms/_collab/socket.ts +51 -0
  207. package/server/api/cms/_collab/unsaved.delete.ts +16 -0
  208. package/server/api/cms/_graph.get.ts +20 -0
  209. package/server/api/cms/_links/resolve.post.ts +17 -0
  210. package/server/api/cms/_links.get.ts +81 -0
  211. package/server/api/cms/_live.ts +30 -0
  212. package/server/api/cms/_media/[id]/usage.get.ts +14 -0
  213. package/server/api/cms/_media/[id].delete.ts +29 -0
  214. package/server/api/cms/_media/[id].get.ts +13 -0
  215. package/server/api/cms/_media/[id].patch.ts +64 -0
  216. package/server/api/cms/_media/exists.post.ts +15 -0
  217. package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
  218. package/server/api/cms/_media/index.get.ts +44 -0
  219. package/server/api/cms/_media/index.post.ts +147 -0
  220. package/server/api/cms/_media/raw/[...key].get.ts +85 -0
  221. package/server/api/cms/_media/raw/[...key].head.ts +2 -0
  222. package/server/api/cms/_overview.get.ts +87 -0
  223. package/server/api/cms/_preview.post.ts +17 -0
  224. package/server/api/cms/_privacy/delete.post.ts +14 -0
  225. package/server/api/cms/_privacy/export.post.ts +25 -0
  226. package/server/api/cms/_privacy/lookup.post.ts +27 -0
  227. package/server/api/cms/_search.get.ts +17 -0
  228. package/server/api/cms/_singleton/[name].get.ts +19 -0
  229. package/server/api/cms/_singleton/[name].put.ts +76 -0
  230. package/server/api/cms/_users/[id]/resend.post.ts +25 -0
  231. package/server/api/cms/_users/[id].delete.ts +21 -0
  232. package/server/api/cms/_users/[id].patch.ts +45 -0
  233. package/server/api/cms/_users/index.get.ts +28 -0
  234. package/server/api/cms/_users/index.post.ts +39 -0
  235. package/server/api/content/[entity]/[slug].get.ts +74 -0
  236. package/server/api/content/[entity]/index.get.ts +100 -0
  237. package/server/api/content/_global/[name].get.ts +23 -0
  238. package/server/api/content/_search.get.ts +28 -0
  239. package/server/api/content/_sitemap.get.ts +8 -0
  240. package/server/api/content/_tree/[entity].get.ts +31 -0
  241. package/server/api/content-types.ts.get.ts +17 -0
  242. package/server/api/health/live.get.ts +10 -0
  243. package/server/api/health/ready.get.ts +21 -0
  244. package/server/api/openapi.get.ts +48 -0
  245. package/server/api/openapi.json.get.ts +32 -0
  246. package/server/db/driver.ts +8 -0
  247. package/server/mail/mailer.ts +43 -0
  248. package/server/middleware/cms-0-headers.ts +37 -0
  249. package/server/middleware/cms-0-migrate-gate.ts +14 -0
  250. package/server/middleware/cms-csrf.ts +28 -0
  251. package/server/middleware/cms-limits.ts +20 -0
  252. package/server/middleware/cms-redirects.ts +21 -0
  253. package/server/middleware/cms-session.ts +41 -0
  254. package/server/middleware/cms-z-plugin-access.ts +15 -0
  255. package/server/plugins/cms-0-config-check.ts +35 -0
  256. package/server/plugins/cms-changes.ts +53 -0
  257. package/server/plugins/cms-content-etag.ts +21 -0
  258. package/server/plugins/cms-errors.ts +45 -0
  259. package/server/plugins/cms-migrate.ts +5 -0
  260. package/server/plugins/cms-plugins.ts +17 -0
  261. package/server/plugins/cms-search.ts +12 -0
  262. package/server/plugins/cms-setup-code.ts +15 -0
  263. package/server/routes/sitemap.xml.get.ts +25 -0
  264. package/server/store/redis.ts +95 -0
  265. package/server/store/types.ts +21 -0
  266. package/server/tasks/cms/retention.ts +10 -0
  267. package/server/tasks/cms/scheduled.ts +10 -0
  268. package/server/tasks/cms/search-reindex.ts +12 -0
  269. package/server/utils/cmsAudit.ts +29 -0
  270. package/server/utils/cmsAuth.ts +143 -0
  271. package/server/utils/cmsBlocks.ts +47 -0
  272. package/server/utils/cmsChanges.ts +222 -0
  273. package/server/utils/cmsCollab.ts +286 -0
  274. package/server/utils/cmsCollabServer.ts +451 -0
  275. package/server/utils/cmsCollate.ts +27 -0
  276. package/server/utils/cmsContent.ts +376 -0
  277. package/server/utils/cmsCrud.ts +361 -0
  278. package/server/utils/cmsCsv.ts +24 -0
  279. package/server/utils/cmsDb.ts +23 -0
  280. package/server/utils/cmsDemo.ts +36 -0
  281. package/server/utils/cmsEmails.ts +125 -0
  282. package/server/utils/cmsErrors.ts +40 -0
  283. package/server/utils/cmsFold.ts +17 -0
  284. package/server/utils/cmsImageSizes.ts +51 -0
  285. package/server/utils/cmsJson.ts +6 -0
  286. package/server/utils/cmsLinkAddress.ts +50 -0
  287. package/server/utils/cmsLinks.ts +248 -0
  288. package/server/utils/cmsListQuery.ts +93 -0
  289. package/server/utils/cmsLive.ts +393 -0
  290. package/server/utils/cmsLocales.ts +96 -0
  291. package/server/utils/cmsMail.ts +28 -0
  292. package/server/utils/cmsMediaGc.ts +188 -0
  293. package/server/utils/cmsMediaKeys.ts +62 -0
  294. package/server/utils/cmsMigrate.ts +86 -0
  295. package/server/utils/cmsPreviewDraft.ts +72 -0
  296. package/server/utils/cmsPrivacy.ts +176 -0
  297. package/server/utils/cmsPublished.ts +57 -0
  298. package/server/utils/cmsRange.ts +38 -0
  299. package/server/utils/cmsRateLimit.ts +82 -0
  300. package/server/utils/cmsRedirects.ts +160 -0
  301. package/server/utils/cmsRetention.ts +61 -0
  302. package/server/utils/cmsRevisions.ts +103 -0
  303. package/server/utils/cmsScheduler.ts +45 -0
  304. package/server/utils/cmsScrub.ts +107 -0
  305. package/server/utils/cmsSearch.ts +354 -0
  306. package/server/utils/cmsSession.ts +102 -0
  307. package/server/utils/cmsSetupCode.ts +97 -0
  308. package/server/utils/cmsSitemap.ts +176 -0
  309. package/server/utils/cmsStorage.ts +580 -0
  310. package/server/utils/cmsStore.ts +109 -0
  311. package/server/utils/cmsTiming.ts +14 -0
  312. package/server/utils/cmsTooMany.ts +9 -0
  313. package/server/utils/cmsTranslationWrite.ts +186 -0
  314. package/server/utils/cmsTree.ts +251 -0
  315. package/server/utils/cmsTreeAddress.ts +65 -0
  316. package/server/utils/cmsUpload.ts +228 -0
  317. package/server/utils/cmsWebhooks.ts +104 -0
  318. package/shared/json.ts +11 -0
  319. package/shared/richtext-editor.ts +56 -0
  320. package/shared/richtext-normalize.ts +33 -0
  321. package/shared/richtext-paste.ts +151 -0
  322. package/shared/storage-delivery.ts +53 -0
  323. package/src/blocks.ts +624 -0
  324. package/src/changes.ts +54 -0
  325. package/src/content-shape.ts +340 -0
  326. package/src/deprecate.ts +64 -0
  327. package/src/descriptor.ts +149 -0
  328. package/src/docs.ts +27 -0
  329. package/src/emitter.ts +94 -0
  330. package/src/entity.ts +624 -0
  331. package/src/errorReport.ts +41 -0
  332. package/src/fields.ts +465 -0
  333. package/src/i18n.ts +26 -0
  334. package/src/index.ts +54 -0
  335. package/src/linkref.ts +31 -0
  336. package/src/links.ts +179 -0
  337. package/src/locales.ts +39 -0
  338. package/src/media.ts +48 -0
  339. package/src/migrate.ts +236 -0
  340. package/src/openapi.ts +377 -0
  341. package/src/paths.ts +88 -0
  342. package/src/plugin.ts +505 -0
  343. package/src/presets.ts +230 -0
  344. package/src/richtext.ts +88 -0
  345. package/src/sdk.ts +181 -0
  346. package/src/search.ts +312 -0
  347. package/src/seed.ts +225 -0
  348. package/src/setupCode.ts +30 -0
  349. package/src/system.ts +219 -0
  350. package/src/tree.ts +421 -0
  351. package/src/upgrades.ts +137 -0
  352. package/starter/.env.example +10 -0
  353. package/starter/README.md +65 -0
  354. package/starter/app/app.config.ts +10 -0
  355. package/starter/app/app.vue +12 -0
  356. package/starter/app/assets/css/main.css +15 -0
  357. package/starter/app/components/global/BlockContact.vue +15 -0
  358. package/starter/app/components/global/BlockCta.vue +15 -0
  359. package/starter/app/components/global/BlockHero.vue +23 -0
  360. package/starter/app/components/global/BlockImage.vue +13 -0
  361. package/starter/app/components/global/BlockText.vue +12 -0
  362. package/starter/app/composables/useSite.ts +77 -0
  363. package/starter/app/error.vue +28 -0
  364. package/starter/app/layouts/default.vue +106 -0
  365. package/starter/app/lib/cms-content.ts +152 -0
  366. package/starter/app/pages/[...slug].vue +66 -0
  367. package/starter/app/pages/hledat.vue +59 -0
  368. package/starter/app/pages/index.vue +20 -0
  369. package/starter/cms.config.ts +137 -0
  370. package/starter/compose.yaml +15 -0
  371. package/starter/drizzle.config.ts +8 -0
  372. package/starter/gitignore.txt +8 -0
  373. package/starter/nuxt.config.ts +13 -0
  374. package/starter/package.json +38 -0
  375. package/starter/scripts/typecheck.mjs +38 -0
  376. package/starter/seed/index.ts +297 -0
  377. package/starter/seed/media/about.jpg +0 -0
  378. package/starter/seed/media/hero.jpg +0 -0
  379. package/starter/seed/media/logo.png +0 -0
  380. package/starter/seed/media/news-bread.jpg +0 -0
  381. package/starter/seed/media/news-morning.jpg +0 -0
  382. package/starter/server/db/migrations/0000_init.sql +269 -0
  383. package/starter/server/db/migrations/0001_page_tree.sql +5 -0
  384. package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
  385. package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
  386. package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
  387. package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
  388. package/starter/server/db/migrations/meta/_journal.json +27 -0
  389. package/starter/server/db/schema.ts +253 -0
  390. package/starter/server/plugins/sitemap.ts +11 -0
  391. package/starter/server/routes/robots.txt.get.ts +12 -0
  392. package/starter/tsconfig.json +9 -0
@@ -0,0 +1,677 @@
1
+ /**
2
+ * What `tinyzoo export` and `tinyzoo import` share: the archive layout, how a site's
3
+ * content model is described and compared, how rows become archive entries and back,
4
+ * the order tables can be filled in, and how the CLI finds the database and storage.
5
+ *
6
+ * ── The archive ─────────────────────────────────────────────────────────────────
7
+ * One `.tar.gz` (format 1). Everything is JSON, so it can be read without tinyzoo.
8
+ *
9
+ * manifest.json format, tinyzoo version, when, the content model's
10
+ * fingerprint and shape, languages, what is inside
11
+ * content/<entity>.jsonl one entry per line (collections)
12
+ * globals/<name>.json the one row of a global (settings, menus, home page)
13
+ * media.jsonl the media library's rows, one per line
14
+ * media/<storage key> the files themselves
15
+ * revisions.jsonl saved versions (only with `--with-revisions`)
16
+ *
17
+ * An entry is keyed by FIELD names (not columns), so it survives a column rename:
18
+ *
19
+ * { "id": "…", "createdAt": "…", "updatedAt": "…",
20
+ * "fields": { "title": "…", "author": "<id>", "hero": "<media id>", "body": {…} },
21
+ * "translations": [ { "id": "…", "locale": "en", "createdAt": "…", "updatedAt": "…",
22
+ * "fields": { "title": "…", "body": {…} } } ],
23
+ * "relations": { "tags": ["<id>", …] } } // many-to-many, by field
24
+ *
25
+ * Not in the archive, on purpose: accounts (users, invitation and reset links),
26
+ * sessions, the audit log, live-editing state and the search index. People and their
27
+ * history belong to an environment — a copy of production must not carry its people
28
+ * into staging — and the search index is rebuilt from the content on import. Saved
29
+ * versions (revisions) come with `--with-revisions`; form submissions (personal
30
+ * data) with `--with-submissions`. Who created or last changed a row is dropped for
31
+ * the same reason (it points at accounts).
32
+ */
33
+ import { createHash } from 'node:crypto'
34
+ import { createReadStream, createWriteStream, type WriteStream } from 'node:fs'
35
+ import { readFileSync } from 'node:fs'
36
+ import { hostname } from 'node:os'
37
+ import { once } from 'node:events'
38
+ import { join, resolve, sep } from 'node:path'
39
+ import { createInterface } from 'node:readline'
40
+ import { getTableColumns, getTableName, sql } from 'drizzle-orm'
41
+ import { drizzle } from 'drizzle-orm/node-postgres'
42
+ import pg from 'pg'
43
+ import { mediaIdsInBlocks, mediaIdsInFields, propOf, type CmsEntity, type CmsRegistry, type Junction } from '../src/index'
44
+ import { blockVersion, storedBlockVersion } from '../src/blocks'
45
+ import { loadSettings, settingsFrom, type StorageSettings } from './backup'
46
+ import { encodeStorageKey, resolveS3Delivery, storedMediaUrl } from '../shared/storage-delivery'
47
+
48
+ export const ARCHIVE_FORMAT = 1
49
+
50
+ export const ARCHIVE_FILES = {
51
+ manifest: 'manifest.json',
52
+ media: 'media.jsonl',
53
+ revisions: 'revisions.jsonl',
54
+ mediaDir: 'media/',
55
+ content: (entity: string) => `content/${entity}.jsonl`,
56
+ global: (entity: string) => `globals/${entity}.json`,
57
+ }
58
+
59
+ /** Entity names become file names: only plain identifiers are accepted from an archive. */
60
+ export const SAFE_NAME = /^[A-Za-z0-9_-]+$/
61
+
62
+ /** Rows read or written per query. */
63
+ export const BATCH = 500
64
+
65
+ // ── manifest ────────────────────────────────────────────────────────────────────
66
+
67
+ export interface ManifestEntity {
68
+ name: string
69
+ kind: 'collection' | 'global'
70
+ rows: number
71
+ translations: number
72
+ file: string | null
73
+ }
74
+
75
+ export interface Manifest {
76
+ format: number
77
+ tinyzoo: string
78
+ exportedAt: string
79
+ fingerprint: string
80
+ schema: SchemaShape
81
+ locales: { default: string; all: string[] } | null
82
+ /** The entities asked for with `--only`, or null for everything. */
83
+ only: string[] | null
84
+ entities: ManifestEntity[]
85
+ excluded: { entity: string; reason: string }[]
86
+ media: {
87
+ rows: number; included: boolean; files: number; missing: number
88
+ /**
89
+ * A fingerprint (sha256) of the storage the files were exported from — never the
90
+ * bucket name or a path. An import into the SAME storage warns: two environments
91
+ * must not share one, or deleting a file in one deletes it in the other.
92
+ */
93
+ storage?: string | null
94
+ }
95
+ revisions: { included: boolean; rows: number }
96
+ }
97
+
98
+ // ── the content model: shape, fingerprint, differences ─────────────────────────
99
+
100
+ export interface FieldShape {
101
+ type: string
102
+ localized?: true
103
+ relation?: { to: string; kind: string }
104
+ values?: string[]
105
+ blocks?: Record<string, Record<string, FieldShape>>
106
+ /** A group's own fields. */
107
+ fields?: Record<string, FieldShape>
108
+ /** Block types (or list items) whose definition is at a version above 1. */
109
+ blockVersions?: Record<string, number>
110
+ }
111
+
112
+ export interface SchemaShape {
113
+ entities: Record<string, { singleton: boolean; fields: Record<string, FieldShape> }>
114
+ }
115
+
116
+ function fieldShape(fld: any): FieldShape {
117
+ const shape: FieldShape = { type: fld.type }
118
+ if (fld.isLocalized) shape.localized = true
119
+ if (fld.relation) shape.relation = { to: (fld.relation.to() as CmsEntity).name, kind: fld.relation.kind }
120
+ if (fld.spec?.base.kind === 'enum') shape.values = [...fld.spec.base.values]
121
+ if (fld.blocks) {
122
+ const defs = fld.blocks as Record<string, any>
123
+ shape.blocks = Object.fromEntries(Object.entries(defs).map(([type, def]) => [
124
+ type,
125
+ Object.fromEntries(Object.entries(def.fields as Record<string, any>).map(([k, f]) => [k, fieldShape(f)])),
126
+ ]))
127
+ // Stored blocks carry `v` once their definition is above 1: an archive at v2 does
128
+ // not fit a site still at v1. Left out at 1, so sites without versions keep their fingerprint.
129
+ const versions = Object.fromEntries(Object.entries(defs).filter(([, def]) => blockVersion(def) > 1).map(([type, def]) => [type, blockVersion(def)]))
130
+ if (Object.keys(versions).length) shape.blockVersions = versions
131
+ }
132
+ if (fld.type === 'group' && fld.fields) shape.fields = Object.fromEntries(Object.entries(fld.fields as Record<string, any>).map(([k, f]) => [k, fieldShape(f)]))
133
+ return shape
134
+ }
135
+
136
+ /** Entity and field names, types and block definitions — what the content is made of. */
137
+ export function describeSchema(reg: CmsRegistry): SchemaShape {
138
+ return {
139
+ entities: Object.fromEntries(reg.entities.map((e) => [e.name, {
140
+ singleton: !!e.singleton,
141
+ fields: Object.fromEntries(Object.entries(e.fields).map(([k, f]) => [k, fieldShape(f)])),
142
+ }])),
143
+ }
144
+ }
145
+
146
+ /** JSON with every object's keys sorted: the same model always serializes the same. */
147
+ export function canonicalJson(value: unknown): string {
148
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`
149
+ if (value && typeof value === 'object') {
150
+ return `{${Object.keys(value).sort().map((k) => `${JSON.stringify(k)}:${canonicalJson((value as any)[k])}`).join(',')}}`
151
+ }
152
+ return JSON.stringify(value) ?? 'null'
153
+ }
154
+
155
+ export function fingerprintOf(schema: SchemaShape): string {
156
+ return createHash('sha256').update(canonicalJson(schema)).digest('hex')
157
+ }
158
+
159
+ export interface Difference {
160
+ scope: 'entity' | 'field' | 'blocks' | 'locales'
161
+ entity?: string
162
+ field?: string
163
+ message: string
164
+ /** Under `--allow-mismatch` the import leaves this entity / field out. */
165
+ skip: boolean
166
+ }
167
+
168
+ /** A field's shape without block versions (what the structure comparison looks at). */
169
+ function withoutVersions(shape: FieldShape): FieldShape {
170
+ const { blockVersions: _v, ...rest } = shape
171
+ if (!rest.blocks) return rest
172
+ return { ...rest, blocks: Object.fromEntries(Object.entries(rest.blocks).map(([type, fields]) => [type, Object.fromEntries(Object.entries(fields).map(([k, f]) => [k, withoutVersions(f)]))])) }
173
+ }
174
+
175
+ /** Block types (at any depth) the archive has at a NEWER version than this site's definition. */
176
+ function newerBlockVersions(path: string, a: FieldShape, b: FieldShape, out: string[]): void {
177
+ for (const type of Object.keys(a.blocks ?? {})) {
178
+ const fb = b.blocks?.[type]
179
+ if (!fb) continue
180
+ const av = a.blockVersions?.[type] ?? 1
181
+ const bv = b.blockVersions?.[type] ?? 1
182
+ if (av > bv) out.push(`"${type}" is at version ${av} in the archive but this site's definition is at ${bv} (update this site first)`)
183
+ for (const [k, fa] of Object.entries(a.blocks![type]!)) if (fb[k]) newerBlockVersions(`${path}: "${type}.${k}"`, fa, fb[k]!, out)
184
+ }
185
+ }
186
+
187
+ function diffBlocks(path: string, fa: FieldShape, fb: FieldShape, entity: string, field: string, out: Difference[]) {
188
+ const mk = (message: string, skip = false): Difference => ({ scope: 'blocks', entity, field, message: `${path}: ${message}`, skip })
189
+ const a = fa.blocks ?? {}
190
+ const b = fb.blocks ?? {}
191
+ for (const type of Object.keys(a)) if (!b[type]) out.push(mk(`block "${type}" is in the archive but no longer exists here`))
192
+ for (const type of Object.keys(b)) if (!a[type]) out.push(mk(`block "${type}" is new here (the archive has none)`))
193
+ for (const type of Object.keys(a)) {
194
+ const xa = a[type]
195
+ const xb = b[type]
196
+ if (!xa || !xb) continue
197
+ for (const k of Object.keys(xa)) {
198
+ if (!xb[k]) out.push(mk(`"${type}.${k}" is in the archive but no longer exists here`))
199
+ else if (canonicalJson(withoutVersions(xa[k]!)) !== canonicalJson(withoutVersions(xb[k]!))) out.push(mk(`"${type}.${k}" has changed (${describeChange(xa[k]!, xb[k]!)})`))
200
+ }
201
+ for (const k of Object.keys(xb)) if (!xa[k]) out.push(mk(`"${type}.${k}" is new here`))
202
+ }
203
+ // Older blocks in the archive are brought up to date on import; newer ones cannot be read.
204
+ // The field is left out under --allow-mismatch.
205
+ const newer: string[] = []
206
+ newerBlockVersions(path, fa, fb, newer)
207
+ for (const message of newer) out.push(mk(message, true))
208
+ }
209
+
210
+ function describeChange(a: FieldShape, b: FieldShape): string {
211
+ if (a.type !== b.type) return `${a.type} → ${b.type}`
212
+ if (!!a.localized !== !!b.localized) return a.localized ? 'no longer translated' : 'now translated'
213
+ if (a.relation?.to !== b.relation?.to || a.relation?.kind !== b.relation?.kind) return `links to ${a.relation?.to}/${a.relation?.kind} → ${b.relation?.to}/${b.relation?.kind}`
214
+ if (canonicalJson(a.values) !== canonicalJson(b.values)) return `choices ${(a.values ?? []).join('|')} → ${(b.values ?? []).join('|')}`
215
+ return 'its blocks have changed'
216
+ }
217
+
218
+ /**
219
+ * What differs between the content model an archive was made from and this site's.
220
+ * Entities and fields missing on either side, changed types, choices and blocks.
221
+ */
222
+ export function diffSchemas(archive: SchemaShape, target: SchemaShape): Difference[] {
223
+ const out: Difference[] = []
224
+ const names = [...new Set([...Object.keys(archive.entities), ...Object.keys(target.entities)])].sort()
225
+ for (const name of names) {
226
+ const a = archive.entities[name]
227
+ const b = target.entities[name]
228
+ if (a && !b) { out.push({ scope: 'entity', entity: name, message: `"${name}" is in the archive but this site has no such content`, skip: true }); continue }
229
+ if (!a && b) { out.push({ scope: 'entity', entity: name, message: `"${name}" exists here but not in the archive (it stays empty)`, skip: false }); continue }
230
+ if (!a || !b) continue
231
+ if (a.singleton !== b.singleton) {
232
+ out.push({ scope: 'entity', entity: name, message: `"${name}" is ${a.singleton ? 'a global' : 'a collection'} in the archive but ${b.singleton ? 'a global' : 'a collection'} here`, skip: true })
233
+ continue
234
+ }
235
+ const keys = [...new Set([...Object.keys(a.fields), ...Object.keys(b.fields)])].sort()
236
+ for (const key of keys) {
237
+ const fa = a.fields[key]
238
+ const fb = b.fields[key]
239
+ const at = `${name}.${key}`
240
+ if (fa && !fb) out.push({ scope: 'field', entity: name, field: key, message: `${at} is in the archive but this site has no such field`, skip: true })
241
+ else if (!fa && fb) out.push({ scope: 'field', entity: name, field: key, message: `${at} exists here but not in the archive (it stays empty)`, skip: false })
242
+ else if (fa && fb && canonicalJson(fa) !== canonicalJson(fb)) {
243
+ const structural = fa.type !== fb.type || !!fa.localized !== !!fb.localized
244
+ || fa.relation?.to !== fb.relation?.to || fa.relation?.kind !== fb.relation?.kind
245
+ if (structural) out.push({ scope: 'field', entity: name, field: key, message: `${at} has changed (${describeChange(fa, fb)})`, skip: true })
246
+ else if (canonicalJson(fa.values) !== canonicalJson(fb.values)) out.push({ scope: 'field', entity: name, field: key, message: `${at} has other choices (${describeChange(fa, fb)})`, skip: false })
247
+ else diffBlocks(at, fa, fb, name, key, out)
248
+ }
249
+ }
250
+ }
251
+ return out
252
+ }
253
+
254
+ export function formatDifferences(list: Difference[]): string {
255
+ return list.map((d) => ` • ${d.message}${d.skip ? ' [left out with --allow-mismatch]' : ''}`).join('\n')
256
+ }
257
+
258
+ // ── rows ↔ entries ──────────────────────────────────────────────────────────────
259
+
260
+ export interface LayoutField {
261
+ key: string
262
+ /** The row's property (FK fields end in `Id`). */
263
+ prop: string
264
+ fld: any
265
+ col: any
266
+ isDate: boolean
267
+ }
268
+
269
+ export interface EntityLayout {
270
+ e: CmsEntity
271
+ fields: LayoutField[]
272
+ byKey: Map<string, LayoutField>
273
+ /** Many-to-many fields (they own no column). */
274
+ m2m: { key: string; junction: Junction }[]
275
+ /** Translated field keys → their column in the translations table. */
276
+ localeCols: Record<string, any>
277
+ }
278
+
279
+ export function layoutOf(reg: CmsRegistry, e: CmsEntity): EntityLayout {
280
+ const columns = getTableColumns(e.table) as Record<string, any>
281
+ const fields: LayoutField[] = []
282
+ for (const [key, fld] of Object.entries(e.fields)) {
283
+ if (!fld.spec) continue
284
+ const prop = propOf(key, fld)
285
+ const col = columns[prop]
286
+ if (!col) continue
287
+ fields.push({ key, prop, fld, col, isDate: col.dataType === 'date' })
288
+ }
289
+ const m2m: EntityLayout['m2m'] = []
290
+ for (const [key, fld] of Object.entries(e.fields)) {
291
+ if (fld.relation?.kind !== 'many-to-many') continue
292
+ const junction = reg.junctionByOwnerField.get(`${e.name}.${key}`)
293
+ if (junction) m2m.push({ key, junction })
294
+ }
295
+ const localeCols = e.localesTable ? (getTableColumns(e.localesTable) as Record<string, any>) : {}
296
+ return { e, fields, byKey: new Map(fields.map((x) => [x.key, x])), m2m, localeCols }
297
+ }
298
+
299
+ export const iso = (v: unknown) => (v instanceof Date ? v.toISOString() : v ?? null)
300
+
301
+ export interface Entry {
302
+ id: string
303
+ createdAt: string
304
+ updatedAt: string
305
+ fields: Record<string, unknown>
306
+ translations: { id: string; locale: string; createdAt: string; updatedAt: string; fields: Record<string, unknown> }[]
307
+ relations: Record<string, string[]>
308
+ }
309
+
310
+ export function encodeEntry(layout: EntityLayout, row: any, translations: any[], relations: Record<string, string[]>): Entry {
311
+ const fields: Record<string, unknown> = {}
312
+ for (const f of layout.fields) fields[f.key] = iso(row[f.prop])
313
+ return {
314
+ id: row.id,
315
+ createdAt: iso(row.createdAt) as string,
316
+ updatedAt: iso(row.updatedAt) as string,
317
+ fields,
318
+ translations: translations.map((t) => ({
319
+ id: t.id,
320
+ locale: t.locale,
321
+ createdAt: iso(t.createdAt) as string,
322
+ updatedAt: iso(t.updatedAt) as string,
323
+ fields: Object.fromEntries(layout.e.localized.map((k) => [k, iso(t[k])])),
324
+ })),
325
+ relations,
326
+ }
327
+ }
328
+
329
+ /** Embedded pictures of a rich-text document (image nodes carry the media id). */
330
+ function embeddedIds(doc: any, out: string[]): void {
331
+ if (!doc || typeof doc !== 'object') return
332
+ if (typeof doc.attrs?.mediaId === 'string') out.push(doc.attrs.mediaId)
333
+ if (Array.isArray(doc.content)) for (const child of doc.content) embeddedIds(child, out)
334
+ }
335
+
336
+ /** Media ids one field value refers to (a picture, a gallery, blocks, rich text). */
337
+ export function mediaIdsOfValue(fld: any, value: unknown, out: string[] = []): string[] {
338
+ if (value == null) return out
339
+ if (fld.type === 'media') { if (typeof value === 'string') out.push(value) }
340
+ else if (fld.type === 'gallery') { if (Array.isArray(value)) for (const g of value) if (typeof g?.mediaId === 'string') out.push(g.mediaId) }
341
+ else if (fld.type === 'blocks') mediaIdsInBlocks(value, fld.blocks ?? {}, out)
342
+ else if (fld.type === 'group') { if (typeof value === 'object') mediaIdsInFields(fld.fields ?? {}, value as Record<string, any>, out) }
343
+ else if (fld.type === 'richtext') embeddedIds(value, out)
344
+ return out
345
+ }
346
+
347
+ /** Media ids an entry uses, in its own row and in its translations. */
348
+ export function mediaIdsOfEntry(layout: EntityLayout, entry: Entry): string[] {
349
+ const out: string[] = []
350
+ for (const f of layout.fields) mediaIdsOfValue(f.fld, entry.fields[f.key], out)
351
+ for (const t of entry.translations) for (const k of layout.e.localized) mediaIdsOfValue(layout.e.fields[k], t.fields[k], out)
352
+ return out
353
+ }
354
+
355
+ // ── who uses a media file ───────────────────────────────────────────────────────
356
+
357
+ /** Field types whose JSON can hold media ids (rich text images, blocks, galleries). */
358
+ const JSON_MEDIA_TYPES = new Set(['richtext', 'blocks', 'gallery', 'group'])
359
+
360
+ const escapeLike = (s: string) => s.replace(/[\\%_]/g, (c) => `\\${c}`)
361
+
362
+ /**
363
+ * Which of these media rows are still used by content: as a link in ANY table that has
364
+ * a foreign key to the media library, or inside the JSON of rich text, blocks and
365
+ * galleries — matched by id AND by storage key, as the server's own clean-up does
366
+ * (an embedded picture whose id was lost still carries its address). Tables in
367
+ * `exceptTables` (by name) are not looked at: the rows about to be replaced.
368
+ */
369
+ export async function usedMediaIds(db: any, reg: CmsRegistry, media: { id: string; key: string }[], exceptTables: Set<string> = new Set()): Promise<Set<string>> {
370
+ const used = new Set<string>()
371
+ if (!media.length) return used
372
+ const mediaTable = getTableName(reg.cmsMedia)
373
+
374
+ // Links: every foreign key that points at the media library, whoever declared it.
375
+ const fks: { schema: string; tbl: string; col: string }[] = (await db.execute(sql`
376
+ select n.nspname as schema, c.relname as tbl, a.attname as col
377
+ from pg_constraint k
378
+ join pg_class c on c.oid = k.conrelid
379
+ join pg_namespace n on n.oid = c.relnamespace
380
+ join pg_attribute a on a.attrelid = k.conrelid and a.attnum = any (k.conkey)
381
+ where k.contype = 'f' and k.confrelid = to_regclass(${`"${mediaTable}"`})`)).rows
382
+ const ids = media.map((m) => m.id)
383
+ for (let at = 0; at < ids.length; at += 1000) {
384
+ const batch = JSON.stringify(ids.slice(at, at + 1000))
385
+ for (const fk of fks) {
386
+ if (exceptTables.has(fk.tbl)) continue
387
+ const res = await db.execute(sql`select distinct ${sql.identifier(fk.col)}::text as id from ${sql.identifier(fk.schema)}.${sql.identifier(fk.tbl)}
388
+ where ${sql.identifier(fk.col)} in (select jsonb_array_elements_text(${batch}::jsonb)::uuid)`)
389
+ for (const r of res.rows) used.add(r.id)
390
+ }
391
+ }
392
+
393
+ // JSON: a column that can hold pictures, in the entity and in its translations.
394
+ const columns: { table: any; name: string; key: string }[] = []
395
+ for (const e of reg.entities) {
396
+ for (const [key, fld] of Object.entries(e.fields as Record<string, any>)) {
397
+ if (!JSON_MEDIA_TYPES.has(fld.type)) continue
398
+ columns.push({ table: e.table, name: getTableName(e.table), key })
399
+ if (fld.isLocalized && e.localesTable) columns.push({ table: e.localesTable, name: getTableName(e.localesTable), key })
400
+ }
401
+ }
402
+ for (let at = 0; at < media.length; at += 200) {
403
+ const batch = media.slice(at, at + 200)
404
+ const patterns = JSON.stringify(batch.flatMap((m) => [`%${escapeLike(m.id)}%`, `%${escapeLike(m.key)}%`]))
405
+ for (const { table, name, key } of columns) {
406
+ if (exceptTables.has(name)) continue
407
+ const col = (table as any)[key]
408
+ const res = await db.execute(sql`select cast(${col} as text) as t from ${table}
409
+ where cast(${col} as text) ilike any (array(select jsonb_array_elements_text(${patterns}::jsonb)))`)
410
+ for (const r of res.rows) {
411
+ const text = String(r.t).toLowerCase()
412
+ for (const m of batch) if (!used.has(m.id) && (text.includes(m.id.toLowerCase()) || text.includes(m.key.toLowerCase()))) used.add(m.id)
413
+ }
414
+ }
415
+ }
416
+ return used
417
+ }
418
+
419
+ /**
420
+ * Blocks (at any depth) stored at a version NEWER than this site's definition of the
421
+ * type: the site could neither read nor save them. One line per block, naming where.
422
+ */
423
+ export function newerStoredBlocks(value: unknown, defs: Record<string, any>, path: (string | number)[] = [], out: string[] = []): string[] {
424
+ if (!Array.isArray(value)) return out
425
+ value.forEach((b, i) => {
426
+ if (!b || typeof b !== 'object' || typeof b.type !== 'string' || !Object.hasOwn(defs, b.type)) return
427
+ const def = defs[b.type]
428
+ const have = storedBlockVersion(b)
429
+ if (have > blockVersion(def)) out.push(`the "${b.type}" block${typeof b.id === 'string' ? ` ${b.id}` : ''} at ${[...path, i].join('.')} is stored at version ${have}, this site's definition is at ${blockVersion(def)}`)
430
+ for (const [key, fld] of Object.entries(def.fields as Record<string, any>)) if (fld.type === 'blocks' && fld.blocks) newerStoredBlocks(b[key], fld.blocks, [...path, i, key], out)
431
+ })
432
+ return out
433
+ }
434
+
435
+ // ── the order tables can be filled in ───────────────────────────────────────────
436
+
437
+ export interface FkEdge { entity: string; key: string; prop: string; col: any; to: string; required: boolean }
438
+
439
+ /**
440
+ * Dependency order of `entities` by their many-to-one links (a post needs its author
441
+ * first). A link that cannot be satisfied in any order — a page's parent, two
442
+ * entities pointing at each other — is *deferred*: its column is left empty while
443
+ * rows are inserted and filled in afterwards. A required link in a cycle cannot be
444
+ * deferred and is an error.
445
+ */
446
+ export function dependencyOrder(reg: CmsRegistry, entities: CmsEntity[]): { order: CmsEntity[]; deferred: Map<string, FkEdge[]> } {
447
+ const byName = new Map(entities.map((e) => [e.name, e]))
448
+ const edges = new Map<string, FkEdge[]>()
449
+ for (const e of entities) {
450
+ const layout = layoutOf(reg, e)
451
+ const list: FkEdge[] = []
452
+ for (const f of layout.fields) {
453
+ if (f.fld.relation?.kind !== 'many-to-one') continue
454
+ const to = (f.fld.relation.to() as CmsEntity).name
455
+ if (byName.has(to)) list.push({ entity: e.name, key: f.key, prop: f.prop, col: f.col, to, required: !!f.col.notNull })
456
+ }
457
+ edges.set(e.name, list)
458
+ }
459
+ const placed = new Set<string>()
460
+ const remaining = new Set([...byName.keys()].sort())
461
+ const order: CmsEntity[] = []
462
+ const deferred = new Map<string, FkEdge[]>()
463
+ const unmet = (n: string) => (edges.get(n) ?? []).filter((x) => !placed.has(x.to))
464
+ while (remaining.size) {
465
+ const names = [...remaining]
466
+ let pick = names.find((n) => unmet(n).length === 0)
467
+ if (!pick) {
468
+ pick = names.filter((n) => unmet(n).every((x) => !x.required)).sort((a, b) => unmet(a).length - unmet(b).length)[0]
469
+ if (!pick) {
470
+ const cycle = names.filter((n) => unmet(n).some((x) => x.required))
471
+ throw new Error(`Required links form a cycle (${cycle.join(', ')}); content like this cannot be imported in any order.`)
472
+ }
473
+ deferred.set(pick, unmet(pick))
474
+ }
475
+ remaining.delete(pick)
476
+ placed.add(pick)
477
+ order.push(byName.get(pick)!)
478
+ }
479
+ return { order, deferred }
480
+ }
481
+
482
+ // ── files: JSON lines ───────────────────────────────────────────────────────────
483
+
484
+ export class JsonlWriter {
485
+ private out: WriteStream
486
+ private failure: Error | null = null
487
+ count = 0
488
+ constructor(path: string) {
489
+ this.out = createWriteStream(path)
490
+ this.out.on('error', (err) => { this.failure = err })
491
+ }
492
+ async write(value: unknown): Promise<void> {
493
+ if (this.failure) throw this.failure
494
+ this.count++
495
+ if (!this.out.write(JSON.stringify(value) + '\n')) await once(this.out, 'drain')
496
+ }
497
+ async close(): Promise<void> {
498
+ if (this.failure) throw this.failure
499
+ await new Promise<void>((resolveClose, reject) => { this.out.once('error', reject); this.out.end(resolveClose) })
500
+ }
501
+ }
502
+
503
+ export async function* readJsonl<T = any>(path: string): AsyncGenerator<T> {
504
+ const lines = createInterface({ input: createReadStream(path, 'utf8'), crlfDelay: Infinity })
505
+ let n = 0
506
+ for await (const line of lines) {
507
+ n++
508
+ if (!line.trim()) continue
509
+ try { yield JSON.parse(line) as T }
510
+ catch { throw new Error(`${path.split(sep).slice(-2).join('/')}: line ${n} is not valid JSON — is the archive damaged?`) }
511
+ }
512
+ }
513
+
514
+ // ── the host: database and storage, as the server finds them ───────────────────
515
+
516
+ /** `host:port/name` of a connection string — never its password. */
517
+ export function describeDatabase(url: string): string {
518
+ try {
519
+ const u = new URL(url)
520
+ return `${u.host}${u.pathname}`
521
+ }
522
+ catch { return 'the database' }
523
+ }
524
+
525
+ export function databaseName(url: string): string {
526
+ try { return new URL(url).pathname.replace(/^\//, '') }
527
+ catch { return '' }
528
+ }
529
+
530
+ export function connectDb(url: string, reg: CmsRegistry) {
531
+ const pool = new pg.Pool({ connectionString: url, connectionTimeoutMillis: 10_000 })
532
+ const db = drizzle(pool, { schema: reg.schema })
533
+ return { db, pool, close: () => pool.end() }
534
+ }
535
+
536
+ /** Blob storage as the CLI needs it: read, write, and the public URL of a key. */
537
+ export interface TransferStorage {
538
+ describe: string
539
+ /**
540
+ * What identifies the place the files live in — the folder (on this machine), or the
541
+ * endpoint and bucket. Never printed or stored as it is: see `storageFingerprint`.
542
+ */
543
+ identity: string
544
+ get(key: string): Promise<Buffer | null>
545
+ put(key: string, body: Buffer, contentType: string): Promise<void>
546
+ url(key: string): string
547
+ }
548
+
549
+ /** A fingerprint of a storage's identity, safe to put in an archive (no path, no bucket name). */
550
+ export function storageFingerprint(identity: string): string {
551
+ return createHash('sha256').update(identity).digest('hex').slice(0, 32)
552
+ }
553
+
554
+ /**
555
+ * The host's database and storage settings, found the way the running app finds them:
556
+ * the environment, the host's `.env`, then the `cms:` options of nuxt.config
557
+ * (`loadSettings` of backup.ts — the same code `tinyzoo backup` uses).
558
+ */
559
+ export async function hostSettings(cwd: string, env: Record<string, string | undefined> = process.env) {
560
+ return loadSettings(cwd, env as NodeJS.ProcessEnv)
561
+ }
562
+
563
+ /**
564
+ * Storage the way the server resolves it (server/utils/cmsStorage.ts): the driver and
565
+ * its settings from `NUXT_CMS_STORAGE_*` or the `cms.storage` options, defaulting to the
566
+ * host's `.cms-uploads` folder. Keep the two in step (key layout, URLs).
567
+ */
568
+ export function storageFromSettings(storage: StorageSettings, override: { dir?: string; cwd?: string } = {}): TransferStorage {
569
+ const driver = storage.driver
570
+ if (driver === 'local') {
571
+ const dir = override.dir ? resolve(override.cwd ?? process.cwd(), override.dir) : storage.localDir
572
+ return localStorageAt(dir, (storage.localPublicBase || '/api/cms/_media/raw').replace(/\/$/, ''))
573
+ }
574
+ if (driver === 's3' || driver === 'r2') {
575
+ const s = { ...storage.s3, publicBaseUrl: storage.s3.publicBaseUrl || undefined, appBase: storage.localPublicBase }
576
+ const missing = (['endpoint', 'bucket', 'accessKeyId', 'secretAccessKey'] as const).filter((k) => !s[k])
577
+ if (missing.length) throw new Error(`Storage driver '${driver}' is missing ${missing.join(', ')} (set NUXT_CMS_STORAGE_S3_* in the environment, the host's .env, or cms.storage.s3 in nuxt.config).`)
578
+ return s3Storage({ ...s, delivery: resolveS3Delivery(s.delivery, s.publicBaseUrl) })
579
+ }
580
+ throw new Error(`Unknown storage driver '${driver}' (NUXT_CMS_STORAGE_DRIVER is 'local', 's3' or 'r2').`)
581
+ }
582
+
583
+ /** Settings from the environment and module options as plain data (for tests and `hostSettings`). */
584
+ export function storageFromEnv(cwd: string, env: Record<string, string | undefined> = process.env, override: { dir?: string } = {}, cms: unknown = {}): TransferStorage {
585
+ return storageFromSettings(settingsFrom(cwd, env as NodeJS.ProcessEnv, cms).storage, { ...override, cwd })
586
+ }
587
+
588
+ export function localStorageAt(dir: string, publicBase = '/api/cms/_media/raw'): TransferStorage {
589
+ const pathFor = (key: string) => {
590
+ const abs = resolve(dir, key)
591
+ if (!abs.startsWith(dir + sep)) throw new Error(`Invalid storage key: ${key}`)
592
+ return abs
593
+ }
594
+ return {
595
+ describe: `the folder ${dir}`,
596
+ // Another machine's folder with the same path is not the same files.
597
+ identity: `local:${hostname()}:${dir}`,
598
+ async get(key) {
599
+ try { return await (await import('node:fs/promises')).readFile(pathFor(key)) }
600
+ catch { return null }
601
+ },
602
+ async put(key, body) {
603
+ const { mkdir, writeFile } = await import('node:fs/promises')
604
+ const abs = pathFor(key)
605
+ await mkdir(resolve(abs, '..'), { recursive: true })
606
+ await writeFile(abs, body)
607
+ },
608
+ url: (key) => `${publicBase}/${key}`,
609
+ }
610
+ }
611
+
612
+ function s3Storage(cfg: { endpoint: string; region: string; bucket: string; accessKeyId: string; secretAccessKey: string; publicBaseUrl?: string; delivery: 'public' | 'proxy' | 'redirect'; appBase?: string }): TransferStorage {
613
+ let client: any
614
+ const getClient = async () => {
615
+ if (!client) {
616
+ const { AwsClient } = await import('aws4fetch')
617
+ client = new AwsClient({ accessKeyId: cfg.accessKeyId, secretAccessKey: cfg.secretAccessKey, region: cfg.region, service: 's3' })
618
+ }
619
+ return client
620
+ }
621
+ const encode = encodeStorageKey
622
+ const objectUrl = (key: string) => `${cfg.endpoint.replace(/\/$/, '')}/${cfg.bucket}/${encode(key)}`
623
+ return {
624
+ describe: `the bucket ${cfg.bucket}`,
625
+ identity: `s3:${cfg.endpoint.replace(/\/$/, '').toLowerCase()}/${cfg.bucket}`,
626
+ async get(key) {
627
+ const res = await (await getClient()).fetch(objectUrl(key))
628
+ if (res.status === 404) return null
629
+ if (!res.ok) throw new Error(`Could not read ${key} from the bucket (${res.status} ${res.statusText})`)
630
+ return Buffer.from(await res.arrayBuffer())
631
+ },
632
+ async put(key, body, contentType) {
633
+ const res = await (await getClient()).fetch(objectUrl(key), { method: 'PUT', body: new Uint8Array(body), headers: { 'content-type': contentType } })
634
+ if (!res.ok) throw new Error(`Could not write ${key} to the bucket (${res.status} ${res.statusText})`)
635
+ },
636
+ // What the running app stores for a new upload: the bucket's own address under `public`, the app's path otherwise.
637
+ url: (key) => storedMediaUrl(cfg, key),
638
+ }
639
+ }
640
+
641
+ // ── command line ────────────────────────────────────────────────────────────────
642
+
643
+ export interface ParsedArgs {
644
+ positional: string[]
645
+ values: Record<string, string>
646
+ flags: Set<string>
647
+ }
648
+
649
+ /** `--flag`, `--name value` / `--name=value`, and positional words. Unknown flags are an error. */
650
+ export function parseArgs(argv: string[], valueFlags: string[], boolFlags: string[]): ParsedArgs {
651
+ const out: ParsedArgs = { positional: [], values: {}, flags: new Set() }
652
+ for (let i = 0; i < argv.length; i++) {
653
+ const arg = argv[i]!
654
+ if (!arg.startsWith('--')) { out.positional.push(arg); continue }
655
+ const [name, inline] = arg.slice(2).split(/=(.*)/s, 2) as [string, string | undefined]
656
+ if (valueFlags.includes(name)) {
657
+ const value = inline ?? argv[++i]
658
+ if (value === undefined || value.startsWith('--')) throw new Error(`--${name} needs a value`)
659
+ out.values[name] = value
660
+ }
661
+ else if (boolFlags.includes(name)) out.flags.add(name)
662
+ else throw new Error(`Unknown option --${name}`)
663
+ }
664
+ return out
665
+ }
666
+
667
+ /** The tinyzoo version, from the package this file ships in. */
668
+ export function tinyzooVersion(): string {
669
+ try { return JSON.parse(readFileSync(resolve(import.meta.dirname, '..', 'package.json'), 'utf8')).version as string }
670
+ catch { return 'unknown' }
671
+ }
672
+
673
+ /** What a command line run does with a failure: one clear line, a non-zero exit. */
674
+ export function reportFailure(err: unknown): void {
675
+ console.error(`✗ ${err instanceof Error ? err.message : String(err)}`)
676
+ process.exitCode = 1
677
+ }