[Proposal] Prisma Client Extensions
Problem
Client Extensions Proposal Hey folks, we started designing this feature and we’re ready to share our proposal. Please let us know what you think! Design We aim to provide you with a type-safe way to extend your Prisma Client to support many new use-cases and also give a way to express your creativity. We plan to work on four extension layers which are `$result`, `$model`, `$client`, `$use`. Let’s consider the following model: [code block] Computed fields We have this database model but we want to “augment” it at runtime. We want to add fields to our query results, have them computed at runtime, and let our own types flow through. To do this, we use `$result` to extend the results: [code block] We just extended our `User` model results with a new field called `fullName`. To do that, we defined our field with `compute`, where we expressed our field dependencies and computation logic. [code block] Result extensions will never over-fetch. It means that we only compute if we are able to: [code block] Finally, you will be able to add fields to all your model results at once via a generic call using `$all` instead of a model name: [code block] Results are never computed ahead of time, but only on access for performance reasons. Model methods Extending the results is useful, but we would also love to store some custom logic on our models too… so that we can encapsulate repetitive logic, or business logic. To do this, we want to use the new `$model` extension capabil
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Prisma Client Extensions for Computed Fields and Model Methods
The current Prisma Client lacks a type-safe mechanism to extend query results and encapsulate business logic, which limits flexibility and reusability in applications. The proposed extensions aim to enhance the client by allowing developers to add computed fields and custom model methods dynamically.
Awaiting Verification
Be the first to verify this fix
- 1
Define Computed Fields Using $result
Create computed fields in your Prisma Client by utilizing the $result extension. This allows you to augment your query results with additional fields that are computed at runtime based on existing data.
typescriptconst userWithFullName = await prisma.user.findMany({ select: { id: true, firstName: true, lastName: true, $result: { fullName: { compute: (user) => `${user.firstName} ${user.lastName}` } } } }); - 2
Implement $model for Custom Logic
Use the $model extension to add custom methods to your Prisma models. This allows you to encapsulate repetitive or business logic directly within the model, enhancing code organization and reusability.
typescriptprisma.$model('User').methods({ getFullName() { return `${this.firstName} ${this.lastName}`; } }); - 3
Utilize $all for Batch Extensions
To apply extensions to all models at once, use the $all extension. This can simplify the process of adding computed fields across multiple models without needing to specify each model individually.
typescriptprisma.$all({ $result: { fullName: { compute: (user) => `${user.firstName} ${user.lastName}` } } }); - 4
Test and Validate Extensions
After implementing the extensions, run tests to ensure that computed fields and model methods function as intended. Verify that the computed fields return the expected results and that custom methods execute without errors.
typescriptconst user = await prisma.user.findFirst(); console.log(user.getFullName()); // Should output the full name correctly
Validation
Confirm the fix by running queries that utilize the new computed fields and model methods. Ensure that the output matches expected results and that no errors occur during execution.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep