how to create a prisma schema for milliseconds in typescript

To create a Prisma schema for milliseconds in TypeScript, you can define a new field in your Prisma model as a BigInt type. This data type can store integers up to 2^53 - 1 which is large enough to represent the number of milliseconds in a date.

model MyModel {
  id        Int      @id @default(autoincrement())
  createdAt BigInt
}
88 chars
5 lines

Then, you can use the Date object in TypeScript to represent the date as a Unix timestamp and convert it to milliseconds by multiplying it by 1000.

index.ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

const myDate = new Date()
const millisecs = myDate.getTime() // get number of milliseconds

const result = await prisma.myModel.create({
  data: {
    createdAt: BigInt(millisecs) // store milliseconds as BigInt
  },
})
302 chars
13 lines

This way, you can store and retrieve dates with millisecond accuracy in your Prisma schema using TypeScript.

gistlibby LogSnag