Flat response from relational query?
I am trying to use the following Prisma query to get a post object back containing post details and the username of the user that posted it:
const posts = await prisma.post.findMany({
where: {
users: {
username: {
equals: username,
mode: "insensitive",
},
},
},
select: {
contentText: true,
users: {
select: {
username: true,
},
},
},
});
This returns
[
{
contentText: 'Embarking on a thrilling journey in Glacier National Park โฐ๏ธ was an unforgettable experie
nce! Amidst stunning landscapes, we had a heart-pounding encounter with a majestic bear, a reminder of the
untamed beauty of nature. Days were spent foraging mushrooms ๐, adding a delicious touch to our lakeside feasts. Nights by the crackling fire on the lakeshore were filled with laughter, storytelling, and a deep sense of camaraderie. Surrounded by towering mountains and the tranquil lake, each moment felt like a cherished memory in the making. With my partner and friends by my side, we immersed ourselves in the serenity of the wilderness, forging bonds that will last a lifetime. ๐ช' ,
users: { username: 'Alaskan' }
}
]
I would like username
to be level with contentText
without having to manually flatten the objects returned in this response array. How is this accomplished?2 Replies
If I have no choice but to manually flatten, I will just use a map function on the array.
You could use a client extension to return a custom result in which you return response in a flattened way.
You can consider using this example as a reference:
https://github.com/prisma/prisma-client-extensions/tree/main/transformed-fields
GitHub
prisma-client-extensions/transformed-fields at main ยท prisma/prisma...
Examples of Prisma Client extensions. Contribute to prisma/prisma-client-extensions development by creating an account on GitHub.