W
Wasp•4w ago
4jandals

Is there a recommended way to add computed fields to a model?

For example, let's say I have a Person entity with a dateOfBirth, and I want to derive age in the model so I can reuse that across the UI. I think Prisma client supports compute, but not sure how to use that in Wasp? Or maybe there is a better way?
3 Replies
kapa.ai
kapa.ai•4w ago
Hi @4jandals. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, there isn't specific information about adding computed fields to models in Wasp. While Wasp uses Prisma as its ORM, the documentation doesn't explicitly cover how to implement computed fields like an age derived from a date of birth. There are a few approaches you might consider: 1. Using Prisma directly: You can access the Prisma client directly in your server code as mentioned in the documentation:
import { prisma } from 'wasp/server'

// You can then use Prisma's API directly
import { prisma } from 'wasp/server'

// You can then use Prisma's API directly
Wasp documentation shows that you can import and use the Prisma client for more control when needed. 2. Custom Query: You could create a custom Query operation that calculates the age when fetching the Person data. 3. Enriching user objects: There's an open issue (#1554) about allowing additional information to be added to user objects, which suggests this is a recognized need in the Wasp ecosystem. However, the knowledge sources don't provide a specific, recommended way to implement computed fields in Wasp models. If you need this functionality, you might want to ask in the Wasp Discord community for more specific guidance or check the latest Prisma documentation for computed field patterns that you could implement in your Wasp application. I'd recommend exploring the direct Prisma client approach first, as that gives you the most flexibility to implement Prisma features that might not be directly exposed through Wasp's abstractions yet. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @4jandals, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•4w ago
I think Prisma client supports compute, but not sure how to use that in Wasp? Or maybe there is a better way?
Could you give me an example piece of code how you did it with Prisma before? I haven't really used computed field to be honest 🙂
miho
miho•4w ago
Looking at the docs: https://www.prisma.io/docs/orm/prisma-client/queries/computed-fields I see that you'd need access to the PrismaClient which is not something you can do with Wasp... but we have an issue open for this: https://github.com/wasp-lang/wasp/issues/2116
Computed fields | Prisma Documentation
This page explains how to use client extensions to add computed fields to Prisma models.

Did you find this page helpful?