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/entity.ts
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import { type PgColumn, pgEnum, pgTable, primaryKey, text, timestamp, uniqueIndex, uuid } from 'drizzle-orm/pg-core'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import { buildColumn, camel, snake } from './fields'
|
|
4
|
+
import { cmsMedia } from './media'
|
|
5
|
+
import { cmsAudit, cmsCollab, cmsRevisions, cmsSearch, cmsState, cmsTokens, cmsUsers } from './system'
|
|
6
|
+
import type { LocalesDef } from './locales'
|
|
7
|
+
import { pathTemplateProblem, pathTemplateKeys, prefixedTemplates, type PathTemplate } from './paths'
|
|
8
|
+
import { TREE_DEFAULT_MAX_DEPTH, TREE_MAX_DEPTH_LIMIT, type TreeConfig } from './tree'
|
|
9
|
+
import type { AnyFieldDescriptor, FieldDescriptor } from './descriptor'
|
|
10
|
+
import type { LocalizedText } from './i18n'
|
|
11
|
+
import { collectPlugins, pluginEntities, type CmsPlugin, type CmsPluginRequirement } from './plugin'
|
|
12
|
+
|
|
13
|
+
// ── type-level inference helpers ────────────────────────────────────────────────
|
|
14
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {}
|
|
15
|
+
type KindOf<D> = D extends FieldDescriptor<infer K> ? K : never
|
|
16
|
+
|
|
17
|
+
/** DB-column property key for a field: scalars keep the key, FKs become `${K}Id`, virtual relations drop. */
|
|
18
|
+
type ColumnKey<K extends string, D> = KindOf<D>['col'] extends 'none' ? never
|
|
19
|
+
: KindOf<D>['col'] extends 'fk' ? `${K}Id`
|
|
20
|
+
: K
|
|
21
|
+
|
|
22
|
+
/** A selected row: system columns + each owned column at its DB key. */
|
|
23
|
+
type RowFields<F> = Prettify<
|
|
24
|
+
{ id: string; createdAt: Date; updatedAt: Date; createdBy: string | null; updatedBy: string | null } &
|
|
25
|
+
{ [K in keyof F & string as ColumnKey<K, F[K]>]: KindOf<F[K]>['out'] }
|
|
26
|
+
>
|
|
27
|
+
|
|
28
|
+
type Included<D> = [KindOf<D>['in']] extends [never] ? false : true // false for one-to-many
|
|
29
|
+
/** Insert payload (API keys, not DB keys): required vs optional split by `optionalIn`. */
|
|
30
|
+
type InsertFields<F> = Prettify<
|
|
31
|
+
{ [K in keyof F & string as Included<F[K]> extends true ? (KindOf<F[K]>['optionalIn'] extends true ? never : K) : never]: KindOf<F[K]>['in'] } &
|
|
32
|
+
{ [K in keyof F & string as Included<F[K]> extends true ? (KindOf<F[K]>['optionalIn'] extends true ? K : never) : never]?: KindOf<F[K]>['in'] }
|
|
33
|
+
>
|
|
34
|
+
|
|
35
|
+
/** The entity's drizzle table, typed enough for `eq()` + key autocomplete in access rules. */
|
|
36
|
+
type TableColumns<F> = Prettify<
|
|
37
|
+
{ id: PgColumn; createdAt: PgColumn; updatedAt: PgColumn; createdBy: PgColumn; updatedBy: PgColumn } &
|
|
38
|
+
{ [K in keyof F & string as ColumnKey<K, F[K]>]: PgColumn }
|
|
39
|
+
>
|
|
40
|
+
|
|
41
|
+
/** Headless content-delivery config — drives the public `/api/content/*` API. */
|
|
42
|
+
export interface ContentConfig<F extends Record<string, AnyFieldDescriptor>> {
|
|
43
|
+
/** Expose this entity through the public content API. Default: false. */
|
|
44
|
+
public?: boolean
|
|
45
|
+
/**
|
|
46
|
+
* Drizzle condition (against the entity's own table) that an item must satisfy
|
|
47
|
+
* to be delivered publicly, e.g. `(t) => eq(t.status, 'published')`. Omit to make
|
|
48
|
+
* every row public (reference data such as tags/categories).
|
|
49
|
+
*/
|
|
50
|
+
published?: (table: TableColumns<F>) => unknown
|
|
51
|
+
/** Field used to look a single item up by, e.g. 'slug'. Falls back to id. */
|
|
52
|
+
slug?: keyof F & string
|
|
53
|
+
/**
|
|
54
|
+
* The item's address on the website, e.g. '/blog/:slug' (`:field` placeholders),
|
|
55
|
+
* or one per language when the address is translated:
|
|
56
|
+
* { cs: '/aktuality/:slug', en: '/news/:slug' }. Used for the sitemap, webhooks and
|
|
57
|
+
* cache purging (with a language prefix per `defineLocales`).
|
|
58
|
+
*/
|
|
59
|
+
path?: PathTemplate
|
|
60
|
+
/** Relation/media field keys to resolve into the response. Default: all of them. */
|
|
61
|
+
populate?: (keyof F & string)[]
|
|
62
|
+
/**
|
|
63
|
+
* Denylist of field keys to omit from the public response — applied to this entity
|
|
64
|
+
* AND when it is populated as another entity's relation. Every sensitive scalar on a
|
|
65
|
+
* publicly-readable/populatable entity must be listed here.
|
|
66
|
+
*/
|
|
67
|
+
hidden?: (keyof F & string)[]
|
|
68
|
+
/**
|
|
69
|
+
* The website search (`/api/content/_search`) covers every public collection with a
|
|
70
|
+
* `path`: its title, text, rich text and the text inside blocks. `false` leaves the
|
|
71
|
+
* collection out; `{ fields }` matches only those fields (and the title).
|
|
72
|
+
*/
|
|
73
|
+
search?: false | { fields?: (keyof F & string)[] }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Drives the admin publish/unpublish workflow + draft preview. */
|
|
77
|
+
export interface PublishConfig<F extends Record<string, AnyFieldDescriptor>> {
|
|
78
|
+
/** The status field toggled by publish/unpublish (e.g. 'status'). */
|
|
79
|
+
field: keyof F & string
|
|
80
|
+
/** Value meaning "live". */
|
|
81
|
+
publishedValue: string
|
|
82
|
+
/** Value meaning "draft". */
|
|
83
|
+
draftValue: string
|
|
84
|
+
/**
|
|
85
|
+
* Optional datetime field stamped with now() on publish. A date in the future
|
|
86
|
+
* schedules the entry: it goes live at that moment (see `schedule`).
|
|
87
|
+
*/
|
|
88
|
+
dateField?: keyof F & string
|
|
89
|
+
/**
|
|
90
|
+
* Treat a future `dateField` as "scheduled": live only from that moment on (the
|
|
91
|
+
* content API, sitemap and webhooks all follow it). Default true when there is a
|
|
92
|
+
* `dateField`.
|
|
93
|
+
*/
|
|
94
|
+
schedule?: boolean
|
|
95
|
+
/**
|
|
96
|
+
* Frontend preview path template with `:field` placeholders, e.g. '/blog/:slug',
|
|
97
|
+
* or one per language ({ cs: '/aktuality/:slug', en: '/news/:slug' }). The admin
|
|
98
|
+
* opens it (language prefix added) with `?preview` so an editor can see a draft.
|
|
99
|
+
*/
|
|
100
|
+
preview?: PathTemplate
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface EntityDef<F extends Record<string, AnyFieldDescriptor>> {
|
|
104
|
+
/** Plural name shown in the admin ("Posts"). One string, or one per admin locale. */
|
|
105
|
+
label: LocalizedText
|
|
106
|
+
/** Singular name for buttons and messages ("New post"). Defaults to `label`. */
|
|
107
|
+
labelSingular?: LocalizedText
|
|
108
|
+
/** One or two sentences on what this content is for — shown in the admin's lists and empty states. */
|
|
109
|
+
description?: LocalizedText
|
|
110
|
+
/** Iconify name for the admin navigation, e.g. 'i-lucide-newspaper'. */
|
|
111
|
+
icon?: string
|
|
112
|
+
titleField: keyof F & string
|
|
113
|
+
fields: F
|
|
114
|
+
/** A single-row global (Settings/Homepage) — admin shows one form, no list. */
|
|
115
|
+
singleton?: boolean
|
|
116
|
+
/**
|
|
117
|
+
* Every write takes effect on the website at once (e.g. redirects). People who may
|
|
118
|
+
* not publish (authors) cannot write such content.
|
|
119
|
+
*/
|
|
120
|
+
live?: boolean
|
|
121
|
+
admin?: {
|
|
122
|
+
list?: {
|
|
123
|
+
columns: (keyof F & string)[]
|
|
124
|
+
searchable?: (keyof F & string)[]
|
|
125
|
+
/**
|
|
126
|
+
* A number field holding a manual order: the list can then be arranged by
|
|
127
|
+
* dragging, and the content API returns this order unless asked to sort.
|
|
128
|
+
*/
|
|
129
|
+
orderField?: keyof F & string
|
|
130
|
+
/**
|
|
131
|
+
* How the list looks. Default: chosen from the content — `grid` when the first
|
|
132
|
+
* column is a picture and there is no text body (albums, logos), `dated` (Upcoming / Past) when there are no drafts and a
|
|
133
|
+
* date column, otherwise `table`.
|
|
134
|
+
*/
|
|
135
|
+
layout?: CmsListLayout
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* How the editor is arranged. Default: chosen from the content — `write` for a
|
|
139
|
+
* rich-text body with a page on the website, `preview` for pages built from blocks
|
|
140
|
+
* (and globals with a `preview` address), otherwise `form`.
|
|
141
|
+
*/
|
|
142
|
+
editor?: CmsEditorLayout
|
|
143
|
+
/**
|
|
144
|
+
* Where a global (defineSingleton) shows on the website, e.g. '/' for the home
|
|
145
|
+
* page: the editor then shows it beside the form. Entries use `publish.preview`.
|
|
146
|
+
*/
|
|
147
|
+
preview?: PathTemplate
|
|
148
|
+
/**
|
|
149
|
+
* Tabs for a long form: each lists the fields it shows in the main column
|
|
150
|
+
* (fields in no tab go into the first). The side panel is not tabbed.
|
|
151
|
+
*/
|
|
152
|
+
tabs?: { label: LocalizedText; fields: (keyof F & string)[] }[]
|
|
153
|
+
/**
|
|
154
|
+
* How the form is grouped: each section is a heading (best as a plain question,
|
|
155
|
+
* "When?", "Where?") over the fields it lists, in order. Fields in no section
|
|
156
|
+
* follow in sections of their own kind. Unset = grouped by kind of field (dates,
|
|
157
|
+
* pictures, links, related entries) once a form has enough fields to need it.
|
|
158
|
+
* Long forms (four sections or more) get a section menu beside them.
|
|
159
|
+
*/
|
|
160
|
+
sections?: CmsFormSection<keyof F & string>[]
|
|
161
|
+
}
|
|
162
|
+
access?: Partial<Record<'read' | 'create' | 'update' | 'delete', (ctx: { user: any; table: TableColumns<F> }) => unknown>>
|
|
163
|
+
content?: ContentConfig<F>
|
|
164
|
+
publish?: PublishConfig<F>
|
|
165
|
+
/**
|
|
166
|
+
* Entries that sit under each other (a page tree): `parent` is a many-to-one relation to
|
|
167
|
+
* the entity itself, `order` a whole-number field with the position among siblings. The
|
|
168
|
+
* website address becomes the chain of address parts (`content.slug`) from the top down
|
|
169
|
+
* ('/o-nas/tym'), unique per language; moving an entry moves everything below it and
|
|
170
|
+
* leaves redirects; deleting an entry that has children is refused. `definePages({ tree:
|
|
171
|
+
* true })` sets all of this up.
|
|
172
|
+
*/
|
|
173
|
+
tree?: { parent: keyof F & string; order: keyof F & string; maxDepth?: number }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** A titled group of fields in the admin form (see `admin.sections`). */
|
|
177
|
+
export interface CmsFormSection<K extends string = string> {
|
|
178
|
+
label: LocalizedText
|
|
179
|
+
/** An Iconify icon for the heading, e.g. `i-lucide-calendar`. */
|
|
180
|
+
icon?: string
|
|
181
|
+
/** One sentence under the heading: what the fields are for or where they show. */
|
|
182
|
+
description?: LocalizedText
|
|
183
|
+
fields: K[]
|
|
184
|
+
/** Lay short fields (single-line text, numbers, choices) two to a row on wide screens. */
|
|
185
|
+
columns?: 1 | 2
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The editor's arrangement: one calm writing column, a form with a properties panel, or the form beside the live page. */
|
|
189
|
+
export type CmsEditorLayout = 'write' | 'form' | 'preview'
|
|
190
|
+
/** The list's look: rows, picture cards, Upcoming / Past by date, or an indented tree. */
|
|
191
|
+
export type CmsListLayout = 'table' | 'grid' | 'dated' | 'tree'
|
|
192
|
+
|
|
193
|
+
/** The precisely-typed entity returned by defineEntity. */
|
|
194
|
+
export interface Entity<F extends Record<string, AnyFieldDescriptor> = Record<string, AnyFieldDescriptor>> {
|
|
195
|
+
__cms: 'entity'
|
|
196
|
+
name: string
|
|
197
|
+
label: LocalizedText
|
|
198
|
+
labelSingular?: LocalizedText
|
|
199
|
+
description?: LocalizedText
|
|
200
|
+
icon?: string
|
|
201
|
+
titleField: keyof F & string
|
|
202
|
+
table: TableColumns<F>
|
|
203
|
+
/** Translations of the `.localized()` fields (`<name>_locales`), or undefined. */
|
|
204
|
+
localesTable?: any
|
|
205
|
+
/** Keys of the `.localized()` fields. */
|
|
206
|
+
localized: string[]
|
|
207
|
+
fields: F
|
|
208
|
+
enums: EntityEnum[]
|
|
209
|
+
insertSchema: z.ZodType<InsertFields<F>>
|
|
210
|
+
updateSchema: z.ZodType<Partial<InsertFields<F>>>
|
|
211
|
+
singleton?: boolean
|
|
212
|
+
admin?: EntityDef<F>['admin']
|
|
213
|
+
access?: EntityDef<F>['access']
|
|
214
|
+
content?: EntityDef<F>['content']
|
|
215
|
+
publish?: EntityDef<F>['publish']
|
|
216
|
+
/** Set when the entries form a tree (see `EntityDef.tree`); `maxDepth` is filled in. */
|
|
217
|
+
tree?: TreeConfig
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Public data-shape helpers — `Row<typeof Post>`, `Insert<typeof Post>`, `Update<typeof Post>`. */
|
|
221
|
+
export type Row<E> = E extends Entity<infer F> ? RowFields<F> : never
|
|
222
|
+
export type Insert<E> = E extends Entity<infer F> ? InsertFields<F> : never
|
|
223
|
+
export type Update<E> = E extends Entity<infer F> ? Partial<InsertFields<F>> : never
|
|
224
|
+
|
|
225
|
+
/** Runtime-facing (erased) entity view used by the registry/server. */
|
|
226
|
+
export interface CmsEntity {
|
|
227
|
+
__cms: 'entity'
|
|
228
|
+
name: string
|
|
229
|
+
label: LocalizedText
|
|
230
|
+
labelSingular?: LocalizedText
|
|
231
|
+
description?: LocalizedText
|
|
232
|
+
icon?: string
|
|
233
|
+
titleField: string
|
|
234
|
+
table: any
|
|
235
|
+
localesTable?: any
|
|
236
|
+
localized: string[]
|
|
237
|
+
fields: Record<string, FieldDescriptor>
|
|
238
|
+
enums: EntityEnum[]
|
|
239
|
+
insertSchema: z.ZodTypeAny
|
|
240
|
+
updateSchema: z.ZodTypeAny
|
|
241
|
+
singleton?: boolean
|
|
242
|
+
admin?: {
|
|
243
|
+
list?: { columns: string[]; searchable?: string[]; orderField?: string; layout?: CmsListLayout }
|
|
244
|
+
editor?: CmsEditorLayout
|
|
245
|
+
preview?: PathTemplate
|
|
246
|
+
tabs?: { label: LocalizedText; fields: string[] }[]
|
|
247
|
+
sections?: CmsFormSection[]
|
|
248
|
+
}
|
|
249
|
+
access?: Record<string, (ctx: { user: any; table: any }) => unknown>
|
|
250
|
+
content?: {
|
|
251
|
+
public?: boolean
|
|
252
|
+
published?: (table: any) => unknown
|
|
253
|
+
slug?: string
|
|
254
|
+
path?: PathTemplate
|
|
255
|
+
populate?: string[]
|
|
256
|
+
hidden?: string[]
|
|
257
|
+
search?: false | { fields?: string[] }
|
|
258
|
+
}
|
|
259
|
+
publish?: { field: string; publishedValue: string; draftValue: string; dateField?: string; schedule?: boolean; preview?: PathTemplate }
|
|
260
|
+
live?: boolean
|
|
261
|
+
tree?: TreeConfig
|
|
262
|
+
/** Set by built-in presets (e.g. 'redirects') so the runtime can find them. */
|
|
263
|
+
preset?: string
|
|
264
|
+
/** Set by a plugin's helpers (`defineForm`): the plugin this entity cannot work without. */
|
|
265
|
+
requiresPlugin?: CmsPluginRequirement
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
interface EntityEnum { key: string; varName: string; enumName: string; values: readonly string[] }
|
|
269
|
+
|
|
270
|
+
/** A translated column: same type, nullable, no default, uniqueness handled per locale. */
|
|
271
|
+
export function translationSpec(spec: import('./descriptor').ColumnSpec): import('./descriptor').ColumnSpec {
|
|
272
|
+
return { ...spec, notNull: false, unique: false, default: undefined, reference: undefined }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** DB column property name for a field (FKs become `${key}Id`). */
|
|
276
|
+
export function propOf(key: string, fld: FieldDescriptor): string {
|
|
277
|
+
return fld.spec?.nameSuffix === '_id' ? key + 'Id' : key
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Field names the CMS uses itself (system columns, translations, public item keys). */
|
|
281
|
+
const RESERVED_FIELD_NAMES = new Set(['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy', 'parentId', 'locale', 'locales'])
|
|
282
|
+
|
|
283
|
+
/** Entity names the CMS uses internally; user entities may not take them. */
|
|
284
|
+
const RESERVED_ENTITY_NAMES = new Set(['media', 'graph', 'styleguide', 'login', 'setup', 'forgot', 'reset', 'invite', 'team', 'account', 'p'])
|
|
285
|
+
|
|
286
|
+
export function defineEntity<F extends Record<string, AnyFieldDescriptor>>(
|
|
287
|
+
name: string,
|
|
288
|
+
def: EntityDef<F>,
|
|
289
|
+
): Entity<F> {
|
|
290
|
+
// Reserve `_`-prefixed names and built-ins so an entity can never shadow the
|
|
291
|
+
// internal API routes (/api/cms/_media, /api/cms/_graph).
|
|
292
|
+
if (name.startsWith('_') || RESERVED_ENTITY_NAMES.has(name)) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
`Invalid entity name "${name}": names starting with "_" or in `
|
|
295
|
+
+ `{${[...RESERVED_ENTITY_NAMES].join(', ')}} are reserved by the CMS.`,
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
// The name becomes a database table, a URL address and a name in the generated schema.ts:
|
|
299
|
+
// a space or a dash there is a syntax error in that file, found only when it is compiled.
|
|
300
|
+
if (typeof name !== 'string' || !/^[A-Za-z][A-Za-z0-9_]*$/.test(name)) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`Invalid entity name "${name}": use letters, digits and underscores, starting with a letter, without spaces or dashes `
|
|
303
|
+
+ '(for example "blogPost" or "blog_post"). The name becomes a database table, an address in the API and a name in the generated schema; the label shown in the admin is separate.',
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
const fields = def.fields as unknown as Record<string, FieldDescriptor>
|
|
307
|
+
for (const key of Object.keys(fields)) {
|
|
308
|
+
if (RESERVED_FIELD_NAMES.has(key)) {
|
|
309
|
+
throw new Error(`Field "${key}" in "${name}" is reserved by the CMS (${[...RESERVED_FIELD_NAMES].join(', ')}).`)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const orderField = def.admin?.list?.orderField as string | undefined
|
|
313
|
+
if (orderField && (fields[orderField]?.type !== 'number' || fields[orderField]?.spec?.base.kind !== 'integer')) {
|
|
314
|
+
throw new Error(`admin.list.orderField of "${name}" must be a whole-number field ("${orderField}" is not).`)
|
|
315
|
+
}
|
|
316
|
+
const sectioned = new Set<string>()
|
|
317
|
+
for (const section of def.admin?.sections ?? []) {
|
|
318
|
+
for (const key of section.fields as string[]) {
|
|
319
|
+
if (!fields[key]) throw new Error(`admin.sections of "${name}" lists "${key}", which is not a field.`)
|
|
320
|
+
if (sectioned.has(key)) throw new Error(`admin.sections of "${name}" lists "${key}" twice.`)
|
|
321
|
+
sectioned.add(key)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
let tree: TreeConfig | undefined
|
|
326
|
+
if (def.tree) {
|
|
327
|
+
const { parent, order } = def.tree as { parent: string; order: string; maxDepth?: number }
|
|
328
|
+
const maxDepth = def.tree.maxDepth ?? TREE_DEFAULT_MAX_DEPTH
|
|
329
|
+
if (fields[parent]?.relation?.kind !== 'many-to-one') throw new Error(`tree.parent of "${name}" must be a many-to-one relation ("${parent}" is not).`)
|
|
330
|
+
if (fields[order]?.type !== 'number' || fields[order]?.spec?.base.kind !== 'integer') throw new Error(`tree.order of "${name}" must be a whole-number field ("${order}" is not).`)
|
|
331
|
+
if (!Number.isInteger(maxDepth) || maxDepth < 2 || maxDepth > TREE_MAX_DEPTH_LIMIT) throw new Error(`tree.maxDepth of "${name}" must be a whole number from 2 to ${TREE_MAX_DEPTH_LIMIT}.`)
|
|
332
|
+
const slug = def.content?.slug as string | undefined
|
|
333
|
+
if (!slug || fields[slug]?.type !== 'slug') throw new Error(`"${name}" is a tree, so content.slug must name a slug field.`)
|
|
334
|
+
if (fields[slug]!.isUnique) throw new Error(`The slug of "${name}" is part of a nested address, so it cannot be unique on its own: leave out unique (the address is checked instead).`)
|
|
335
|
+
if (!def.content?.path || !pathTemplateKeys(def.content.path).includes(slug)) throw new Error(`"${name}" is a tree, so content.path must contain :${slug}.`)
|
|
336
|
+
tree = { parent, order, maxDepth }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// 1) pgEnums for select fields
|
|
340
|
+
const enums: EntityEnum[] = []
|
|
341
|
+
const enumInstances: Record<string, any> = {}
|
|
342
|
+
for (const [key, fld] of Object.entries(fields)) {
|
|
343
|
+
if (fld.spec?.base.kind === 'enum') {
|
|
344
|
+
// SQL name snake_case; the TS variable name stays camelCase of the entity name.
|
|
345
|
+
const enumName = `${snake(name)}_${snake(key)}`
|
|
346
|
+
enumInstances[key] = pgEnum(enumName, [...fld.spec.base.values] as [string, ...string[]])
|
|
347
|
+
enums.push({ key, varName: camel(`${name}_${snake(key)}`), enumName, values: fld.spec.base.values })
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// 2) live columns (the InferSelectModel anchor)
|
|
352
|
+
const columns: Record<string, any> = { id: uuid('id').primaryKey().defaultRandom() }
|
|
353
|
+
for (const [key, fld] of Object.entries(fields)) {
|
|
354
|
+
if (!fld.spec) continue
|
|
355
|
+
const dbName = snake(key) + (fld.spec.nameSuffix ?? '')
|
|
356
|
+
const refTarget = fld.spec.reference
|
|
357
|
+
? (fld.spec.reference.target === 'media'
|
|
358
|
+
? () => cmsMedia.id
|
|
359
|
+
: () => (fld.relation!.to() as CmsEntity).table.id)
|
|
360
|
+
: undefined
|
|
361
|
+
columns[propOf(key, fld)] = buildColumn(fld.spec, dbName, { enumInstance: enumInstances[key], refTarget })
|
|
362
|
+
}
|
|
363
|
+
columns.createdAt = timestamp('created_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow()
|
|
364
|
+
columns.updatedAt = timestamp('updated_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow()
|
|
365
|
+
// Who created / last changed the row (a CMS user id; no FK so external auth works too).
|
|
366
|
+
columns.createdBy = uuid('created_by')
|
|
367
|
+
columns.updatedBy = uuid('updated_by')
|
|
368
|
+
const table = pgTable(snake(name), columns)
|
|
369
|
+
|
|
370
|
+
// Translations: one row per (entry, language) holding the localized fields.
|
|
371
|
+
// Nullable — an empty translation falls back to the default language.
|
|
372
|
+
const localized = Object.entries(fields).filter(([, f]) => f.isLocalized).map(([k]) => k)
|
|
373
|
+
let localesTable: any
|
|
374
|
+
if (localized.length) {
|
|
375
|
+
const lcols: Record<string, any> = {
|
|
376
|
+
id: uuid('id').primaryKey().defaultRandom(),
|
|
377
|
+
parentId: uuid('parent_id').notNull().references(() => (table as any).id, { onDelete: 'cascade' }),
|
|
378
|
+
locale: text('locale').notNull(),
|
|
379
|
+
}
|
|
380
|
+
for (const key of localized) {
|
|
381
|
+
const fld = fields[key]!
|
|
382
|
+
lcols[key] = buildColumn(translationSpec(fld.spec!), snake(key))
|
|
383
|
+
}
|
|
384
|
+
lcols.createdAt = timestamp('created_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow()
|
|
385
|
+
lcols.updatedAt = timestamp('updated_at', { withTimezone: true, mode: 'date', precision: 3 }).notNull().defaultNow()
|
|
386
|
+
const uniqueLocalized = localized.filter((k) => fields[k]!.isUnique)
|
|
387
|
+
const localesName = `${snake(name)}_locales`
|
|
388
|
+
localesTable = pgTable(localesName, lcols, (t: any) => [
|
|
389
|
+
uniqueIndex(`${localesName}_parent_locale_unique`).on(t.parentId, t.locale),
|
|
390
|
+
...uniqueLocalized.map((k) => uniqueIndex(`${localesName}_${snake(k)}_unique`).on(t.locale, t[k])),
|
|
391
|
+
])
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// 3) validation — create and update built separately (update carries no defaults)
|
|
395
|
+
const createShape: Record<string, z.ZodTypeAny> = {}
|
|
396
|
+
const updateShape: Record<string, z.ZodTypeAny> = {}
|
|
397
|
+
for (const [key, fld] of Object.entries(fields)) {
|
|
398
|
+
if (fld.relation?.kind === 'one-to-many') continue
|
|
399
|
+
createShape[key] = fld.zod('create')
|
|
400
|
+
updateShape[key] = fld.zod('update')
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return {
|
|
404
|
+
__cms: 'entity',
|
|
405
|
+
name, label: def.label, titleField: def.titleField, localesTable, localized,
|
|
406
|
+
labelSingular: def.labelSingular, description: def.description, icon: def.icon,
|
|
407
|
+
table, admin: def.admin, access: def.access, content: def.content, fields: def.fields,
|
|
408
|
+
singleton: def.singleton, publish: def.publish, live: def.live, tree,
|
|
409
|
+
enums,
|
|
410
|
+
insertSchema: z.object(createShape),
|
|
411
|
+
updateSchema: z.object(updateShape),
|
|
412
|
+
} as unknown as Entity<F>
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Define a single-row global (Settings, Homepage). One editable record, no list. */
|
|
416
|
+
export function defineSingleton<F extends Record<string, AnyFieldDescriptor>>(
|
|
417
|
+
name: string,
|
|
418
|
+
def: Omit<EntityDef<F>, 'singleton' | 'admin'> & { admin?: Pick<NonNullable<EntityDef<F>['admin']>, 'editor' | 'preview' | 'tabs' | 'sections'> },
|
|
419
|
+
): Entity<F> {
|
|
420
|
+
return defineEntity(name, { ...def, singleton: true } as EntityDef<F>)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ── registry: junctions + serializable graph, memoized per config module ────────
|
|
424
|
+
export interface Junction {
|
|
425
|
+
/** Schema key and TS export name (camelCase entity names); not the SQL table name. */
|
|
426
|
+
name: string
|
|
427
|
+
/** The SQL table name: snake_case, or the relation's `through` exactly as given. */
|
|
428
|
+
tableName: string
|
|
429
|
+
table: any
|
|
430
|
+
ownerEntity: string
|
|
431
|
+
targetEntity: string
|
|
432
|
+
ownerCol: string
|
|
433
|
+
targetCol: string
|
|
434
|
+
fieldKey: string
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export interface CmsRegistry {
|
|
438
|
+
entities: CmsEntity[]
|
|
439
|
+
byName: Map<string, CmsEntity>
|
|
440
|
+
junctions: Junction[]
|
|
441
|
+
junctionByOwnerField: Map<string, Junction>
|
|
442
|
+
schema: Record<string, any>
|
|
443
|
+
graph: any[]
|
|
444
|
+
/** Content languages (defineLocales), or null when content is single-language. */
|
|
445
|
+
locales: LocalesDef | null
|
|
446
|
+
cmsMedia: typeof cmsMedia
|
|
447
|
+
cmsUsers: typeof cmsUsers
|
|
448
|
+
cmsTokens: typeof cmsTokens
|
|
449
|
+
cmsAudit: typeof cmsAudit
|
|
450
|
+
cmsCollab: typeof cmsCollab
|
|
451
|
+
cmsRevisions: typeof cmsRevisions
|
|
452
|
+
cmsState: typeof cmsState
|
|
453
|
+
cmsSearch: typeof cmsSearch
|
|
454
|
+
/** Registered plugins (`export const plugins = [...]`), in order. */
|
|
455
|
+
plugins: CmsPlugin[]
|
|
456
|
+
/** Entity name → the plugin that owns it (only entities a plugin brought). */
|
|
457
|
+
pluginOf: Map<string, string>
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const _regCache = new WeakMap<object, CmsRegistry>()
|
|
461
|
+
const _junctionCache = new Map<string, any>()
|
|
462
|
+
|
|
463
|
+
function isEntity(v: any): v is CmsEntity {
|
|
464
|
+
return v && typeof v === 'object' && v.__cms === 'entity'
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export function buildRegistry(config: Record<string, any>): CmsRegistry {
|
|
468
|
+
const cached = _regCache.get(config)
|
|
469
|
+
if (cached) return cached
|
|
470
|
+
|
|
471
|
+
// Sorted by name: a module namespace's key order depends on who loaded it (Node
|
|
472
|
+
// sorts exports alphabetically, Vite/Rollup may keep declaration order), and
|
|
473
|
+
// every generated artifact (schema.ts, typed client, OpenAPI) must not.
|
|
474
|
+
const plugins = collectPlugins(config)
|
|
475
|
+
const siteEntities = Object.values(config).filter(isEntity)
|
|
476
|
+
const pluginOf = new Map<string, string>()
|
|
477
|
+
for (const p of plugins) for (const e of pluginEntities([p])) pluginOf.set(e.name, p.name)
|
|
478
|
+
const taken = new Set(siteEntities.map((e) => e.name))
|
|
479
|
+
for (const e of pluginEntities(plugins)) {
|
|
480
|
+
if (taken.has(e.name)) throw new Error(`The entity "${e.name}" of the plugin "${pluginOf.get(e.name)}" is also defined by the site (or another plugin).`)
|
|
481
|
+
taken.add(e.name)
|
|
482
|
+
}
|
|
483
|
+
const entities = [...siteEntities, ...pluginEntities(plugins)].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
|
|
484
|
+
const byName = new Map(entities.map((e) => [e.name, e]))
|
|
485
|
+
|
|
486
|
+
const junctions: Junction[] = []
|
|
487
|
+
const junctionByOwnerField = new Map<string, Junction>()
|
|
488
|
+
for (const e of entities) {
|
|
489
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
490
|
+
if (fld.relation?.kind !== 'many-to-many') continue
|
|
491
|
+
const target = fld.relation.to() as CmsEntity
|
|
492
|
+
const pair = [e.name, target.name].sort()
|
|
493
|
+
const jname = fld.relation.through ?? `${pair[0]}_to_${pair[1]}`
|
|
494
|
+
const jtableName = fld.relation.through ?? `${snake(pair[0]!)}_to_${snake(pair[1]!)}`
|
|
495
|
+
const ownerCol = `${e.name}Id`
|
|
496
|
+
const targetCol = `${target.name}Id`
|
|
497
|
+
let jtable = _junctionCache.get(jtableName)
|
|
498
|
+
if (!jtable) {
|
|
499
|
+
jtable = pgTable(jtableName, {
|
|
500
|
+
[ownerCol]: uuid(`${snake(e.name)}_id`).notNull().references(() => e.table.id, { onDelete: 'cascade' }),
|
|
501
|
+
[targetCol]: uuid(`${snake(target.name)}_id`).notNull().references(() => target.table.id, { onDelete: 'cascade' }),
|
|
502
|
+
}, (t: any) => [primaryKey({ columns: [t[ownerCol], t[targetCol]] })])
|
|
503
|
+
_junctionCache.set(jtableName, jtable)
|
|
504
|
+
}
|
|
505
|
+
const j: Junction = { name: jname, tableName: jtableName, table: jtable, ownerEntity: e.name, targetEntity: target.name, ownerCol, targetCol, fieldKey: key }
|
|
506
|
+
junctions.push(j)
|
|
507
|
+
junctionByOwnerField.set(`${e.name}.${key}`, j)
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const locales = (Object.values(config).find((v: any) => v && v.__cms === 'locales') as LocalesDef | undefined) ?? null
|
|
512
|
+
const withLocalized = entities.filter((e) => e.localized?.length)
|
|
513
|
+
if (withLocalized.length && !locales) {
|
|
514
|
+
throw new Error(`${withLocalized.map((e) => e.name).join(', ')} ha${withLocalized.length > 1 ? 've' : 's'} localized fields, but no languages are defined. Export \`locales = defineLocales({ default: 'cs', locales: ['cs', 'en'] })\` from cms.config.ts.`)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
for (const e of entities) {
|
|
518
|
+
if (e.tree && (e.fields[e.tree.parent]!.relation!.to() as CmsEntity).name !== e.name) {
|
|
519
|
+
throw new Error(`tree.parent of "${e.name}" must point at "${e.name}" itself.`)
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
for (const e of entities) {
|
|
524
|
+
for (const [what, t] of [['content.path', e.content?.path], ['publish.preview', e.publish?.preview], ['admin.preview', e.admin?.preview]] as const) {
|
|
525
|
+
const problem = pathTemplateProblem(t, locales)
|
|
526
|
+
if (problem) throw new Error(`${what} of "${e.name}" ${problem}.`)
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const schema: Record<string, any> = { cmsMedia, cmsUsers, cmsTokens, cmsAudit, cmsCollab, cmsRevisions, cmsState, cmsSearch }
|
|
531
|
+
for (const e of entities) schema[e.name] = e.table
|
|
532
|
+
for (const e of entities) if (e.localesTable) schema[`${e.name}_locales`] = e.localesTable
|
|
533
|
+
for (const j of junctions) schema[j.name] = j.table
|
|
534
|
+
|
|
535
|
+
const graph = entities.map((e) => withPlugins(entityGraph(e, locales), e, plugins, pluginOf))
|
|
536
|
+
const reg: CmsRegistry = { entities, byName, junctions, junctionByOwnerField, schema, graph, locales, cmsMedia, cmsUsers, cmsTokens, cmsAudit, cmsCollab, cmsRevisions, cmsState, cmsSearch, plugins, pluginOf }
|
|
537
|
+
_regCache.set(config, reg)
|
|
538
|
+
return reg
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The editor's arrangement, from what the content is (the developer can say otherwise
|
|
543
|
+
* with `admin.editor`): the layout is the point → `preview`; a text to write → `write`;
|
|
544
|
+
* facts to fill in → `form`.
|
|
545
|
+
*/
|
|
546
|
+
export function editorLayout(e: CmsEntity): CmsEditorLayout {
|
|
547
|
+
if (e.admin?.editor) return e.admin.editor
|
|
548
|
+
const page = !!(e.publish?.preview ?? e.admin?.preview)
|
|
549
|
+
const types = Object.values(e.fields).filter((f) => !f.meta?.group).map((f) => f.type)
|
|
550
|
+
if (page && (e.singleton || types.includes('blocks'))) return 'preview'
|
|
551
|
+
if (page && types.includes('richtext')) return 'write'
|
|
552
|
+
return 'form'
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** The list's look, from what the content is (or `admin.list.layout`). */
|
|
556
|
+
export function listLayout(e: CmsEntity): CmsListLayout {
|
|
557
|
+
if (e.admin?.list?.layout) return e.admin.list.layout
|
|
558
|
+
if (e.tree) return 'tree'
|
|
559
|
+
const columns = e.admin?.list?.columns ?? Object.keys(e.fields).slice(0, 4)
|
|
560
|
+
// The picture is the content (albums, logos): it leads the list and there is no text body.
|
|
561
|
+
const hasBody = Object.values(e.fields).some((f) => f.type === 'richtext' && !f.meta?.group)
|
|
562
|
+
if (e.fields[columns[0] ?? '']?.type === 'media' && !hasBody) return 'grid'
|
|
563
|
+
if (!e.publish && columns.some((k) => e.fields[k]?.type === 'datetime')) return 'dated'
|
|
564
|
+
return 'table'
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** The graph entry with what plugins add to it (their `graph` functions) and the owner, if a plugin brought it. */
|
|
568
|
+
function withPlugins<G extends { adminCreate: boolean; notices: any[]; plugin: string | null }>(g: G, e: CmsEntity, plugins: CmsPlugin[], pluginOf: Map<string, string>): G {
|
|
569
|
+
const out = { ...g, plugin: pluginOf.get(e.name) ?? null }
|
|
570
|
+
for (const p of plugins) {
|
|
571
|
+
const ext = p.graph?.(e)
|
|
572
|
+
if (!ext) continue
|
|
573
|
+
if (ext.create === false) out.adminCreate = false
|
|
574
|
+
if (ext.notices?.length) out.notices = [...out.notices, ...ext.notices]
|
|
575
|
+
}
|
|
576
|
+
return out
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function entityGraph(e: CmsEntity, locales: LocalesDef | null) {
|
|
580
|
+
return {
|
|
581
|
+
name: e.name,
|
|
582
|
+
// Content languages, for entities with localized fields (drives the editor's switcher).
|
|
583
|
+
locales: e.localized?.length && locales ? { default: locales.default, all: locales.locales, labels: locales.labels } : null,
|
|
584
|
+
label: e.label,
|
|
585
|
+
labelSingular: e.labelSingular ?? e.label,
|
|
586
|
+
description: e.description ?? null,
|
|
587
|
+
icon: e.icon ?? (e.singleton ? 'i-lucide-settings-2' : 'i-lucide-folder'),
|
|
588
|
+
titleField: e.titleField,
|
|
589
|
+
// A ready-made kind (pages, redirects, form…) the admin may treat specially.
|
|
590
|
+
preset: (e as any).preset ?? null,
|
|
591
|
+
// Plugins can turn the "New …" button off and add notes under the list (see `graph` of a plugin).
|
|
592
|
+
adminCreate: true,
|
|
593
|
+
notices: [] as { icon?: string; key: string; params?: Record<string, string | number> }[],
|
|
594
|
+
plugin: null as string | null,
|
|
595
|
+
singleton: !!e.singleton,
|
|
596
|
+
publish: e.publish,
|
|
597
|
+
// The preview address per language, prefix included ({ cs: '/aktuality/:slug', en: '/en/news/:slug' }).
|
|
598
|
+
previewPaths: prefixedTemplates(e.publish?.preview ?? e.admin?.preview, locales),
|
|
599
|
+
editor: editorLayout(e),
|
|
600
|
+
listLayout: listLayout(e),
|
|
601
|
+
listColumns: e.admin?.list?.columns ?? Object.keys(e.fields).slice(0, 4),
|
|
602
|
+
searchable: e.admin?.list?.searchable ?? [],
|
|
603
|
+
orderField: e.admin?.list?.orderField ?? null,
|
|
604
|
+
// Entries under each other (a page tree): which fields hold the parent and the position.
|
|
605
|
+
tree: e.tree ?? null,
|
|
606
|
+
tabs: e.admin?.tabs ?? null,
|
|
607
|
+
sections: e.admin?.sections ?? null,
|
|
608
|
+
fields: Object.entries(e.fields).map(([key, fld]) => ({
|
|
609
|
+
key,
|
|
610
|
+
type: fld.type,
|
|
611
|
+
optional: fld.isOptional,
|
|
612
|
+
localized: !!fld.isLocalized,
|
|
613
|
+
required: fld.isRequired,
|
|
614
|
+
unique: fld.isUnique,
|
|
615
|
+
// What a new entry starts with in the admin (select defaults, switches).
|
|
616
|
+
default: fld.default ?? null,
|
|
617
|
+
ui: fld.ui,
|
|
618
|
+
meta: fld.meta,
|
|
619
|
+
relation: fld.relation
|
|
620
|
+
? { to: (fld.relation.to() as CmsEntity).name, kind: fld.relation.kind, required: !!fld.relation.required }
|
|
621
|
+
: undefined,
|
|
622
|
+
})),
|
|
623
|
+
}
|
|
624
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the `cms:error` Nitro hook receives: a server error the host may want to send
|
|
3
|
+
* to its error tracker (Sentry, GlitchTip, a Slack message…). Nothing in the package
|
|
4
|
+
* talks to a vendor; the host subscribes in a server plugin:
|
|
5
|
+
*
|
|
6
|
+
* export default defineNitroPlugin((nitro) => {
|
|
7
|
+
* nitro.hooks.hook('cms:error', (report) => Sentry.captureException(report.error, { tags: { source: report.source } }))
|
|
8
|
+
* })
|
|
9
|
+
*/
|
|
10
|
+
export interface CmsErrorReport {
|
|
11
|
+
/** Where it happened: a request (5xx), a scheduled task, or a webhook delivery that gave up. */
|
|
12
|
+
source: 'request' | 'task' | 'webhook'
|
|
13
|
+
/**
|
|
14
|
+
* The error to send to a tracker: an `Error` whose message is `message` (without query
|
|
15
|
+
* values), with the original stack frames and its causes scrubbed the same way. Safe to
|
|
16
|
+
* pass to `Sentry.captureException`.
|
|
17
|
+
*/
|
|
18
|
+
error: Error
|
|
19
|
+
/**
|
|
20
|
+
* The error exactly as thrown. **May contain personal data**: a failed database
|
|
21
|
+
* query's message carries the bound values (emails, form text, password hashes), and
|
|
22
|
+
* so can the driver's error objects. Do not send it to a third party; it is here for
|
|
23
|
+
* code that inspects the error (its `statusCode`, its class) inside the process.
|
|
24
|
+
*/
|
|
25
|
+
rawError: unknown
|
|
26
|
+
/** The error's message without query values — safe to log and to send away. */
|
|
27
|
+
message: string
|
|
28
|
+
/** HTTP status of the failed request (5xx); null for tasks and webhooks. */
|
|
29
|
+
status: number | null
|
|
30
|
+
/** The `X-Request-Id` the browser got, for requests. */
|
|
31
|
+
requestId: string | null
|
|
32
|
+
method: string | null
|
|
33
|
+
/** The request path, without its query string. */
|
|
34
|
+
path: string | null
|
|
35
|
+
/** The signed-in person's id (never their email), for requests. */
|
|
36
|
+
userId: string | null
|
|
37
|
+
/** The task name (`cms:scheduled`), when `source` is 'task'. */
|
|
38
|
+
task?: string
|
|
39
|
+
/** The webhook that failed, when `source` is 'webhook': its address (origin and a short hash of the path, never the path itself: services like Slack keep their secret there) and the change. */
|
|
40
|
+
webhook?: { url: string; type: string; delivery: string }
|
|
41
|
+
}
|