'Prisma queryRaw returning date as string

Issue: When I use prisma.$queryRaw, it returns my date as a string, even though I specify the query's return type. If I use prisma.find then it returns it correctly. But, I have to use queryRaw because of the complexity of the query.

schema.prisma has the date defined like such:

effectiveDate DateTime? @map("effective_date") @db.Date

So, the model object has the field defined like effectiveDate: Date | null

The query looks something like this:

const catalogCourses: CatalogCourse[] = await prisma.$queryRaw<CatalogCourse[]>`
    SELECT
      id,
      campus,
      effective_date as "effectiveDate",

...rest of the query ommitted here because it's not important

If I then do something like

console.log(`typeof date: ${typeof catalogCourses[0].effectiveDate}, value ${catalogCourses[0].effectiveDate}`)

The result shows typeof date: string, value 2000-12-31. Why isn't it a date? I need to be able to work with it as a Date, but if I do effectiveDate.getTime() for example, it errors during runtime, saying 'getTime is not a function', which it is doc. If I try and do new Date(effectiveDate), that doesn't work either because typescript sees the field as a Date object already. EDIT: I was incorrect about why the previous statement wasn't working; doing new Date(effectiveDate) does work.

I do see in the prisma docs that it says:

Type caveats when using raw SQL When you type the results of $queryRaw, the raw data does not always match the suggested TypeScript type.

Is there a way for queryRaw to return my date as a Date object?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source