Thanks to @bill karvin comment There is no support

https://github.com/prisma/prisma/issues/6336

Answer from Bernardao on Stack Overflow
🌐
Prisma
prisma.io › home › computed fields › computed fields › computed fields › computed fields › computed fields
Computed fields | Prisma Documentation
In your database, you may only store the first and last name, but you can define a function that computes a full name by combining the first and last name. Computed fields are read-only and stored in your application's memory, not in your database.
Discussions

mysql - How to create computed/calculated field in Prisma? - Stack Overflow
It is possible to create the gamesPlayed field "on the fly" by the database? What should be the code in the schema.prisma file? More on stackoverflow.com
🌐 stackoverflow.com
How to add a column of computed fields and filter by that column
I would like to use Prisma to get filtered rows by reference to a field containing the computed results, but I could not figure out how to do this. If I want these results, do I need to use a raw q... More on github.com
🌐 github.com
4
3
Help creating computed fields
Hi! im trying to do an extension of PrismaClient so I can add a computed field. each pageItem has a relation of many-to-many with products, and each product have a type. I wish to generate a computed field named "isMerch" that its true when all the products it contain are of type "MERCH". More on answeroverflow.com
🌐 answeroverflow.com
December 19, 2024
Is it possible to use Prisma ORM computed fields for filtering?
You can use and + contains filters on firstName and lastName and js to split fullName into firstName and lastName and then pass them into filters More on reddit.com
🌐 r/nestjs
7
4
January 11, 2025
🌐
Prisma
prisma.io › home › add custom fields and methods to query results › add custom fields and methods to query results › add custom fields and methods to query results › add custom fields and methods to query results
Prisma Client extensions: result component | Prisma Documentation
These fields are computed at runtime and are type-safe. In the following example, we add a new virtual field called fullName to the user model. const prisma = new PrismaClient().$extends({ result: { user: { fullName: { // the dependencies needs: ...
🌐
GitHub
github.com › prisma › prisma › discussions › 16718
How to add a column of computed fields and filter by that column · prisma/prisma · Discussion #16718
To work around this limitation, you can use a common table expression (CTE) or a subquery to calculate the field and then filter based on the result · const result = await prisma.$queryRaw` SELECT * FROM ( SELECT *, your_calculated_field + 10 AS modified_calculated_field FROM YourModel ) AS subquery WHERE modified_calculated_field = ${yourCondition}; `;
Author   prisma
🌐
Prisma
prisma.io › blog › client-extensions-preview-8t3w27xkrxxn
Prisma Client Extensions: 15 Practical Examples
December 19, 2022 - Computed fields must specify which other fields they depend on, and they may be composed / reused by other computed fields. ... This example shows how to use a Prisma Client extension to transform fields in results returned by Prisma queries.
🌐
Answer Overflow
answeroverflow.com › m › 1319424725767819264
Help creating computed fields - Prisma
December 19, 2024 - export const prismaClient = new PrismaClient() .$extends({ result: { pageItem: { isMerch: { needs: { productRelations: true }, compute(pageItem){ return pageItem.products.reduce((prev, curr) => prev || curr.type=='MERCH', true); } }, } } }); The first issue I encounter is on the needs field, how can I use relations?
Find elsewhere
🌐
Reddit
reddit.com › r/nestjs › is it possible to use prisma orm computed fields for filtering?
r/nestjs on Reddit: Is it possible to use Prisma ORM computed fields for filtering?
January 11, 2025 -

Hey, I've been researching about this but don't know if this is possible. I have a table with a firstName and lastName fields and I want to get the full name. That seems possible with a computed field. But I also want to filter based on this, for example, using { fullName: { contains: 'something' } }. To my understanding, this wouldn't be possible with a computed field because it would only exist until a query is performed and there's available data, so you couldn't use it as a filter from the beginning. Anyways, I didn't manage to get the computed fields working on my PrismaService, it would keep telling me that I couldn't select fullName as a field to be retrieved in the query, but if using those fields for filtering is possible, then I would keep trying.

If not, what's the solution you would use? This is a pretty common case, so I'm surprised by not finding simple solutions that don't involve using raw queries to filter based on the full name.

Thanks in advance!

🌐
GitHub
github.com › prisma › prisma-client-extensions › tree › main › computed-fields
prisma-client-extensions/computed-fields at main · prisma/prisma-client-extensions
These fields are not included in the database, but rather are computed at runtime. Computed fields are type-safe and may return anything from simple values to complex objects, or even functions that can act as methods for your models.
Author   prisma
Top answer
1 of 1
1

According to this prisma contributor in this issue

This is works as intended. Purpose of result extensions is to add synchronously computed field to the model with minimum overhead, so it can be queried implicitly

Also the official doc specifies

For performance reasons, Prisma Client computes results on access, not on retrieval.

I've struggles too with this limitation of prisma and my solution was to simply return an async function as the result of the compute method. Then the value can be computed when needed.

const prisma = new PrismaClient().$extends({
  result: {
    user: {
      getNameAndAge: {
        needs: { name: true, age: true },
        compute(user) {
          return async () => (`${user.name} (${user.age}y) ${await getdata()}`)
        },
      },
    },
  },
})

// ...when needed

const joeNameAndAge = await users[0].getNameAndAge()

On the other hand you can define a custom specific for that model that queries and manipulate the data as you need.

You can find more here.

export type UserFindManyWithData = {
  where: Prisma.UserWhereInput
  select: Prisma.UserSelect
}

const prisma = new PrismaClient().$extends({
  model: {
    user: {
      async findManyWithData({ where, select }: UserFindManyWithData) {
        const users = await prisma.user.findMany({ where, select })
                
        // This is not the best strategy for performance, ideally you would like to await all promises at once, but it depends on your project
        for (const user of users) {
          user.nameAndAge = `${user.name} (${user.age}y) ${await getdata()}`
        }
        
        return users
      },
    },
  },
})
🌐
Nexus Schema
nexusjs.org › docs › plugins › prisma › overview
Overview
Note: t.crud is an experimental feature. You must explicitly enable it via the plugin options. ... You can add computed fields to a GraphQL object using the standard GraphQL Nexus API.
🌐
GitHub
github.com › prisma › prisma › issues › 19548
PCE: Allow result extensions fields to depend on relational data · Issue #19548 · prisma/prisma
May 31, 2023 - Extending a result with a computed field that needs data from a related model I noticed that the when defining a result extension we are limited to scalar fields of the owning entity. We were really hoping to move an n+1 problem for comp...
Author   prisma
🌐
GitHub
github.com › prisma › prisma › discussions › 19947
Extracting and using type of models with computed fields from an extended client. · prisma/prisma · Discussion #19947
Is there a way for us to get and use a type of a model with computed field of an extended client? ... export const prismaAuction = prisma.$extends({ result: { auction: { started: { needs: { start_date: true }, compute(auction) { return DateTime.fromJSDate(auction.start_date) <= DateTime.now() } }, ended: { needs: { end_date: true }, compute(auction) { console.log(DateTime.fromJSDate(auction.end_date)) return DateTime.fromJSDate(auction.end_date) <= DateTime.now() } }, ongoing: { needs: { end_date: true, start_date: true}, compute(auction) { return DateTime.fromJSDate(auction.end_date) > DateTime.now() && DateTime.fromJSDate(auction.start_date) <= DateTime.now() } } }, } });
Author   prisma
🌐
GitHub
github.com › prisma › prisma › issues › 14793
Prisma client - computed fields v2 · Issue #14793 · prisma/prisma
Problem I often select user's firstName and lastName to just concat it or password to tell if user has it and then cut off from the rest of response Suggested solution Computed fields For example i'll use this user model: model User { id...
Author   prisma
🌐
Prisma Playground
playground.prisma.io › examples › advanced › prisma-client-extensions › computed-fields
Prisma Playground | Learn the Prisma ORM in your browser
The Playground is an interactive learning environment for Prisma. Learn how to send database queries and explore migrations workflows with the Prisma ORM.
🌐
GitHub
github.com › prisma › prisma1 › issues › 5069
Add computed fields for complex data types · Issue #5069 · prisma/prisma1
April 13, 2020 - Problem It is not possible to create a computed field with complex data. With native data types it is no problem, but with complex data types, Prisma says that there must be a relation. type User { id: ID! @unique stories: [Story]! @rela...
Author   prisma
🌐
GitHub
github.com › prisma › prisma › discussions › 21773
Prisma Extension: Access prisma model's related fields while adding computed fields · prisma/prisma · Discussion #21773
i want to add 'totalWeight" computed field to the Box model by calculating sum of the "weight" field of each related Item.
Author   prisma
🌐
ZenStack
zenstack.dev › orm › computed fields
Computed Fields | ZenStack
Prisma client extensions allow you to define computed fields.