P
Prisma3mo ago
acidjazz

using prisma $extends to add fields to a model, whats the best practice for TS?

Below is how i add a custom "client" field to my tokens model - but now my Token model doesn't really match since there is no client field in that. What is the best practice for taking care of that?
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton().$extends({
name: 'tokenParser',
result: {
token: {
client: {
needs: {
agent: true,
},
compute({ agent }) {
return new UAParser(agent).getResult()
},
},
},
},
})
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton().$extends({
name: 'tokenParser',
result: {
token: {
client: {
needs: {
agent: true,
},
compute({ agent }) {
return new UAParser(agent).getResult()
},
},
},
},
})
5 Replies
acidjazz
acidjazz3mo ago
anyone know the proper way of doing this?
Ustice
Ustice3mo ago
You should be able to get the client property on the type returned by a query.
function fetchTokenClient () {
return prisma.token.findFirstOrThrow()
}

type TokenWithClient = Prisma.PromiseReturnType<typeof fetchTokenClient>
function fetchTokenClient () {
return prisma.token.findFirstOrThrow()
}

type TokenWithClient = Prisma.PromiseReturnType<typeof fetchTokenClient>
Ustice
Ustice3mo ago
You can read about this in the docs.
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
acidjazz
acidjazz3mo ago
well, the way i'm doing/asking this, i'm doing an $extend, so i want it in Token . it will always be Token. I'm not just doing a query with an include
Ustice
Ustice3mo ago
That’s what I am saying. You can redefine the types that are generated by Prisma; however, it is possible to extract the types from the Prisma client, and use those instead.
Want results from more Discord servers?
Add your server