Home > Software design >  Prisma get Date as string instead of Date object
Prisma get Date as string instead of Date object

Time:08-11

Suppose I have table in Postgres SQL with prisma.

// Prisma Model

model my_table {
    comment   String   @db.VarChar(255)
    date      DateTime @db.Timestamp(6) // Stored as Now() in sql
}

After insert into my table I have. (the date is stored with no timezone)

// SQL DB Table

| comment  |           date             |
| -------- | -------------------------- |
| Hello    | 2022-08-04 08:16:32.716904 |

I want to query here

// Prisma Query

const rows = await this.prismaClient.my_table.findMany({
            orderBy: {
               date: 'desc'
            },
        });

How can I get back the date as '2022-08-04 08:16:32.716904' instead of a Date object as the date.toISOString() returns 2022-08-04T08:16:32.716Z and not 2022-08-04T08:16:32.716904Z ?

CodePudding user response:

I found a work around, that works

const row = await prismaClient.$queryRaw<{date: string}[]>
(Prisma.sql`SELECT CAST(date AS VARCHAR) FROM my_table where name = "Hello"`)

This returns the date as string without mapping the date to Date object.

  • Related