Hi, Is there a way to convert an object into a pri...
# orm-help
k
Hi, Is there a way to convert an object into a prisma object
r
@Kieran Salawu đź‘‹ Could you provide an example of that object if possible?
k
SUre
I have a user model in my schema
rather than using
findMany
to get a list of users from my db, i use some customSQL to get the data from that table i need, but i want the results to be in the same for as if it where using findMany
r
In this case, a
map
function that does just this conversion should work. And then you can use that in your logic to return in the same way.
Prisma unfortunately doesn’t have any inbuilt function to do this.
k
what would i be mapping into?
r
This type:
Copy code
import { Prisma } from '@prisma/client'

const map = (data): Prisma.UserGetPayload => {
  // return the data as per the return type
}
k
Hmm im not sure that will do what Im after
im probably not doing a great job explaining
r
An example with sample data would be great!
k
ill try my best
Copy code
model User {
  id        Int      @id @default(autoincrement()) @map("user_id")
  email     String   @unique @map("user_email")
  posts     Post[]
}

model Post {
  id        Int      @id @default(autoincrement()) @map("post_id")
  title     String   @db.VarChar(255) @map("post_title")
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int? @map("post_author_id")
}
if this is my schema
i could do
const users = await prisma.user.findMany()
and get my users
Copy code
ctx.prisma.users
    .findUnique({
      where: { id: 1 },
    })
    .posts()
i could also do this to get the posts of a specific user
but i woud like to do
Copy code
const myUser = ctx.prisma.$queryRaw(
      `SELECT * from customSearch(${userId})`,
    )
const myUserPrisma = someConvertFunction(myUser)

const posts = myUserPrisma.posts()