@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,296 @@
|
|
|
1
|
+
export function normalizeField(field, fieldName = '') {
|
|
2
|
+
if (!isPlainRecord(field)) {
|
|
3
|
+
return inferFieldFromValue(field, fieldName, { required: false });
|
|
4
|
+
}
|
|
5
|
+
const normalized = {
|
|
6
|
+
type: (field.type ?? 'unknown'),
|
|
7
|
+
};
|
|
8
|
+
if ('nullable' in field) {
|
|
9
|
+
normalized.nullable = Boolean(field.nullable);
|
|
10
|
+
}
|
|
11
|
+
if ('required' in field) {
|
|
12
|
+
normalized.required = Boolean(field.required);
|
|
13
|
+
}
|
|
14
|
+
if ('computed' in field) {
|
|
15
|
+
normalized.computed = Boolean(field.computed);
|
|
16
|
+
}
|
|
17
|
+
if ('readOnly' in field) {
|
|
18
|
+
normalized.readOnly = Boolean(field.readOnly);
|
|
19
|
+
}
|
|
20
|
+
if (normalized.computed) {
|
|
21
|
+
normalized.readOnly = true;
|
|
22
|
+
normalized.required = false;
|
|
23
|
+
}
|
|
24
|
+
if ('description' in field) {
|
|
25
|
+
normalized.description = String(field.description);
|
|
26
|
+
}
|
|
27
|
+
if ('default' in field) {
|
|
28
|
+
normalized.default = field.default;
|
|
29
|
+
}
|
|
30
|
+
if ('unique' in field) {
|
|
31
|
+
normalized.unique = Boolean(field.unique);
|
|
32
|
+
}
|
|
33
|
+
for (const constraintName of ['min', 'max', 'minLength', 'maxLength', 'pattern']) {
|
|
34
|
+
if (constraintName in field) {
|
|
35
|
+
normalized[constraintName] = field[constraintName];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (isPlainRecord(field.relation)) {
|
|
39
|
+
normalized.relation = normalizeRelation(field.relation, fieldName);
|
|
40
|
+
}
|
|
41
|
+
if (field.type === 'enum') {
|
|
42
|
+
normalized.values = Array.isArray(field.values) ? [...field.values] : [];
|
|
43
|
+
}
|
|
44
|
+
if (field.type === 'array') {
|
|
45
|
+
normalized.items = normalizeField(field.items ?? { type: 'unknown' }, `${fieldName}Item`);
|
|
46
|
+
}
|
|
47
|
+
if (field.type === 'object' && 'additionalProperties' in field) {
|
|
48
|
+
normalized.additionalProperties = Boolean(field.additionalProperties);
|
|
49
|
+
}
|
|
50
|
+
if (field.type === 'object' && 'discriminator' in field) {
|
|
51
|
+
normalized.discriminator = String(field.discriminator);
|
|
52
|
+
}
|
|
53
|
+
if (field.type === 'object' && isPlainRecord(field.fields)) {
|
|
54
|
+
normalized.fields = Object.fromEntries(Object.entries(field.fields).map(([childName, childField]) => [childName, normalizeField(childField, childName)]));
|
|
55
|
+
}
|
|
56
|
+
if (field.type === 'object' && isPlainRecord(field.variants)) {
|
|
57
|
+
normalized.variants = Object.fromEntries(Object.entries(field.variants).map(([variantName, variant]) => [variantName, normalizeVariant(variantName, variant, normalized.discriminator)]));
|
|
58
|
+
}
|
|
59
|
+
return normalized;
|
|
60
|
+
}
|
|
61
|
+
export function inferFieldsFromData(value, kind = 'collection') {
|
|
62
|
+
if (kind === 'collection') {
|
|
63
|
+
const records = Array.isArray(value) ? value : [];
|
|
64
|
+
const names = new Set();
|
|
65
|
+
for (const record of records) {
|
|
66
|
+
if (isPlainRecord(record)) {
|
|
67
|
+
for (const key of Object.keys(record)) {
|
|
68
|
+
names.add(key);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return Object.fromEntries([...names].sort().map((fieldName) => {
|
|
73
|
+
const samples = records.map((record) => isPlainRecord(record) ? record[fieldName] : undefined);
|
|
74
|
+
const present = samples.filter((sample) => sample !== undefined && sample !== null);
|
|
75
|
+
const required = records.length > 0 && present.length === records.length;
|
|
76
|
+
return [fieldName, inferFieldFromSamples(present, fieldName, { required })];
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
if (!isPlainRecord(value)) {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
return Object.fromEntries(Object.entries(value)
|
|
83
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
84
|
+
.map(([fieldName, sample]) => [fieldName, inferFieldFromValue(sample, fieldName, { required: false })]));
|
|
85
|
+
}
|
|
86
|
+
export function inferFieldFromSamples(samples, fieldName, options = {}) {
|
|
87
|
+
if (samples.length === 0) {
|
|
88
|
+
return { type: 'unknown', required: Boolean(options.required) };
|
|
89
|
+
}
|
|
90
|
+
if (options.inferVariants === true) {
|
|
91
|
+
const variantField = inferDiscriminatedObjectField(samples, options.required);
|
|
92
|
+
if (variantField) {
|
|
93
|
+
return variantField;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const inferred = samples.map((sample) => inferFieldFromValue(sample, fieldName, options));
|
|
97
|
+
return mergeInferredFields(inferred, options.required);
|
|
98
|
+
}
|
|
99
|
+
export function inferFieldFromValue(value, fieldName, options = {}) {
|
|
100
|
+
const required = Boolean(options.required);
|
|
101
|
+
if (value === null || value === undefined) {
|
|
102
|
+
return { type: 'unknown', required: false };
|
|
103
|
+
}
|
|
104
|
+
if (Array.isArray(value)) {
|
|
105
|
+
return {
|
|
106
|
+
type: 'array',
|
|
107
|
+
required,
|
|
108
|
+
items: inferFieldFromSamples(value.filter((item) => item !== null && item !== undefined), `${fieldName}Item`, {
|
|
109
|
+
required: false,
|
|
110
|
+
inferVariants: true,
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (isPlainRecord(value)) {
|
|
115
|
+
return {
|
|
116
|
+
type: 'object',
|
|
117
|
+
required,
|
|
118
|
+
fields: inferFieldsFromData(value, 'document'),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
122
|
+
return { type: typeof value, required };
|
|
123
|
+
}
|
|
124
|
+
return { type: 'unknown', required };
|
|
125
|
+
}
|
|
126
|
+
function mergeInferredFields(fields, required) {
|
|
127
|
+
if (fields.length === 0) {
|
|
128
|
+
return { type: 'unknown', required: Boolean(required) };
|
|
129
|
+
}
|
|
130
|
+
const types = new Set(fields.map((field) => field.type));
|
|
131
|
+
if (types.size > 1) {
|
|
132
|
+
return { type: 'unknown', required: Boolean(required) };
|
|
133
|
+
}
|
|
134
|
+
const [first] = fields;
|
|
135
|
+
if (first.type === 'object') {
|
|
136
|
+
if (fields.every((field) => field.type === 'object' && field.variants && field.discriminator === first.discriminator)) {
|
|
137
|
+
return mergeVariantObjectFields(fields, required);
|
|
138
|
+
}
|
|
139
|
+
const names = new Set();
|
|
140
|
+
for (const field of fields) {
|
|
141
|
+
for (const childName of Object.keys(field.fields ?? {})) {
|
|
142
|
+
names.add(childName);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
type: 'object',
|
|
147
|
+
required: Boolean(required),
|
|
148
|
+
fields: Object.fromEntries([...names].sort().map((childName) => {
|
|
149
|
+
const childSamples = fields.map((field) => field.fields?.[childName]).filter(Boolean);
|
|
150
|
+
return [childName, mergeInferredFields(childSamples, childSamples.every((field) => field.required))];
|
|
151
|
+
})),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
if (first.type === 'array') {
|
|
155
|
+
return {
|
|
156
|
+
type: 'array',
|
|
157
|
+
required: Boolean(required),
|
|
158
|
+
items: mergeInferredFields(fields.map((field) => field.items).filter(Boolean), false),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
...first,
|
|
163
|
+
required: Boolean(required),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function mergeVariantObjectFields(fields, required) {
|
|
167
|
+
const discriminator = fields[0].discriminator;
|
|
168
|
+
const variantNames = new Set();
|
|
169
|
+
for (const field of fields) {
|
|
170
|
+
for (const variantName of Object.keys(field.variants ?? {})) {
|
|
171
|
+
variantNames.add(variantName);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
type: 'object',
|
|
176
|
+
required: Boolean(required),
|
|
177
|
+
discriminator,
|
|
178
|
+
variants: Object.fromEntries([...variantNames].map((variantName) => [
|
|
179
|
+
variantName,
|
|
180
|
+
mergeVariantDefinitions(fields.map((field) => field.variants?.[variantName]).filter(Boolean), discriminator, variantName),
|
|
181
|
+
])),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function mergeVariantDefinitions(variants, discriminator, variantName) {
|
|
185
|
+
const fieldNames = new Set();
|
|
186
|
+
for (const variant of variants) {
|
|
187
|
+
for (const fieldName of Object.keys(variant.fields ?? {})) {
|
|
188
|
+
fieldNames.add(fieldName);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const fields = Object.fromEntries([...fieldNames].sort().map((fieldName) => {
|
|
192
|
+
const childFields = variants.map((variant) => variant.fields?.[fieldName]).filter(Boolean);
|
|
193
|
+
const required = childFields.length === variants.length && childFields.every((field) => field.required);
|
|
194
|
+
return [fieldName, mergeInferredFields(childFields, required)];
|
|
195
|
+
}));
|
|
196
|
+
fields[discriminator] = {
|
|
197
|
+
type: 'enum',
|
|
198
|
+
values: [variantName],
|
|
199
|
+
required: true,
|
|
200
|
+
};
|
|
201
|
+
return { fields };
|
|
202
|
+
}
|
|
203
|
+
function normalizeVariant(variantName, variant, discriminator) {
|
|
204
|
+
const raw = isPlainRecord(variant)
|
|
205
|
+
? variant
|
|
206
|
+
: {};
|
|
207
|
+
const rawFields = isPlainRecord(raw.fields)
|
|
208
|
+
? raw.fields
|
|
209
|
+
: {};
|
|
210
|
+
const normalized = {
|
|
211
|
+
fields: Object.fromEntries(Object.entries(rawFields).map(([fieldName, field]) => [fieldName, normalizeField(field, fieldName)])),
|
|
212
|
+
};
|
|
213
|
+
if ('additionalProperties' in raw) {
|
|
214
|
+
normalized.additionalProperties = Boolean(raw.additionalProperties);
|
|
215
|
+
}
|
|
216
|
+
if (discriminator && !(discriminator in normalized.fields)) {
|
|
217
|
+
normalized.fields = {
|
|
218
|
+
[discriminator]: {
|
|
219
|
+
type: 'enum',
|
|
220
|
+
values: [variantName],
|
|
221
|
+
required: true,
|
|
222
|
+
},
|
|
223
|
+
...normalized.fields,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
return normalized;
|
|
227
|
+
}
|
|
228
|
+
function inferDiscriminatedObjectField(samples, required) {
|
|
229
|
+
const records = samples.filter((sample) => sample !== null && sample !== undefined);
|
|
230
|
+
if (records.length === 0 || records.some((sample) => !isPlainRecord(sample))) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
const objectRecords = records;
|
|
234
|
+
for (const discriminator of ['type', 'kind', 'blockType']) {
|
|
235
|
+
const groups = groupByDiscriminator(objectRecords, discriminator);
|
|
236
|
+
if (!groups) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const signatures = new Set([...groups.values()].map((group) => objectSignature(group[0], discriminator)));
|
|
240
|
+
if (signatures.size < 2) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
type: 'object',
|
|
245
|
+
required: Boolean(required),
|
|
246
|
+
discriminator,
|
|
247
|
+
variants: Object.fromEntries([...groups.entries()].map(([variantName, group]) => {
|
|
248
|
+
const fields = inferFieldsFromData(group, 'collection');
|
|
249
|
+
fields[discriminator] = {
|
|
250
|
+
type: 'enum',
|
|
251
|
+
values: [variantName],
|
|
252
|
+
required: true,
|
|
253
|
+
};
|
|
254
|
+
return [variantName, {
|
|
255
|
+
fields,
|
|
256
|
+
}];
|
|
257
|
+
})),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
function groupByDiscriminator(records, discriminator) {
|
|
263
|
+
const groups = new Map();
|
|
264
|
+
for (const record of records) {
|
|
265
|
+
const value = record[discriminator];
|
|
266
|
+
if (typeof value !== 'string' || value === '') {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
if (!groups.has(value)) {
|
|
270
|
+
groups.set(value, []);
|
|
271
|
+
}
|
|
272
|
+
groups.get(value).push(record);
|
|
273
|
+
}
|
|
274
|
+
return groups.size >= 2 ? groups : null;
|
|
275
|
+
}
|
|
276
|
+
function objectSignature(record, discriminator) {
|
|
277
|
+
return Object.keys(record)
|
|
278
|
+
.filter((fieldName) => fieldName !== discriminator)
|
|
279
|
+
.sort()
|
|
280
|
+
.join('|');
|
|
281
|
+
}
|
|
282
|
+
function normalizeRelation(relation, fieldName) {
|
|
283
|
+
return {
|
|
284
|
+
name: String(relation.name ?? relationNameFromField(fieldName)),
|
|
285
|
+
to: relation.to === undefined ? undefined : String(relation.to),
|
|
286
|
+
toField: String(relation.toField ?? 'id'),
|
|
287
|
+
cardinality: relation.cardinality === 'many' ? 'many' : 'one',
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function isPlainRecord(value) {
|
|
291
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
292
|
+
}
|
|
293
|
+
function relationNameFromField(fieldName) {
|
|
294
|
+
const withoutId = String(fieldName).replace(/Id$/i, '');
|
|
295
|
+
return withoutId || String(fieldName);
|
|
296
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type SchemaDiagnostic, type SchemaResource } from './metadata.js';
|
|
2
|
+
type GeneratedSchemaResource = {
|
|
3
|
+
kind?: string;
|
|
4
|
+
typeName: string;
|
|
5
|
+
routePath: string;
|
|
6
|
+
idField?: string;
|
|
7
|
+
description?: unknown;
|
|
8
|
+
fields?: unknown;
|
|
9
|
+
relations?: unknown;
|
|
10
|
+
seed?: unknown;
|
|
11
|
+
source: {
|
|
12
|
+
typeSource?: unknown;
|
|
13
|
+
dataPath?: unknown;
|
|
14
|
+
dataFormat?: unknown;
|
|
15
|
+
dataHash?: unknown;
|
|
16
|
+
schemaPath?: unknown;
|
|
17
|
+
generatedIds?: unknown;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export declare function makeGeneratedSchema(resources: SchemaResource[], diagnostics?: SchemaDiagnostic[]): {
|
|
21
|
+
diagnostics: SchemaDiagnostic[];
|
|
22
|
+
version: number;
|
|
23
|
+
generatedAt: string;
|
|
24
|
+
resources: {
|
|
25
|
+
[k: string]: GeneratedSchemaResource;
|
|
26
|
+
};
|
|
27
|
+
relations: unknown[];
|
|
28
|
+
};
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defaultGeneratedSchemaMetadataContributors, generatedSchemaMetadata, } from './metadata.js';
|
|
2
|
+
export function makeGeneratedSchema(resources, diagnostics = []) {
|
|
3
|
+
const metadata = generatedSchemaMetadata(resources, diagnostics, defaultGeneratedSchemaMetadataContributors());
|
|
4
|
+
return {
|
|
5
|
+
version: 1,
|
|
6
|
+
generatedAt: new Date().toISOString(),
|
|
7
|
+
resources: Object.fromEntries(resources.map((resource) => [resource.name, serializeResource(resource)])),
|
|
8
|
+
relations: resources.flatMap((resource) => resource.relations ?? []),
|
|
9
|
+
...metadata,
|
|
10
|
+
diagnostics,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function serializeResource(resource) {
|
|
14
|
+
return {
|
|
15
|
+
kind: resource.kind,
|
|
16
|
+
typeName: resource.typeName,
|
|
17
|
+
routePath: resource.routePath,
|
|
18
|
+
idField: resource.kind === 'collection' ? resource.idField : undefined,
|
|
19
|
+
description: resource.description,
|
|
20
|
+
fields: Object.keys(resource.fields ?? {}).length > 0 ? resource.fields : undefined,
|
|
21
|
+
relations: resource.relations,
|
|
22
|
+
seed: resource.seed,
|
|
23
|
+
source: {
|
|
24
|
+
typeSource: resource.typeSource,
|
|
25
|
+
dataPath: resource.dataPath,
|
|
26
|
+
dataFormat: resource.dataFormat,
|
|
27
|
+
dataHash: resource.dataHash,
|
|
28
|
+
schemaPath: resource.schemaPath,
|
|
29
|
+
generatedIds: resource.generatedIds,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type SchemaLoadMode = 'schema' | 'data' | 'runtime';
|
|
2
|
+
export type SchemaLocator = {
|
|
3
|
+
cwd: string;
|
|
4
|
+
sourceDir: string;
|
|
5
|
+
mode: 'project' | 'source-dir' | 'root-schema' | 'schema-file';
|
|
6
|
+
file: string | null;
|
|
7
|
+
baseDir: string;
|
|
8
|
+
resourceName: string | null;
|
|
9
|
+
};
|
|
10
|
+
type ResolveSchemaLocatorOptions = {
|
|
11
|
+
cwd?: string;
|
|
12
|
+
from?: string | null;
|
|
13
|
+
};
|
|
14
|
+
export declare function resolveSchemaLocator(options?: ResolveSchemaLocatorOptions | string): Promise<SchemaLocator>;
|
|
15
|
+
export declare function normalizeSchemaLoadMode(value?: unknown): SchemaLoadMode;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
const SCHEMA_FILE_RE = /(?:^|[/\\])(?:db\.schema\.(?:mjs|js)|[^/\\]+\.schema\.(?:json|jsonc|mjs|js))$/;
|
|
4
|
+
export async function resolveSchemaLocator(options = {}) {
|
|
5
|
+
const input = typeof options === 'string' ? { from: options } : options ?? {};
|
|
6
|
+
const baseCwd = path.resolve(input.cwd ?? process.cwd());
|
|
7
|
+
const from = input.from === undefined || input.from === null || input.from === ''
|
|
8
|
+
? null
|
|
9
|
+
: path.resolve(baseCwd, String(input.from));
|
|
10
|
+
if (!from) {
|
|
11
|
+
return projectLocator(baseCwd);
|
|
12
|
+
}
|
|
13
|
+
let stats;
|
|
14
|
+
try {
|
|
15
|
+
stats = await stat(from);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
const fsError = error;
|
|
19
|
+
if (fsError.code === 'ENOENT') {
|
|
20
|
+
throw schemaLocatorError('DB_SCHEMA_LOCATOR_NOT_FOUND', `Schema locator path does not exist: ${path.relative(baseCwd, from) || from}`, {
|
|
21
|
+
hint: 'Pass from: "." for a project root, from: "./db" for a fixture folder, or from: "./db.schema.mjs" / "./db.schema.js" / "./db/users.schema.mjs" for a schema file.',
|
|
22
|
+
details: {
|
|
23
|
+
from: input.from,
|
|
24
|
+
path: from,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
if (stats.isDirectory()) {
|
|
31
|
+
return directoryLocator(from, baseCwd);
|
|
32
|
+
}
|
|
33
|
+
if (stats.isFile()) {
|
|
34
|
+
return fileLocator(from, baseCwd);
|
|
35
|
+
}
|
|
36
|
+
throw schemaLocatorError('DB_SCHEMA_LOCATOR_UNSUPPORTED', `Schema locator must be a file or directory: ${path.relative(baseCwd, from) || from}`, {
|
|
37
|
+
hint: 'Use a project root, a db folder, db.schema.mjs, db.schema.js, or a .schema.json/.schema.jsonc/.schema.mjs/.schema.js file.',
|
|
38
|
+
details: {
|
|
39
|
+
from: input.from,
|
|
40
|
+
path: from,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export function normalizeSchemaLoadMode(value = 'data') {
|
|
45
|
+
if (value === undefined || value === null || value === '') {
|
|
46
|
+
return 'data';
|
|
47
|
+
}
|
|
48
|
+
if (value === 'schema' || value === 'data' || value === 'runtime') {
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
throw schemaLocatorError('DB_SCHEMA_LOAD_MODE_INVALID', `Unsupported schema load mode "${value}".`, {
|
|
52
|
+
hint: 'Use load: "schema", load: "data", or load: "runtime".',
|
|
53
|
+
details: {
|
|
54
|
+
load: value,
|
|
55
|
+
allowed: ['schema', 'data', 'runtime'],
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function projectLocator(cwd) {
|
|
60
|
+
return {
|
|
61
|
+
cwd,
|
|
62
|
+
sourceDir: path.join(cwd, 'db'),
|
|
63
|
+
mode: 'project',
|
|
64
|
+
file: null,
|
|
65
|
+
baseDir: cwd,
|
|
66
|
+
resourceName: null,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function directoryLocator(directory, fallbackCwd) {
|
|
70
|
+
const basename = path.basename(directory);
|
|
71
|
+
const cwd = basename === 'db' ? path.dirname(directory) : directory;
|
|
72
|
+
return {
|
|
73
|
+
cwd: path.resolve(cwd || fallbackCwd),
|
|
74
|
+
sourceDir: basename === 'db' ? directory : path.join(directory, 'db'),
|
|
75
|
+
mode: basename === 'db' ? 'source-dir' : 'project',
|
|
76
|
+
file: null,
|
|
77
|
+
baseDir: directory,
|
|
78
|
+
resourceName: null,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function fileLocator(file, fallbackCwd) {
|
|
82
|
+
const normalized = file.split(path.sep).join('/');
|
|
83
|
+
if (!SCHEMA_FILE_RE.test(normalized)) {
|
|
84
|
+
throw schemaLocatorError('DB_SCHEMA_LOCATOR_UNSUPPORTED', `Unsupported schema locator file: ${path.basename(file)}`, {
|
|
85
|
+
hint: 'Use db.schema.mjs, db.schema.js, or a .schema.json/.schema.jsonc/.schema.mjs/.schema.js file.',
|
|
86
|
+
details: {
|
|
87
|
+
path: file,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (path.basename(file) === 'db.schema.mjs' || path.basename(file) === 'db.schema.js') {
|
|
92
|
+
const cwd = path.dirname(file);
|
|
93
|
+
return {
|
|
94
|
+
cwd,
|
|
95
|
+
sourceDir: path.join(cwd, 'db'),
|
|
96
|
+
mode: 'root-schema',
|
|
97
|
+
file,
|
|
98
|
+
baseDir: cwd,
|
|
99
|
+
resourceName: null,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const sourceDir = inferSourceDirForSchemaFile(file);
|
|
103
|
+
const cwd = path.basename(sourceDir) === 'db' ? path.dirname(sourceDir) : fallbackCwd;
|
|
104
|
+
return {
|
|
105
|
+
cwd,
|
|
106
|
+
sourceDir,
|
|
107
|
+
mode: 'schema-file',
|
|
108
|
+
file,
|
|
109
|
+
baseDir: path.dirname(file),
|
|
110
|
+
resourceName: resourceNameForSchemaFile(file, sourceDir),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function inferSourceDirForSchemaFile(file) {
|
|
114
|
+
const segments = file.split(path.sep);
|
|
115
|
+
const dbIndex = segments.lastIndexOf('db');
|
|
116
|
+
if (dbIndex >= 0) {
|
|
117
|
+
return segments.slice(0, dbIndex + 1).join(path.sep) || path.sep;
|
|
118
|
+
}
|
|
119
|
+
return path.dirname(file);
|
|
120
|
+
}
|
|
121
|
+
function resourceNameForSchemaFile(file, sourceDir) {
|
|
122
|
+
const relative = path.relative(sourceDir, file).split(path.sep).join('/');
|
|
123
|
+
if (relative.endsWith('/index.schema.mjs') || relative.endsWith('/index.schema.js')) {
|
|
124
|
+
return path.basename(path.dirname(file));
|
|
125
|
+
}
|
|
126
|
+
return path.basename(relative).replace(/\.schema\.(?:json|jsonc|mjs|js)$/i, '');
|
|
127
|
+
}
|
|
128
|
+
function schemaLocatorError(code, message, options = {}) {
|
|
129
|
+
const error = new Error(message);
|
|
130
|
+
return Object.assign(error, {
|
|
131
|
+
code,
|
|
132
|
+
hint: options.hint,
|
|
133
|
+
details: options.details ?? {},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { SchemaField } from './fields.js';
|
|
2
|
+
type ResourceKind = 'collection' | 'document';
|
|
3
|
+
type ManifestDiagnostic = {
|
|
4
|
+
code: string;
|
|
5
|
+
severity: 'error' | 'warn' | 'info';
|
|
6
|
+
resource?: string;
|
|
7
|
+
field?: string;
|
|
8
|
+
message: string;
|
|
9
|
+
hint?: string;
|
|
10
|
+
details?: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
type SchemaResource = {
|
|
13
|
+
kind: ResourceKind;
|
|
14
|
+
name: string;
|
|
15
|
+
idField?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
fields?: Record<string, SchemaField>;
|
|
18
|
+
schemaPath?: string | null;
|
|
19
|
+
dataPath?: string | null;
|
|
20
|
+
};
|
|
21
|
+
type FieldUiManifest = Record<string, unknown> & {
|
|
22
|
+
label: string;
|
|
23
|
+
component: string;
|
|
24
|
+
readonly?: boolean;
|
|
25
|
+
optionsFrom?: string;
|
|
26
|
+
};
|
|
27
|
+
type FieldManifest = Record<string, unknown> & {
|
|
28
|
+
type: string;
|
|
29
|
+
required: boolean;
|
|
30
|
+
nullable: boolean;
|
|
31
|
+
items?: FieldManifest;
|
|
32
|
+
fields?: Record<string, unknown>;
|
|
33
|
+
variants?: Record<string, VariantManifest>;
|
|
34
|
+
ui?: FieldUiManifest;
|
|
35
|
+
};
|
|
36
|
+
type VariantManifest = Record<string, unknown> & {
|
|
37
|
+
fields: Record<string, unknown>;
|
|
38
|
+
additionalProperties?: boolean;
|
|
39
|
+
};
|
|
40
|
+
type ResourceManifest = Record<string, unknown> & {
|
|
41
|
+
kind: ResourceKind;
|
|
42
|
+
name: string;
|
|
43
|
+
fields: Record<string, unknown>;
|
|
44
|
+
description?: string;
|
|
45
|
+
idField?: string;
|
|
46
|
+
};
|
|
47
|
+
type SchemaManifest = {
|
|
48
|
+
version: 1;
|
|
49
|
+
collections: Record<string, unknown>;
|
|
50
|
+
documents: Record<string, unknown>;
|
|
51
|
+
};
|
|
52
|
+
type ResourceCustomizeContext = {
|
|
53
|
+
resource: SchemaResource;
|
|
54
|
+
resourceName: string;
|
|
55
|
+
file: string | null;
|
|
56
|
+
sourceFile: string | null;
|
|
57
|
+
defaultManifest: ResourceManifest;
|
|
58
|
+
};
|
|
59
|
+
type FieldCustomizeContext = {
|
|
60
|
+
field: SchemaField;
|
|
61
|
+
fieldName: string;
|
|
62
|
+
resource: SchemaResource;
|
|
63
|
+
resourceName: string;
|
|
64
|
+
path: string;
|
|
65
|
+
file: string | null;
|
|
66
|
+
sourceFile: string | null;
|
|
67
|
+
defaultManifest: FieldManifest;
|
|
68
|
+
};
|
|
69
|
+
type SchemaManifestConfig = {
|
|
70
|
+
cwd?: string;
|
|
71
|
+
schemaOutFile?: string | null;
|
|
72
|
+
schemaManifest?: {
|
|
73
|
+
customizeResource?: (context: ResourceCustomizeContext) => unknown;
|
|
74
|
+
customizeField?: (context: FieldCustomizeContext) => unknown;
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
} | null;
|
|
77
|
+
[key: string]: unknown;
|
|
78
|
+
};
|
|
79
|
+
type GenerateSchemaManifestOptions = {
|
|
80
|
+
project?: unknown;
|
|
81
|
+
outFile?: string | null;
|
|
82
|
+
};
|
|
83
|
+
type GenerateSchemaManifestResult = {
|
|
84
|
+
manifest: SchemaManifest;
|
|
85
|
+
content: string;
|
|
86
|
+
outFiles: string[];
|
|
87
|
+
diagnostics: ManifestDiagnostic[];
|
|
88
|
+
};
|
|
89
|
+
export declare function generateSchemaManifest(config: SchemaManifestConfig, options?: GenerateSchemaManifestOptions): Promise<GenerateSchemaManifestResult>;
|
|
90
|
+
export declare function renderSchemaManifest(resources: SchemaResource[], config?: SchemaManifestConfig): SchemaManifest;
|
|
91
|
+
export {};
|