P
Prismaโ€ข7mo ago
Alaskan donut

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
Alaskan donut
Alaskan donutOPโ€ข7mo ago
If I have no choice but to manually flatten, I will just use a map function on the array.
Nurul
Nurulโ€ข7mo ago
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.
Want results from more Discord servers?
Add your server