Skip to content

fix: improve support for new prisma-client generator #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 7, 2025

Conversation

genu
Copy link
Contributor

@genu genu commented Jul 5, 2025

Fixes #2168

Improves support for new prisma-client generator.

@ymc9 What I think needs some attention, which I wasn't able to figure out. The enhance.ts that is generated imports these:

import type { InternalArgs, DynamicClientExtensionThis } from '../prisma/client/runtime/library';

But I don't believe these exist anymore with the new prisma-client or they are relocated. I'm not exactly sure here.

This impacts the types when we do:

const db = enhance(prisma) //<-- db is any

Copy link
Contributor

coderabbitai bot commented Jul 5, 2025

📝 Walkthrough

Walkthrough

The changes update the processClientTypesNewPrismaGenerator method to generate a shared json-types.ts file containing all JSON type definitions once per generation. Individual model type files now conditionally import $Types from runtime.Types and only the relevant JSON type aliases from this shared file when JSON fields are present. The import paths for Prisma runtime types are also fixed.

Changes

File(s) Change Summary
packages/schema/src/plugins/enhancer/enhance/index.ts Modified processClientTypesNewPrismaGenerator to generate and save a shared json-types.ts with all JSON types before processing models; added conditional imports of $Types and JSON type aliases in model files; updated import paths for Prisma runtime types; changed generateExtraTypes method to synchronous.
tests/regression/tests/issue-2168.test.ts Added a regression test for issue #2168 verifying TypeScript types generation for JSON fields with the new Prisma client generator.

Sequence Diagram(s)

sequenceDiagram
    participant EnhancerGenerator
    participant FileSystem
    participant ModelProcessor

    EnhancerGenerator->>FileSystem: Generate shared json-types.ts with all JSON type definitions
    loop For each model file
        EnhancerGenerator->>ModelProcessor: Extract type aliases and JSON field usage
        ModelProcessor-->>EnhancerGenerator: Return usage details
        EnhancerGenerator->>FileSystem: Generate model type file
        alt $Types used
            EnhancerGenerator->>FileSystem: Add import for $Types from runtime.Types
        end
        alt JSON type aliases used
            EnhancerGenerator->>FileSystem: Add import for relevant JSON types from json-types.ts
        end
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Generate TypeScript types for JSON fields when using the new prisma-client generator (#2168)
Call generateExtraTypes for the new Prisma client generator to create JSON type definitions
Ensure generated model files import relevant JSON types and $Types from shared definitions

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Addition of regression test file tests/regression/tests/issue-2168.test.ts Testing is not part of the coding objectives but supports validation of the fix for issue #2168.

Possibly related PRs

  • zenstackhq/zenstack#2151: Extends processClientTypesNewPrismaGenerator to share JSON type definitions, closely related to this PR’s JSON type handling.
  • zenstackhq/zenstack#1996: Focuses on identifying models with JSON fields and fixing JSON type generation using regex patterns, complementary to this PR’s centralization of JSON types.
  • zenstackhq/zenstack#1874: Refactors source file handling and saving in EnhancerGenerator, related to the generation flow changes in this PR.

Warning

Review ran into problems

🔥 Problems

Check-run timed out after 90 seconds. Some checks/pipelines were still in progress when the timeout was reached. Consider increasing the reviews.tools.github-checks.timeout_ms value in your CodeRabbit configuration to allow more time for checks to complete.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 146fa96 and 42e5dfe.

📒 Files selected for processing (2)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (5 hunks)
  • tests/regression/tests/issue-2168.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/schema/src/plugins/enhancer/enhance/index.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/regression/tests/issue-2168.test.ts (1)
packages/testtools/src/schema.ts (1)
  • loadSchema (173-379)
⏰ 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). (6)
  • GitHub Check: build-test (20.x)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: build-test (20.x)
  • GitHub Check: dependency-review
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
tests/regression/tests/issue-2168.test.ts (1)

1-46: Well-structured regression test for JSON type handling.

This test effectively validates the fix for issue 2168 by:

  • Testing the new prisma-client generator with JSON fields
  • Verifying that generated types can be imported and used in TypeScript
  • Using appropriate test configuration with custom output paths
  • Including compilation to catch type-related issues

The test structure and schema definition are correct and align well with the PR objectives.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 02f458d and 5773049.

📒 Files selected for processing (1)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: dependency-review
  • GitHub Check: build-test (20.x)
🔇 Additional comments (3)
packages/schema/src/plugins/enhancer/enhance/index.ts (3)

124-127: LGTM! Good approach to centralize JSON field types.

The conditional export of the json-fields module for the new Prisma client generator aligns well with the centralized type definition strategy.


497-510: Well-structured centralization of JSON type definitions.

Creating a shared json-fields.ts file is a good architectural decision that reduces duplication and improves maintainability. The implementation correctly generates all TypeDef types in one place.


523-545: Smart conditional import logic for $Types.

The implementation thoroughly checks for $Types usage both in the original type and after transformation, ensuring the import is added only when necessary. This prevents missing imports while avoiding unnecessary ones.

@ymc9
Copy link
Member

ymc9 commented Jul 7, 2025

DynamicClientExtensionThis

Thanks for making the fix @genu ! These types can indeed be imported directly from "@prisma/client/runtime/library" instead. The code changes look good to me, and I'll make some minor tweaks and add a test then merge.

Thank you!

- Change the file name containing json types from "json-fields.ts" to "json-types.ts"
- Fixed the import path of `DynamicClientExtensionThis` etc. to "@prisma/client/runtime/library"
- Removed an unnecessary async modifier
- Added a regression test case
@ymc9 ymc9 merged commit a60ae2a into zenstackhq:dev Jul 7, 2025
12 checks passed
@genu genu deleted the fix/add-new-prisma-generator branch July 7, 2025 03:27
@genu
Copy link
Contributor Author

genu commented Jul 7, 2025

DynamicClientExtensionThis

Thanks for making the fix @genu ! These types can indeed be imported directly from "@prisma/client/runtime/library" instead. The code changes look good to me, and I'll make some minor tweaks and add a test then merge.

Thank you!

@ymc9 From my observation, this partly resolved the issue.

type inference is lost when using prisma extensions:

const prisma = new PrismaClient()
const db = enhance(prisma)

db.user.find() // <-- db.user is properly typed

With Enhanced client:

const prisma = new PrismaClient().$extends({})

const db = enhance(prisma)

db.user.find() // <-- db.user is any

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.

2 participants