-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
+1,580
−671
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
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
PrismaClient
type in caseClientOptions
are provided. e.g.:Why is providing client options causing type performance issues?
The
PrismaClient
interface changes dynamically based on the provided options. There are two options relevant for this behavior:log
andomit
configuration.log
configuration changes the signature of the$on
method of the client. This provides type safety so one can only observe log messages/events that are actually activated through the client options.omit
configuration changes what fields on models are available by default. This has wide reaching type implications across thePrismaClient
instance and also deep into potential client extensions.Due to those customizations a
PrismaClient
is not equal to any otherPrismaClient
. To check whether one instance is assignable to another (e.g. when passing aPrismaClient
into 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
typeof
operator to avoid redundant type resolution. Through this the user can give a specificPrismaClient
instance 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
omit
orlog
configuration.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
omit
andlog
are actually necessary. To achieve thisPrismaClient
itself was changed to not depend on a genericClientOptions
but on separateLogOpts
andOmitOpts
. These are extracted from theClientOptions
in the constructor.Now two type instances of
PrismaClient
are only different if theirLogOpts
orOmitOpts
are 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 providinglog
configuration causes way less type check overhead than before. Only withomit
configuration provided one still pays a high complexity price (although that also improved significantly!).This can be an acceptable tradeoff as
omit
client options is a more rarely used feature and we can extend the documentation to encourage users to use thetypeof
workaround when usingomit
client 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
PrismaClient
generic 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
PrismaClient
type. 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 thetypeof
operator, 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 theglobalOmit
test in particular I duplicated the test to have one test run on the new generator only and the other on the old generator only.