Home > Software design >  How to set a column with current time on Prisma?
How to set a column with current time on Prisma?

Time:02-03

I'm making a node api using prisma ORM and i'm trying to update a column which i had set with the type DateTime, here is the model, the column is the deleted_at one

model Employee {
 id Int @id @default(autoincrement())
 name String
 created_at DateTime
 deleted_at DateTime
}

how can i change it to the current time in my controller? the controller looks like this

export const DeleteCompany = async (req:IEmployee, res:Response) => {
 const data:ICompany = req
 const deletedCompany = await prisma.employee.update({
     where: {
         id: Number(data.id)
     },
     data: {
        deleted_at: //what should I put here?
     }
 })
 return res.status(200).json(deletedCompany)
}

I've tried using

now()

but it didnt work.

CodePudding user response:

You can use the JavaScript Date object to set the deleted_at column to the current time in your controller.

export const DeleteCompany = async (req:IEmployee, res:Response) => {
 const data:ICompany = req
 const deletedCompany = await prisma.employee.update({
     where: {
         id: Number(data.id)
     },
     data: {
        deleted_at: new Date().toISOString()
     }
 })
 return res.status(200).json(deletedCompany)
}

In this code, the new Date() constructor creates a new Date object with the current date and time. The toISOString method is then used to format the date and time as an ISO string, which is the format that Prisma expects for the DateTime type.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

CodePudding user response:

Prisma supports plain javascript dates for setting date fields.

So new Date() should work fine:

deleted_at: new Date()
  • Related