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
package/src/fields.ts
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import type { LocalizedText } from './i18n'
|
|
2
|
+
import { isSafeLink } from './richtext'
|
|
3
|
+
import { looksLikeLinkRef } from './linkref'
|
|
4
|
+
import { type BlockDef, type BlockValue, type BlocksValue, type GroupValue, blocksGraph, blocksSchema, checkContainedFields, defineBlock, groupGraph } from './blocks'
|
|
5
|
+
import {
|
|
6
|
+
boolean, integer, jsonb, numeric, text, timestamp, uuid,
|
|
7
|
+
type PgColumnBuilder,
|
|
8
|
+
} from 'drizzle-orm/pg-core'
|
|
9
|
+
import { z, type ZodTypeAny } from 'zod'
|
|
10
|
+
import { richTextSchema, type RichTextDoc } from './richtext'
|
|
11
|
+
import type {
|
|
12
|
+
AnyFieldDescriptor, ColumnBase, ColumnSpec, FieldDescriptor, FieldKind, OnDelete, RelationKind, RelationSpec, ValidationContext, ValidationMode,
|
|
13
|
+
} from './descriptor'
|
|
14
|
+
|
|
15
|
+
// ── naming helpers ───────────────────────────────────────────────────────────
|
|
16
|
+
export const snake = (s: string) => s.replace(/[A-Z]/g, (m) => '_' + m.toLowerCase())
|
|
17
|
+
export const camel = (s: string) => s.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase())
|
|
18
|
+
export const pascal = (s: string) => { const c = camel(s); return c.charAt(0).toUpperCase() + c.slice(1) }
|
|
19
|
+
|
|
20
|
+
// ── ONE IR, TWO SINKS ─────────────────────────────────────────────────────────
|
|
21
|
+
// buildColumn → live drizzle builder (runtime + type-inference anchor)
|
|
22
|
+
// serializeColumn → source string written to db/schema.ts (drizzle-kit input)
|
|
23
|
+
// Both consume the same ColumnSpec, so the live table and the committed file cannot diverge.
|
|
24
|
+
|
|
25
|
+
export interface BuildCtx {
|
|
26
|
+
/** the pgEnum instance for `select` columns (created by defineEntity) */
|
|
27
|
+
enumInstance?: (name: string) => PgColumnBuilder
|
|
28
|
+
/** lazy thunk to the referenced column for FK columns */
|
|
29
|
+
refTarget?: () => any
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function buildColumn(spec: ColumnSpec, colName: string, ctx: BuildCtx = {}): PgColumnBuilder {
|
|
33
|
+
let col: any
|
|
34
|
+
switch (spec.base.kind) {
|
|
35
|
+
case 'uuid': col = uuid(colName); break
|
|
36
|
+
case 'text': col = text(colName); break
|
|
37
|
+
case 'integer': col = integer(colName); break
|
|
38
|
+
case 'numeric': col = numeric(colName, { precision: spec.base.precision, scale: spec.base.scale }); break
|
|
39
|
+
case 'boolean': col = boolean(colName); break
|
|
40
|
+
case 'timestamp': col = timestamp(colName, { withTimezone: true, mode: 'date', precision: 3 }); break
|
|
41
|
+
case 'jsonb': col = jsonb(colName); break
|
|
42
|
+
case 'enum': col = ctx.enumInstance!(colName); break
|
|
43
|
+
}
|
|
44
|
+
if (spec.tsType) col = col.$type()
|
|
45
|
+
if (spec.notNull) col = col.notNull()
|
|
46
|
+
if (spec.unique) col = col.unique()
|
|
47
|
+
if (spec.default?.kind === 'now') col = col.defaultNow()
|
|
48
|
+
else if (spec.default?.kind === 'random') col = col.defaultRandom()
|
|
49
|
+
else if (spec.default?.kind === 'value') col = col.default(spec.default.value)
|
|
50
|
+
if (spec.reference && ctx.refTarget) col = col.references(ctx.refTarget, { onDelete: spec.reference.onDelete })
|
|
51
|
+
return col
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SerializeCtx {
|
|
55
|
+
enumVar?: string
|
|
56
|
+
refTargetVar?: string
|
|
57
|
+
/** The column points at its own table (a page's parent): TypeScript needs the return type spelled out. */
|
|
58
|
+
selfReference?: boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function serializeColumn(spec: ColumnSpec, colName: string, ctx: SerializeCtx = {}): string {
|
|
62
|
+
let s: string
|
|
63
|
+
switch (spec.base.kind) {
|
|
64
|
+
case 'uuid': s = `uuid('${colName}')`; break
|
|
65
|
+
case 'text': s = `text('${colName}')`; break
|
|
66
|
+
case 'integer': s = `integer('${colName}')`; break
|
|
67
|
+
case 'numeric': s = `numeric('${colName}', { precision: ${spec.base.precision}, scale: ${spec.base.scale} })`; break
|
|
68
|
+
case 'boolean': s = `boolean('${colName}')`; break
|
|
69
|
+
case 'timestamp': s = `timestamp('${colName}', { withTimezone: true, mode: 'date', precision: 3 })`; break
|
|
70
|
+
case 'jsonb': s = `jsonb('${colName}')`; break
|
|
71
|
+
case 'enum': s = `${ctx.enumVar}('${colName}')`; break
|
|
72
|
+
}
|
|
73
|
+
if (spec.tsType) s += `.$type<${spec.tsType}>()`
|
|
74
|
+
if (spec.notNull) s += '.notNull()'
|
|
75
|
+
if (spec.unique) s += '.unique()'
|
|
76
|
+
if (spec.default?.kind === 'now') s += '.defaultNow()'
|
|
77
|
+
else if (spec.default?.kind === 'random') s += '.defaultRandom()'
|
|
78
|
+
else if (spec.default?.kind === 'value') s += `.default(${JSON.stringify(spec.default.value)})`
|
|
79
|
+
if (spec.reference) s += `.references(${ctx.selfReference ? '(): AnyPgColumn' : '()'} => ${ctx.refTargetVar}.id, { onDelete: '${spec.reference.onDelete}' })`
|
|
80
|
+
return s
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Field types whose value can differ per language. Media, relations, numbers, dates
|
|
85
|
+
* and switches are the same in every language (translate the text around them).
|
|
86
|
+
* Blocks are translated as a whole list per language, a group as a whole group.
|
|
87
|
+
*/
|
|
88
|
+
export const LOCALIZABLE_TYPES = new Set(['text', 'slug', 'richtext', 'blocks', 'group'])
|
|
89
|
+
|
|
90
|
+
/** The stored value of a list field: its items, each with a stable id. */
|
|
91
|
+
export type CmsListValue<F, K extends string = 'item'> = Array<BlockValue<K, F>>
|
|
92
|
+
|
|
93
|
+
/** The stored value of a gallery field. */
|
|
94
|
+
export type CmsGalleryValue = { mediaId: string; caption?: string | null }[]
|
|
95
|
+
|
|
96
|
+
// ── the single field factory ──────────────────────────────────────────────────
|
|
97
|
+
interface FieldConfig {
|
|
98
|
+
type: string
|
|
99
|
+
ui: { edit: string; cell: string }
|
|
100
|
+
base: ColumnBase | null // null ⇒ owns no column
|
|
101
|
+
zod: (context?: ValidationContext) => ZodTypeAny
|
|
102
|
+
meta?: Record<string, unknown>
|
|
103
|
+
relation?: RelationSpec
|
|
104
|
+
tsType?: string
|
|
105
|
+
nameSuffix?: string
|
|
106
|
+
reference?: { target: 'media' | 'relation'; onDeleteRequired: OnDelete; onDeleteOptional: OnDelete }
|
|
107
|
+
defaultDbDefault?: 'now' | 'random'
|
|
108
|
+
/** Payload default applied ONLY on create (e.g. [] for m2m). Never on update. */
|
|
109
|
+
createDefault?: unknown
|
|
110
|
+
/** false: the default fills the column (the database) but is never put into a payload, so a value that must be given cannot be skipped (a consent that must be ticked). */
|
|
111
|
+
zodDefault?: boolean
|
|
112
|
+
/** Block type definitions (blocks fields only). */
|
|
113
|
+
blocks?: Record<string, BlockDef>
|
|
114
|
+
/** The fields a group holds (group fields only). */
|
|
115
|
+
groupFields?: Record<string, AnyFieldDescriptor>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface Mods {
|
|
119
|
+
isRequired?: boolean
|
|
120
|
+
isUnique?: boolean
|
|
121
|
+
isLocalized?: boolean
|
|
122
|
+
isOptional?: boolean
|
|
123
|
+
default?: unknown
|
|
124
|
+
dbDefault?: 'now' | 'random'
|
|
125
|
+
label?: LocalizedText
|
|
126
|
+
help?: LocalizedText
|
|
127
|
+
position?: 'main' | 'sidebar'
|
|
128
|
+
group?: LocalizedText
|
|
129
|
+
icon?: string
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function createField(cfg: FieldConfig, mods: Mods = {}): FieldDescriptor {
|
|
133
|
+
const isOptional = mods.isOptional ?? false
|
|
134
|
+
const isRequired = mods.isRequired ?? false
|
|
135
|
+
const isUnique = mods.isUnique ?? false
|
|
136
|
+
const dbDefault = mods.dbDefault ?? cfg.defaultDbDefault
|
|
137
|
+
|
|
138
|
+
const spec: ColumnSpec | null = cfg.base == null ? null : {
|
|
139
|
+
base: cfg.base,
|
|
140
|
+
notNull: !isOptional,
|
|
141
|
+
unique: isUnique,
|
|
142
|
+
tsType: cfg.tsType,
|
|
143
|
+
nameSuffix: cfg.nameSuffix,
|
|
144
|
+
default:
|
|
145
|
+
dbDefault === 'now' ? { kind: 'now' }
|
|
146
|
+
: dbDefault === 'random' ? { kind: 'random' }
|
|
147
|
+
: mods.default !== undefined ? { kind: 'value', value: mods.default }
|
|
148
|
+
: undefined,
|
|
149
|
+
reference: cfg.reference
|
|
150
|
+
? { target: cfg.reference.target, onDelete: isOptional ? cfg.reference.onDeleteOptional : cfg.reference.onDeleteRequired }
|
|
151
|
+
: undefined,
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const zod = (mode: ValidationMode = 'create', context?: ValidationContext): ZodTypeAny => {
|
|
155
|
+
let s = cfg.zod(context)
|
|
156
|
+
// Defaults apply ONLY on create. On update, omitted keys must stay omitted
|
|
157
|
+
// (a fired default would silently overwrite — e.g. clear m2m / reset enum).
|
|
158
|
+
const createDefault = mods.default !== undefined ? mods.default : cfg.createDefault
|
|
159
|
+
if (mode === 'create' && createDefault !== undefined && cfg.zodDefault !== false) s = (s as any).default(createDefault)
|
|
160
|
+
if (isOptional) s = s.nullish() as ZodTypeAny
|
|
161
|
+
else if (mode === 'update') s = s.optional() as ZodTypeAny
|
|
162
|
+
return s
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Admin label/help live in meta so they flow through the serialized graph untouched.
|
|
166
|
+
const meta: Record<string, unknown> = { ...(cfg.meta ?? {}) }
|
|
167
|
+
if (mods.label !== undefined) meta.label = mods.label
|
|
168
|
+
if (mods.help !== undefined) meta.help = mods.help
|
|
169
|
+
if (mods.position !== undefined) meta.position = mods.position
|
|
170
|
+
if (mods.group !== undefined) meta.group = mods.group
|
|
171
|
+
if (mods.icon !== undefined) meta.icon = mods.icon
|
|
172
|
+
|
|
173
|
+
const descriptor = {
|
|
174
|
+
type: cfg.type,
|
|
175
|
+
isRequired, isUnique, isOptional, isLocalized: !!mods.isLocalized,
|
|
176
|
+
default: mods.default, dbDefault,
|
|
177
|
+
ui: cfg.ui, meta, relation: cfg.relation, blocks: cfg.blocks, fields: cfg.groupFields,
|
|
178
|
+
spec, zod,
|
|
179
|
+
optional: () => createField(cfg, { ...mods, isOptional: true, isRequired: false }),
|
|
180
|
+
unique: () => createField(cfg, { ...mods, isUnique: true }),
|
|
181
|
+
localized: () => {
|
|
182
|
+
if (!LOCALIZABLE_TYPES.has(cfg.type)) {
|
|
183
|
+
throw new Error(`Only ${[...LOCALIZABLE_TYPES].join(', ')} fields can be localized (got "${cfg.type}").`)
|
|
184
|
+
}
|
|
185
|
+
return createField(cfg, { ...mods, isLocalized: true })
|
|
186
|
+
},
|
|
187
|
+
withDefault: (value: unknown) => createField(cfg, { ...mods, default: value }),
|
|
188
|
+
label: (text: LocalizedText) => createField(cfg, { ...mods, label: text }),
|
|
189
|
+
help: (text: LocalizedText) => createField(cfg, { ...mods, help: text }),
|
|
190
|
+
position: (where: 'main' | 'sidebar') => createField(cfg, { ...mods, position: where }),
|
|
191
|
+
group: (label: LocalizedText) => createField(cfg, { ...mods, group: label }),
|
|
192
|
+
icon: (name: string) => createField(cfg, { ...mods, icon: name }),
|
|
193
|
+
}
|
|
194
|
+
return descriptor as unknown as FieldDescriptor
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ── type-level kinds for each field (drive Row/Insert inference) ────────────────
|
|
198
|
+
type ScalarKind<Out, In = Out, OptIn extends boolean = false> = { out: Out; in: In; col: 'scalar'; optionalIn: OptIn }
|
|
199
|
+
type FkKind<OptIn extends boolean = false> = { out: OptIn extends true ? string | null : string; in: OptIn extends true ? string | null | undefined : string; col: 'fk'; optionalIn: OptIn }
|
|
200
|
+
type RelationFieldKind<Kind extends RelationKind, Req extends boolean> =
|
|
201
|
+
Kind extends 'many-to-many' ? { out: never; in: string[]; col: 'none'; optionalIn: true }
|
|
202
|
+
: Kind extends 'one-to-many' ? { out: never; in: never; col: 'none'; optionalIn: true }
|
|
203
|
+
: FkKind<Req extends true ? false : true>
|
|
204
|
+
|
|
205
|
+
const as = <K extends FieldKind>(d: FieldDescriptor): FieldDescriptor<K> => d as unknown as FieldDescriptor<K>
|
|
206
|
+
|
|
207
|
+
// ── the closed MVP field catalog ───────────────────────────────────────────────
|
|
208
|
+
export const f = {
|
|
209
|
+
/**
|
|
210
|
+
* A line of text. `multiline` gives a growing text box (a perex, a short note —
|
|
211
|
+
* plain text, line breaks kept; use richtext for formatting). `max` caps the length.
|
|
212
|
+
*/
|
|
213
|
+
text: (o: { required?: boolean; format?: 'email' | 'url' | 'path' | 'link'; multiline?: boolean; max?: number } = {}) =>
|
|
214
|
+
as<ScalarKind<string>>(createField({
|
|
215
|
+
type: 'text', ui: { edit: 'CmsFieldText', cell: 'CmsCellText' },
|
|
216
|
+
base: { kind: 'text' },
|
|
217
|
+
meta: o.multiline || o.max || o.format
|
|
218
|
+
? { ...(o.multiline ? { multiline: true } : {}), ...(o.max ? { max: o.max } : {}), ...(o.format ? { format: o.format } : {}) }
|
|
219
|
+
: undefined,
|
|
220
|
+
zod: () => {
|
|
221
|
+
// Spaces around a value are never meant (a pasted "/kontakt ", a title typed
|
|
222
|
+
// after a space): trimmed before checking. Multi-line text keeps its layout.
|
|
223
|
+
let s: any = o.multiline ? z.string() : z.string().trim()
|
|
224
|
+
if (o.max) s = s.max(o.max)
|
|
225
|
+
if (o.required) s = s.min(1, 'Required')
|
|
226
|
+
if (o.format === 'email') return s.pipe(z.email())
|
|
227
|
+
// (A `cms:` address is an internal link reference: only link fields hold, and resolve, those.)
|
|
228
|
+
if (o.format === 'url') return s.pipe(z.url()).refine((v: string) => !looksLikeLinkRef(v), 'Use a full address (https://…)')
|
|
229
|
+
// An address on this site: starts with one slash; no spaces, query, fragment
|
|
230
|
+
// or trailing slash (so each address has exactly one spelling).
|
|
231
|
+
if (o.format === 'path') return s.max(2000).regex(/^\/(?!\/)(?:[^\s\\?#]*[^\s\\?#/])?$/, 'Start with / (e.g. /old-page), without ? or # or a trailing /')
|
|
232
|
+
// A link: a site path, #anchor, or http(s) / mailto / tel — never javascript: or data:.
|
|
233
|
+
if (o.format === 'link') return s.max(2000).refine(isSafeLink, 'Use /page, https://…, mailto: or tel:')
|
|
234
|
+
return s as ZodTypeAny
|
|
235
|
+
},
|
|
236
|
+
}, { isRequired: o.required })),
|
|
237
|
+
|
|
238
|
+
slug: (o: { from?: string; unique?: boolean } = {}) =>
|
|
239
|
+
as<ScalarKind<string>>(createField({
|
|
240
|
+
type: 'slug', ui: { edit: 'CmsFieldSlug', cell: 'CmsCellCode' },
|
|
241
|
+
base: { kind: 'text' }, meta: { from: o.from },
|
|
242
|
+
zod: () => z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, 'Lowercase letters, numbers and dashes only'),
|
|
243
|
+
}, { isRequired: true, isUnique: o.unique })),
|
|
244
|
+
|
|
245
|
+
number: (o: { int?: boolean; min?: number; max?: number } = {}) =>
|
|
246
|
+
as<ScalarKind<number>>(createField({
|
|
247
|
+
type: 'number', ui: { edit: 'CmsFieldNumber', cell: 'CmsCellNumber' },
|
|
248
|
+
base: o.int === false ? { kind: 'numeric', precision: 12, scale: 2 } : { kind: 'integer' },
|
|
249
|
+
zod: () => {
|
|
250
|
+
let s = z.number()
|
|
251
|
+
if (o.int !== false) s = s.int()
|
|
252
|
+
if (o.min != null) s = s.min(o.min)
|
|
253
|
+
if (o.max != null) s = s.max(o.max)
|
|
254
|
+
return s
|
|
255
|
+
},
|
|
256
|
+
})),
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* A yes/no switch (stored `false` unless given). `mustBeTrue: true` is for a consent or
|
|
260
|
+
* a confirmation ("I agree to the terms"): a value of `false` is refused, so a website
|
|
261
|
+
* form cannot be sent without it ticked. `needsPrevious` is for a switch in a list of
|
|
262
|
+
* blocks that is about the item before it ("under the link above"): the first item does
|
|
263
|
+
* not show it.
|
|
264
|
+
*/
|
|
265
|
+
boolean: (o: { default?: boolean; mustBeTrue?: boolean; needsPrevious?: boolean } = {}) =>
|
|
266
|
+
as<ScalarKind<boolean, boolean, true>>(createField({
|
|
267
|
+
type: 'boolean', ui: { edit: 'CmsFieldBoolean', cell: 'CmsCellBoolean' },
|
|
268
|
+
base: { kind: 'boolean' },
|
|
269
|
+
zod: () => (o.mustBeTrue ? z.boolean().refine((v) => v === true, { message: 'Must be ticked' }) : z.boolean()),
|
|
270
|
+
...(o.mustBeTrue || o.needsPrevious
|
|
271
|
+
? { meta: { ...(o.mustBeTrue ? { mustBeTrue: true } : {}), ...(o.needsPrevious ? { needsPrevious: true } : {}) } }
|
|
272
|
+
: {}),
|
|
273
|
+
...(o.mustBeTrue ? { zodDefault: false } : {}),
|
|
274
|
+
}, { default: o.default ?? false })),
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* A date and time, edited as wall-clock time in the admin's time zone. `allDay`
|
|
278
|
+
* adds an "All day" switch to the editor: an all-day value is stored at 0:00.
|
|
279
|
+
*/
|
|
280
|
+
datetime: (o: { default?: 'now'; allDay?: boolean } = {}) =>
|
|
281
|
+
as<ScalarKind<Date, Date | string>>(createField({
|
|
282
|
+
type: 'datetime', ui: { edit: 'CmsFieldDate', cell: 'CmsCellDate' },
|
|
283
|
+
base: { kind: 'timestamp' }, zod: () => z.coerce.date(),
|
|
284
|
+
meta: o.allDay ? { allDay: true } : undefined,
|
|
285
|
+
}, { dbDefault: o.default === 'now' ? 'now' : undefined })),
|
|
286
|
+
|
|
287
|
+
select: <const T extends readonly [string, ...string[]], D extends T[number] | undefined = undefined>(
|
|
288
|
+
o: {
|
|
289
|
+
options: T
|
|
290
|
+
default?: D
|
|
291
|
+
/** How each option reads in the admin, e.g. `{ draft: { en: 'Draft', cs: 'Koncept' } }`. Defaults to the value. */
|
|
292
|
+
labels?: Partial<Record<T[number], LocalizedText>>
|
|
293
|
+
/** An icon per option (any Iconify name, e.g. `i-lucide-trophy`), shown on the choice buttons. */
|
|
294
|
+
icons?: Partial<Record<T[number], string>>
|
|
295
|
+
},
|
|
296
|
+
) =>
|
|
297
|
+
as<ScalarKind<T[number], T[number], undefined extends D ? false : true>>(createField({
|
|
298
|
+
type: 'select', ui: { edit: 'CmsFieldSelect', cell: 'CmsCellBadge' },
|
|
299
|
+
base: { kind: 'enum', values: o.options },
|
|
300
|
+
tsType: o.options.map((v) => `'${v}'`).join(' | '),
|
|
301
|
+
meta: { options: o.options, ...(o.labels ? { labels: o.labels } : {}), ...(o.icons ? { icons: o.icons } : {}) },
|
|
302
|
+
zod: () => z.enum([...o.options] as [string, ...string[]]),
|
|
303
|
+
}, { default: o.default })),
|
|
304
|
+
|
|
305
|
+
richtext: () =>
|
|
306
|
+
as<ScalarKind<RichTextDoc>>(createField({
|
|
307
|
+
type: 'richtext', ui: { edit: 'CmsFieldRichText', cell: 'CmsCellExcerpt' },
|
|
308
|
+
base: { kind: 'jsonb' }, tsType: 'RichTextDoc', zod: () => richTextSchema as ZodTypeAny,
|
|
309
|
+
})),
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* An ordered list of typed sections (hero, text, gallery…) that editors arrange
|
|
313
|
+
* into a page. Define each type with `defineBlock()`. Stored as jsonb; defaults to [].
|
|
314
|
+
*
|
|
315
|
+
* sections: f.blocks({ hero: defineBlock({ label: 'Hero', fields: { heading: f.text() } }) })
|
|
316
|
+
*/
|
|
317
|
+
blocks: <const B extends Record<string, BlockDef<any>>>(blocks: B) =>
|
|
318
|
+
as<ScalarKind<BlocksValue<B>, BlocksValue<B>, true>>(createField({
|
|
319
|
+
type: 'blocks', ui: { edit: 'CmsFieldBlocks', cell: 'CmsCellBlocks' },
|
|
320
|
+
base: { kind: 'jsonb' }, tsType: 'CmsBlock[]',
|
|
321
|
+
meta: { blocks: blocksGraph(blocks as Record<string, BlockDef>) },
|
|
322
|
+
zod: (ctx) => blocksSchema(blocks as Record<string, BlockDef>, 500, ctx?.stored),
|
|
323
|
+
createDefault: [],
|
|
324
|
+
blocks: blocks as Record<string, BlockDef>,
|
|
325
|
+
}, { default: [] })),
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* A repeating group of fields: opening hours, team members, FAQ, a price list.
|
|
329
|
+
* Editors add, reorder (drag), duplicate and remove items. Stored as jsonb like
|
|
330
|
+
* blocks with one item type, so each item has a stable `id` (a good `:key`):
|
|
331
|
+
*
|
|
332
|
+
* team: f.list({ fields: { name: f.text({ required: true }), role: f.text() }, max: 30 })
|
|
333
|
+
* // → [{ id: 'k3j…', type: 'item', name: 'Jana', role: 'Trenérka' }, …]
|
|
334
|
+
*
|
|
335
|
+
* Item fields: text, slug, number, boolean, datetime, select, rich text, media.
|
|
336
|
+
* Can be `.localized()`. Items carry a `v` once `version` is above 1 (see `defineBlock`).
|
|
337
|
+
*/
|
|
338
|
+
list: <const F extends Record<string, AnyFieldDescriptor>, const K extends string = 'item'>(o: {
|
|
339
|
+
fields: F; max?: number; itemLabel?: LocalizedText
|
|
340
|
+
/** The stored `type` of every item (default 'item'); lets an existing one-type blocks field become a list. */
|
|
341
|
+
itemType?: K
|
|
342
|
+
/** The items' shape version (1 when left out), as for `defineBlock`: raise it when stored items need changing… */
|
|
343
|
+
version?: number
|
|
344
|
+
/** …and bring an item stored at `fromVersion` to `fromVersion + 1` here (called once per step). */
|
|
345
|
+
upgrade?: BlockDef['upgrade']
|
|
346
|
+
}) => {
|
|
347
|
+
const blocks = { [o.itemType ?? 'item']: defineBlock({ label: o.itemLabel ?? { en: 'Item', cs: 'Položka', de: 'Eintrag' }, icon: 'i-lucide-list', fields: o.fields, version: o.version, upgrade: o.upgrade }) } as Record<string, BlockDef>
|
|
348
|
+
return as<ScalarKind<CmsListValue<F, K>, CmsListValue<F, K>, true>>(createField({
|
|
349
|
+
type: 'blocks', ui: { edit: 'CmsFieldBlocks', cell: 'CmsCellBlocks' },
|
|
350
|
+
base: { kind: 'jsonb' }, tsType: 'CmsBlock[]',
|
|
351
|
+
meta: { blocks: blocksGraph(blocks), list: { max: o.max ?? null, itemLabel: o.itemLabel ?? null } },
|
|
352
|
+
zod: (ctx) => blocksSchema(blocks, o.max ?? 500, ctx?.stored),
|
|
353
|
+
createDefault: [],
|
|
354
|
+
blocks,
|
|
355
|
+
}, { default: [] }))
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* A named object of fields kept together: an address, a contact, an opening-hours
|
|
360
|
+
* summary. Stored as one jsonb column (not one column per field), shown in the admin
|
|
361
|
+
* as a fieldset and delivered as one object by the content API. Each field is checked
|
|
362
|
+
* on its own, like a block's: `required: true` fields must be filled in, the rest may
|
|
363
|
+
* be empty. `.optional()` lets the whole group be absent (null); `.localized()` gives
|
|
364
|
+
* each language its own whole group.
|
|
365
|
+
*
|
|
366
|
+
* address: f.group({ fields: {
|
|
367
|
+
* street: f.text({ required: true }),
|
|
368
|
+
* city: f.text({ required: true }),
|
|
369
|
+
* zip: f.text(),
|
|
370
|
+
* map: f.text({ format: 'url' }).optional(),
|
|
371
|
+
* } })
|
|
372
|
+
* // → { street: 'Dlouhá 1', city: 'Tábor', zip: '390 01', map: null }
|
|
373
|
+
*
|
|
374
|
+
* Inside: text, slug, number, boolean, datetime, select, rich text, media, groups, and
|
|
375
|
+
* a list (only directly in a group on an entry, not in one inside a block, a list or
|
|
376
|
+
* another group). A group is usable inside blocks and list items as well, holding
|
|
377
|
+
* plain fields there.
|
|
378
|
+
*/
|
|
379
|
+
group: <const F extends Record<string, AnyFieldDescriptor>>(o: { fields: F }) => {
|
|
380
|
+
checkContainedFields('Group', o.fields, { reserved: false })
|
|
381
|
+
const inner = o.fields as unknown as Record<string, FieldDescriptor>
|
|
382
|
+
return as<ScalarKind<GroupValue<F>, GroupValue<F>>>(createField({
|
|
383
|
+
type: 'group', ui: { edit: 'CmsFieldGroup', cell: 'CmsCellGroup' },
|
|
384
|
+
base: { kind: 'jsonb' }, tsType: 'Record<string, unknown>',
|
|
385
|
+
meta: { fields: groupGraph(o.fields) },
|
|
386
|
+
groupFields: o.fields,
|
|
387
|
+
zod: (ctx) => {
|
|
388
|
+
// What the same group holds now (lists inside tell stored items from invented ones).
|
|
389
|
+
const stored = ctx?.stored && typeof ctx.stored === 'object' && !Array.isArray(ctx.stored) ? ctx.stored as Record<string, unknown> : undefined
|
|
390
|
+
const shape: Record<string, ZodTypeAny> = {}
|
|
391
|
+
for (const [key, fld] of Object.entries(inner)) {
|
|
392
|
+
// Like inside a block: a field may be left empty unless it is explicitly required.
|
|
393
|
+
const s = fld.zod('create', { stored: stored?.[key] })
|
|
394
|
+
shape[key] = fld.isRequired ? s : s.nullish()
|
|
395
|
+
}
|
|
396
|
+
return z.object(shape)
|
|
397
|
+
},
|
|
398
|
+
}))
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* A file to download (a price list, rules, a menu as PDF): picked from the media
|
|
403
|
+
* library or uploaded. Same storage and API shape as `f.media`, with a file row
|
|
404
|
+
* (name, size, open) instead of a picture.
|
|
405
|
+
*/
|
|
406
|
+
file: (o: { accept?: string } = {}) =>
|
|
407
|
+
as<FkKind>(createField({
|
|
408
|
+
type: 'media', ui: { edit: 'CmsFieldMedia', cell: 'CmsCellThumb' },
|
|
409
|
+
base: { kind: 'uuid' }, meta: { accept: o.accept ?? 'application/pdf', file: true }, nameSuffix: '_id',
|
|
410
|
+
reference: { target: 'media', onDeleteRequired: 'restrict', onDeleteOptional: 'set null' },
|
|
411
|
+
zod: () => z.uuid(),
|
|
412
|
+
})),
|
|
413
|
+
|
|
414
|
+
/** A colour as #rrggbb (a category's colour, a page's accent). */
|
|
415
|
+
color: (o: { required?: boolean } = {}) =>
|
|
416
|
+
as<ScalarKind<string>>(createField({
|
|
417
|
+
type: 'text', ui: { edit: 'CmsFieldColor', cell: 'CmsCellColor' },
|
|
418
|
+
base: { kind: 'text' }, meta: { format: 'color' },
|
|
419
|
+
zod: () => z.string().trim().regex(/^#[0-9a-fA-F]{6}$/, 'Use a colour like #1f6feb'),
|
|
420
|
+
}, { isRequired: o.required })),
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Several files in order, each with an optional caption (a photo gallery, an album).
|
|
424
|
+
* Stored as `[{ mediaId, caption }]`; the content API returns each as the full media
|
|
425
|
+
* object plus `caption`.
|
|
426
|
+
*/
|
|
427
|
+
gallery: (o: { accept?: string; max?: number } = {}) =>
|
|
428
|
+
as<ScalarKind<CmsGalleryValue, CmsGalleryValue, true>>(createField({
|
|
429
|
+
type: 'gallery', ui: { edit: 'CmsFieldGallery', cell: 'CmsCellGallery' },
|
|
430
|
+
base: { kind: 'jsonb' }, tsType: 'CmsGalleryValue',
|
|
431
|
+
meta: { accept: o.accept ?? 'image/*' },
|
|
432
|
+
zod: () => z.array(z.object({ mediaId: z.uuid(), caption: z.string().trim().max(500).nullish() })).max(o.max ?? 200),
|
|
433
|
+
createDefault: [],
|
|
434
|
+
}, { default: [] })),
|
|
435
|
+
|
|
436
|
+
media: (o: { accept?: string } = {}) =>
|
|
437
|
+
as<FkKind>(createField({
|
|
438
|
+
type: 'media', ui: { edit: 'CmsFieldMedia', cell: 'CmsCellThumb' },
|
|
439
|
+
base: { kind: 'uuid' }, meta: { accept: o.accept }, nameSuffix: '_id',
|
|
440
|
+
reference: { target: 'media', onDeleteRequired: 'restrict', onDeleteOptional: 'set null' },
|
|
441
|
+
zod: () => z.uuid(),
|
|
442
|
+
})),
|
|
443
|
+
|
|
444
|
+
relation: <Kind extends RelationKind, Req extends boolean = false>(
|
|
445
|
+
o: { to: () => any; kind: Kind; required?: Req; onDelete?: OnDelete; inverseOf?: string; through?: string },
|
|
446
|
+
) => {
|
|
447
|
+
const isMany = o.kind === 'many-to-many'
|
|
448
|
+
const isVirtual = o.kind === 'one-to-many'
|
|
449
|
+
const ownsColumn = !isMany && !isVirtual
|
|
450
|
+
return as<RelationFieldKind<Kind, Req>>(createField({
|
|
451
|
+
type: 'relation',
|
|
452
|
+
ui: { edit: isMany ? 'CmsFieldRelationMany' : 'CmsFieldRelationOne', cell: 'CmsCellRelation' },
|
|
453
|
+
base: ownsColumn ? { kind: 'uuid' } : null,
|
|
454
|
+
nameSuffix: ownsColumn ? '_id' : undefined,
|
|
455
|
+
relation: o as RelationSpec,
|
|
456
|
+
// NB: never call o.to() here — forward refs (Author.posts → Post) would TDZ.
|
|
457
|
+
meta: { kind: o.kind },
|
|
458
|
+
reference: ownsColumn
|
|
459
|
+
? { target: 'relation', onDeleteRequired: o.onDelete ?? 'restrict', onDeleteOptional: o.onDelete ?? 'set null' }
|
|
460
|
+
: undefined,
|
|
461
|
+
zod: () => (isMany ? z.array(z.uuid()) : z.uuid()),
|
|
462
|
+
createDefault: isMany ? [] : undefined,
|
|
463
|
+
}, { isRequired: !!o.required, isOptional: ownsColumn ? !o.required : false }))
|
|
464
|
+
},
|
|
465
|
+
}
|
package/src/i18n.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text shown in the admin that a developer writes in `cms.config.ts` (entity and
|
|
3
|
+
* field labels, help, descriptions). Either one string for every admin language,
|
|
4
|
+
* or one string per locale code:
|
|
5
|
+
*
|
|
6
|
+
* label: 'Posts'
|
|
7
|
+
* label: { en: 'Posts', cs: 'Články' }
|
|
8
|
+
*
|
|
9
|
+
* This is the *admin interface* language — not content localization.
|
|
10
|
+
*/
|
|
11
|
+
export type LocalizedText = string | Readonly<Record<string, string>>
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve a LocalizedText for a locale: exact locale → its language ('cs-CZ' → 'cs')
|
|
15
|
+
* → each fallback in order → the first value present. Undefined in, undefined out.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveText(text: LocalizedText | undefined | null, locale: string, fallbacks: readonly string[] = ['en']): string | undefined {
|
|
18
|
+
if (text == null) return undefined
|
|
19
|
+
if (typeof text === 'string') return text
|
|
20
|
+
const lang = locale.split('-')[0]!
|
|
21
|
+
for (const key of [locale, lang, ...fallbacks]) {
|
|
22
|
+
const v = text[key]
|
|
23
|
+
if (v) return v
|
|
24
|
+
}
|
|
25
|
+
return Object.values(text).find(Boolean)
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Public API of tinyzoo (consumed by host cms.config.ts and by the runtime).
|
|
2
|
+
export { defineEntity, defineSingleton, buildRegistry, propOf, editorLayout, listLayout } from './entity'
|
|
3
|
+
export type { CmsEntity, CmsRegistry, ContentConfig, PublishConfig, EntityDef, Entity, Junction, Row, Insert, Update, CmsEditorLayout, CmsListLayout, CmsFormSection } from './entity'
|
|
4
|
+
export { f, snake, camel, pascal, buildColumn, serializeColumn, type CmsGalleryValue, type CmsListValue } from './fields'
|
|
5
|
+
export { seo, linkBlock, definePages, defineNavigation, defineRedirects, defineForm, defineFormSettings, FORM_FIELD_TYPES, type FormOptions } from './presets'
|
|
6
|
+
export { defineBlock, blocksSchema, mediaIdsInBlocks, mediaIdsInFields, blockMediaKeys, BLOCK_FIELD_TYPES, blockVersion, storedBlockVersion, upgradeBlock, upgradeBlocks, upgradeListItems, upgradeStoredRow, logUpgradeFailure, UPGRADE_FAILED_KEY, UNKNOWN_BLOCK_TYPE, type UpgradeIssue, type UpgradeOptions, type BlockDef, type BlockValue, type BlocksValue, type GroupValue, type CmsBlock } from './blocks'
|
|
7
|
+
export { resolveText, type LocalizedText } from './i18n'
|
|
8
|
+
export { defineLocales, type LocalesDef } from './locales'
|
|
9
|
+
export {
|
|
10
|
+
TREE_DEFAULT_MAX_DEPTH, TREE_MAX_DEPTH_LIMIT, indexTree, existsIn, slugIn, titleIn, chainOf, depthOf, childrenMap, sortSiblings, descendantsOf, heightOf,
|
|
11
|
+
slugChain, treeAddress, applyChange, checkTreeChange, checkTree, placeAmong, TREE_POSITION_STEP, movedPaths,
|
|
12
|
+
type TreeConfig, type TreeNode, type TreeIndex, type TreeChange, type TreeProblem, type TreeIssue, type TreeCheckOptions, type MovedPath,
|
|
13
|
+
} from './tree'
|
|
14
|
+
export { sitePrefix, pathTemplateFor, pathTemplateKeys, fillPathTemplate, sitePath, prefixedTemplates, type PathTemplate } from './paths'
|
|
15
|
+
export {
|
|
16
|
+
applyMigrations, runUpgrade, inspectMigrations, classifyMigrations, MigrationOrderError, ORDER_FIX, sqlWithParams, isConnectionError,
|
|
17
|
+
type MigrationJournal, type MigrationSource, type SqlRunner, type JournalEntry, type UpgradeResult, type MigrationStatus, type StepStatus, type MigrationClassification,
|
|
18
|
+
} from './migrate'
|
|
19
|
+
export { CORE_UPGRADE_STEPS, allUpgradeSteps, type UpgradeStep, type UpgradeContext, type StepOutcome } from './upgrades'
|
|
20
|
+
export {
|
|
21
|
+
defineCmsPlugin, collectPlugins, planPlugins, pluginFile, pluginUpgradeSteps, pluginStepId, pluginEntities, pluginRouteAccess, matchRoute, isCmsPlugin,
|
|
22
|
+
pluginApiBase, pluginAdminBase, pluginEntityPrefix,
|
|
23
|
+
type CmsPlugin, type CmsPluginDef, type CmsPluginHandler, type CmsPluginAdminPage, type CmsPluginNavItem, type CmsPluginHooks, type CmsPluginUpgradeStep,
|
|
24
|
+
type CmsPluginRequirement, type CmsFieldRenderer, type CmsGraphExtension, type CmsRetentionContext, type CmsRouteAccess, type CmsHttpMethod, type PluginPlan,
|
|
25
|
+
} from './plugin'
|
|
26
|
+
export { emitSchema } from './emitter'
|
|
27
|
+
export { buildOpenApiDocument, type OpenApiOptions } from './openapi'
|
|
28
|
+
export { emitContentTypes, type SdkOptions } from './sdk'
|
|
29
|
+
export {
|
|
30
|
+
buildContentModel,
|
|
31
|
+
type ContentModel,
|
|
32
|
+
type ContentShape,
|
|
33
|
+
type ContentField,
|
|
34
|
+
type ContentFieldType,
|
|
35
|
+
type EmbeddedShape,
|
|
36
|
+
type RefShape,
|
|
37
|
+
type FilterParam,
|
|
38
|
+
} from './content-shape'
|
|
39
|
+
export { cmsMedia } from './media'
|
|
40
|
+
export type { CmsChange, CmsChangeType, CmsSitemapEntry } from './changes'
|
|
41
|
+
export type { CmsErrorReport } from './errorReport'
|
|
42
|
+
export { generateSetupCode, normalizeSetupCode } from './setupCode'
|
|
43
|
+
export { cmsUsers, cmsTokens, cmsAudit, cmsCollab, cmsRevisions, cmsState, cmsSearch, CMS_ROLES, type CmsRole } from './system'
|
|
44
|
+
export { searchTerms, foldSearchText, entryTexts, richTextPlain, isSearchableField, matchEntry, excerptOf, highlightRanges, wordMatch, editDistance, allowedTypos, SEARCH_MAX_QUERY, type SearchMatch } from './search'
|
|
45
|
+
export { linkRef, parseLinkRef, isLinkRef, looksLikeLinkRef, holdsLinks, linkFieldsOf, mapFieldLinks, mapRowLinks, mapDocLinks, linkRefsOf, type LinkRef, type LinkVisitor, type LinkFieldLike } from './links'
|
|
46
|
+
export { isSafeLink, richTextSchema, sanitizeRichText, emptyDoc, richTextUpgrades, upgradeRichText, type RichTextDoc, type RichTextUpgrade } from './richtext'
|
|
47
|
+
export { defineSeed, isSeed, type SeedContext, type SeedDefinition, type SeedEntry, type SeedInsert, type SeedWriteOptions, type SeedGlobalOptions, type SeedUploadOptions, type SeedLinkOptions, type SeedLink, type SeedRich, type SeedRichNode } from './seed'
|
|
48
|
+
export type * from './descriptor'
|
|
49
|
+
export { cmsDeprecated, cmsDeprecations, resetCmsDeprecations, type CmsDeprecation, type CmsDeprecatedOptions } from './deprecate'
|
|
50
|
+
|
|
51
|
+
// Re-export drizzle query helpers so host access rules can use them
|
|
52
|
+
// (e.g. `update: ({ user }) => eq(Post.table.authorId, user.id)`).
|
|
53
|
+
export { alias } from 'drizzle-orm/pg-core'
|
|
54
|
+
export { eq, ne, gt, gte, lt, lte, and, or, not, ilike, inArray, notInArray, isNull, isNotNull, sql, desc, asc } from 'drizzle-orm'
|
package/src/linkref.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stored form of a link that follows its target: `cms:<entity>/<id>`. Kept apart
|
|
3
|
+
* (no imports) so the rich-text sanitiser and the link walker can both use it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const PREFIX = 'cms:'
|
|
7
|
+
const UUID = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'
|
|
8
|
+
const REF = new RegExp(`^cms:([A-Za-z][A-Za-z0-9_-]{0,63})/(${UUID})$`)
|
|
9
|
+
|
|
10
|
+
export interface LinkRef { entity: string; id: string }
|
|
11
|
+
|
|
12
|
+
/** The stored form of a link to an entry: `cms:post/2f6c…` (the id in lower case). */
|
|
13
|
+
export function linkRef(entity: string, id: string): string {
|
|
14
|
+
return `${PREFIX}${entity}/${id.toLowerCase()}`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The entity and id a stored link refers to; null when it is not a well-formed reference. */
|
|
18
|
+
export function parseLinkRef(value: unknown): LinkRef | null {
|
|
19
|
+
if (typeof value !== 'string') return null
|
|
20
|
+
const m = REF.exec(value)
|
|
21
|
+
return m ? { entity: m[1]!, id: m[2]!.toLowerCase() } : null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Is this a well-formed reference to an entry (`cms:<entity>/<id>`)? */
|
|
25
|
+
export const isLinkRef = (value: unknown): boolean => parseLinkRef(value) !== null
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Does this look like a reference, well-formed or not? Anything that starts with `cms:`
|
|
29
|
+
* is an internal value: it is resolved or dropped on read, never served as it is.
|
|
30
|
+
*/
|
|
31
|
+
export const looksLikeLinkRef = (value: unknown): boolean => typeof value === 'string' && value.slice(0, PREFIX.length).toLowerCase() === PREFIX
|