Skip to content

Conversation

@genu
Copy link
Contributor

@genu genu commented Nov 19, 2025

Fixes #422

Summary by CodeRabbit

  • Refactor

    • Enhanced auth type handling to properly support nested relations and array types in type definitions.
  • API Changes

    • Exported utility type for improved consistency across ORM type definitions.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Nov 19, 2025

Walkthrough

The PR updates auth type composition in the ORM client to include relation fields. It introduces an internal AuthModelType that recursively expands relation fields with proper array handling, refines the AuthType definition to use this new type when authType is a model, and exports DefaultModelResult for public use.

Changes

Cohort / File(s) Summary
Auth Type Infrastructure
packages/orm/src/client/contract.ts
Expanded type imports to include FieldIsArray, RelationFields, RelationFieldType, and SchemaDef. Introduced internal AuthModelType<Schema, Model> that recursively composes auth context with scalar and relation fields, properly handling array relations. Updated AuthType to use AuthModelType when Schema['authType'] resolves to a model.
Type Exports
packages/orm/src/client/crud-types.ts
Promoted DefaultModelResult from internal type alias to publicly exported type by adding export modifier in the Query results region.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay close attention to the recursive AuthModelType type composition logic, particularly how it traverses relation fields and respects array relations
  • Verify the conditional handling of FieldIsArray correctly wraps array relations in brackets
  • Confirm the refinement of AuthType properly delegates to AuthModelType and maintains backward compatibility with existing type branches

Possibly related PRs

Poem

🐰 Relations now dance in auth's embrace,
Recursion traces each nested space,
Arrays bend true, scalars align,
This fuzzy coder's work is fine! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: enhance auth type' clearly summarizes the main change: enhancing the auth type system to support relations in auth models, which directly addresses the linked issue #422.
Linked Issues check ✅ Passed The pull request fully addresses issue #422 by enabling relation fields in auth type. Changes include exporting DefaultModelResult, introducing AuthModelType to recursively handle relation fields with array support, and updating AuthType to use this new type when authType is a model.
Out of Scope Changes check ✅ Passed All changes are directly scoped to supporting relation fields in auth types. The modifications to contract.ts and crud-types.ts focus exclusively on type definitions for auth handling without introducing unrelated alterations.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between df08bad and d3c9e21.

📒 Files selected for processing (2)
  • packages/orm/src/client/contract.ts (3 hunks)
  • packages/orm/src/client/crud-types.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/orm/src/client/crud-types.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-21T16:09:31.218Z
Learnt from: ymc9
Repo: zenstackhq/zenstack-v3 PR: 319
File: packages/runtime/src/client/executor/zenstack-query-executor.ts:63-72
Timestamp: 2025-10-21T16:09:31.218Z
Learning: In ZenStack, TypeDefs can be inherited by models. When a TypeDef contains fields with `map` attributes, those mapped field names need to be processed by the QueryNameMapper since they become part of the inheriting model's schema. Therefore, when checking if a schema has mapped names (e.g., in `schemaHasMappedNames`), both `schema.models` and `schema.typeDefs` must be inspected for `@map` and `map` attributes.

Applied to files:

  • packages/orm/src/client/contract.ts
🧬 Code graph analysis (1)
packages/orm/src/client/contract.ts (2)
packages/schema/src/schema.ts (5)
  • SchemaDef (11-19)
  • GetModels (120-120)
  • RelationFields (210-214)
  • FieldIsArray (249-253)
  • RelationFieldType (222-229)
packages/orm/src/client/crud-types.ts (1)
  • DefaultModelResult (50-69)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build-test (20.x, sqlite)
  • GitHub Check: build-test (20.x, postgresql)
🔇 Additional comments (3)
packages/orm/src/client/contract.ts (3)

2-10: LGTM! Import additions support the enhanced auth type.

The new imports are exactly what's needed for the recursive auth type construction. The inclusion of DefaultModelResult follows the previous review feedback and is the correct choice for extracting scalar fields.

Also applies to: 23-23


834-839: LGTM! AuthType now uses the enhanced AuthModelType.

The change from ModelResult to AuthModelType enables the recursive inclusion of relations, completing the fix for issue #422. The conditional structure is preserved correctly.


822-832: Type implementation correctly enables nested auth context with relations.

The recursive AuthModelType design directly addresses the reported use case. Tests confirm $setAuth now accepts nested relation objects like $setAuth({ id: '...', profile: { image: { url } } }), which was previously unsupported.

The implementation correctly:

  • Wraps DefaultModelResult in Partial for optional scalar fields
  • Maps over RelationFields to recursively include relations
  • Uses FieldIsArray to distinguish array vs. single relations

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@genu genu changed the base branch from main to dev November 19, 2025 16:29
@genu genu marked this pull request as draft November 19, 2025 16:37
@genu genu marked this pull request as ready for review November 19, 2025 16:37
@genu
Copy link
Contributor Author

genu commented Nov 19, 2025

The target branch was initially set to main, I'm not sure how to get coderabbitai to reprocess the PR as most of its summaries are wrong.

Copy link
Member

@ymc9 ymc9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @genu , thanks for making the fix! Please check my comments.

@genu
Copy link
Contributor Author

genu commented Nov 20, 2025

@ymc9 I updated the solution to use DefaultModelResult. Let me know if this is what you had in mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting the auth doesn't include relations

2 participants