refactor(schema): widen types for attributes, default, and foreignKeyFor#2482
refactor(schema): widen types for attributes, default, and foreignKeyFor#2482
Conversation
…For in generated schema The generated schema previously used `as const` on entire model/enum/typeDef objects, causing TypeScript to deeply infer literal types for all nested properties. This is unnecessary for `attributes`, `default`, and `foreignKeyFor` which are only used at runtime, not in CRUD type computations. Changes: - Add type assertions (`as readonly AttributeApplication[]`, `as FieldDefault`, `as readonly string[]`) to prevent deep const inference on these properties - Extract `FieldDefault` type alias from `FieldDef` for cleaner generated code - Change `FieldHasDefault` to use key existence check (`'default' extends keyof`) instead of value type check, enabling the `default` widening - Conditionally import `AttributeApplication` and `FieldDefault` only when used Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @ymc9's task —— View job Code Review for PR #2482Reviewing the schema type assertion refactoring changes. Review Progress
SummaryThis PR refactors the schema generation to add explicit type assertions for Code Quality and Best Practices ✅Excellent implementation with the following strengths:
Potential Issues
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAdds a public Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/schema/src/schema.ts (1)
290-296:⚠️ Potential issue | 🟠 Major
FieldHasDefaultkey-check can over-report defaults for widened field types.Using
'default' extends keyof ...can evaluate totruefor structurally widened field defs (for example,FieldDef-shaped types), which can incorrectly mark fields as defaulted in downstream create/query typings.💡 Safer type check
export type FieldHasDefault< Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, -> = 'default' extends keyof GetModelField<Schema, Model, Field> +> = GetModelField<Schema, Model, Field> extends { default: FieldDefault } ? true : GetModelField<Schema, Model, Field>['updatedAt'] extends true | UpdatedAtInfo ? true : GetModelField<Schema, Model, Field>['relation'] extends { hasDefault: true } ? true : false;#!/bin/bash # Verify definition + downstream usage paths that rely on FieldHasDefault semantics rg -n -C3 "export type FieldDef|default\\?:|export type FieldHasDefault|default' extends keyof" packages/schema/src/schema.ts rg -n -C4 "FieldHasDefault<" packages/orm/src/client/crud-types.ts packages/orm/src/client/query-builder.ts🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/schema/src/schema.ts` around lines 290 - 296, FieldHasDefault is over-reporting defaults because the current check "'default' extends keyof GetModelField<Schema, Model, Field>" matches structurally similar/widened types; instead change the presence test to an explicit own-key check (e.g. make the branch use a conditional like GetModelField<Schema, Model, Field> extends { default?: unknown } ? true : ...) or introduce a small helper HasOwnKey<T, K> and use that inside FieldHasDefault; update the FieldHasDefault definition (and any helper) so the other branches (updatedAt and relation.hasDefault) remain the same and the new test only returns true when the field actually declares a default key on that exact field type.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/sdk/src/ts-schema-generator.ts`:
- Around line 73-76: The per-target import flags (usedAttributeApplication,
usedFieldDefault, usedExpressionUtils) are being reset only once and thus leak
across multiple targets; update generateSchema (and any surrounding methods in
ts-schema-generator.ts that iterate targets) to reset these flags inside the
per-target loop (before emitting each target) so each target's usage is tracked
independently, and remove or relocate the earlier single-time resets
(references: usedAttributeApplication, usedFieldDefault, usedExpressionUtils) to
ensure conditional imports are computed per target rather than globally.
---
Outside diff comments:
In `@packages/schema/src/schema.ts`:
- Around line 290-296: FieldHasDefault is over-reporting defaults because the
current check "'default' extends keyof GetModelField<Schema, Model, Field>"
matches structurally similar/widened types; instead change the presence test to
an explicit own-key check (e.g. make the branch use a conditional like
GetModelField<Schema, Model, Field> extends { default?: unknown } ? true : ...)
or introduce a small helper HasOwnKey<T, K> and use that inside FieldHasDefault;
update the FieldHasDefault definition (and any helper) so the other branches
(updatedAt and relation.hasDefault) remain the same and the new test only
returns true when the field actually declares a default key on that exact field
type.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e5352de2-45be-4466-bd79-261e04241868
📒 Files selected for processing (32)
packages/clients/tanstack-query/test/schemas/basic/schema-lite.tspackages/schema/src/schema.tspackages/sdk/src/ts-schema-generator.tssamples/next.js/zenstack/schema-lite.tssamples/next.js/zenstack/schema.tssamples/nuxt/zenstack/schema-lite.tssamples/nuxt/zenstack/schema.tssamples/orm/zenstack/schema.tssamples/sveltekit/src/zenstack/schema-lite.tssamples/sveltekit/src/zenstack/schema.tstests/e2e/apps/rally/zenstack/schema.tstests/e2e/github-repos/cal.com/schema.tstests/e2e/github-repos/formbricks/schema.tstests/e2e/github-repos/trigger.dev/schema.tstests/e2e/orm/plugin-infra/ext-query-args/schema.tstests/e2e/orm/schemas/auth-type/schema.tstests/e2e/orm/schemas/basic/schema.tstests/e2e/orm/schemas/default-auth/schema.tstests/e2e/orm/schemas/delegate/schema.tstests/e2e/orm/schemas/json/schema.tstests/e2e/orm/schemas/name-mapping/schema.tstests/e2e/orm/schemas/omit/schema.tstests/e2e/orm/schemas/petstore/schema.tstests/e2e/orm/schemas/procedures/schema.tstests/e2e/orm/schemas/todo/schema.tstests/e2e/orm/schemas/typed-json/schema.tstests/e2e/orm/schemas/typing/schema.tstests/regression/test/issue-204/schema.tstests/regression/test/issue-422/schema.tstests/regression/test/issue-503/schema.tstests/runtimes/bun/schemas/schema.tstests/runtimes/edge-runtime/schemas/schema.ts
|
Claude finished @ymc9's task —— View job Code Review for PR #2482Reviewing the schema type assertion refactoring changes. Review Progress
SummaryThis PR refactors the schema generation to add explicit type assertions for Code Quality and Best Practices ✅Excellent implementation with the following strengths:
Critical Issues
|
Summary
as readonly AttributeApplication[],as FieldDefault,as readonly string[]) onattributes,default, andforeignKeyForproperties in generated schema to prevent unnecessary deep const inference by TypeScriptFieldDefaulttype alias fromFieldDeffor cleaner generated codeFieldHasDefaultto use key existence check instead of value type check, enabling thedefaulttype wideningAttributeApplicationandFieldDefaultonly when used in generated schemaTest plan
samples/orm) type-checks with no errors🤖 Generated with Claude Code
Summary by CodeRabbit