Repository navigation
Replies: 5 comments
|
Hello @carlskii 👋 You can perform an async operation inside a compute function that is provided by result client extension. Here's an example. |
|
I think I've worked this out, but not quite. Here's the code I'm using: Basically, I'm importing my custom function that takes the user name from the row's returned from the database and gets a certificate for each of them. However, its an async function so I can't "await" using const prisma = new PrismaClient()
.$extends({
result: {
user: {
fullName: {
needs: { firstName: true, lastName: true },
compute(user) {
return `${user.firstName} ${user.lastName}`;
},
},
},
},
})
.$extends({
result: {
user: {
displayName: {
needs: { fullName: true, email: true },
compute(user) {
const cert = await getCert()
return `${user.fullName} <${user.email}>, ${cert} `;
},
},
},
},
}); |
|
So simplifying things a little. I think my question is "how to use an async function. This what I've now tried, but Typescript only allows 'await' expressions within async functions and at the top levels of modules. I appreciate this is probably more of a Typescript issue but I'm fairly new to this. async function carl() {
return "carl is cool!"
}
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient().$extends({
result: {
user: {
// Add a computed field called `nameAndAge` to the user
nameAndAge: {
needs: {
name: true,
age: true,
country: true,
},
compute(user) {
return `${user.name} (${user.age}y), ${user.country}` + await carl();
},
},
},
},
});
const user = await prisma.user.findFirst();
console.log(user);I can get the output from the standard function into the result but no the async function. |
|
Any thoughts on this - is it possible to do this ? So I think my question can be simply distilled down to - "can you call async functions from Prisma client extensions" ? The reason it needs to be asynchronous is because my computed field needs to include information from an external service. |
|
Hi there, To keep our discussions organized and focused on the most relevant topics, we’re reviewing and tidying up our backlog. As part of this process, we’re closing discussions that haven’t had any recent activity and appear to be outdated. If this discussion is still important to you or unresolved, we’d love to hear from you! Feel free to reopen it or start a new one with updated details. For more details about our priorities and vision for the future of Prisma ORM, check out our latest blog post: https://www.prisma.io/blog/prisma-orm-manifesto. Thank you for your understanding and being part of the community! |
Uh oh!
There was an error while loading. Please reload this page.
Not sure if I should ask this here, but I was wondering if its possible and if there's an example for using the new client extensions to add a custom field based on the response from an external REST API ?
All reactions