@async/db 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/CHANGELOG.md +167 -0
- package/README.md +431 -0
- package/SPEC.md +1429 -0
- package/db.config.example.mjs +128 -0
- package/dist/cli/args.d.ts +8 -0
- package/dist/cli/args.js +16 -0
- package/dist/cli/commands/create.d.ts +3 -0
- package/dist/cli/commands/create.js +13 -0
- package/dist/cli/commands/doctor.d.ts +3 -0
- package/dist/cli/commands/doctor.js +31 -0
- package/dist/cli/commands/generate.d.ts +6 -0
- package/dist/cli/commands/generate.js +24 -0
- package/dist/cli/commands/operations.d.ts +12 -0
- package/dist/cli/commands/operations.js +61 -0
- package/dist/cli/commands/schema.d.ts +11 -0
- package/dist/cli/commands/schema.js +1086 -0
- package/dist/cli/commands/serve.d.ts +9 -0
- package/dist/cli/commands/serve.js +18 -0
- package/dist/cli/commands/sync.d.ts +3 -0
- package/dist/cli/commands/sync.js +11 -0
- package/dist/cli/commands/types.d.ts +7 -0
- package/dist/cli/commands/types.js +37 -0
- package/dist/cli/commands/viewer.d.ts +6 -0
- package/dist/cli/commands/viewer.js +29 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +108 -0
- package/dist/cli/output.d.ts +25 -0
- package/dist/cli/output.js +149 -0
- package/dist/cli/schema-prompt.d.ts +20 -0
- package/dist/cli/schema-prompt.js +66 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +3 -0
- package/dist/client-cache.d.ts +105 -0
- package/dist/client-cache.js +916 -0
- package/dist/client.d.ts +64 -0
- package/dist/client.js +405 -0
- package/dist/config-public.d.ts +1 -0
- package/dist/config-public.js +1 -0
- package/dist/config.d.ts +54 -0
- package/dist/config.js +2 -0
- package/dist/csv.d.ts +1 -0
- package/dist/csv.js +1 -0
- package/dist/db.d.ts +3 -0
- package/dist/db.js +3 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/features/config/defaults.d.ts +98 -0
- package/dist/features/config/defaults.js +95 -0
- package/dist/features/config/load.d.ts +11 -0
- package/dist/features/config/load.js +265 -0
- package/dist/features/config/public.d.ts +17 -0
- package/dist/features/config/public.js +75 -0
- package/dist/features/doctor/duplicate-ids.d.ts +18 -0
- package/dist/features/doctor/duplicate-ids.js +79 -0
- package/dist/features/doctor/field-consistency.d.ts +17 -0
- package/dist/features/doctor/field-consistency.js +48 -0
- package/dist/features/doctor/index.d.ts +39 -0
- package/dist/features/doctor/index.js +177 -0
- package/dist/features/doctor/relations.d.ts +22 -0
- package/dist/features/doctor/relations.js +90 -0
- package/dist/features/doctor/schema-guidance.d.ts +35 -0
- package/dist/features/doctor/schema-guidance.js +184 -0
- package/dist/features/generate/registry.d.ts +14 -0
- package/dist/features/generate/registry.js +37 -0
- package/dist/features/http/registry.d.ts +46 -0
- package/dist/features/http/registry.js +86 -0
- package/dist/features/operations/index.d.ts +49 -0
- package/dist/features/operations/index.js +199 -0
- package/dist/features/operations/maps.d.ts +1 -0
- package/dist/features/operations/maps.js +10 -0
- package/dist/features/operations/readiness.d.ts +30 -0
- package/dist/features/operations/readiness.js +228 -0
- package/dist/features/operations/runtime.d.ts +57 -0
- package/dist/features/operations/runtime.js +288 -0
- package/dist/features/runtime/collection.d.ts +51 -0
- package/dist/features/runtime/collection.js +198 -0
- package/dist/features/runtime/db.d.ts +152 -0
- package/dist/features/runtime/db.js +824 -0
- package/dist/features/runtime/document.d.ts +43 -0
- package/dist/features/runtime/document.js +111 -0
- package/dist/features/runtime/fanout.d.ts +24 -0
- package/dist/features/runtime/fanout.js +77 -0
- package/dist/features/runtime/json-pointer.d.ts +5 -0
- package/dist/features/runtime/json-pointer.js +49 -0
- package/dist/features/runtime/scope-state.d.ts +44 -0
- package/dist/features/runtime/scope-state.js +185 -0
- package/dist/features/runtime/state.d.ts +1 -0
- package/dist/features/runtime/state.js +1 -0
- package/dist/features/schema/api.d.ts +107 -0
- package/dist/features/schema/api.js +460 -0
- package/dist/features/schema/builders.d.ts +86 -0
- package/dist/features/schema/builders.js +110 -0
- package/dist/features/schema/fields.d.ts +38 -0
- package/dist/features/schema/fields.js +296 -0
- package/dist/features/schema/generated.d.ts +29 -0
- package/dist/features/schema/generated.js +32 -0
- package/dist/features/schema/locator.d.ts +16 -0
- package/dist/features/schema/locator.js +135 -0
- package/dist/features/schema/manifest.d.ts +91 -0
- package/dist/features/schema/manifest.js +384 -0
- package/dist/features/schema/metadata.d.ts +30 -0
- package/dist/features/schema/metadata.js +75 -0
- package/dist/features/schema/project.d.ts +46 -0
- package/dist/features/schema/project.js +442 -0
- package/dist/features/schema/relations.d.ts +38 -0
- package/dist/features/schema/relations.js +109 -0
- package/dist/features/schema/resolvers.d.ts +36 -0
- package/dist/features/schema/resolvers.js +111 -0
- package/dist/features/schema/resource.d.ts +75 -0
- package/dist/features/schema/resource.js +253 -0
- package/dist/features/schema/source-definitions.d.ts +21 -0
- package/dist/features/schema/source-definitions.js +29 -0
- package/dist/features/schema/sources.d.ts +83 -0
- package/dist/features/schema/sources.js +689 -0
- package/dist/features/schema/standard-schema.d.ts +57 -0
- package/dist/features/schema/standard-schema.js +232 -0
- package/dist/features/schema/validation.d.ts +69 -0
- package/dist/features/schema/validation.js +434 -0
- package/dist/features/storage/events.d.ts +12 -0
- package/dist/features/storage/events.js +30 -0
- package/dist/features/storage/json.d.ts +112 -0
- package/dist/features/storage/json.js +239 -0
- package/dist/features/storage/memory.d.ts +30 -0
- package/dist/features/storage/memory.js +44 -0
- package/dist/features/storage/resource-json.d.ts +31 -0
- package/dist/features/storage/resource-json.js +76 -0
- package/dist/features/storage/runtime.d.ts +37 -0
- package/dist/features/storage/runtime.js +184 -0
- package/dist/features/storage/source-metadata.d.ts +20 -0
- package/dist/features/storage/source-metadata.js +25 -0
- package/dist/features/storage/source.d.ts +37 -0
- package/dist/features/storage/source.js +60 -0
- package/dist/features/storage/static.d.ts +29 -0
- package/dist/features/storage/static.js +42 -0
- package/dist/features/sync/defaults.d.ts +21 -0
- package/dist/features/sync/defaults.js +21 -0
- package/dist/features/sync/index.d.ts +35 -0
- package/dist/features/sync/index.js +85 -0
- package/dist/features/sync/mirror-state.d.ts +14 -0
- package/dist/features/sync/mirror-state.js +4 -0
- package/dist/features/sync/runtime-dirs.d.ts +5 -0
- package/dist/features/sync/runtime-dirs.js +9 -0
- package/dist/features/sync/source-writes.d.ts +15 -0
- package/dist/features/sync/source-writes.js +27 -0
- package/dist/features/sync/synthetic-seed.d.ts +26 -0
- package/dist/features/sync/synthetic-seed.js +83 -0
- package/dist/features/viewer/manifest.d.ts +148 -0
- package/dist/features/viewer/manifest.js +165 -0
- package/dist/fs-utils.d.ts +1 -0
- package/dist/fs-utils.js +1 -0
- package/dist/generate/hono/app.d.ts +6 -0
- package/dist/generate/hono/app.js +51 -0
- package/dist/generate/hono/graphql.d.ts +7 -0
- package/dist/generate/hono/graphql.js +53 -0
- package/dist/generate/hono/index.d.ts +55 -0
- package/dist/generate/hono/index.js +140 -0
- package/dist/generate/hono/package.d.ts +6 -0
- package/dist/generate/hono/package.js +44 -0
- package/dist/generate/hono/readme.d.ts +13 -0
- package/dist/generate/hono/readme.js +28 -0
- package/dist/generate/hono/repository.d.ts +1 -0
- package/dist/generate/hono/repository.js +27 -0
- package/dist/generate/hono/rest.d.ts +1 -0
- package/dist/generate/hono/rest.js +38 -0
- package/dist/generate/hono/schema.d.ts +13 -0
- package/dist/generate/hono/schema.js +18 -0
- package/dist/generate/hono/sqlite.d.ts +20 -0
- package/dist/generate/hono/sqlite.js +266 -0
- package/dist/generate/hono/validators.d.ts +1 -0
- package/dist/generate/hono/validators.js +141 -0
- package/dist/generate/hono.d.ts +1 -0
- package/dist/generate/hono.js +1 -0
- package/dist/graphql/execute.d.ts +14 -0
- package/dist/graphql/execute.js +719 -0
- package/dist/graphql/http.d.ts +15 -0
- package/dist/graphql/http.js +29 -0
- package/dist/graphql/index.d.ts +3 -0
- package/dist/graphql/index.js +3 -0
- package/dist/graphql/parser.d.ts +54 -0
- package/dist/graphql/parser.js +433 -0
- package/dist/hono.d.ts +77 -0
- package/dist/hono.js +1 -0
- package/dist/index.d.ts +1065 -0
- package/dist/index.js +14 -0
- package/dist/integrations/hono.d.ts +136 -0
- package/dist/integrations/hono.js +508 -0
- package/dist/integrations/kv.d.ts +69 -0
- package/dist/integrations/kv.js +69 -0
- package/dist/integrations/postgres.d.ts +52 -0
- package/dist/integrations/postgres.js +113 -0
- package/dist/integrations/sqlite.d.ts +112 -0
- package/dist/integrations/sqlite.js +489 -0
- package/dist/integrations/vite.d.ts +45 -0
- package/dist/integrations/vite.js +111 -0
- package/dist/json.d.ts +48 -0
- package/dist/json.js +1 -0
- package/dist/jsonc.d.ts +1 -0
- package/dist/jsonc.js +1 -0
- package/dist/kv.d.ts +24 -0
- package/dist/kv.js +1 -0
- package/dist/mock.d.ts +1 -0
- package/dist/mock.js +1 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +1 -0
- package/dist/operations.d.ts +3 -0
- package/dist/operations.js +3 -0
- package/dist/postgres.d.ts +24 -0
- package/dist/postgres.js +1 -0
- package/dist/redis.d.ts +14 -0
- package/dist/redis.js +1 -0
- package/dist/rest/formats.d.ts +80 -0
- package/dist/rest/formats.js +318 -0
- package/dist/rest/handler.d.ts +111 -0
- package/dist/rest/handler.js +833 -0
- package/dist/rest/shape.d.ts +33 -0
- package/dist/rest/shape.js +218 -0
- package/dist/schema-builders.d.ts +1 -0
- package/dist/schema-builders.js +1 -0
- package/dist/schema-manifest.d.ts +1 -0
- package/dist/schema-manifest.js +1 -0
- package/dist/schema.d.ts +193 -0
- package/dist/schema.js +6 -0
- package/dist/server.d.ts +116 -0
- package/dist/server.js +601 -0
- package/dist/shared/csv.d.ts +8 -0
- package/dist/shared/csv.js +149 -0
- package/dist/shared/errors.d.ts +40 -0
- package/dist/shared/errors.js +55 -0
- package/dist/shared/fs-utils.d.ts +4 -0
- package/dist/shared/fs-utils.js +30 -0
- package/dist/shared/jsonc.d.ts +2 -0
- package/dist/shared/jsonc.js +99 -0
- package/dist/shared/mock.d.ts +40 -0
- package/dist/shared/mock.js +83 -0
- package/dist/shared/names.d.ts +28 -0
- package/dist/shared/names.js +127 -0
- package/dist/shared/operations.d.ts +32 -0
- package/dist/shared/operations.js +302 -0
- package/dist/sqlite.d.ts +24 -0
- package/dist/sqlite.js +1 -0
- package/dist/state.d.ts +1 -0
- package/dist/state.js +1 -0
- package/dist/sync.d.ts +1 -0
- package/dist/sync.js +1 -0
- package/dist/tracing.d.ts +95 -0
- package/dist/tracing.js +260 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.js +285 -0
- package/dist/viewer-manifest.d.ts +1 -0
- package/dist/viewer-manifest.js +1 -0
- package/dist/vite.d.ts +59 -0
- package/dist/vite.js +1 -0
- package/dist/web/json-viewer.d.ts +5 -0
- package/dist/web/json-viewer.js +176 -0
- package/dist/web/viewer.d.ts +12 -0
- package/dist/web/viewer.js +1015 -0
- package/docs/README.md +42 -0
- package/docs/architecture.md +112 -0
- package/docs/ci-and-release.md +177 -0
- package/docs/cms-storage-patterns.md +108 -0
- package/docs/concepts.md +141 -0
- package/docs/configuration.md +552 -0
- package/docs/fixtures-and-schemas.md +527 -0
- package/docs/fork-branch-workflows.md +108 -0
- package/docs/generated-files.md +174 -0
- package/docs/getting-started.md +165 -0
- package/docs/integrations.md +206 -0
- package/docs/json-production.md +120 -0
- package/docs/package-api.md +418 -0
- package/docs/prototype-to-production.md +378 -0
- package/docs/server-and-viewer.md +466 -0
- package/docs/store-graduation.md +120 -0
- package/docs/typescript-schema-sources.md +79 -0
- package/examples/advanced/README.md +55 -0
- package/examples/advanced/db/projects.schema.jsonc +44 -0
- package/examples/advanced/db/settings.jsonc +9 -0
- package/examples/advanced/db/users.json +23 -0
- package/examples/advanced/db/users.schema.mjs +31 -0
- package/examples/advanced/db.config.mjs +18 -0
- package/examples/advanced/example.json +5 -0
- package/examples/advanced/src/generated/db.types.d.ts +64 -0
- package/examples/basic/README.md +95 -0
- package/examples/basic/db/operations/get-user.jsonc +8 -0
- package/examples/basic/db/settings.json +7 -0
- package/examples/basic/db/users.schema.jsonc +36 -0
- package/examples/basic/db.config.mjs +68 -0
- package/examples/basic/example.json +5 -0
- package/examples/basic/src/generated/db.types.d.ts +39 -0
- package/examples/cms-json-publish/README.md +21 -0
- package/examples/cms-json-publish/db/navigation.json +7 -0
- package/examples/cms-json-publish/db/pages.json +18 -0
- package/examples/cms-json-publish/example.json +5 -0
- package/examples/cms-json-publish/src/cms.mjs +104 -0
- package/examples/computed-fields/README.md +93 -0
- package/examples/computed-fields/db/orders.schema.mjs +62 -0
- package/examples/computed-fields/db/posts.schema.mjs +59 -0
- package/examples/computed-fields/db/products.schema.mjs +39 -0
- package/examples/computed-fields/db/users.schema.mjs +43 -0
- package/examples/computed-fields/db.config.mjs +15 -0
- package/examples/computed-fields/example.json +5 -0
- package/examples/computed-fields/src/generated/db.types.d.ts +81 -0
- package/examples/content-collections/README.md +91 -0
- package/examples/content-collections/db/authors.json +12 -0
- package/examples/content-collections/db/authors.schema.mjs +20 -0
- package/examples/content-collections/db/blog/draft-roadmap.mdx +12 -0
- package/examples/content-collections/db/blog/index.schema.mjs +61 -0
- package/examples/content-collections/db/blog/launch-notes.mdx +15 -0
- package/examples/content-collections/db/docs/index.schema.mjs +32 -0
- package/examples/content-collections/db/docs/intro.mdx +11 -0
- package/examples/content-collections/db/docs/schema-workflow.mdx +10 -0
- package/examples/content-collections/db/site.schema.jsonc +21 -0
- package/examples/content-collections/db.config.mjs +26 -0
- package/examples/content-collections/example.json +5 -0
- package/examples/content-collections/src/content-preview.mjs +66 -0
- package/examples/content-collections/src/generated/db.types.d.ts +81 -0
- package/examples/csv/README.md +52 -0
- package/examples/csv/db/customers.csv +4 -0
- package/examples/csv/db.config.mjs +13 -0
- package/examples/csv/example.json +5 -0
- package/examples/data-first/README.md +54 -0
- package/examples/data-first/db/posts.json +16 -0
- package/examples/data-first/db/settings.json +8 -0
- package/examples/data-first/db/users.json +14 -0
- package/examples/data-first/db.config.mjs +13 -0
- package/examples/data-first/example.json +5 -0
- package/examples/diagnostics/README.md +55 -0
- package/examples/diagnostics/db/projects.schema.jsonc +27 -0
- package/examples/diagnostics/db/users.json +9 -0
- package/examples/diagnostics/db/users.schema.jsonc +23 -0
- package/examples/diagnostics/db.config.mjs +16 -0
- package/examples/diagnostics/example.json +5 -0
- package/examples/free-plan-upgrade/README.md +22 -0
- package/examples/free-plan-upgrade/db/appSettings.json +4 -0
- package/examples/free-plan-upgrade/db/projects.json +7 -0
- package/examples/free-plan-upgrade/example.json +5 -0
- package/examples/free-plan-upgrade/src/upgrade-tenant-to-paid.mjs +105 -0
- package/examples/hono-auth/README.md +74 -0
- package/examples/hono-auth/db/pages.schema.jsonc +44 -0
- package/examples/hono-auth/db/users.schema.jsonc +42 -0
- package/examples/hono-auth/db.config.mjs +17 -0
- package/examples/hono-auth/example.json +5 -0
- package/examples/hono-auth/package.json +14 -0
- package/examples/hono-auth/src/app.mjs +79 -0
- package/examples/hono-auth/src/server.mjs +13 -0
- package/examples/production-json/README.md +102 -0
- package/examples/production-json/db/appSettings.schema.jsonc +41 -0
- package/examples/production-json/db/featureFlags.schema.jsonc +84 -0
- package/examples/production-json/db/operations/get-control-plane.jsonc +6 -0
- package/examples/production-json/db/operations/get-feature-flag.jsonc +9 -0
- package/examples/production-json/db/operations/list-feature-flags.jsonc +8 -0
- package/examples/production-json/db/operations/read-public-settings.jsonc +8 -0
- package/examples/production-json/db.config.mjs +33 -0
- package/examples/production-json/example.json +5 -0
- package/examples/production-json/src/client-demo.mjs +28 -0
- package/examples/production-json/src/generated/db.types.d.ts +60 -0
- package/examples/relations/README.md +56 -0
- package/examples/relations/db/posts.schema.jsonc +46 -0
- package/examples/relations/db/users.schema.jsonc +34 -0
- package/examples/relations/db.config.mjs +13 -0
- package/examples/relations/example.json +5 -0
- package/examples/rest-client/README.md +54 -0
- package/examples/rest-client/db/settings.json +5 -0
- package/examples/rest-client/db/users.schema.jsonc +42 -0
- package/examples/rest-client/db.config.mjs +13 -0
- package/examples/rest-client/example.json +5 -0
- package/examples/rest-client/src/client-demo.mjs +24 -0
- package/examples/schema-first/README.md +55 -0
- package/examples/schema-first/db/auditEvents.schema.jsonc +24 -0
- package/examples/schema-first/db/settings.schema.jsonc +29 -0
- package/examples/schema-first/db/users.schema.jsonc +36 -0
- package/examples/schema-first/db.config.mjs +15 -0
- package/examples/schema-first/example.json +5 -0
- package/examples/schema-first/src/generated/db.types.d.ts +47 -0
- package/examples/schema-manifest/README.md +50 -0
- package/examples/schema-manifest/db/projects.schema.jsonc +48 -0
- package/examples/schema-manifest/db/users.schema.jsonc +35 -0
- package/examples/schema-manifest/db.config.mjs +41 -0
- package/examples/schema-manifest/example.json +5 -0
- package/examples/schema-manifest/src/generated/db.schema.json +130 -0
- package/examples/schema-manifest/src/generated/db.types.d.ts +50 -0
- package/examples/schema-ui/README.md +103 -0
- package/examples/schema-ui/db/pages.schema.jsonc +53 -0
- package/examples/schema-ui/db/users.schema.jsonc +30 -0
- package/examples/schema-ui/db.config.mjs +55 -0
- package/examples/schema-ui/example.json +5 -0
- package/examples/schema-ui/src/cms-ssr.mjs +276 -0
- package/examples/schema-ui/src/generated/db.schema.json +133 -0
- package/examples/schema-ui/src/generated/db.types.d.ts +46 -0
- package/examples/schema-ui/src/render-admin.mjs +175 -0
- package/examples/schema-ui/src/schema-ui-ssr-handler.mjs +149 -0
- package/examples/schema-ui/src/start-schema-ui-server.mjs +140 -0
- package/examples/standard-schema/README.md +55 -0
- package/examples/standard-schema/db/settings.schema.mjs +22 -0
- package/examples/standard-schema/db/users.schema.mjs +72 -0
- package/examples/standard-schema/example.json +5 -0
- package/package.json +108 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal SSR HTML from schema manifest metadata plus collection records.
|
|
3
|
+
* @param {unknown} manifest
|
|
4
|
+
* @param {Record<string, unknown[]>} recordsByCollection
|
|
5
|
+
*/
|
|
6
|
+
export function renderHomePage(manifest, recordsByCollection, options = {}) {
|
|
7
|
+
const basePath = normalizeBasePath(options.basePath);
|
|
8
|
+
const collections = Object.values(manifest.collections ?? {}).filter((c) => c.kind === 'collection');
|
|
9
|
+
const links = collections.map((resource) => {
|
|
10
|
+
const count = recordsByCollection[resource.name]?.length ?? 0;
|
|
11
|
+
return `<li><a href="${escapeHtml(joinPaths(basePath, `/cms/${resource.name}`))}">${escapeHtml(resource.editor?.title ?? resource.name)}</a> — ${count} record${count === 1 ? '' : 's'}</li>`;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return pageShell({
|
|
15
|
+
title: 'Schema UI · CMS home',
|
|
16
|
+
body: `
|
|
17
|
+
<h1>CMS home</h1>
|
|
18
|
+
<p>Resources from <code>src/generated/db.schema.json</code>, records loaded from the db mirror.</p>
|
|
19
|
+
<ul>${links.join('\n')}</ul>
|
|
20
|
+
<p><a href="${escapeHtml(joinPaths(basePath, '/templates'))}">Static component templates</a> (no live data).</p>`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {unknown} manifest
|
|
26
|
+
* @param {string} collectionName
|
|
27
|
+
* @param {unknown[]} records
|
|
28
|
+
*/
|
|
29
|
+
export function renderCollectionListPage(manifest, collectionName, records, options = {}) {
|
|
30
|
+
const basePath = normalizeBasePath(options.basePath);
|
|
31
|
+
const resource = manifest.collections?.[collectionName];
|
|
32
|
+
if (!resource || resource.kind !== 'collection') {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const title = resource.editor?.title ?? collectionName;
|
|
37
|
+
const rows = records.map((record) => {
|
|
38
|
+
const id = record?.[resource.idField ?? 'id'];
|
|
39
|
+
const label = pickListLabel(record, resource);
|
|
40
|
+
return `<li><a href="${escapeHtml(joinPaths(basePath, `/cms/${collectionName}/${encodeURIComponent(String(id))}`))}">${escapeHtml(label)}</a> <small>(<code>${escapeHtml(String(id))}</code>)</small></li>`;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return pageShell({
|
|
44
|
+
title: `Schema UI · ${title}`,
|
|
45
|
+
body: `
|
|
46
|
+
<p><a href="${escapeHtml(joinPaths(basePath, '/'))}">← Home</a></p>
|
|
47
|
+
<h1>${escapeHtml(title)}</h1>
|
|
48
|
+
${resource.editor?.description ? `<p>${escapeHtml(resource.editor.description)}</p>` : ''}
|
|
49
|
+
<ul>${rows.join('\n')}</ul>`,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {unknown} manifest
|
|
55
|
+
* @param {string} collectionName
|
|
56
|
+
* @param {Record<string, unknown> | null} record
|
|
57
|
+
* @param {Record<string, unknown[]>} recordsByCollection
|
|
58
|
+
*/
|
|
59
|
+
export function renderRecordDetailPage(manifest, collectionName, record, recordsByCollection, options = {}) {
|
|
60
|
+
const basePath = normalizeBasePath(options.basePath);
|
|
61
|
+
const resource = manifest.collections?.[collectionName];
|
|
62
|
+
if (!resource || resource.kind !== 'collection' || !record) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const title = resource.editor?.title ?? collectionName;
|
|
67
|
+
const fields = Object.entries(resource.fields ?? {});
|
|
68
|
+
const viewBlocks = fields.map(([fieldName, field]) => renderFieldBlock('view', fieldName, field, record[fieldName], recordsByCollection, { basePath }));
|
|
69
|
+
const editorBlocks = fields.map(([fieldName, field]) => renderFieldBlock('editor', fieldName, field, record[fieldName], recordsByCollection));
|
|
70
|
+
|
|
71
|
+
const id = record[resource.idField ?? 'id'];
|
|
72
|
+
|
|
73
|
+
return pageShell({
|
|
74
|
+
title: `Schema UI · ${title} · ${id}`,
|
|
75
|
+
body: `
|
|
76
|
+
<p><a href="${escapeHtml(joinPaths(basePath, '/'))}">← Home</a> · <a href="${escapeHtml(joinPaths(basePath, `/cms/${collectionName}`))}">← ${escapeHtml(title)}</a></p>
|
|
77
|
+
<h1>${escapeHtml(String(record.title ?? record.name ?? id))}</h1>
|
|
78
|
+
${resource.editor?.description ? `<p>${escapeHtml(resource.editor.description)}</p>` : ''}
|
|
79
|
+
<section class="cms-live-view">
|
|
80
|
+
<h2>Rendered view</h2>
|
|
81
|
+
${viewBlocks.join('\n')}
|
|
82
|
+
</section>
|
|
83
|
+
<section class="cms-live-editor">
|
|
84
|
+
<h2>Rendered editor</h2>
|
|
85
|
+
<form method="post" action="#" class="cms-editor-demo">
|
|
86
|
+
<p><small>This example only renders markup; it does not persist edits.</small></p>
|
|
87
|
+
${editorBlocks.join('\n')}
|
|
88
|
+
</form>
|
|
89
|
+
</section>`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function pageShell({ title, body }) {
|
|
94
|
+
return `<!doctype html>
|
|
95
|
+
<html lang="en">
|
|
96
|
+
<head>
|
|
97
|
+
<meta charset="utf-8">
|
|
98
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
99
|
+
<title>${escapeHtml(title)}</title>
|
|
100
|
+
<style>
|
|
101
|
+
body { font-family: system-ui, sans-serif; line-height: 1.45; max-width: 52rem; margin: 2rem auto; padding: 0 1rem; color: #111; }
|
|
102
|
+
code { font-size: 0.9em; }
|
|
103
|
+
section { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid #ddd; }
|
|
104
|
+
.field-block { margin: 1rem 0; }
|
|
105
|
+
.field-block label { display: block; font-weight: 600; margin-bottom: 0.25rem; }
|
|
106
|
+
.field-block small { display: block; color: #555; margin-top: 0.25rem; }
|
|
107
|
+
article[data-markdown] { white-space: pre-wrap; border-left: 3px solid #ccc; padding-left: 1rem; }
|
|
108
|
+
</style>
|
|
109
|
+
</head>
|
|
110
|
+
<body>
|
|
111
|
+
<main>
|
|
112
|
+
${body}
|
|
113
|
+
</main>
|
|
114
|
+
</body>
|
|
115
|
+
</html>`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function pickListLabel(record, resource) {
|
|
119
|
+
if (!record || typeof record !== 'object') {
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
if (typeof record.title === 'string') {
|
|
123
|
+
return record.title;
|
|
124
|
+
}
|
|
125
|
+
if (typeof record.name === 'string') {
|
|
126
|
+
return record.name;
|
|
127
|
+
}
|
|
128
|
+
const idField = resource.idField ?? 'id';
|
|
129
|
+
return String(record[idField] ?? '');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {'view' | 'editor'} mode
|
|
134
|
+
*/
|
|
135
|
+
function renderFieldBlock(mode, fieldName, field, value, recordsByCollection, options = {}) {
|
|
136
|
+
const component = field.ui?.component ?? 'text';
|
|
137
|
+
const label = field.ui?.label ?? labelFromFieldName(fieldName);
|
|
138
|
+
const inner = mode === 'view'
|
|
139
|
+
? renderViewField(component, fieldName, field, value, recordsByCollection, options)
|
|
140
|
+
: renderEditorField(component, fieldName, field, value, recordsByCollection);
|
|
141
|
+
|
|
142
|
+
return ` <div class="field-block" data-component="${escapeHtml(component)}" data-field="${escapeHtml(fieldName)}">
|
|
143
|
+
<label>${escapeHtml(label)}</label>
|
|
144
|
+
${inner}
|
|
145
|
+
</div>`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function renderViewField(component, fieldName, field, value, recordsByCollection, options = {}) {
|
|
149
|
+
switch (component) {
|
|
150
|
+
case 'email': {
|
|
151
|
+
const text = scalarText(value);
|
|
152
|
+
return `<a href="mailto:${escapeHtml(text)}">${escapeHtml(text)}</a>${hint(field)}`;
|
|
153
|
+
}
|
|
154
|
+
case 'textarea':
|
|
155
|
+
return `<p>${escapeHtml(scalarText(value))}</p>${hint(field)}`;
|
|
156
|
+
case 'markdown':
|
|
157
|
+
return `<article data-markdown data-field="${escapeHtml(fieldName)}">${escapeHtml(scalarText(value))}</article>${hint(field)}`;
|
|
158
|
+
case 'segmented-control':
|
|
159
|
+
return `<span>${escapeHtml(scalarText(value))}</span>${hint(field)}`;
|
|
160
|
+
case 'relationSelect':
|
|
161
|
+
return `${relationAnchor(field, value, recordsByCollection, options)}${hint(field)}`;
|
|
162
|
+
case 'text':
|
|
163
|
+
default:
|
|
164
|
+
if (field.ui?.readonly) {
|
|
165
|
+
return `<span><code>${escapeHtml(scalarText(value))}</code></span>${hint(field)}`;
|
|
166
|
+
}
|
|
167
|
+
return `<span>${escapeHtml(scalarText(value))}</span>${hint(field)}`;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function renderEditorField(component, fieldName, field, value, recordsByCollection) {
|
|
172
|
+
if (field.ui?.readonly) {
|
|
173
|
+
return `<span><code>${escapeHtml(scalarText(value))}</code></span>${hint(field)}`;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const req = field.required ? 'required' : '';
|
|
177
|
+
|
|
178
|
+
switch (component) {
|
|
179
|
+
case 'email':
|
|
180
|
+
return `<input type="email" name="${escapeHtml(fieldName)}" value="${escapeHtml(scalarText(value))}" ${req}>${hint(field)}`;
|
|
181
|
+
case 'textarea':
|
|
182
|
+
return `<textarea name="${escapeHtml(fieldName)}" rows="4" cols="48" ${req}>${escapeHtml(scalarText(value))}</textarea>${hint(field)}`;
|
|
183
|
+
case 'markdown':
|
|
184
|
+
return `<textarea name="${escapeHtml(fieldName)}" rows="10" cols="48" data-editor="markdown" ${req}>${escapeHtml(scalarText(value))}</textarea>${hint(field)}`;
|
|
185
|
+
case 'segmented-control': {
|
|
186
|
+
const vals = Array.isArray(field.values) ? field.values : [];
|
|
187
|
+
const current = scalarText(value);
|
|
188
|
+
const radios = vals.map((option) => (
|
|
189
|
+
`<label><input type="radio" name="${escapeHtml(fieldName)}" value="${escapeHtml(String(option))}" ${current === String(option) ? 'checked' : ''}> ${escapeHtml(String(option))}</label>`
|
|
190
|
+
));
|
|
191
|
+
return `<fieldset>${radios.join('<br>\n')}</fieldset>${hint(field)}`;
|
|
192
|
+
}
|
|
193
|
+
case 'relationSelect':
|
|
194
|
+
return `${relationSelect(field, fieldName, value, recordsByCollection)}${hint(field)}`;
|
|
195
|
+
case 'text':
|
|
196
|
+
default:
|
|
197
|
+
return `<input type="text" name="${escapeHtml(fieldName)}" value="${escapeHtml(scalarText(value))}" ${req}>${hint(field)}`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function relationAnchor(field, foreignKey, recordsByCollection, options = {}) {
|
|
202
|
+
const keyText = scalarText(foreignKey);
|
|
203
|
+
if (!field.relation?.to || keyText === '') {
|
|
204
|
+
return `<span>${escapeHtml(keyText)}</span>`;
|
|
205
|
+
}
|
|
206
|
+
const label = relationDisplayLabel(field, foreignKey, recordsByCollection);
|
|
207
|
+
const targetCollection = field.relation.to;
|
|
208
|
+
return `<a href="${escapeHtml(joinPaths(options.basePath, `/cms/${targetCollection}/${encodeURIComponent(keyText)}`))}">${escapeHtml(label)}</a>`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function relationSelect(field, fieldName, selectedKey, recordsByCollection) {
|
|
212
|
+
const targetCollection = field.relation?.to ?? field.ui?.optionsFrom;
|
|
213
|
+
const idField = field.relation?.toField ?? 'id';
|
|
214
|
+
const rows = targetCollection ? recordsByCollection[targetCollection] ?? [] : [];
|
|
215
|
+
const current = scalarText(selectedKey);
|
|
216
|
+
const options = [`<option value="">Choose…</option>`].concat(rows.map((row) => {
|
|
217
|
+
const id = row?.[idField];
|
|
218
|
+
const label = row?.name ?? row?.email ?? row?.title ?? String(id ?? '');
|
|
219
|
+
return `<option value="${escapeHtml(String(id ?? ''))}" ${String(id ?? '') === current ? 'selected' : ''}>${escapeHtml(String(label))}</option>`;
|
|
220
|
+
}));
|
|
221
|
+
return `<select name="${escapeHtml(fieldName)}">${options.join('\n')}</select>`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function relationDisplayLabel(field, foreignKey, recordsByCollection) {
|
|
225
|
+
const targetCollection = field.relation?.to;
|
|
226
|
+
const idField = field.relation?.toField ?? 'id';
|
|
227
|
+
if (!targetCollection) {
|
|
228
|
+
return scalarText(foreignKey);
|
|
229
|
+
}
|
|
230
|
+
const rows = recordsByCollection[targetCollection] ?? [];
|
|
231
|
+
const match = rows.find((row) => String(row?.[idField] ?? '') === String(foreignKey ?? ''));
|
|
232
|
+
if (!match) {
|
|
233
|
+
return scalarText(foreignKey);
|
|
234
|
+
}
|
|
235
|
+
return String(match.name ?? match.email ?? match.title ?? foreignKey ?? '');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function scalarText(value) {
|
|
239
|
+
if (value === undefined || value === null) {
|
|
240
|
+
return '';
|
|
241
|
+
}
|
|
242
|
+
return String(value);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function hint(field) {
|
|
246
|
+
return field.description ? `<small>${escapeHtml(field.description)}</small>` : '';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function labelFromFieldName(fieldName) {
|
|
250
|
+
return fieldName
|
|
251
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
252
|
+
.replace(/[-_]+/g, ' ')
|
|
253
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase());
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function normalizeBasePath(basePath) {
|
|
257
|
+
if (!basePath || basePath === '/') {
|
|
258
|
+
return '';
|
|
259
|
+
}
|
|
260
|
+
return `/${String(basePath).replace(/^\/+|\/+$/gu, '')}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function joinPaths(basePath, childPath) {
|
|
264
|
+
const normalizedBase = normalizeBasePath(basePath);
|
|
265
|
+
const normalizedChild = `/${String(childPath).replace(/^\/+/u, '')}`;
|
|
266
|
+
return `${normalizedBase}${normalizedChild}`;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function escapeHtml(value) {
|
|
270
|
+
return String(value)
|
|
271
|
+
.replaceAll('&', '&')
|
|
272
|
+
.replaceAll('<', '<')
|
|
273
|
+
.replaceAll('>', '>')
|
|
274
|
+
.replaceAll('"', '"')
|
|
275
|
+
.replaceAll("'", ''');
|
|
276
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"collections": {
|
|
4
|
+
"pages": {
|
|
5
|
+
"kind": "collection",
|
|
6
|
+
"name": "pages",
|
|
7
|
+
"fields": {
|
|
8
|
+
"id": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"required": true,
|
|
11
|
+
"nullable": false,
|
|
12
|
+
"description": "Stable page id.",
|
|
13
|
+
"ui": {
|
|
14
|
+
"label": "Id",
|
|
15
|
+
"component": "text",
|
|
16
|
+
"readonly": true
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"title": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"required": true,
|
|
22
|
+
"nullable": false,
|
|
23
|
+
"description": "Page title shown in the CMS list.",
|
|
24
|
+
"ui": {
|
|
25
|
+
"label": "Title",
|
|
26
|
+
"component": "text"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"status": {
|
|
30
|
+
"type": "enum",
|
|
31
|
+
"required": false,
|
|
32
|
+
"nullable": false,
|
|
33
|
+
"description": "Publication state.",
|
|
34
|
+
"default": "draft",
|
|
35
|
+
"values": [
|
|
36
|
+
"draft",
|
|
37
|
+
"review",
|
|
38
|
+
"published"
|
|
39
|
+
],
|
|
40
|
+
"ui": {
|
|
41
|
+
"label": "Status",
|
|
42
|
+
"component": "segmented-control"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"authorId": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"required": true,
|
|
48
|
+
"nullable": false,
|
|
49
|
+
"description": "Page author.",
|
|
50
|
+
"relation": {
|
|
51
|
+
"name": "author",
|
|
52
|
+
"to": "users",
|
|
53
|
+
"toField": "id",
|
|
54
|
+
"cardinality": "one"
|
|
55
|
+
},
|
|
56
|
+
"ui": {
|
|
57
|
+
"label": "Author Id",
|
|
58
|
+
"component": "relationSelect",
|
|
59
|
+
"optionsFrom": "users"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"summary": {
|
|
63
|
+
"type": "string",
|
|
64
|
+
"required": false,
|
|
65
|
+
"nullable": false,
|
|
66
|
+
"description": "Short page summary.",
|
|
67
|
+
"maxLength": 160,
|
|
68
|
+
"ui": {
|
|
69
|
+
"label": "Summary",
|
|
70
|
+
"component": "textarea"
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"bodyMarkdown": {
|
|
74
|
+
"type": "string",
|
|
75
|
+
"required": false,
|
|
76
|
+
"nullable": false,
|
|
77
|
+
"description": "Markdown body content.",
|
|
78
|
+
"ui": {
|
|
79
|
+
"label": "Body",
|
|
80
|
+
"component": "markdown"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"description": "CMS pages rendered by a tiny schema-driven UI example.",
|
|
85
|
+
"idField": "id",
|
|
86
|
+
"editor": {
|
|
87
|
+
"title": "Pages",
|
|
88
|
+
"description": "CMS pages edited from generated schema metadata."
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"users": {
|
|
92
|
+
"kind": "collection",
|
|
93
|
+
"name": "users",
|
|
94
|
+
"fields": {
|
|
95
|
+
"id": {
|
|
96
|
+
"type": "string",
|
|
97
|
+
"required": true,
|
|
98
|
+
"nullable": false,
|
|
99
|
+
"description": "Stable author id.",
|
|
100
|
+
"ui": {
|
|
101
|
+
"label": "Id",
|
|
102
|
+
"component": "text",
|
|
103
|
+
"readonly": true
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"name": {
|
|
107
|
+
"type": "string",
|
|
108
|
+
"required": true,
|
|
109
|
+
"nullable": false,
|
|
110
|
+
"description": "Author display name.",
|
|
111
|
+
"ui": {
|
|
112
|
+
"label": "Name",
|
|
113
|
+
"component": "text"
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"email": {
|
|
117
|
+
"type": "string",
|
|
118
|
+
"required": true,
|
|
119
|
+
"nullable": false,
|
|
120
|
+
"description": "Author email address.",
|
|
121
|
+
"unique": true,
|
|
122
|
+
"ui": {
|
|
123
|
+
"label": "Email",
|
|
124
|
+
"component": "email"
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
"description": "Authors that can own CMS pages.",
|
|
129
|
+
"idField": "id"
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
"documents": {}
|
|
133
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// This file is generated by db. Do not edit by hand.
|
|
3
|
+
|
|
4
|
+
export type PageStatus = "draft" | "review" | "published";
|
|
5
|
+
|
|
6
|
+
/** CMS pages rendered by a tiny schema-driven UI example. */
|
|
7
|
+
export type Page = {
|
|
8
|
+
/** Stable page id. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Page title shown in the CMS list. */
|
|
11
|
+
title: string;
|
|
12
|
+
/** Publication state. */
|
|
13
|
+
status?: PageStatus;
|
|
14
|
+
/** Page author. */
|
|
15
|
+
authorId: string;
|
|
16
|
+
/** Short page summary. */
|
|
17
|
+
summary?: string;
|
|
18
|
+
/** Markdown body content. */
|
|
19
|
+
bodyMarkdown?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Authors that can own CMS pages. */
|
|
23
|
+
export type User = {
|
|
24
|
+
/** Stable author id. */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Author display name. */
|
|
27
|
+
name: string;
|
|
28
|
+
/** Author email address. */
|
|
29
|
+
email: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type DbCollections = {
|
|
33
|
+
pages: Page;
|
|
34
|
+
users: User;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type DbDocuments = {
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type DbTypes = {
|
|
41
|
+
collections: DbCollections;
|
|
42
|
+
documents: DbDocuments;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type DbCollectionName = keyof DbCollections;
|
|
46
|
+
export type DbDocumentName = keyof DbDocuments;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const fieldTemplates = [
|
|
7
|
+
{
|
|
8
|
+
component: 'text',
|
|
9
|
+
view({ fieldName, field }) {
|
|
10
|
+
return `<span data-field="${escapeHtml(fieldName)}">{{ ${escapeHtml(fieldName)} }}</span>${hint(field)}`;
|
|
11
|
+
},
|
|
12
|
+
editor({ fieldName, field }) {
|
|
13
|
+
return `<input name="${escapeHtml(fieldName)}" type="text" ${required(field)}>${hint(field)}`;
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
component: 'email',
|
|
18
|
+
view({ fieldName, field }) {
|
|
19
|
+
return `<a href="mailto:{{ ${escapeHtml(fieldName)} }}">{{ ${escapeHtml(fieldName)} }}</a>${hint(field)}`;
|
|
20
|
+
},
|
|
21
|
+
editor({ fieldName, field }) {
|
|
22
|
+
return `<input name="${escapeHtml(fieldName)}" type="email" ${required(field)}>${hint(field)}`;
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
component: 'textarea',
|
|
27
|
+
view({ fieldName, field }) {
|
|
28
|
+
return `<p data-field="${escapeHtml(fieldName)}">{{ ${escapeHtml(fieldName)} }}</p>${hint(field)}`;
|
|
29
|
+
},
|
|
30
|
+
editor({ fieldName, field }) {
|
|
31
|
+
return `<textarea name="${escapeHtml(fieldName)}" ${required(field)}></textarea>${hint(field)}`;
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
component: 'markdown',
|
|
36
|
+
view({ fieldName, field }) {
|
|
37
|
+
return `<article data-field="${escapeHtml(fieldName)}">{{ rendered ${escapeHtml(fieldName)} }}</article>${hint(field)}`;
|
|
38
|
+
},
|
|
39
|
+
editor({ fieldName, field }) {
|
|
40
|
+
return `<textarea name="${escapeHtml(fieldName)}" data-editor="markdown" ${required(field)}></textarea>${hint(field)}`;
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
component: 'segmented-control',
|
|
45
|
+
view({ fieldName, field }) {
|
|
46
|
+
return `<span data-field="${escapeHtml(fieldName)}">{{ ${escapeHtml(fieldName)} }}</span>${hint(field)}`;
|
|
47
|
+
},
|
|
48
|
+
editor({ fieldName, field }) {
|
|
49
|
+
return `<fieldset>${legend(field)}${options(field).map((value) => (
|
|
50
|
+
`<label><input type="radio" name="${escapeHtml(fieldName)}" value="${escapeHtml(value)}"> ${escapeHtml(value)}</label>`
|
|
51
|
+
)).join('')}</fieldset>${hint(field)}`;
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
component: 'relationSelect',
|
|
56
|
+
view({ fieldName, field }) {
|
|
57
|
+
const relation = field.relation?.name ?? fieldName;
|
|
58
|
+
return `<a href="#${escapeHtml(relation)}-{{ ${escapeHtml(fieldName)} }}">{{ ${escapeHtml(relation)} label }}</a>${hint(field)}`;
|
|
59
|
+
},
|
|
60
|
+
editor({ fieldName, field }) {
|
|
61
|
+
const source = field.ui?.optionsFrom ?? field.relation?.to ?? 'records';
|
|
62
|
+
return `<select name="${escapeHtml(fieldName)}" data-options-from="${escapeHtml(source)}" ${required(field)}></select>${hint(field)}`;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
const templateByComponent = new Map(fieldTemplates.map((template) => [template.component, template]));
|
|
68
|
+
const fallbackTemplate = templateByComponent.get('text');
|
|
69
|
+
|
|
70
|
+
/** @param {unknown} schemaManifest */
|
|
71
|
+
export function renderSchemaUiHtml(schemaManifest) {
|
|
72
|
+
return renderCms(schemaManifest);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @param {URL | string} manifestUrl */
|
|
76
|
+
export async function readManifest(manifestUrl) {
|
|
77
|
+
const raw = await readFile(manifestUrl, 'utf8');
|
|
78
|
+
return JSON.parse(raw);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function renderCms(schemaManifest) {
|
|
82
|
+
const collections = Object.values(schemaManifest.collections ?? {});
|
|
83
|
+
return `<!doctype html>
|
|
84
|
+
<html lang="en">
|
|
85
|
+
<head>
|
|
86
|
+
<meta charset="utf-8">
|
|
87
|
+
<title>Schema UI Example</title>
|
|
88
|
+
</head>
|
|
89
|
+
<body>
|
|
90
|
+
<main>
|
|
91
|
+
<h1>Schema UI Example</h1>
|
|
92
|
+
<p>This HTML is generated from src/generated/db.schema.json.</p>
|
|
93
|
+
${collections.map(renderCollection).join('\n')}
|
|
94
|
+
</main>
|
|
95
|
+
</body>
|
|
96
|
+
</html>`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function renderCollection(resource) {
|
|
100
|
+
const title = resource.editor?.title ?? resource.name;
|
|
101
|
+
const description = resource.editor?.description ?? resource.description ?? '';
|
|
102
|
+
const fields = Object.entries(resource.fields ?? {});
|
|
103
|
+
return ` <section data-resource="${escapeHtml(resource.name)}">
|
|
104
|
+
<header>
|
|
105
|
+
<h2>${escapeHtml(title)}</h2>
|
|
106
|
+
${description ? `<p>${escapeHtml(description)}</p>` : ''}
|
|
107
|
+
</header>
|
|
108
|
+
<div class="cms-view">
|
|
109
|
+
<h3>View template</h3>
|
|
110
|
+
${fields.map(([fieldName, field]) => renderField('view', fieldName, field)).join('\n')}
|
|
111
|
+
</div>
|
|
112
|
+
<form class="cms-editor">
|
|
113
|
+
<h3>Editor template</h3>
|
|
114
|
+
${fields.map(([fieldName, field]) => renderField('editor', fieldName, field)).join('\n')}
|
|
115
|
+
</form>
|
|
116
|
+
</section>`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function renderField(mode, fieldName, field) {
|
|
120
|
+
const component = field.ui?.component ?? 'text';
|
|
121
|
+
const template = templateByComponent.get(component) ?? fallbackTemplate;
|
|
122
|
+
const label = field.ui?.label ?? labelFromFieldName(fieldName);
|
|
123
|
+
const body = template[mode]({ fieldName, field });
|
|
124
|
+
|
|
125
|
+
return ` <template data-mode="${mode}" data-component="${escapeHtml(component)}" data-field="${escapeHtml(fieldName)}">
|
|
126
|
+
<label>${escapeHtml(label)}</label>
|
|
127
|
+
${body}
|
|
128
|
+
</template>`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function required(field) {
|
|
132
|
+
return field.required ? 'required' : '';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function legend(field) {
|
|
136
|
+
return `<legend>${escapeHtml(field.ui?.label ?? 'Choose one')}</legend>`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function options(field) {
|
|
140
|
+
return Array.isArray(field.values) ? field.values : [];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function hint(field) {
|
|
144
|
+
return field.description ? `<small>${escapeHtml(field.description)}</small>` : '';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function labelFromFieldName(fieldName) {
|
|
148
|
+
return fieldName
|
|
149
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
150
|
+
.replace(/[-_]+/g, ' ')
|
|
151
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase());
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function escapeHtml(value) {
|
|
155
|
+
return String(value)
|
|
156
|
+
.replaceAll('&', '&')
|
|
157
|
+
.replaceAll('<', '<')
|
|
158
|
+
.replaceAll('>', '>')
|
|
159
|
+
.replaceAll('"', '"')
|
|
160
|
+
.replaceAll("'", ''');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function isPrimaryModule() {
|
|
164
|
+
const entry = process.argv[1];
|
|
165
|
+
if (!entry) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return path.resolve(entry) === path.resolve(fileURLToPath(import.meta.url));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isPrimaryModule()) {
|
|
172
|
+
const manifestUrl = new URL('./generated/db.schema.json', import.meta.url);
|
|
173
|
+
const manifest = await readManifest(manifestUrl);
|
|
174
|
+
console.log(renderSchemaUiHtml(manifest));
|
|
175
|
+
}
|