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
package/cli/import.ts ADDED
@@ -0,0 +1,643 @@
1
+ /**
2
+ * `tinyzoo import <file>` — put an archive made by `tinyzoo export` into this site.
3
+ *
4
+ * tinyzoo import site.tar.gz # the target must be empty of content
5
+ * tinyzoo import site.tar.gz --replace # delete this site's content first (asks; --yes skips)
6
+ * tinyzoo import site.tar.gz --merge # add and update entries by id, delete nothing
7
+ * tinyzoo import site.tar.gz --allow-mismatch # the content models differ: skip what doesn't fit
8
+ * tinyzoo import site.tar.gz --dry-run # check and report, write nothing
9
+ *
10
+ * Ids are kept, so links between entries and references from pages survive. Order of
11
+ * work: media files → media rows → entries in dependency order (a post after its
12
+ * author; a link that cannot be satisfied in any order — a page's parent — is left
13
+ * empty first and filled in afterwards) → translations → many-to-many links → saved
14
+ * versions. All database changes happen in ONE transaction: a failure leaves the
15
+ * database as it was. Media files are uploaded before it (checked against their
16
+ * hashes); a failed import can leave files in storage that nothing points to, which
17
+ * is harmless. The search index is rebuilt afterwards.
18
+ *
19
+ * `--replace` deletes the content the archive carries. Of the media library it deletes
20
+ * ALL of it for a full archive (the archive brings the whole library back), but for an
21
+ * `--only` archive just the media rows the archive lists that no other content uses
22
+ * (links and pictures inside rich text and blocks are checked, as the server's own
23
+ * clean-up does): the rest of the library — a site logo, pictures of pages that are not
24
+ * in the archive — is never touched. The question asked first says exactly this.
25
+ *
26
+ * Blocks stored at a version newer than this site's definition are refused (the
27
+ * content model section of the report), like any other difference in the model.
28
+ *
29
+ * A page tree (`definePages({ tree: true })`) is checked as a whole once it is written, in
30
+ * the same transaction, with the checks the server applies to every edit (no loops, not
31
+ * deeper than allowed, one page per address in each language): the database itself does not
32
+ * enforce them, and an archive (or `--merge` onto what is already there) can break them.
33
+ * A tree that is not valid refuses the import, with the pages named, and nothing is
34
+ * changed. With `--merge`, only problems that involve a page of the archive count, so an
35
+ * old problem in the target does not stop an unrelated import.
36
+ */
37
+ import { createHash } from 'node:crypto'
38
+ import { existsSync } from 'node:fs'
39
+ import { mkdtemp, readFile, rm } from 'node:fs/promises'
40
+ import { tmpdir } from 'node:os'
41
+ import { join, resolve } from 'node:path'
42
+ import { createInterface } from 'node:readline'
43
+ import { getTableColumns, getTableName, inArray, ne, sql } from 'drizzle-orm'
44
+ import { checkTree, type CmsEntity, type CmsRegistry } from '../src/index'
45
+ import { logUpgradeFailure, upgradeStoredRow } from '../src/blocks'
46
+ // The page tree's own loader and settings (explicit imports only, so the CLI can load them).
47
+ import { loadTreeNodes, treeOptions } from '../server/utils/cmsTreeAddress'
48
+ import { extractTarGz, safeArchivePath } from './tar'
49
+ import { loadRegistry } from './export'
50
+ import {
51
+ ARCHIVE_FILES, ARCHIVE_FORMAT, BATCH, SAFE_NAME, connectDb, databaseName, dependencyOrder, describeDatabase,
52
+ describeSchema, diffSchemas, formatDifferences, hostSettings, layoutOf, newerStoredBlocks, parseArgs, readJsonl, storageFingerprint, storageFromSettings,
53
+ usedMediaIds,
54
+ type Difference, type Entry, type EntityLayout, type FkEdge, type LayoutField, type Manifest, type TransferStorage,
55
+ } from './transfer'
56
+
57
+ export type ImportMode = 'strict' | 'replace' | 'merge'
58
+
59
+ export interface ImportOptions {
60
+ reg: CmsRegistry
61
+ databaseUrl: string
62
+ /** Where media files are written, and what their public addresses are. */
63
+ storage: TransferStorage
64
+ file: string
65
+ /** `strict` (default): the target must be empty. */
66
+ mode?: ImportMode
67
+ allowMismatch?: boolean
68
+ /** Asked before `replace` deletes anything; return false to stop. Omit to go ahead. */
69
+ confirm?: (summary: string) => Promise<boolean>
70
+ /** Check everything and report; write nothing. */
71
+ dryRun?: boolean
72
+ /** Rebuild the search index afterwards (default true). */
73
+ searchIndex?: boolean
74
+ log?: (message: string) => void
75
+ }
76
+
77
+ export interface ImportReport {
78
+ mode: ImportMode
79
+ dryRun: boolean
80
+ entries: Record<string, { rows: number; translations: number }>
81
+ media: { rows: number; files: number; missingFiles: number }
82
+ relations: number
83
+ revisions: number
84
+ deleted: Record<string, number>
85
+ skipped: { entities: string[]; fields: string[]; translations: number }
86
+ differences: Difference[]
87
+ searchIndex: 'rebuilt' | 'busy' | 'failed' | 'skipped'
88
+ /** Things the person should read even though the import went ahead. */
89
+ warnings: string[]
90
+ }
91
+
92
+ export class ImportRefused extends Error {
93
+ constructor(public code: 'not-empty' | 'mismatch' | 'format' | 'aborted' | 'invalid', message: string, public differences: Difference[] = []) {
94
+ super(message)
95
+ this.name = 'ImportRefused'
96
+ }
97
+ }
98
+
99
+ const invalid = (message: string) => new ImportRefused('invalid', message)
100
+
101
+ // ── the archive ─────────────────────────────────────────────────────────────────
102
+
103
+ async function readManifest(dir: string): Promise<Manifest> {
104
+ const path = join(dir, ARCHIVE_FILES.manifest)
105
+ if (!existsSync(path)) throw invalid('This is not a tinyzoo content archive (there is no manifest.json in it).')
106
+ let m: Manifest
107
+ try { m = JSON.parse(await readFile(path, 'utf8')) }
108
+ catch { throw invalid('manifest.json is not valid JSON — is the archive damaged?') }
109
+ if (typeof m.format !== 'number') throw invalid('manifest.json has no format number.')
110
+ if (m.format > ARCHIVE_FORMAT) {
111
+ throw new ImportRefused('format', `This archive is format ${m.format} (made by tinyzoo ${m.tinyzoo}); this tinyzoo reads format ${ARCHIVE_FORMAT}. Upgrade tinyzoo, then import again.`)
112
+ }
113
+ // Archive upgrade hook: a format older than ARCHIVE_FORMAT would be brought up to it
114
+ // here (rewriting `m` and the files in `dir`) before anything is read. There is only
115
+ // format 1 so far.
116
+ if (m.format < 1) throw invalid(`Unknown archive format ${m.format}.`)
117
+ if (!m.schema?.entities || !Array.isArray(m.entities)) throw invalid('manifest.json is incomplete.')
118
+ for (const x of m.entities) if (!SAFE_NAME.test(x.name)) throw invalid(`The archive names content "${x.name}", which is not a valid name.`)
119
+ return m
120
+ }
121
+
122
+ // ── decoding ────────────────────────────────────────────────────────────────────
123
+
124
+ const JSON_TYPES = new Set(['richtext', 'blocks', 'group'])
125
+
126
+ /** Point every embedded picture at the file's address in THIS storage. */
127
+ function rewriteImages(node: any, urls: Map<string, string>): void {
128
+ if (!node || typeof node !== 'object') return
129
+ if (Array.isArray(node)) { for (const x of node) rewriteImages(x, urls); return }
130
+ if (node.type === 'image' && node.attrs && typeof node.attrs.mediaId === 'string') {
131
+ const url = urls.get(node.attrs.mediaId)
132
+ if (url) node.attrs.src = url
133
+ }
134
+ for (const v of Object.values(node)) if (v && typeof v === 'object') rewriteImages(v, urls)
135
+ }
136
+
137
+ interface Plan {
138
+ reg: CmsRegistry
139
+ manifest: Manifest
140
+ urls: Map<string, string>
141
+ skipFields: Map<string, Set<string>>
142
+ skippedFields: Set<string>
143
+ locales: Set<string> | null
144
+ deferred: Map<string, FkEdge[]>
145
+ }
146
+
147
+ function decodeValue(plan: Plan, e: CmsEntity, f: LayoutField, raw: unknown, id: string): unknown {
148
+ if (raw === null || raw === undefined) return null
149
+ if (f.isDate) {
150
+ const d = new Date(raw as string)
151
+ if (Number.isNaN(d.getTime())) throw invalid(`${e.name} ${id}: "${f.key}" is not a valid date (${JSON.stringify(raw)}).`)
152
+ return d
153
+ }
154
+ const base = f.fld.spec?.base
155
+ if (base?.kind === 'enum' && !base.values.includes(raw as string)) {
156
+ throw invalid(`${e.name} ${id}: "${f.key}" is "${String(raw)}", which is not one of ${base.values.join(', ')} here. Fix the choices or the entry, then import again.`)
157
+ }
158
+ if (JSON_TYPES.has(f.fld.type)) {
159
+ // An archive from an older site may hold blocks, list items or rich text at older
160
+ // versions: bring them to the current ones (as every read does), so what is
161
+ // written is what this site's code expects. A block whose upgrade throws is
162
+ // logged and imported as it was; the admin shows it as needing attention.
163
+ const where = `${e.name} ${id}`
164
+ // Blocks written by a NEWER version of the site cannot be read or saved here.
165
+ if (f.fld.type === 'blocks' && f.fld.blocks) {
166
+ const newer = newerStoredBlocks(raw, f.fld.blocks, [f.key])
167
+ if (newer.length) {
168
+ throw new ImportRefused('mismatch', `${where}: ${newer.slice(0, 3).join('; ')}${newer.length > 3 ? `; and ${newer.length - 3} more` : ''}. The archive comes from a newer version of the content model: update this site first.`)
169
+ }
170
+ }
171
+ raw = upgradeStoredRow({ [f.key]: f.fld }, { [f.key]: raw }, { where, onError: (issue) => logUpgradeFailure(issue, where) })[f.key]
172
+ rewriteImages(raw, plan.urls)
173
+ }
174
+ return raw
175
+ }
176
+
177
+ function decodeEntry(plan: Plan, layout: EntityLayout, entry: Entry) {
178
+ const e = layout.e
179
+ const skip = plan.skipFields.get(e.name)
180
+ const deferredProps = new Set((plan.deferred.get(e.name) ?? []).map((x) => x.prop))
181
+ const row: Record<string, unknown> = { id: entry.id, createdAt: new Date(entry.createdAt), updatedAt: new Date(entry.updatedAt) }
182
+ const fixups: { prop: string; id: string; value: string }[] = []
183
+ for (const [key, raw] of Object.entries(entry.fields ?? {})) {
184
+ const f = layout.byKey.get(key)
185
+ if (!f || skip?.has(key)) { plan.skippedFields.add(`${e.name}.${key}`); continue }
186
+ const value = decodeValue(plan, e, f, raw, entry.id)
187
+ if (deferredProps.has(f.prop) && value != null) { fixups.push({ prop: f.prop, id: entry.id, value: value as string }); row[f.prop] = null }
188
+ else row[f.prop] = value
189
+ }
190
+ const translations: Record<string, unknown>[] = []
191
+ let skippedTranslations = 0
192
+ for (const tr of entry.translations ?? []) {
193
+ if (!e.localesTable || (plan.locales && !plan.locales.has(tr.locale))) { skippedTranslations++; continue }
194
+ const t: Record<string, unknown> = { id: tr.id, parentId: entry.id, locale: tr.locale, createdAt: new Date(tr.createdAt), updatedAt: new Date(tr.updatedAt) }
195
+ for (const key of e.localized) {
196
+ const f = layout.byKey.get(key)
197
+ if (f && key in tr.fields && !skip?.has(key)) t[key] = decodeValue(plan, e, f, tr.fields[key], tr.id)
198
+ }
199
+ translations.push(t)
200
+ }
201
+ return { row, fixups, translations, skippedTranslations }
202
+ }
203
+
204
+ // ── writing ─────────────────────────────────────────────────────────────────────
205
+
206
+ type Write = 'insert' | 'upsert' | 'ignore'
207
+
208
+ /** Insert `rows` in batches that stay under Postgres' parameter limit. */
209
+ async function writeRows(tx: any, table: any, rows: Record<string, unknown>[], how: Write, target?: any[]): Promise<void> {
210
+ if (!rows.length) return
211
+ const keys = [...new Set(rows.flatMap((r) => Object.keys(r)))]
212
+ const size = Math.max(1, Math.min(BATCH, Math.floor(30_000 / keys.length)))
213
+ const cols = getTableColumns(table) as Record<string, any>
214
+ const targetNames = new Set((target ?? [table.id]).map((c) => c.name))
215
+ for (let i = 0; i < rows.length; i += size) {
216
+ const q = tx.insert(table).values(rows.slice(i, i + size))
217
+ if (how === 'insert') await q
218
+ else if (how === 'ignore') await q.onConflictDoNothing()
219
+ else {
220
+ const set = Object.fromEntries(keys.filter((k) => cols[k] && !targetNames.has(cols[k].name)).map((k) => [k, sql`excluded.${sql.identifier(cols[k].name)}`]))
221
+ await (Object.keys(set).length ? q.onConflictDoUpdate({ target: target ?? [table.id], set }) : q.onConflictDoNothing())
222
+ }
223
+ }
224
+ }
225
+
226
+ function explain(err: any, doing: string): Error {
227
+ if (err instanceof ImportRefused) return err
228
+ const code = err?.code ?? err?.cause?.code
229
+ const detail = err?.detail ?? err?.cause?.detail ?? ''
230
+ const message = err?.cause?.message ?? err?.message ?? String(err)
231
+ if (code === '42P01') return invalid(`The database has no table for this content (${message}). Run the migrations first (tinyzoo up).`)
232
+ if (code === '23503') return invalid(`${doing}: it points at something that is not there — ${detail || message}. An archive made with --only needs the rest of the content in the target already (use --merge), or export everything.`)
233
+ if (code === '23502') return invalid(`${doing}: a required value is missing — ${message}. Was the archive exported from a different version of the content model?`)
234
+ if (code === '23505') return invalid(`${doing}: ${detail || message}. Something with the same unique value is already there (use --merge to update it by id, or --replace).`)
235
+ return new Error(`${doing}: ${message}`)
236
+ }
237
+
238
+ /** The refusal for a page tree that would not be valid: what is wrong, and what to do. */
239
+ function treeReport(entity: string, problems: string[], mode: ImportMode): string {
240
+ const shown = problems.slice(0, 10).map((m) => ` - ${m}`)
241
+ if (problems.length > shown.length) shown.push(` - … and ${problems.length - shown.length} more`)
242
+ return `The page tree of ${entity} would not be valid after this import, so nothing was imported:\n${shown.join('\n')}\n`
243
+ + `Fix the parent or the address part of these pages (in the site the archive was made from${mode === 'merge' ? ', or in the pages already here' : ''}), then export and import again.`
244
+ }
245
+
246
+ async function countRows(db: any, table: any): Promise<number> {
247
+ const [row] = await db.select({ n: sql<number>`count(*)::int` }).from(table)
248
+ return Number(row?.n ?? 0)
249
+ }
250
+
251
+ /** The archive file holding an entity's entries, or null when it has none. The name comes from the entity, never from the manifest. */
252
+ function entryFile(dir: string, e: CmsEntity): string | null {
253
+ const path = join(dir, e.singleton ? ARCHIVE_FILES.global(e.name) : ARCHIVE_FILES.content(e.name))
254
+ return existsSync(path) ? path : null
255
+ }
256
+
257
+ function entriesOf(e: CmsEntity, path: string): AsyncIterable<Entry> {
258
+ return e.singleton
259
+ ? (async function* () { yield JSON.parse(await readFile(path, 'utf8')).entry as Entry })()
260
+ : readJsonl<Entry>(path)
261
+ }
262
+
263
+ // ── the import ──────────────────────────────────────────────────────────────────
264
+
265
+ export async function importContent(opts: ImportOptions): Promise<ImportReport> {
266
+ const { reg, storage } = opts
267
+ const mode = opts.mode ?? 'strict'
268
+ const log = opts.log ?? (() => {})
269
+ const file = resolve(opts.file)
270
+ if (!existsSync(file)) throw new Error(`${file} does not exist`)
271
+
272
+ const tmp = await mkdtemp(join(tmpdir(), 'tinyzoo-import-'))
273
+ const { db, close } = connectDb(opts.databaseUrl, reg)
274
+ try {
275
+ try { await extractTarGz(file, tmp) }
276
+ catch (err: any) { throw invalid(`Could not read the archive: ${err?.message ?? err}`) }
277
+ const manifest = await readManifest(tmp)
278
+
279
+ // ── is the archive for this site? ─────────────────────────────────────────
280
+ const archiveNames = new Set(manifest.entities.map((x) => x.name))
281
+ const all = diffSchemas(manifest.schema, describeSchema(reg))
282
+ // Only what the archive carries matters (an --only archive says nothing of the rest).
283
+ const differences: Difference[] = all.filter((d) => !d.entity || archiveNames.has(d.entity))
284
+ const locales = reg.locales ? new Set(reg.locales.locales) : null
285
+ if (manifest.locales && reg.locales) {
286
+ if (manifest.locales.default !== reg.locales.default) {
287
+ throw new ImportRefused('mismatch', `The default language differs: the archive's entries are written in "${manifest.locales.default}", this site's default is "${reg.locales.default}". The entries cannot be mapped, even with --allow-mismatch.`)
288
+ }
289
+ for (const l of manifest.locales.all) if (!locales!.has(l)) differences.push({ scope: 'locales', message: `language "${l}" is in the archive but not configured here (its translations are left out)`, skip: true })
290
+ for (const l of locales!) if (!manifest.locales.all.includes(l)) differences.push({ scope: 'locales', message: `language "${l}" is configured here but not in the archive (nothing to import for it)`, skip: false })
291
+ }
292
+ if (differences.length && !opts.allowMismatch) {
293
+ throw new ImportRefused('mismatch',
294
+ `The archive was exported from a different content model (tinyzoo ${manifest.tinyzoo}, fingerprint ${manifest.fingerprint.slice(0, 12)}):\n${formatDifferences(differences)}\n`
295
+ + 'Bring the content models in line (same version of the site), or import with --allow-mismatch to skip what does not fit.',
296
+ differences)
297
+ }
298
+ const skipEntities = new Set(differences.filter((d) => d.scope === 'entity' && d.skip).map((d) => d.entity!))
299
+ const skipFields = new Map<string, Set<string>>()
300
+ for (const d of differences) if ((d.scope === 'field' || d.scope === 'blocks') && d.skip) skipFields.set(d.entity!, (skipFields.get(d.entity!) ?? new Set()).add(d.field!))
301
+
302
+ const planned = manifest.entities.filter((x) => !skipEntities.has(x.name) && reg.byName.has(x.name))
303
+ const included = planned.map((x) => reg.byName.get(x.name)!)
304
+ const { order, deferred } = dependencyOrder(reg, included)
305
+ const mediaFile = join(tmp, ARCHIVE_FILES.media)
306
+ const mediaEntries: Record<string, any>[] = []
307
+ if (existsSync(mediaFile)) for await (const m of readJsonl<Record<string, any>>(mediaFile)) mediaEntries.push(m)
308
+ // A full archive owns the whole media library. An --only archive owns just the files
309
+ // it lists: the rest of the library belongs to content that is not in the archive.
310
+ const fullMedia = manifest.only === null
311
+ const listedMedia = !fullMedia && mediaEntries.length > 0
312
+
313
+ const report: ImportReport = {
314
+ mode, dryRun: !!opts.dryRun, entries: {}, media: { rows: 0, files: 0, missingFiles: 0 }, relations: 0, revisions: 0, deleted: {},
315
+ skipped: { entities: [...skipEntities].sort(), fields: [], translations: 0 }, differences, searchIndex: 'skipped', warnings: [],
316
+ }
317
+
318
+ // The files may be the very ones another environment serves: say so.
319
+ if (manifest.media.storage && manifest.media.storage === storageFingerprint(storage.identity)) {
320
+ const warning = `The archive's files come from the SAME storage this site writes to (${storage.describe}). Two environments must not share a bucket or folder: deleting a file in one deletes it in the other. Give each environment its own (or its own prefix) and import with the files, or only use --no-media if the target really has its own copy of every file.`
321
+ report.warnings.push(warning)
322
+ log(`\n !! ${warning}\n`)
323
+ }
324
+
325
+ // ── what is in the target ─────────────────────────────────────────────────
326
+ const present: Record<string, number> = {}
327
+ const replacedTables = new Set(order.flatMap((e) => [getTableName(e.table), ...(e.localesTable ? [getTableName(e.localesTable)] : [])]))
328
+ let existingListed: { id: string; key: string }[] = []
329
+ let freeToDelete: string[] = []
330
+ try {
331
+ for (const e of order) present[e.name] = await countRows(db, e.table)
332
+ if (fullMedia) present.media = await countRows(db, reg.cmsMedia)
333
+ else if (listedMedia) {
334
+ const ids = mediaEntries.map((m) => m.id as string)
335
+ for (let at = 0; at < ids.length; at += 1000) {
336
+ existingListed.push(...(await db.select({ id: reg.cmsMedia.id, key: reg.cmsMedia.key }).from(reg.cmsMedia).where(inArray(reg.cmsMedia.id, ids.slice(at, at + 1000)))))
337
+ }
338
+ present.media = existingListed.length
339
+ // With --replace: of those, the ones nothing outside the replaced content uses.
340
+ if (mode === 'replace') {
341
+ const used = await usedMediaIds(db, reg, existingListed, replacedTables)
342
+ freeToDelete = existingListed.filter((m) => !used.has(m.id)).map((m) => m.id)
343
+ }
344
+ }
345
+ }
346
+ catch (err) { throw explain(err, 'Could not read the target') }
347
+ const occupied = Object.entries(present).filter(([, n]) => n > 0)
348
+ const label = (list: [string, number][]) => list.map(([k, n]) => `${k} (${n})`).join(', ')
349
+
350
+ if (mode === 'strict' && occupied.length) {
351
+ throw new ImportRefused('not-empty',
352
+ `The target is not empty: ${label(occupied)}.\n`
353
+ + 'Import into an empty database, or use --replace (deletes that content first) or --merge (adds and updates entries by id).')
354
+ }
355
+ // What --replace deletes, said exactly: the entries of the archive's content, and of the
356
+ // media library either all of it (a full archive) or only what the archive lists.
357
+ const contentOccupied = occupied.filter(([k]) => k !== 'media')
358
+ const mediaDeleteText = fullMedia
359
+ ? (present.media ? `, and the whole media library (${present.media} rows; the media files stay in storage)` : '')
360
+ : listedMedia
361
+ ? `, and of the media library only the ${freeToDelete.length} row(s) the archive lists that nothing else uses`
362
+ + `${existingListed.length - freeToDelete.length ? ` (${existingListed.length - freeToDelete.length} more it lists are used by other content and are updated in place)` : ''}; the rest of the library is not touched; the media files stay in storage`
363
+ : ''
364
+ if (mode === 'replace' && (contentOccupied.length || present.media) && opts.confirm && !opts.dryRun) {
365
+ const ok = await opts.confirm(`This deletes ${contentOccupied.length ? label(contentOccupied) : 'no entries'} from ${describeDatabase(opts.databaseUrl)}${mediaDeleteText}, `
366
+ + `then imports ${planned.reduce((n, x) => n + x.rows, 0)} entries`
367
+ + `${manifest.media.rows ? ` and ${manifest.media.rows} media` : ''} from the archive.`)
368
+ if (!ok) throw new ImportRefused('aborted', 'Stopped — nothing was changed.')
369
+ }
370
+ if (mode === 'replace') {
371
+ report.deleted = Object.fromEntries(contentOccupied)
372
+ if (fullMedia && present.media) report.deleted.media = present.media
373
+ else if (listedMedia && freeToDelete.length) report.deleted.media = freeToDelete.length
374
+ }
375
+
376
+ for (const d of differences) log(` ! ${d.message}`)
377
+ if (opts.dryRun) {
378
+ for (const x of planned) report.entries[x.name] = { rows: x.rows, translations: x.translations }
379
+ report.media.rows = manifest.media.rows
380
+ return report
381
+ }
382
+
383
+ // ── media files (before the transaction) ──────────────────────────────────
384
+ const urls = new Map<string, string>()
385
+ for (const m of mediaEntries) urls.set(m.id, storage.url(m.key))
386
+ if (manifest.media.included) {
387
+ let next = 0
388
+ await Promise.all(Array.from({ length: Math.min(4, mediaEntries.length) }, async () => {
389
+ while (next < mediaEntries.length) {
390
+ const m = mediaEntries[next++]!
391
+ if (!m.blob) { report.media.missingFiles++; continue }
392
+ const path = join(tmp, ARCHIVE_FILES.mediaDir, safeArchivePath(m.key))
393
+ if (!existsSync(path)) throw invalid(`The archive lists the file ${m.key} but does not contain it.`)
394
+ const body = await readFile(path)
395
+ if (createHash('sha256').update(body).digest('hex') !== m.blob.sha256) throw invalid(`The file ${m.key} in the archive does not match its checksum — is the archive damaged?`)
396
+ await storage.put(m.key, body, m.mime)
397
+ report.media.files++
398
+ }
399
+ }))
400
+ }
401
+
402
+ const plan: Plan = { reg, manifest, urls, skipFields, skippedFields: new Set(), locales, deferred }
403
+ const write: Write = mode === 'merge' ? 'upsert' : 'insert'
404
+ // Replacing with an --only archive keeps the media that other content still uses: those rows are updated, not re-created.
405
+ const mediaWrite: Write = mode === 'merge' || (mode === 'replace' && !fullMedia) ? 'upsert' : 'insert'
406
+
407
+ // ── one transaction ───────────────────────────────────────────────────────
408
+ await db.transaction(async (tx: any) => {
409
+ if (mode === 'replace') {
410
+ let doing = 'Could not delete the existing content'
411
+ try {
412
+ // A link left empty first, so tables that point at themselves can be emptied.
413
+ for (const edges of deferred.values()) for (const ed of edges) await tx.update(reg.byName.get(ed.entity)!.table).set({ [ed.prop]: null })
414
+ for (const e of [...order].reverse()) { doing = `Could not delete ${e.name}`; await tx.delete(e.table) }
415
+ const names = order.map((e) => e.name)
416
+ if (names.length) {
417
+ await tx.delete(reg.cmsRevisions).where(inArray(reg.cmsRevisions.entity, names))
418
+ // Live-editing state of deleted entries would otherwise outlive them.
419
+ for (const n of names) await tx.delete(reg.cmsCollab).where(sql`starts_with(${reg.cmsCollab.name}, ${n + '/'})`)
420
+ }
421
+ if (fullMedia) { doing = 'Could not delete the media library'; await tx.delete(reg.cmsMedia) }
422
+ else if (listedMedia && existingListed.length) {
423
+ // Only what the archive lists, and only what nothing uses now that the replaced content is gone
424
+ // (checked here, inside the transaction, on the real state).
425
+ doing = 'Could not delete the media the archive replaces'
426
+ const used = await usedMediaIds(tx, reg, existingListed)
427
+ const free = existingListed.filter((m) => !used.has(m.id)).map((m) => m.id)
428
+ for (let at = 0; at < free.length; at += 1000) await tx.delete(reg.cmsMedia).where(inArray(reg.cmsMedia.id, free.slice(at, at + 1000)))
429
+ if (free.length) report.deleted.media = free.length
430
+ else delete report.deleted.media
431
+ }
432
+ }
433
+ catch (err) { throw explain(err, doing) }
434
+ }
435
+
436
+ // media rows
437
+ try {
438
+ const cols = getTableColumns(reg.cmsMedia) as Record<string, any>
439
+ const rows = mediaEntries.map((m) => {
440
+ const row: Record<string, unknown> = { url: urls.get(m.id) }
441
+ for (const [k, col] of Object.entries(cols)) {
442
+ if (k === 'url' || k === 'uploadedBy' || !(k in m)) continue
443
+ row[k] = col.dataType === 'date' && m[k] != null ? new Date(m[k]) : m[k]
444
+ }
445
+ return row
446
+ })
447
+ await writeRows(tx, reg.cmsMedia, rows, mediaWrite)
448
+ report.media.rows = rows.length
449
+ if (rows.length) log(` media: ${rows.length} rows${report.media.files ? `, ${report.media.files} files uploaded` : ''}`)
450
+ }
451
+ catch (err) { throw explain(err, 'Could not write the media library') }
452
+
453
+ // entries
454
+ const fixes: { e: CmsEntity; edge: FkEdge; rows: { id: string; v: string }[] }[] = []
455
+ // Which pages of each tree the archive wrote (for the check of the tree afterwards).
456
+ const writtenTree = new Map<string, Set<string>>()
457
+ for (const e of order) {
458
+ const layout = layoutOf(reg, e)
459
+ const counts = (report.entries[e.name] = { rows: 0, translations: 0 })
460
+ const path = entryFile(tmp, e)
461
+ if (!path) continue
462
+ const entries = entriesOf(e, path)
463
+ const edges = deferred.get(e.name) ?? []
464
+ const pending = new Map(edges.map((ed) => [ed.prop, { e, edge: ed, rows: [] as { id: string; v: string }[] }]))
465
+ let rows: Record<string, unknown>[] = []
466
+ let trs: Record<string, unknown>[] = []
467
+ const flush = async () => {
468
+ await writeRows(tx, e.table, rows, write)
469
+ if (trs.length) await writeRows(tx, e.localesTable, trs, write === 'upsert' ? 'upsert' : 'insert', write === 'upsert' ? [e.localesTable.parentId, e.localesTable.locale] : undefined)
470
+ counts.rows += rows.length
471
+ counts.translations += trs.length
472
+ rows = []
473
+ trs = []
474
+ }
475
+ try {
476
+ for await (const entry of entries) {
477
+ const d = decodeEntry(plan, layout, entry)
478
+ if (e.tree) writtenTree.set(e.name, (writtenTree.get(e.name) ?? new Set()).add(entry.id))
479
+ // A global has one row: the archive's replaces whatever the target has.
480
+ if (e.singleton && mode === 'merge') await tx.delete(e.table).where(ne(e.table.id, entry.id))
481
+ rows.push(d.row)
482
+ trs.push(...d.translations)
483
+ report.skipped.translations += d.skippedTranslations
484
+ for (const fx of d.fixups) pending.get(fx.prop)!.rows.push({ id: fx.id, v: fx.value })
485
+ if (rows.length >= BATCH) await flush()
486
+ }
487
+ await flush()
488
+ }
489
+ catch (err) { throw explain(err, `Could not write ${e.name}`) }
490
+ for (const p of pending.values()) if (p.rows.length) fixes.push(p)
491
+ log(` ${e.name}: ${counts.rows} ${e.singleton ? 'global' : 'entries'}${counts.translations ? `, ${counts.translations} translations` : ''}`)
492
+ }
493
+
494
+ // links that had to wait for their target
495
+ for (const { e, edge, rows } of fixes) {
496
+ try {
497
+ for (let i = 0; i < rows.length; i += 1000) {
498
+ await tx.execute(sql`update ${e.table} set ${sql.identifier(edge.col.name)} = d.v from jsonb_to_recordset(${JSON.stringify(rows.slice(i, i + 1000))}::jsonb) as d(id uuid, v uuid) where ${e.table.id} = d.id`)
499
+ }
500
+ }
501
+ catch (err) { throw explain(err, `Could not link ${e.name}.${edge.key}`) }
502
+ }
503
+
504
+ // page trees: parents are in place now; the tree as a whole has to be valid
505
+ for (const e of order) {
506
+ const written = writtenTree.get(e.name)
507
+ if (!e.tree || !written?.size) continue
508
+ let issues
509
+ try {
510
+ issues = checkTree(await loadTreeNodes(tx, reg, e), treeOptions(reg, e), 1000).filter((i) => i.ids.some((id) => written.has(id)))
511
+ }
512
+ catch (err) { throw explain(err, `Could not check the page tree of ${e.name}`) }
513
+ if (issues.length) throw invalid(treeReport(e.name, issues.map((i) => i.message), mode))
514
+ }
515
+
516
+ // many-to-many links: read again, now that every entry exists
517
+ for (const e of order) {
518
+ const layout = layoutOf(reg, e)
519
+ const fields = layout.m2m.filter((x) => !skipFields.get(e.name)?.has(x.key))
520
+ const path = entryFile(tmp, e)
521
+ if (!fields.length || !path) continue
522
+ const entries = entriesOf(e, path)
523
+ const owners: Record<string, string[]> = Object.fromEntries(fields.map((x) => [x.key, []]))
524
+ const links: Record<string, Record<string, unknown>[]> = Object.fromEntries(fields.map((x) => [x.key, []]))
525
+ const flush = async () => {
526
+ for (const { key, junction: j } of fields) {
527
+ // Merging sets a field's links to the archive's: the old ones go first.
528
+ if (mode === 'merge' && owners[key]!.length) await tx.delete(j.table).where(inArray(j.table[j.ownerCol], owners[key]!))
529
+ await writeRows(tx, j.table, links[key]!, 'ignore', [j.table[j.ownerCol], j.table[j.targetCol]])
530
+ report.relations += links[key]!.length
531
+ owners[key] = []
532
+ links[key] = []
533
+ }
534
+ }
535
+ try {
536
+ let n = 0
537
+ for await (const entry of entries) {
538
+ for (const { key, junction: j } of fields) {
539
+ const ids = entry.relations?.[key]
540
+ if (!Array.isArray(ids)) continue
541
+ owners[key]!.push(entry.id)
542
+ for (const target of ids) links[key]!.push({ [j.ownerCol]: entry.id, [j.targetCol]: target })
543
+ }
544
+ if (++n % 500 === 0) await flush()
545
+ }
546
+ await flush()
547
+ }
548
+ catch (err) { throw explain(err, `Could not link ${e.name}`) }
549
+ }
550
+
551
+ // saved versions
552
+ const revPath = join(tmp, ARCHIVE_FILES.revisions)
553
+ if (manifest.revisions?.included && existsSync(revPath)) {
554
+ const names = new Set(order.map((e) => e.name))
555
+ const cols = getTableColumns(reg.cmsRevisions) as Record<string, any>
556
+ let rows: Record<string, unknown>[] = []
557
+ const flush = async () => { await writeRows(tx, reg.cmsRevisions, rows, mode === 'merge' ? 'ignore' : 'insert'); report.revisions += rows.length; rows = [] }
558
+ try {
559
+ for await (const r of readJsonl<Record<string, any>>(revPath)) {
560
+ if (!names.has(r.entity)) continue
561
+ const row: Record<string, unknown> = {}
562
+ for (const [k, col] of Object.entries(cols)) {
563
+ if (k === 'userId' || !(k in r)) continue
564
+ row[k] = col.dataType === 'date' && r[k] != null ? new Date(r[k]) : r[k]
565
+ }
566
+ rows.push(row)
567
+ if (rows.length >= BATCH) await flush()
568
+ }
569
+ await flush()
570
+ }
571
+ catch (err) { throw explain(err, 'Could not write the saved versions') }
572
+ log(` revisions: ${report.revisions}`)
573
+ }
574
+ })
575
+ report.skipped.fields = [...plan.skippedFields].sort()
576
+
577
+ // ── the search index ──────────────────────────────────────────────────────
578
+ if (opts.searchIndex !== false) {
579
+ try {
580
+ // The server's own index builder. Loaded by path (and not at the top) because it
581
+ // belongs to the Nitro server code, which a plain `tsx` run does not bundle.
582
+ const spec = '../server/utils/cmsSearch'
583
+ const built = await (await import(/* @vite-ignore */ spec)).rebuildSearchIndex(db, reg)
584
+ report.searchIndex = built === null ? 'busy' : 'rebuilt'
585
+ log(built === null ? ' search index: another process is rebuilding it' : ` search index: ${built} rows`)
586
+ }
587
+ catch (err: any) {
588
+ report.searchIndex = 'failed'
589
+ // Next start finds no index state and rebuilds it by itself.
590
+ await db.delete(reg.cmsState).where(sql`${reg.cmsState.key} = 'search.index'`).catch(() => {})
591
+ log(` ! could not rebuild the search index (${err?.message ?? err}). It is rebuilt when the site next starts, or run: npx nitro task run cms:search-reindex`)
592
+ }
593
+ }
594
+ return report
595
+ }
596
+ finally {
597
+ await close()
598
+ await rm(tmp, { recursive: true, force: true })
599
+ }
600
+ }
601
+
602
+ // ── command line ────────────────────────────────────────────────────────────────
603
+
604
+ export const IMPORT_USAGE = `usage: tinyzoo import <file.tar.gz> [--replace [--yes] | --merge] [--allow-mismatch] [--dry-run] [--no-search-index]`
605
+
606
+ async function askToContinue(summary: string, dbName: string): Promise<boolean> {
607
+ if (!process.stdin.isTTY) throw new ImportRefused('aborted', `${summary}\nThis is not a terminal to ask in: pass --yes to go ahead.`)
608
+ const rl = createInterface({ input: process.stdin, output: process.stdout })
609
+ const answer = await new Promise<string>((done) => rl.question(`${summary}\nType the database name (${dbName}) to continue: `, done))
610
+ rl.close()
611
+ return answer.trim() === dbName
612
+ }
613
+
614
+ export async function runImport(argv: string[], cwd = process.cwd()): Promise<ImportReport> {
615
+ const args = parseArgs(argv, ['database-url', 'uploads'], ['replace', 'merge', 'yes', 'allow-mismatch', 'dry-run', 'no-search-index'])
616
+ const file = args.positional[0]
617
+ if (!file || args.positional.length > 1) throw new Error(IMPORT_USAGE)
618
+ if (args.flags.has('replace') && args.flags.has('merge')) throw new Error('Choose --replace or --merge, not both.')
619
+ // The same settings the running app uses: environment, .env, then nuxt.config.
620
+ const settings = await hostSettings(cwd)
621
+ const databaseUrl = args.values['database-url'] ?? (settings.databaseUrl || null)
622
+ if (!databaseUrl) throw new Error('No database: set DATABASE_URL in the environment or the website\'s .env')
623
+ const reg = await loadRegistry(cwd)
624
+ const storage = storageFromSettings(settings.storage, { dir: args.values.uploads, cwd })
625
+ const mode: ImportMode = args.flags.has('replace') ? 'replace' : args.flags.has('merge') ? 'merge' : 'strict'
626
+ console.log(`Importing ${resolve(cwd, file)} into ${describeDatabase(databaseUrl)} (media to ${storage.describe})`)
627
+ const started = Date.now()
628
+ const report = await importContent({
629
+ reg, databaseUrl, storage, file: resolve(cwd, file), mode,
630
+ allowMismatch: args.flags.has('allow-mismatch'),
631
+ dryRun: args.flags.has('dry-run'),
632
+ searchIndex: !args.flags.has('no-search-index'),
633
+ confirm: args.flags.has('yes') ? undefined : (summary) => askToContinue(summary, databaseName(databaseUrl)),
634
+ log: (m) => console.log(m),
635
+ })
636
+ const rows = Object.values(report.entries).reduce((n, x) => n + x.rows, 0)
637
+ if (report.skipped.entities.length) console.log(` skipped content: ${report.skipped.entities.join(', ')}`)
638
+ if (report.skipped.fields.length) console.log(` skipped fields: ${report.skipped.fields.join(', ')}`)
639
+ if (report.skipped.translations) console.log(` skipped translations: ${report.skipped.translations}`)
640
+ if (report.media.missingFiles) console.log(` ! ${report.media.missingFiles} media rows had no file in the archive`)
641
+ console.log(`${report.dryRun ? '✓ dry run: nothing written —' : '✓ imported'} ${rows} entries, ${report.media.rows} media${report.relations ? `, ${report.relations} links` : ''} in ${((Date.now() - started) / 1000).toFixed(1)} s`)
642
+ return report
643
+ }