-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: ORM-850 improve client options type performance #27777
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
size-limit report 📦
|
fbd2d00 to
c7ae293
Compare
…generated files pre typecheck
FGoessler
commented
Aug 6, 2025
FGoessler
commented
Aug 6, 2025
aqrln
reviewed
Aug 6, 2025
aqrln
reviewed
Aug 8, 2025
aqrln
reviewed
Aug 8, 2025
aqrln
reviewed
Aug 8, 2025
aqrln
approved these changes
Aug 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR improves the complex type resolution of the
PrismaClienttype in caseClientOptionsare provided. e.g.:Why is providing client options causing type performance issues?
The
PrismaClientinterface changes dynamically based on the provided options. There are two options relevant for this behavior:logandomitconfiguration.logconfiguration changes the signature of the$onmethod of the client. This provides type safety so one can only observe log messages/events that are actually activated through the client options.omitconfiguration changes what fields on models are available by default. This has wide reaching type implications across thePrismaClientinstance and also deep into potential client extensions.Due to those customizations a
PrismaClientis not equal to any otherPrismaClient. To check whether one instance is assignable to another (e.g. when passing aPrismaClientinto a helper method) TypeScript has to perform some very deep checks that run through deep and complex generics. If no client options are provided TypeScript can usually "short circuit" and is very efficient - once client options are provided way more work has to be done.Existing workarounds
We have a known workaround to use the
typeofoperator to avoid redundant type resolution. Through this the user can give a specificPrismaClientinstance an "named alias" and use this in their code. TypeScript then does not have to repeatedly check full type assignability but can rely on the equality of the named alias.Approach of this PR
Currently any variant of client options triggers the full complexity type check and prevents TypeScript from taking the "short circuit" route. Even if the provided client options have no influence on the actual type interface when they specify no
omitorlogconfiguration.The idea is now to modify the type of the PrismaClient such that the type performance penalty is only paid if the complex checks for
omitandlogare actually necessary. To achieve thisPrismaClientitself was changed to not depend on a genericClientOptionsbut on separateLogOptsandOmitOpts. These are extracted from theClientOptionsin the constructor.Now two type instances of
PrismaClientare only different if theirLogOptsorOmitOptsare actually different. For most cases the type check is now way simpler and can go the "short circuit" routes. As the dependencies are now smaller forLogOpts, just providinglogconfiguration causes way less type check overhead than before. Only withomitconfiguration provided one still pays a high complexity price (although that also improved significantly!).This can be an acceptable tradeoff as
omitclient options is a more rarely used feature and we can extend the documentation to encourage users to use thetypeofworkaround when usingomitclient options.You can see the magnitude of improvements in the updated type benchmark instantiation counts. In some cases for large schemas we went from about 13 million to below 1k instantiations!
Change in generic type signature
Warning
This change comes with one caveat: As the
PrismaClientgeneric arguments type signature had to change its a technically breaking change.Which should be okay as this generator is still in preview and I would not expect many users to explicitly specify generic type params on their
PrismaClienttype. Even if so it is easy to work around and resolve. Ideally by not specifying the generic type arguments but letting them get inferred, using thetypeofoperator, or in worst case updating the generic arguments as shown below.Log Level Case:
Omit Case:
Might be bigger issue for some library developers though? 🤔
It is also an issue in our test infrastructure as we have a few client tests that need explicitly specified
LogOpts. The same tests run on the new and the old generator though. So with this change we had to introduce complexity to change the test code based on what generator it is supposed to run with. For theglobalOmittest in particular I duplicated the test to have one test run on the new generator only and the other on the old generator only.