tinyzoo 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +10 -0
- package/README.md +46 -2
- package/app/app.config.ts +35 -0
- package/app/assets/brand/characters/bright/bird.svg +1 -0
- package/app/assets/brand/characters/bright/bunny.svg +1 -0
- package/app/assets/brand/characters/bright/elephant.svg +1 -0
- package/app/assets/brand/characters/bright/fox.svg +1 -0
- package/app/assets/brand/characters/bright/frog.svg +1 -0
- package/app/assets/brand/characters/bright/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/bee.svg +1 -0
- package/app/assets/brand/characters/muted/bird.svg +1 -0
- package/app/assets/brand/characters/muted/butterfly.svg +1 -0
- package/app/assets/brand/characters/muted/chick.svg +1 -0
- package/app/assets/brand/characters/muted/cow.svg +1 -0
- package/app/assets/brand/characters/muted/deer.svg +1 -0
- package/app/assets/brand/characters/muted/duck.svg +1 -0
- package/app/assets/brand/characters/muted/elephant.svg +1 -0
- package/app/assets/brand/characters/muted/fox.svg +1 -0
- package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
- package/app/assets/brand/characters/muted/koala.svg +1 -0
- package/app/assets/brand/characters/muted/ladybug.svg +1 -0
- package/app/assets/brand/characters/muted/lion.svg +1 -0
- package/app/assets/brand/characters/muted/mouse.svg +1 -0
- package/app/assets/brand/characters/muted/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/owl.svg +1 -0
- package/app/assets/brand/characters/muted/panda.svg +1 -0
- package/app/assets/brand/characters/muted/penguin.svg +1 -0
- package/app/assets/brand/characters/muted/pig.svg +1 -0
- package/app/assets/brand/characters/muted/poodle.svg +1 -0
- package/app/assets/brand/characters/muted/raccoon.svg +1 -0
- package/app/assets/brand/characters/muted/seal.svg +1 -0
- package/app/assets/brand/characters/muted/sheep.svg +1 -0
- package/app/assets/brand/characters/muted/snail.svg +1 -0
- package/app/assets/brand/characters/muted/turtle.svg +1 -0
- package/app/assets/brand/characters/muted/whale.svg +1 -0
- package/app/assets/brand/logo/favicon.svg +1 -0
- package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
- package/app/assets/brand/logo/logo-primary.svg +1 -0
- package/app/assets/brand/logo/mascot-reversed.svg +1 -0
- package/app/assets/brand/logo/mascot.svg +1 -0
- package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
- package/app/assets/brand/logo/wordmark.svg +1 -0
- package/app/assets/css/cms-admin.css +607 -0
- package/app/components/CmsBlocks.vue +30 -0
- package/app/components/CmsCell.vue +16 -0
- package/app/components/CmsCellBadge.vue +18 -0
- package/app/components/CmsCellBlocks.vue +9 -0
- package/app/components/CmsCellBoolean.vue +12 -0
- package/app/components/CmsCellCode.vue +7 -0
- package/app/components/CmsCellColor.vue +13 -0
- package/app/components/CmsCellDate.vue +10 -0
- package/app/components/CmsCellExcerpt.vue +21 -0
- package/app/components/CmsCellGallery.vue +10 -0
- package/app/components/CmsCellGroup.vue +23 -0
- package/app/components/CmsCellNumber.vue +7 -0
- package/app/components/CmsCellRelation.vue +8 -0
- package/app/components/CmsCellText.vue +12 -0
- package/app/components/CmsCellThumb.vue +18 -0
- package/app/components/CmsConfirmDialog.vue +55 -0
- package/app/components/CmsContent.vue +103 -0
- package/app/components/CmsDayPicker.vue +44 -0
- package/app/components/CmsEmptyState.vue +35 -0
- package/app/components/CmsField.vue +111 -0
- package/app/components/CmsFieldBlocks.vue +353 -0
- package/app/components/CmsFieldBoolean.vue +8 -0
- package/app/components/CmsFieldColor.vue +42 -0
- package/app/components/CmsFieldDate.vue +131 -0
- package/app/components/CmsFieldGallery.vue +114 -0
- package/app/components/CmsFieldGroup.vue +53 -0
- package/app/components/CmsFieldMedia.vue +219 -0
- package/app/components/CmsFieldNumber.vue +11 -0
- package/app/components/CmsFieldRelationMany.vue +54 -0
- package/app/components/CmsFieldRelationOne.vue +47 -0
- package/app/components/CmsFieldRichText.vue +45 -0
- package/app/components/CmsFieldSelect.vue +41 -0
- package/app/components/CmsFieldSlug.vue +46 -0
- package/app/components/CmsFieldText.vue +75 -0
- package/app/components/CmsFormSections.vue +150 -0
- package/app/components/CmsHighlight.vue +22 -0
- package/app/components/CmsImage.vue +39 -0
- package/app/components/CmsLinkPicker.vue +122 -0
- package/app/components/CmsLinkTarget.vue +35 -0
- package/app/components/CmsListFilters.vue +107 -0
- package/app/components/CmsLiveNotice.vue +64 -0
- package/app/components/CmsLoadError.vue +24 -0
- package/app/components/CmsMascot.vue +10 -0
- package/app/components/CmsMediaLibrary.vue +114 -0
- package/app/components/CmsMediaPreview.vue +34 -0
- package/app/components/CmsMediaThumb.vue +22 -0
- package/app/components/CmsMoment.vue +42 -0
- package/app/components/CmsNav.vue +39 -0
- package/app/components/CmsPage.vue +86 -0
- package/app/components/CmsPasswordInput.vue +22 -0
- package/app/components/CmsPresence.vue +29 -0
- package/app/components/CmsPreviewPanel.vue +131 -0
- package/app/components/CmsPublishReview.vue +102 -0
- package/app/components/CmsRevisions.vue +195 -0
- package/app/components/CmsRichTextEditor.vue +290 -0
- package/app/components/CmsSearch.vue +99 -0
- package/app/components/CmsSection.vue +20 -0
- package/app/components/CmsSingletonForm.vue +283 -0
- package/app/components/CmsStatusBadge.vue +21 -0
- package/app/components/CmsTime.vue +16 -0
- package/app/components/CmsTranslationRef.vue +27 -0
- package/app/components/CmsTreeList.vue +453 -0
- package/app/components/CmsUnsavedText.vue +34 -0
- package/app/components/CmsWordmark.vue +10 -0
- package/app/composables/useCmsAdminHead.ts +18 -0
- package/app/composables/useCmsCollab.ts +165 -0
- package/app/composables/useCmsConfirm.ts +39 -0
- package/app/composables/useCmsEntryActions.ts +75 -0
- package/app/composables/useCmsFocus.ts +13 -0
- package/app/composables/useCmsForm.ts +273 -0
- package/app/composables/useCmsI18n.ts +185 -0
- package/app/composables/useCmsLinkTargets.ts +84 -0
- package/app/composables/useCmsLive.ts +199 -0
- package/app/composables/useCmsSearch.ts +62 -0
- package/app/composables/useCmsSeo.ts +99 -0
- package/app/composables/useCmsSignedIn.ts +24 -0
- package/app/composables/useCmsUiLocale.ts +24 -0
- package/app/i18n/cs.ts +781 -0
- package/app/i18n/de.ts +781 -0
- package/app/i18n/en.ts +782 -0
- package/app/i18n/types.ts +22 -0
- package/app/layouts/admin.vue +224 -0
- package/app/layouts/cms-auth.vue +37 -0
- package/app/middleware/cms-api-not-a-page.global.ts +10 -0
- package/app/middleware/cms-auth.global.ts +16 -0
- package/app/pages/admin/[entity]/[id].vue +1109 -0
- package/app/pages/admin/[entity]/index.vue +733 -0
- package/app/pages/admin/account.vue +117 -0
- package/app/pages/admin/forgot.vue +50 -0
- package/app/pages/admin/index.vue +184 -0
- package/app/pages/admin/invite/[token].vue +73 -0
- package/app/pages/admin/login.vue +84 -0
- package/app/pages/admin/media.vue +494 -0
- package/app/pages/admin/privacy.vue +193 -0
- package/app/pages/admin/reset/[token].vue +52 -0
- package/app/pages/admin/setup.vue +87 -0
- package/app/pages/admin/styleguide.vue +261 -0
- package/app/pages/admin/team.vue +258 -0
- package/app/plugins/cmsLivePreview.client.ts +20 -0
- package/app/plugins/cmsSignedOut.client.ts +31 -0
- package/app/utils/cmsBlockType.ts +11 -0
- package/app/utils/cmsBrand.ts +38 -0
- package/app/utils/cmsDiff.ts +83 -0
- package/app/utils/cmsFetch.ts +7 -0
- package/app/utils/cmsFocusInvalid.ts +22 -0
- package/app/utils/cmsIssues.ts +128 -0
- package/app/utils/cmsJson.ts +28 -0
- package/app/utils/cmsMedia.ts +63 -0
- package/app/utils/cmsSections.ts +154 -0
- package/app/utils/cmsTime.ts +41 -0
- package/cli/backup.ts +713 -0
- package/cli/breaking.ts +174 -0
- package/cli/content-migrate.ts +239 -0
- package/cli/db.ts +86 -0
- package/cli/export.ts +336 -0
- package/cli/generate.ts +146 -0
- package/cli/import.ts +643 -0
- package/cli/index.ts +140 -0
- package/cli/links-convert.ts +291 -0
- package/cli/seed-engine.ts +705 -0
- package/cli/seed.ts +339 -0
- package/cli/status.ts +49 -0
- package/cli/tar.ts +304 -0
- package/cli/transfer.ts +677 -0
- package/cli/up.ts +27 -0
- package/cli/upgrade.ts +60 -0
- package/cli/versions.ts +60 -0
- package/forms/components/CmsForm.vue +186 -0
- package/forms/index.ts +203 -0
- package/forms/messages.ts +104 -0
- package/forms/retention.ts +48 -0
- package/forms/server/form.get.ts +12 -0
- package/forms/server/form.post.ts +109 -0
- package/forms/server/formSettings.ts +12 -0
- package/forms/server/forms.ts +127 -0
- package/module.ts +373 -0
- package/nuxt.config.ts +43 -0
- package/package.json +99 -3
- package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
- package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
- package/server/api/cms/[entity]/[id].delete.ts +52 -0
- package/server/api/cms/[entity]/[id].get.ts +21 -0
- package/server/api/cms/[entity]/[id].patch.ts +116 -0
- package/server/api/cms/[entity]/_export.get.ts +69 -0
- package/server/api/cms/[entity]/_move.post.ts +81 -0
- package/server/api/cms/[entity]/_order.post.ts +49 -0
- package/server/api/cms/[entity]/_tree.get.ts +41 -0
- package/server/api/cms/[entity]/index.get.ts +62 -0
- package/server/api/cms/[entity]/index.post.ts +38 -0
- package/server/api/cms/[entity]/options.get.ts +55 -0
- package/server/api/cms/_auth/accept.post.ts +33 -0
- package/server/api/cms/_auth/forgot.post.ts +39 -0
- package/server/api/cms/_auth/invite/[token].get.ts +18 -0
- package/server/api/cms/_auth/login.post.ts +42 -0
- package/server/api/cms/_auth/logout.post.ts +7 -0
- package/server/api/cms/_auth/me.get.ts +5 -0
- package/server/api/cms/_auth/me.patch.ts +32 -0
- package/server/api/cms/_auth/password.post.ts +29 -0
- package/server/api/cms/_auth/reset.post.ts +34 -0
- package/server/api/cms/_auth/setup.post.ts +46 -0
- package/server/api/cms/_auth/status.get.ts +18 -0
- package/server/api/cms/_collab/session.get.ts +30 -0
- package/server/api/cms/_collab/socket.ts +51 -0
- package/server/api/cms/_collab/unsaved.delete.ts +16 -0
- package/server/api/cms/_graph.get.ts +20 -0
- package/server/api/cms/_links/resolve.post.ts +17 -0
- package/server/api/cms/_links.get.ts +81 -0
- package/server/api/cms/_live.ts +30 -0
- package/server/api/cms/_media/[id]/usage.get.ts +14 -0
- package/server/api/cms/_media/[id].delete.ts +29 -0
- package/server/api/cms/_media/[id].get.ts +13 -0
- package/server/api/cms/_media/[id].patch.ts +64 -0
- package/server/api/cms/_media/exists.post.ts +15 -0
- package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
- package/server/api/cms/_media/index.get.ts +44 -0
- package/server/api/cms/_media/index.post.ts +147 -0
- package/server/api/cms/_media/raw/[...key].get.ts +85 -0
- package/server/api/cms/_media/raw/[...key].head.ts +2 -0
- package/server/api/cms/_overview.get.ts +87 -0
- package/server/api/cms/_preview.post.ts +17 -0
- package/server/api/cms/_privacy/delete.post.ts +14 -0
- package/server/api/cms/_privacy/export.post.ts +25 -0
- package/server/api/cms/_privacy/lookup.post.ts +27 -0
- package/server/api/cms/_search.get.ts +17 -0
- package/server/api/cms/_singleton/[name].get.ts +19 -0
- package/server/api/cms/_singleton/[name].put.ts +76 -0
- package/server/api/cms/_users/[id]/resend.post.ts +25 -0
- package/server/api/cms/_users/[id].delete.ts +21 -0
- package/server/api/cms/_users/[id].patch.ts +45 -0
- package/server/api/cms/_users/index.get.ts +28 -0
- package/server/api/cms/_users/index.post.ts +39 -0
- package/server/api/content/[entity]/[slug].get.ts +74 -0
- package/server/api/content/[entity]/index.get.ts +100 -0
- package/server/api/content/_global/[name].get.ts +23 -0
- package/server/api/content/_search.get.ts +28 -0
- package/server/api/content/_sitemap.get.ts +8 -0
- package/server/api/content/_tree/[entity].get.ts +31 -0
- package/server/api/content-types.ts.get.ts +17 -0
- package/server/api/health/live.get.ts +10 -0
- package/server/api/health/ready.get.ts +21 -0
- package/server/api/openapi.get.ts +48 -0
- package/server/api/openapi.json.get.ts +32 -0
- package/server/db/driver.ts +8 -0
- package/server/mail/mailer.ts +43 -0
- package/server/middleware/cms-0-headers.ts +37 -0
- package/server/middleware/cms-0-migrate-gate.ts +14 -0
- package/server/middleware/cms-csrf.ts +28 -0
- package/server/middleware/cms-limits.ts +20 -0
- package/server/middleware/cms-redirects.ts +21 -0
- package/server/middleware/cms-session.ts +41 -0
- package/server/middleware/cms-z-plugin-access.ts +15 -0
- package/server/plugins/cms-0-config-check.ts +35 -0
- package/server/plugins/cms-changes.ts +53 -0
- package/server/plugins/cms-content-etag.ts +21 -0
- package/server/plugins/cms-errors.ts +45 -0
- package/server/plugins/cms-migrate.ts +5 -0
- package/server/plugins/cms-plugins.ts +17 -0
- package/server/plugins/cms-search.ts +12 -0
- package/server/plugins/cms-setup-code.ts +15 -0
- package/server/routes/sitemap.xml.get.ts +25 -0
- package/server/store/redis.ts +95 -0
- package/server/store/types.ts +21 -0
- package/server/tasks/cms/retention.ts +10 -0
- package/server/tasks/cms/scheduled.ts +10 -0
- package/server/tasks/cms/search-reindex.ts +12 -0
- package/server/utils/cmsAudit.ts +29 -0
- package/server/utils/cmsAuth.ts +143 -0
- package/server/utils/cmsBlocks.ts +47 -0
- package/server/utils/cmsChanges.ts +222 -0
- package/server/utils/cmsCollab.ts +286 -0
- package/server/utils/cmsCollabServer.ts +451 -0
- package/server/utils/cmsCollate.ts +27 -0
- package/server/utils/cmsContent.ts +376 -0
- package/server/utils/cmsCrud.ts +361 -0
- package/server/utils/cmsCsv.ts +24 -0
- package/server/utils/cmsDb.ts +23 -0
- package/server/utils/cmsDemo.ts +36 -0
- package/server/utils/cmsEmails.ts +125 -0
- package/server/utils/cmsErrors.ts +40 -0
- package/server/utils/cmsFold.ts +17 -0
- package/server/utils/cmsImageSizes.ts +51 -0
- package/server/utils/cmsJson.ts +6 -0
- package/server/utils/cmsLinkAddress.ts +50 -0
- package/server/utils/cmsLinks.ts +248 -0
- package/server/utils/cmsListQuery.ts +93 -0
- package/server/utils/cmsLive.ts +392 -0
- package/server/utils/cmsLocales.ts +96 -0
- package/server/utils/cmsMail.ts +28 -0
- package/server/utils/cmsMediaGc.ts +188 -0
- package/server/utils/cmsMediaKeys.ts +62 -0
- package/server/utils/cmsMigrate.ts +86 -0
- package/server/utils/cmsPreviewDraft.ts +72 -0
- package/server/utils/cmsPrivacy.ts +176 -0
- package/server/utils/cmsPublished.ts +57 -0
- package/server/utils/cmsRange.ts +38 -0
- package/server/utils/cmsRateLimit.ts +82 -0
- package/server/utils/cmsRedirects.ts +160 -0
- package/server/utils/cmsRetention.ts +61 -0
- package/server/utils/cmsRevisions.ts +103 -0
- package/server/utils/cmsScheduler.ts +45 -0
- package/server/utils/cmsScrub.ts +107 -0
- package/server/utils/cmsSearch.ts +354 -0
- package/server/utils/cmsSession.ts +102 -0
- package/server/utils/cmsSetupCode.ts +97 -0
- package/server/utils/cmsSitemap.ts +176 -0
- package/server/utils/cmsStorage.ts +580 -0
- package/server/utils/cmsStore.ts +109 -0
- package/server/utils/cmsTooMany.ts +9 -0
- package/server/utils/cmsTranslationWrite.ts +186 -0
- package/server/utils/cmsTree.ts +251 -0
- package/server/utils/cmsTreeAddress.ts +65 -0
- package/server/utils/cmsUpload.ts +197 -0
- package/server/utils/cmsWebhooks.ts +101 -0
- package/shared/json.ts +11 -0
- package/shared/richtext-editor.ts +56 -0
- package/shared/richtext-normalize.ts +33 -0
- package/shared/richtext-paste.ts +151 -0
- package/shared/storage-delivery.ts +53 -0
- package/src/blocks.ts +624 -0
- package/src/changes.ts +54 -0
- package/src/content-shape.ts +340 -0
- package/src/deprecate.ts +64 -0
- package/src/descriptor.ts +149 -0
- package/src/docs.ts +27 -0
- package/src/emitter.ts +94 -0
- package/src/entity.ts +624 -0
- package/src/errorReport.ts +41 -0
- package/src/fields.ts +465 -0
- package/src/i18n.ts +26 -0
- package/src/index.ts +54 -0
- package/src/linkref.ts +31 -0
- package/src/links.ts +179 -0
- package/src/locales.ts +39 -0
- package/src/media.ts +48 -0
- package/src/migrate.ts +236 -0
- package/src/openapi.ts +377 -0
- package/src/paths.ts +88 -0
- package/src/plugin.ts +505 -0
- package/src/presets.ts +230 -0
- package/src/richtext.ts +88 -0
- package/src/sdk.ts +181 -0
- package/src/search.ts +312 -0
- package/src/seed.ts +225 -0
- package/src/setupCode.ts +30 -0
- package/src/system.ts +219 -0
- package/src/tree.ts +421 -0
- package/src/upgrades.ts +137 -0
- package/starter/.env.example +10 -0
- package/starter/README.md +65 -0
- package/starter/app/app.config.ts +10 -0
- package/starter/app/app.vue +12 -0
- package/starter/app/assets/css/main.css +15 -0
- package/starter/app/components/global/BlockContact.vue +15 -0
- package/starter/app/components/global/BlockCta.vue +15 -0
- package/starter/app/components/global/BlockHero.vue +23 -0
- package/starter/app/components/global/BlockImage.vue +13 -0
- package/starter/app/components/global/BlockText.vue +12 -0
- package/starter/app/composables/useSite.ts +77 -0
- package/starter/app/error.vue +28 -0
- package/starter/app/layouts/default.vue +106 -0
- package/starter/app/lib/cms-content.ts +152 -0
- package/starter/app/pages/[...slug].vue +66 -0
- package/starter/app/pages/hledat.vue +59 -0
- package/starter/app/pages/index.vue +20 -0
- package/starter/cms.config.ts +137 -0
- package/starter/compose.yaml +15 -0
- package/starter/drizzle.config.ts +8 -0
- package/starter/gitignore.txt +8 -0
- package/starter/nuxt.config.ts +13 -0
- package/starter/package.json +38 -0
- package/starter/scripts/typecheck.mjs +38 -0
- package/starter/seed/index.ts +297 -0
- package/starter/seed/media/about.jpg +0 -0
- package/starter/seed/media/hero.jpg +0 -0
- package/starter/seed/media/logo.png +0 -0
- package/starter/seed/media/news-bread.jpg +0 -0
- package/starter/seed/media/news-morning.jpg +0 -0
- package/starter/server/db/migrations/0000_init.sql +269 -0
- package/starter/server/db/migrations/0001_page_tree.sql +5 -0
- package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
- package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
- package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
- package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
- package/starter/server/db/migrations/meta/_journal.json +27 -0
- package/starter/server/db/schema.ts +253 -0
- package/starter/server/plugins/sitemap.ts +11 -0
- package/starter/server/routes/robots.txt.get.ts +12 -0
- package/starter/tsconfig.json +9 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { buildRegistry, type CmsEntity, type CmsRegistry } from './entity'
|
|
2
|
+
import { pascal } from './fields'
|
|
3
|
+
import type { FieldDescriptor } from './descriptor'
|
|
4
|
+
|
|
5
|
+
// ── ONE IR, TWO MORE SINKS ──────────────────────────────────────────────────────
|
|
6
|
+
// The public content API projects rows through `server/utils/cmsContent.ts`
|
|
7
|
+
// (projectItem + populateItems + publicProjection). This module recomputes that
|
|
8
|
+
// EXACT projected shape as a serializable model so the OpenAPI document and the
|
|
9
|
+
// generated TypeScript client can never drift from what the API actually returns.
|
|
10
|
+
//
|
|
11
|
+
// Whenever cmsContent.ts changes the projection, this file must change in lockstep
|
|
12
|
+
// — they are deliberately kept side by side and the adversarial drift test checks it.
|
|
13
|
+
|
|
14
|
+
export const SLUG_PATTERN = '^[a-z0-9]+(?:-[a-z0-9]+)*$'
|
|
15
|
+
|
|
16
|
+
/** The value-kind of a single projected field. */
|
|
17
|
+
export type ContentFieldType =
|
|
18
|
+
| { kind: 'scalar'; scalar: 'string' | 'integer' | 'number' | 'boolean' | 'datetime'; enumValues?: readonly string[]; pattern?: string }
|
|
19
|
+
| { kind: 'richtext' }
|
|
20
|
+
| { kind: 'media' }
|
|
21
|
+
| { kind: 'gallery' }
|
|
22
|
+
| { kind: 'relation-one'; target: string; public: boolean }
|
|
23
|
+
| { kind: 'relation-many'; target: string; public: boolean }
|
|
24
|
+
| { kind: 'blocks'; blocks: { type: string; fields: ContentField[] }[] }
|
|
25
|
+
| { kind: 'group'; fields: ContentField[] }
|
|
26
|
+
|
|
27
|
+
export interface ContentField {
|
|
28
|
+
key: string
|
|
29
|
+
type: ContentFieldType
|
|
30
|
+
/** Value may be `null` in the response (still always present as a key). */
|
|
31
|
+
nullable: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A filterable query parameter on the list endpoint. */
|
|
35
|
+
export interface FilterParam {
|
|
36
|
+
key: string
|
|
37
|
+
enumValues?: readonly string[]
|
|
38
|
+
/** 'boolean': `true`/`false`; 'datetime': an ISO date or time (a `.from` / `.to` bound). Default: exact text. */
|
|
39
|
+
value?: 'boolean' | 'datetime'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Top-level shape of a public collection (drives `/api/content/:name` + `/:slug`). */
|
|
43
|
+
export interface ContentShape {
|
|
44
|
+
name: string
|
|
45
|
+
pascal: string
|
|
46
|
+
titleField: string
|
|
47
|
+
slugField?: string
|
|
48
|
+
/** Whether a `published` condition gates this collection (docs only). */
|
|
49
|
+
gated: boolean
|
|
50
|
+
/** Projected fields in declaration order (excludes id + createdAt/updatedAt). */
|
|
51
|
+
fields: ContentField[]
|
|
52
|
+
/** Allowed `?sort=` values. */
|
|
53
|
+
sortKeys: string[]
|
|
54
|
+
/** Allowed filter query params. */
|
|
55
|
+
filters: FilterParam[]
|
|
56
|
+
/** Content languages, when the collection has translatable fields: items then carry `locale` + `locales`. */
|
|
57
|
+
locales: string[] | null
|
|
58
|
+
/** With `locales`: items also carry `paths`, their address in each language. */
|
|
59
|
+
paths?: boolean
|
|
60
|
+
/** A page tree: items also carry `path` and `breadcrumbs`, and `GET /api/content/_tree/<name>` exists. */
|
|
61
|
+
tree?: boolean
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Scalar-only projection of a PUBLIC entity when populated as another's relation. */
|
|
65
|
+
export interface EmbeddedShape {
|
|
66
|
+
name: string
|
|
67
|
+
pascal: string
|
|
68
|
+
fields: ContentField[]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Minimal reference of a NON-PUBLIC entity when populated as another's relation. */
|
|
72
|
+
export interface RefShape {
|
|
73
|
+
name: string
|
|
74
|
+
pascal: string
|
|
75
|
+
titleField: string
|
|
76
|
+
titleType: ContentFieldType
|
|
77
|
+
/** The target's title field may itself be optional → the ref title can be null. */
|
|
78
|
+
titleNullable: boolean
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ContentModel {
|
|
82
|
+
entities: ContentShape[]
|
|
83
|
+
/** Public singletons (globals), served at /api/content/_global/:name. */
|
|
84
|
+
globals: ContentShape[]
|
|
85
|
+
embedded: EmbeddedShape[]
|
|
86
|
+
refs: RefShape[]
|
|
87
|
+
/**
|
|
88
|
+
* The website search (/api/content/_search): the collections it covers (public, with
|
|
89
|
+
* an address, not `content.search: false`) and the languages it takes. Empty: no search.
|
|
90
|
+
*/
|
|
91
|
+
search: { collections: string[]; locales: string[] | null }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ── projection helpers — mirror cmsContent.ts byte for byte ─────────────────────
|
|
95
|
+
|
|
96
|
+
function hiddenSet(e: CmsEntity): Set<string> {
|
|
97
|
+
return new Set(e.content?.hidden ?? [])
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** media + to-one + m2m — the keys that CAN be populated (cmsContent.populatableKeys). */
|
|
101
|
+
function populatableKeys(e: CmsEntity): string[] {
|
|
102
|
+
return Object.entries(e.fields)
|
|
103
|
+
.filter(([k, f]) => k !== e.tree?.parent && (f.type === 'media' || f.relation?.kind === 'many-to-one' || f.relation?.kind === 'many-to-many'))
|
|
104
|
+
.map(([k]) => k)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The keys actually populated for this entity (cmsContent.populateItems default). */
|
|
108
|
+
function populateSet(e: CmsEntity): Set<string> {
|
|
109
|
+
return new Set(e.content?.populate ?? populatableKeys(e))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A link field can come back null whatever its `required`: it may hold a reference to
|
|
114
|
+
* an entry (`cms:<entity>/<id>`) that the content API resolves to an address, or to null
|
|
115
|
+
* when the entry is gone, not published or not public (see links.ts).
|
|
116
|
+
*/
|
|
117
|
+
const isLink = (fld: FieldDescriptor) => fld.type === 'text' && fld.meta?.format === 'link'
|
|
118
|
+
|
|
119
|
+
function scalarType(fld: FieldDescriptor): ContentFieldType {
|
|
120
|
+
switch (fld.type) {
|
|
121
|
+
case 'text': return { kind: 'scalar', scalar: 'string' }
|
|
122
|
+
case 'slug': return { kind: 'scalar', scalar: 'string', pattern: SLUG_PATTERN }
|
|
123
|
+
// integer → JSON number; numeric (int:false) → drizzle pg `numeric` returns a
|
|
124
|
+
// STRING at runtime (e.g. "19.99"), so the wire type is string, not number.
|
|
125
|
+
case 'number': return { kind: 'scalar', scalar: fld.spec?.base.kind === 'integer' ? 'integer' : 'string' }
|
|
126
|
+
case 'boolean': return { kind: 'scalar', scalar: 'boolean' }
|
|
127
|
+
case 'datetime': return { kind: 'scalar', scalar: 'datetime' }
|
|
128
|
+
case 'select': {
|
|
129
|
+
const values = (fld.meta?.options as readonly string[] | undefined)
|
|
130
|
+
?? (fld.spec?.base.kind === 'enum' ? fld.spec.base.values : undefined)
|
|
131
|
+
return { kind: 'scalar', scalar: 'string', enumValues: values }
|
|
132
|
+
}
|
|
133
|
+
case 'richtext': return { kind: 'richtext' }
|
|
134
|
+
default: return { kind: 'scalar', scalar: 'string' }
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A field inside a block or a group as the API returns it (mirror
|
|
140
|
+
* cmsContent.projectFieldValue): media resolved to the public media object, a list an
|
|
141
|
+
* array (never null), a group an object of its fields (null when empty).
|
|
142
|
+
*/
|
|
143
|
+
function innerField(key: string, bf: FieldDescriptor): ContentField {
|
|
144
|
+
if (bf.type === 'media') return { key, type: { kind: 'media' }, nullable: true }
|
|
145
|
+
if (bf.type === 'blocks') return { key, type: blocksType(bf), nullable: false } // a list inside a block: always an array
|
|
146
|
+
if (bf.type === 'group') return { key, type: groupType(bf), nullable: true }
|
|
147
|
+
return { key, type: scalarType(bf), nullable: !bf.isRequired || isLink(bf) }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A blocks field as the API returns it (mirror cmsContent.projectBlocks): every block
|
|
152
|
+
* carries id, type and each of its type's fields (null when empty).
|
|
153
|
+
*/
|
|
154
|
+
function blocksType(fld: FieldDescriptor): ContentFieldType {
|
|
155
|
+
const blocks = Object.entries(fld.blocks ?? {}).map(([type, def]) => ({
|
|
156
|
+
type,
|
|
157
|
+
fields: (Object.entries(def.fields) as [string, FieldDescriptor][]).map(([key, bf]) => innerField(key, bf)),
|
|
158
|
+
}))
|
|
159
|
+
return { kind: 'blocks', blocks }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A group field as the API returns it: an object with each of its fields (null when empty). */
|
|
163
|
+
function groupType(fld: FieldDescriptor): ContentFieldType {
|
|
164
|
+
return { kind: 'group', fields: (Object.entries(fld.fields ?? {}) as [string, FieldDescriptor][]).map(([key, gf]) => innerField(key, gf)) }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Is a projected to-one relation possibly null? (optional FK, or target is publish-gated.) */
|
|
168
|
+
function relationOneNullable(reg: CmsRegistry, fld: FieldDescriptor): boolean {
|
|
169
|
+
const optional = !fld.relation!.required
|
|
170
|
+
const target = reg.byName.get(fld.relation!.to().name)
|
|
171
|
+
const targetGated = !!(target?.content?.published || target?.publish)
|
|
172
|
+
return optional || targetGated
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The projected fields a populated relation/media field contributes (or null = dropped). */
|
|
176
|
+
function relationField(reg: CmsRegistry, key: string, fld: FieldDescriptor): ContentField | null {
|
|
177
|
+
if (fld.type === 'media') return { key, type: { kind: 'media' }, nullable: true }
|
|
178
|
+
const target = reg.byName.get(fld.relation!.to().name)
|
|
179
|
+
if (!target) return null
|
|
180
|
+
const isPublic = !!target.content?.public
|
|
181
|
+
if (fld.relation!.kind === 'many-to-one') {
|
|
182
|
+
return { key, type: { kind: 'relation-one', target: target.name, public: isPublic }, nullable: relationOneNullable(reg, fld) }
|
|
183
|
+
}
|
|
184
|
+
// many-to-many → always an array (never null; items are non-null published targets)
|
|
185
|
+
return { key, type: { kind: 'relation-many', target: target.name, public: isPublic }, nullable: false }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Top-level projected fields (mirror cmsContent.projectItem, minus id/timestamps). */
|
|
189
|
+
function topLevelFields(reg: CmsRegistry, e: CmsEntity): ContentField[] {
|
|
190
|
+
const hidden = hiddenSet(e)
|
|
191
|
+
const populate = populateSet(e)
|
|
192
|
+
const out: ContentField[] = []
|
|
193
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
194
|
+
if (hidden.has(key)) continue
|
|
195
|
+
if (fld.relation?.kind === 'one-to-many') continue // virtual
|
|
196
|
+
// A page tree's parent is delivered as the parent's id.
|
|
197
|
+
if (key === e.tree?.parent) {
|
|
198
|
+
out.push({ key, type: { kind: 'scalar', scalar: 'string' }, nullable: true })
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
if (fld.relation || fld.type === 'media') {
|
|
202
|
+
if (!populate.has(key)) continue // not populated → dropped from the response
|
|
203
|
+
const f = relationField(reg, key, fld)
|
|
204
|
+
if (f) out.push(f)
|
|
205
|
+
continue
|
|
206
|
+
}
|
|
207
|
+
if (fld.type === 'blocks') {
|
|
208
|
+
out.push({ key, type: blocksType(fld), nullable: false })
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
// A group is one object (null only when the group is optional).
|
|
212
|
+
if (fld.type === 'group') {
|
|
213
|
+
out.push({ key, type: groupType(fld), nullable: fld.isOptional })
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
// Galleries are always resolved (like blocks): an array of media + caption.
|
|
217
|
+
if (fld.type === 'gallery') {
|
|
218
|
+
out.push({ key, type: { kind: 'gallery' }, nullable: false })
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
out.push({ key, type: scalarType(fld), nullable: fld.isOptional || isLink(fld) })
|
|
222
|
+
}
|
|
223
|
+
return out
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Scalar projection (mirror cmsContent.projectScalars, minus id). */
|
|
227
|
+
function embeddedFields(e: CmsEntity): ContentField[] {
|
|
228
|
+
const hidden = hiddenSet(e)
|
|
229
|
+
const out: ContentField[] = []
|
|
230
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
231
|
+
// Blocks are not embedded in related items (they can hold media to resolve).
|
|
232
|
+
if (hidden.has(key) || fld.relation || fld.type === 'media' || fld.type === 'blocks' || fld.type === 'group' || fld.type === 'gallery') continue
|
|
233
|
+
out.push({ key, type: scalarType(fld), nullable: fld.isOptional || isLink(fld) })
|
|
234
|
+
}
|
|
235
|
+
return out
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Sort allowlist (mirror index.get.ts): createdAt/updatedAt + non-hidden scalar cols. */
|
|
239
|
+
function sortKeys(e: CmsEntity): string[] {
|
|
240
|
+
const hidden = hiddenSet(e)
|
|
241
|
+
const keys = ['createdAt', 'updatedAt']
|
|
242
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
243
|
+
if (!hidden.has(key) && !fld.relation && fld.type !== 'media' && fld.type !== 'richtext' && fld.type !== 'blocks' && fld.type !== 'group' && fld.type !== 'gallery') keys.push(key)
|
|
244
|
+
}
|
|
245
|
+
return keys
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Query keys the list endpoint reserves for paging/sort/search (index.get.ts RESERVED). */
|
|
249
|
+
const RESERVED_QUERY_KEYS = new Set(['page', 'limit', 'sort', 'dir', 'q', 'locale'])
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Filter allowlist (mirror index.get.ts filter loop): non-hidden text/slug/select +
|
|
253
|
+
* to-one FKs (exact match), yes/no fields, and date ranges (`<field>.from` / `.to`).
|
|
254
|
+
*/
|
|
255
|
+
function filters(e: CmsEntity): FilterParam[] {
|
|
256
|
+
const hidden = hiddenSet(e)
|
|
257
|
+
const out: FilterParam[] = []
|
|
258
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
259
|
+
// Skip reserved keys: the server's filter loop ignores them, so typing them
|
|
260
|
+
// as filterable would be a lie (and `sort` would collide with the sort param).
|
|
261
|
+
if (hidden.has(key) || RESERVED_QUERY_KEYS.has(key)) continue
|
|
262
|
+
if (fld.relation?.kind === 'many-to-one') out.push({ key })
|
|
263
|
+
else if (!fld.relation && ['text', 'slug', 'select'].includes(fld.type)) {
|
|
264
|
+
out.push({ key, enumValues: fld.type === 'select' ? scalarEnum(fld) : undefined })
|
|
265
|
+
}
|
|
266
|
+
else if (fld.type === 'boolean') out.push({ key, value: 'boolean' })
|
|
267
|
+
else if (fld.type === 'datetime') out.push({ key: `${key}.from`, value: 'datetime' }, { key: `${key}.to`, value: 'datetime' })
|
|
268
|
+
}
|
|
269
|
+
return out
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function scalarEnum(fld: FieldDescriptor): readonly string[] | undefined {
|
|
273
|
+
const t = scalarType(fld)
|
|
274
|
+
return t.kind === 'scalar' ? t.enumValues : undefined
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Build the full content model: top-level shapes + the embedded/ref shapes they reference. */
|
|
278
|
+
export function buildContentModel(config: Record<string, any>): ContentModel {
|
|
279
|
+
const reg = buildRegistry(config)
|
|
280
|
+
// Singletons are delivered via /api/content/_global/:name (object), not as a
|
|
281
|
+
// collection list, so they are excluded from the generated collection clients.
|
|
282
|
+
const publicEntities = reg.entities.filter((e) => e.content?.public && !e.singleton)
|
|
283
|
+
const publicGlobals = reg.entities.filter((e) => e.content?.public && e.singleton)
|
|
284
|
+
|
|
285
|
+
const globals: ContentShape[] = publicGlobals.map((e) => ({
|
|
286
|
+
name: e.name,
|
|
287
|
+
pascal: pascal(e.name),
|
|
288
|
+
titleField: e.titleField,
|
|
289
|
+
gated: false,
|
|
290
|
+
fields: topLevelFields(reg, e),
|
|
291
|
+
sortKeys: [],
|
|
292
|
+
filters: [],
|
|
293
|
+
locales: reg.locales && e.localized.length ? reg.locales.locales : null,
|
|
294
|
+
}))
|
|
295
|
+
|
|
296
|
+
const entities: ContentShape[] = publicEntities.map((e) => ({
|
|
297
|
+
name: e.name,
|
|
298
|
+
pascal: pascal(e.name),
|
|
299
|
+
titleField: e.titleField,
|
|
300
|
+
slugField: e.content?.slug,
|
|
301
|
+
gated: !!(e.content?.published || e.publish),
|
|
302
|
+
fields: topLevelFields(reg, e),
|
|
303
|
+
sortKeys: sortKeys(e),
|
|
304
|
+
filters: filters(e),
|
|
305
|
+
locales: reg.locales && e.localized.length ? reg.locales.locales : null,
|
|
306
|
+
paths: !!(reg.locales && e.localized.length && e.content?.path),
|
|
307
|
+
...(e.tree ? { tree: true } : {}),
|
|
308
|
+
}))
|
|
309
|
+
|
|
310
|
+
// Collect every relation target reachable through a populated field.
|
|
311
|
+
const embeddedByName = new Map<string, EmbeddedShape>()
|
|
312
|
+
const refByName = new Map<string, RefShape>()
|
|
313
|
+
for (const e of [...publicEntities, ...publicGlobals]) {
|
|
314
|
+
for (const f of topLevelFields(reg, e)) {
|
|
315
|
+
if (f.type.kind !== 'relation-one' && f.type.kind !== 'relation-many') continue
|
|
316
|
+
const target = reg.byName.get(f.type.target)
|
|
317
|
+
if (!target) continue
|
|
318
|
+
if (f.type.public) {
|
|
319
|
+
if (!embeddedByName.has(target.name)) {
|
|
320
|
+
embeddedByName.set(target.name, { name: target.name, pascal: pascal(target.name), fields: embeddedFields(target) })
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else if (!refByName.has(target.name)) {
|
|
324
|
+
const titleFld = target.fields[target.titleField]
|
|
325
|
+
refByName.set(target.name, {
|
|
326
|
+
name: target.name,
|
|
327
|
+
pascal: pascal(target.name),
|
|
328
|
+
titleField: target.titleField,
|
|
329
|
+
titleType: titleFld ? scalarType(titleFld) : { kind: 'scalar', scalar: 'string' },
|
|
330
|
+
titleNullable: titleFld?.isOptional ?? false,
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Mirrors publicSearchEntities (server/utils/cmsSearch.ts).
|
|
337
|
+
const searchable = publicEntities.filter((e) => e.content?.path && e.content.search !== false).map((e) => e.name)
|
|
338
|
+
const search = { collections: searchable, locales: searchable.length && reg.locales ? reg.locales.locales : null }
|
|
339
|
+
return { entities, globals, embedded: [...embeddedByName.values()], refs: [...refByName.values()], search }
|
|
340
|
+
}
|
package/src/deprecate.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Deprecations: the way tinyzoo retires part of its public API without breaking a
|
|
2
|
+
// site on the next update. The policy lives in CONTRIBUTING.md; in short, the
|
|
3
|
+
// old API keeps working for at least one minor release and says so once.
|
|
4
|
+
//
|
|
5
|
+
// /** @deprecated Use `publish` instead. Removed in 0.4. */
|
|
6
|
+
// export function oldThing() {
|
|
7
|
+
// cmsDeprecated('oldThing', '`oldThing()` is replaced by `publish()`.', { removeIn: '0.4.0' })
|
|
8
|
+
// …
|
|
9
|
+
// }
|
|
10
|
+
//
|
|
11
|
+
// `@deprecated` shows up in the editor; `cmsDeprecated()` shows up in the log of a
|
|
12
|
+
// site that still uses it.
|
|
13
|
+
|
|
14
|
+
export interface CmsDeprecation {
|
|
15
|
+
/** Stable id of the thing that is deprecated: one warning per id and process. */
|
|
16
|
+
id: string
|
|
17
|
+
message: string
|
|
18
|
+
/** The release that removes it, when known. */
|
|
19
|
+
removeIn?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CmsDeprecatedOptions {
|
|
23
|
+
/** The tinyzoo version that removes it, e.g. `'0.4.0'` or `'1.0.0'`. */
|
|
24
|
+
removeIn?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const seen = new Map<string, CmsDeprecation>()
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether to print. `TINYZOO_DEPRECATIONS` decides when set (`1`/`show` always,
|
|
31
|
+
* `0`/`off` never); otherwise warnings print everywhere except under
|
|
32
|
+
* `NODE_ENV=test`, so a site's own test output stays clean.
|
|
33
|
+
*/
|
|
34
|
+
function shouldPrint(): boolean {
|
|
35
|
+
const flag = (process.env.TINYZOO_DEPRECATIONS ?? '').toLowerCase()
|
|
36
|
+
if (flag === '1' || flag === 'show' || flag === 'true') return true
|
|
37
|
+
if (flag === '0' || flag === 'off' || flag === 'false') return false
|
|
38
|
+
return process.env.NODE_ENV !== 'test'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Mark a call site as deprecated: warns once per process
|
|
43
|
+
* (`[tinyzoo] deprecated: … Will be removed in 0.4.0.`), whatever the number of
|
|
44
|
+
* calls. The warning is silent under `NODE_ENV=test` unless
|
|
45
|
+
* `TINYZOO_DEPRECATIONS=1`, but is always recorded: see `cmsDeprecations()`.
|
|
46
|
+
*/
|
|
47
|
+
export function cmsDeprecated(id: string, message: string, options: CmsDeprecatedOptions = {}): void {
|
|
48
|
+
if (seen.has(id)) return
|
|
49
|
+
const entry: CmsDeprecation = { id, message, ...(options.removeIn ? { removeIn: options.removeIn } : {}) }
|
|
50
|
+
seen.set(id, entry)
|
|
51
|
+
if (!shouldPrint()) return
|
|
52
|
+
const removal = options.removeIn ? ` Will be removed in ${options.removeIn}.` : ' Will be removed in a later release.'
|
|
53
|
+
console.warn(`[tinyzoo] deprecated: ${message}${removal} (${id})`)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Every deprecation hit so far in this process, in the order they happened (for tests and diagnostics). */
|
|
57
|
+
export function cmsDeprecations(): CmsDeprecation[] {
|
|
58
|
+
return [...seen.values()]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Forget what was recorded, so the next call warns again (tests). */
|
|
62
|
+
export function resetCmsDeprecations(): void {
|
|
63
|
+
seen.clear()
|
|
64
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { LocalizedText } from './i18n'
|
|
2
|
+
import type { ZodTypeAny } from 'zod'
|
|
3
|
+
|
|
4
|
+
export type OnDelete = 'cascade' | 'restrict' | 'set null' | 'no action'
|
|
5
|
+
export type RelationKind = 'many-to-one' | 'one-to-many' | 'many-to-many'
|
|
6
|
+
export type ValidationMode = 'create' | 'update'
|
|
7
|
+
|
|
8
|
+
/** What a write is checked against besides the payload. */
|
|
9
|
+
export interface ValidationContext {
|
|
10
|
+
/**
|
|
11
|
+
* What the field holds in the row being written, as stored (nothing on create).
|
|
12
|
+
* Blocks and lists use it to tell a block that is already stored from one the
|
|
13
|
+
* client made up (see `blocksSchema`).
|
|
14
|
+
*/
|
|
15
|
+
stored?: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface RelationSpec {
|
|
19
|
+
/**
|
|
20
|
+
* Lazy thunk to the target entity. Typed `() => any` ON PURPOSE: any structural
|
|
21
|
+
* return type (even `() => unknown`) makes TS evaluate the target entity while
|
|
22
|
+
* inferring THIS one, re-creating the circular-reference cycle. `any` short-circuits
|
|
23
|
+
* the assignability check so the target is never evaluated. The runtime casts back.
|
|
24
|
+
*/
|
|
25
|
+
to: () => any
|
|
26
|
+
kind: RelationKind
|
|
27
|
+
required?: boolean
|
|
28
|
+
onDelete?: OnDelete
|
|
29
|
+
inverseOf?: string
|
|
30
|
+
through?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FieldUi {
|
|
34
|
+
edit: string
|
|
35
|
+
cell: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ── the serializable column IR (one source for live builder + emitted source) ──
|
|
39
|
+
export type ColumnBase =
|
|
40
|
+
| { kind: 'uuid' }
|
|
41
|
+
| { kind: 'text' }
|
|
42
|
+
| { kind: 'integer' }
|
|
43
|
+
| { kind: 'numeric'; precision: number; scale: number }
|
|
44
|
+
| { kind: 'boolean' }
|
|
45
|
+
| { kind: 'timestamp' }
|
|
46
|
+
| { kind: 'jsonb' }
|
|
47
|
+
| { kind: 'enum'; values: readonly string[] }
|
|
48
|
+
|
|
49
|
+
export type ColumnDefault =
|
|
50
|
+
| { kind: 'value'; value: unknown }
|
|
51
|
+
| { kind: 'now' }
|
|
52
|
+
| { kind: 'random' }
|
|
53
|
+
|
|
54
|
+
export interface ColumnReference {
|
|
55
|
+
target: 'media' | 'relation'
|
|
56
|
+
onDelete: OnDelete
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ColumnSpec {
|
|
60
|
+
base: ColumnBase
|
|
61
|
+
notNull: boolean
|
|
62
|
+
unique: boolean
|
|
63
|
+
default?: ColumnDefault
|
|
64
|
+
tsType?: string
|
|
65
|
+
reference?: ColumnReference
|
|
66
|
+
nameSuffix?: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ── type-level field metadata (phantom; drives Row/Insert/table inference) ──────
|
|
70
|
+
export interface FieldKind {
|
|
71
|
+
/** value of the column this field owns, in a selected row (e.g. `authorId: string`) */
|
|
72
|
+
out: unknown
|
|
73
|
+
/** value accepted on insert for this field's API key (e.g. `author: string`, `tags: string[]`) */
|
|
74
|
+
in: unknown
|
|
75
|
+
/** how the field maps to a DB column */
|
|
76
|
+
col: 'scalar' | 'fk' | 'none'
|
|
77
|
+
/** is the field omittable on insert (nullable or has a default)? */
|
|
78
|
+
optionalIn: boolean
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `.optional()` → nullable + omittable on insert. */
|
|
82
|
+
export type Nullable<K extends FieldKind> = {
|
|
83
|
+
out: K['out'] | null
|
|
84
|
+
in: K['in'] | null | undefined
|
|
85
|
+
col: K['col']
|
|
86
|
+
optionalIn: true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** `.withDefault()` → omittable on insert. */
|
|
90
|
+
export type Defaulted<K extends FieldKind> = {
|
|
91
|
+
out: K['out']
|
|
92
|
+
in: K['in']
|
|
93
|
+
col: K['col']
|
|
94
|
+
optionalIn: true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface FieldDescriptor<K extends FieldKind = FieldKind> {
|
|
98
|
+
/** phantom — never set at runtime; carries the type-level kind. */
|
|
99
|
+
readonly __k?: K
|
|
100
|
+
type: string
|
|
101
|
+
isRequired: boolean
|
|
102
|
+
isUnique: boolean
|
|
103
|
+
isOptional: boolean
|
|
104
|
+
/** Has a value per language (see `.localized()` and `defineLocales`). */
|
|
105
|
+
isLocalized: boolean
|
|
106
|
+
default?: unknown
|
|
107
|
+
dbDefault?: 'now' | 'random'
|
|
108
|
+
ui: FieldUi
|
|
109
|
+
meta: Record<string, unknown>
|
|
110
|
+
relation?: RelationSpec
|
|
111
|
+
/** Block type definitions (blocks fields only). */
|
|
112
|
+
blocks?: Record<string, import('./blocks').BlockDef>
|
|
113
|
+
/** The fields a group holds (group fields only). */
|
|
114
|
+
fields?: Record<string, FieldDescriptor>
|
|
115
|
+
/** null ⇒ owns no DB column (many-to-many, one-to-many). */
|
|
116
|
+
spec: ColumnSpec | null
|
|
117
|
+
zod(mode?: ValidationMode, context?: ValidationContext): ZodTypeAny
|
|
118
|
+
optional(): FieldDescriptor<Nullable<K>>
|
|
119
|
+
unique(): FieldDescriptor<K>
|
|
120
|
+
/**
|
|
121
|
+
* The field has a value per language (text, slug, rich text, blocks). The entry's
|
|
122
|
+
* own row holds the default language; translations live in `<entity>_locales`, and
|
|
123
|
+
* an untranslated value falls back to the default language. Needs `defineLocales`.
|
|
124
|
+
*/
|
|
125
|
+
localized(): FieldDescriptor<K>
|
|
126
|
+
withDefault(value: K['out']): FieldDescriptor<Defaulted<K>>
|
|
127
|
+
/** Human-friendly admin label (defaults to a humanized field key). One string, or one per admin locale. */
|
|
128
|
+
label(text: LocalizedText): FieldDescriptor<K>
|
|
129
|
+
/** Help text shown under the field in the admin form. */
|
|
130
|
+
help(text: LocalizedText): FieldDescriptor<K>
|
|
131
|
+
/**
|
|
132
|
+
* Where the field sits in the writing editor (rich-text entries such as news):
|
|
133
|
+
* 'main' (the text column) or 'sidebar' (the details beside it). Unset = decided by
|
|
134
|
+
* field type: text, rich text and media go in the main column; slugs, selects,
|
|
135
|
+
* dates, switches and relations in the details. Form and preview editors show every
|
|
136
|
+
* field in sections (see `admin.sections`).
|
|
137
|
+
*/
|
|
138
|
+
position(where: 'main' | 'sidebar'): FieldDescriptor<K>
|
|
139
|
+
/**
|
|
140
|
+
* The icon shown in the field's control in the admin (any Iconify name, e.g.
|
|
141
|
+
* `i-lucide-map-pin`). Unset = from the kind of field: dates, links, e-mails.
|
|
142
|
+
*/
|
|
143
|
+
icon(name: string): FieldDescriptor<K>
|
|
144
|
+
/** Put the field in a named, collapsible group in the editor (e.g. 'Search & sharing'). */
|
|
145
|
+
group(label: LocalizedText): FieldDescriptor<K>
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Any descriptor, regardless of kind. */
|
|
149
|
+
export type AnyFieldDescriptor = FieldDescriptor<FieldKind>
|
package/src/docs.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Where the docs are. The npm package carries no docs, so every message the CLI or the
|
|
2
|
+
// server prints that points at one gives the address on GitHub (a relative `docs/…` path
|
|
3
|
+
// leads nowhere from a site's terminal). test/unit/docs-urls.spec.ts keeps each anchor
|
|
4
|
+
// pointing at a heading that exists.
|
|
5
|
+
|
|
6
|
+
export const DOCS_URL = 'https://github.com/tinyzookeeper/tinyzoo/blob/main/docs'
|
|
7
|
+
|
|
8
|
+
/** The docs pages and headings messages point at, by what they are about. */
|
|
9
|
+
export const DOC_LINKS = {
|
|
10
|
+
upgrading: { file: 'upgrading.md' },
|
|
11
|
+
twoStepRule: { file: 'upgrading.md', anchor: 'the-two-step-rule' },
|
|
12
|
+
renamingField: { file: 'upgrading.md', anchor: 'renaming-a-field' },
|
|
13
|
+
renamingEntity: { file: 'upgrading.md', anchor: 'renaming-an-entity' },
|
|
14
|
+
requiredField: { file: 'upgrading.md', anchor: 'adding-a-required-field' },
|
|
15
|
+
changingType: { file: 'upgrading.md', anchor: 'changing-a-fields-type' },
|
|
16
|
+
neverApplied: { file: 'upgrading.md', anchor: 'migration--was-never-applied-but-a-newer-one-was' },
|
|
17
|
+
backups: { file: 'deploying.md', anchor: 'backups-and-restore' },
|
|
18
|
+
media: { file: 'deploying.md', anchor: 'media' },
|
|
19
|
+
} as const
|
|
20
|
+
|
|
21
|
+
export type DocLink = keyof typeof DOC_LINKS
|
|
22
|
+
|
|
23
|
+
/** The GitHub address of a docs page or heading. */
|
|
24
|
+
export function docsUrl(link: DocLink): string {
|
|
25
|
+
const { file, anchor } = DOC_LINKS[link] as { file: string; anchor?: string }
|
|
26
|
+
return `${DOCS_URL}/${file}${anchor ? `#${anchor}` : ''}`
|
|
27
|
+
}
|
package/src/emitter.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { buildRegistry, type CmsEntity, type Junction, propOf, translationSpec } from './entity'
|
|
2
|
+
import { camel, serializeColumn, snake } from './fields'
|
|
3
|
+
import { CMS_MEDIA_SOURCE } from './media'
|
|
4
|
+
import { CMS_SYSTEM_SOURCE } from './system'
|
|
5
|
+
|
|
6
|
+
/** Emit the on-disk db/schema.ts source from the entity descriptors. */
|
|
7
|
+
export function emitSchema(config: Record<string, any>): string {
|
|
8
|
+
const reg = buildRegistry(config)
|
|
9
|
+
const usesRich = reg.entities.some((e) => Object.values(e.fields).some((fl) => fl.type === 'richtext'))
|
|
10
|
+
const usesBlocks = reg.entities.some((e) => Object.values(e.fields).some((fl) => fl.type === 'blocks'))
|
|
11
|
+
|
|
12
|
+
const out: string[] = []
|
|
13
|
+
out.push('// GENERATED by tinyzoo — DO NOT EDIT.')
|
|
14
|
+
out.push('// Source: cms.config.ts. Regenerate with `pnpm db:generate`.')
|
|
15
|
+
// A table that points at itself (a page tree's parent) needs the column type spelled out for TypeScript.
|
|
16
|
+
const selfReferencing = reg.entities.some((e) => Object.values(e.fields).some((fl) => fl.spec?.reference?.target === 'relation' && fl.relation?.to().name === e.name))
|
|
17
|
+
out.push(`import { pgTable, pgEnum, uuid, text, integer, numeric, boolean, timestamp, jsonb, primaryKey, index, uniqueIndex${selfReferencing ? ', type AnyPgColumn' : ''} } from 'drizzle-orm/pg-core'`)
|
|
18
|
+
const usesGallery = reg.entities.some((e) => Object.values(e.fields).some((fl) => fl.type === 'gallery'))
|
|
19
|
+
const typeImports = [usesBlocks && 'CmsBlock', usesRich && 'RichTextDoc', usesGallery && 'CmsGalleryValue'].filter(Boolean)
|
|
20
|
+
if (typeImports.length) out.push(`import type { ${typeImports.join(', ')} } from 'tinyzoo'`)
|
|
21
|
+
out.push('')
|
|
22
|
+
|
|
23
|
+
// enums (hoisted — referenced by tables below)
|
|
24
|
+
for (const e of reg.entities) {
|
|
25
|
+
for (const en of e.enums) {
|
|
26
|
+
out.push(`export const ${en.varName} = pgEnum('${en.enumName}', ${JSON.stringify([...en.values])})`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
out.push('')
|
|
30
|
+
|
|
31
|
+
// built-in media table
|
|
32
|
+
out.push(CMS_MEDIA_SOURCE, '')
|
|
33
|
+
|
|
34
|
+
// users, one-time links, audit log
|
|
35
|
+
out.push(CMS_SYSTEM_SOURCE, '')
|
|
36
|
+
|
|
37
|
+
// entity tables (+ their translations tables)
|
|
38
|
+
for (const e of reg.entities) {
|
|
39
|
+
out.push(emitEntityTable(e), '')
|
|
40
|
+
if (e.localized?.length) out.push(emitLocalesTable(e), '')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// m2m junctions
|
|
44
|
+
for (const j of reg.junctions) out.push(emitJunction(j), '')
|
|
45
|
+
|
|
46
|
+
return out.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd() + '\n'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function emitEntityTable(e: CmsEntity): string {
|
|
50
|
+
const lines: string[] = [` id: uuid('id').primaryKey().defaultRandom(),`]
|
|
51
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
52
|
+
if (!fld.spec) continue
|
|
53
|
+
const dbName = snake(key) + (fld.spec.nameSuffix ?? '')
|
|
54
|
+
const enumVar = fld.spec.base.kind === 'enum' ? e.enums.find((en) => en.key === key)!.varName : undefined
|
|
55
|
+
const refTargetVar = fld.spec.reference
|
|
56
|
+
? (fld.spec.reference.target === 'media' ? 'cmsMedia' : camel(fld.relation!.to().name))
|
|
57
|
+
: undefined
|
|
58
|
+
const selfReference = fld.spec.reference?.target === 'relation' && fld.relation?.to().name === e.name
|
|
59
|
+
lines.push(` ${propOf(key, fld)}: ${serializeColumn(fld.spec, dbName, { enumVar, refTargetVar, selfReference })},`)
|
|
60
|
+
}
|
|
61
|
+
lines.push(` createdAt: timestamp('created_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow(),`)
|
|
62
|
+
lines.push(` updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow(),`)
|
|
63
|
+
lines.push(` createdBy: uuid('created_by'),`)
|
|
64
|
+
lines.push(` updatedBy: uuid('updated_by'),`)
|
|
65
|
+
return `export const ${camel(e.name)} = pgTable('${snake(e.name)}', {\n${lines.join('\n')}\n})`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function emitJunction(j: Junction): string {
|
|
69
|
+
return `export const ${camel(j.name)} = pgTable('${j.tableName}', {
|
|
70
|
+
${j.ownerCol}: uuid('${snake(j.ownerEntity)}_id').notNull().references(() => ${camel(j.ownerEntity)}.id, { onDelete: 'cascade' }),
|
|
71
|
+
${j.targetCol}: uuid('${snake(j.targetEntity)}_id').notNull().references(() => ${camel(j.targetEntity)}.id, { onDelete: 'cascade' }),
|
|
72
|
+
}, (t) => [primaryKey({ columns: [t.${j.ownerCol}, t.${j.targetCol}] })])`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function emitLocalesTable(e: CmsEntity): string {
|
|
76
|
+
const v = camel(e.name)
|
|
77
|
+
const lines = [
|
|
78
|
+
` id: uuid('id').primaryKey().defaultRandom(),`,
|
|
79
|
+
` parentId: uuid('parent_id').notNull().references(() => ${v}.id, { onDelete: 'cascade' }),`,
|
|
80
|
+
` locale: text('locale').notNull(),`,
|
|
81
|
+
]
|
|
82
|
+
for (const key of e.localized) {
|
|
83
|
+
const fld = e.fields[key]!
|
|
84
|
+
lines.push(` ${key}: ${serializeColumn(translationSpec(fld.spec!), snake(key))},`)
|
|
85
|
+
}
|
|
86
|
+
lines.push(` createdAt: timestamp('created_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow(),`)
|
|
87
|
+
lines.push(` updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow(),`)
|
|
88
|
+
const lname = `${snake(e.name)}_locales`
|
|
89
|
+
const idx = [`uniqueIndex('${lname}_parent_locale_unique').on(t.parentId, t.locale)`]
|
|
90
|
+
for (const key of e.localized) {
|
|
91
|
+
if (e.fields[key]!.isUnique) idx.push(`uniqueIndex('${lname}_${snake(key)}_unique').on(t.locale, t.${key})`)
|
|
92
|
+
}
|
|
93
|
+
return `export const ${v}Locales = pgTable('${lname}', {\n${lines.join('\n')}\n}, (t) => [${idx.join(', ')}])`
|
|
94
|
+
}
|