Hi everyone, I am using `Prisma 3.1.1` with one of...
# orm-help
b
Hi everyone, I am using
Prisma 3.1.1
with one of my projects. Inside my code, I use the
rawQuery
below and I want to type the result of the query.
Copy code
const configs = await this.prismaService.$queryRaw<Config[] >`SELECT id, employee_id, start_date, end_date
    FROM configs mc 
        WHERE employee_id IN (${Prisma.join(employeeIds)}) 
        AND DATE_PART('year', mc.start_date) = CAST(${year} AS INTEGER) 
        AND CAST(${month} AS INTEGER) >= DATE_PART('month', mc.start_date)
        AND CAST(${month} AS INTEGER) <= DATE_PART('month', COALESCE(end_date,'2020-12-01'))`;

// generated type
export type Config = {
  id: number
  employeeId: string
  startDate: Date
  endDate: Date | null
}

// Expected result
 [Config{id:1, startDate:xxxx, endDate:xx, ..}, ..]

// I got
[{id:1, start_date:xxxx, end_date:xx, ..}, ..]
Do you know what’s wrong with my query? I looked at the documentation https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access and I didn’t found my mistake thks
g
Hi, I'm new to prisma so I may be wrong here. When you expect
Config(id: 1, ...)
, do you mean you expect a class instance? To my undesrstanding, all prisma types are interfaces, not classes and what you get back seems to match the generated type.
b
hi, My issue is the raw query result is not compliant with the generated Type. The result object properties names are in the snake case like the DB column name and not in camelCase
g
😅 didn't saw that, sorry
b
according to the documentation, it is possible to type the result of the raw query. but it seems not to work for me🙁
r
@Bamada 👋 It seems you’re database fields and Prisma model names do not match which is why you would need to create the types for the raw query manually in this case.
b
@Ryan thks This is the way that I defined my model.
Copy code
model MercureConfig {
  id                 Int       @id @default(autoincrement())
  po                 Int       @unique
  poLineNumber       Int       @map("po_line_number")
  employeeId         String    @map("employee_id")
  totalDays          Int       @map("total_days")
  startDate          DateTime  @map("start_date")
  endDate            DateTime? @map("end_date")

  @@map("mercure_configs")
}
If I understand well if I use
@map
inside a schema and a
$rawquery
I need to manually define a type right?
r
Yup
b
👍