fdarian / prisma-generator-drizzle

A Prisma generator for generating Drizzle schema with ease
90 stars 10 forks source link

Respect default values in timestamps/dates #20

Closed hilja closed 5 months ago

hilja commented 5 months ago

Given a simple Prisma model:

model Bar {
  foo       String
  createdAT DateTime @default(now())
}

It outputs something like:

import { pgTable, text, timestamp } from 'drizzle-orm/pg-core'

export const sites = pgTable('Bar', {
  foo: text('foo').notNull(),
  createdAT: timestamp('createdAT', { mode: 'date', precision: 3 }).notNull(),
})

Should it have a default value?

import { pgTable, text, timestamp } from 'drizzle-orm/pg-core'

export const sites = pgTable('Bar', {
  foo: text('foo').notNull(),
  createdAt: timestamp('createdAt', { mode: 'date', precision: 3 })
    .defaultNow()
    .notNull(),
})

Or default(sql`CURRENT_TIMESTAMP`). There seems to be some issues with it. Is it intentionally ambiguous?

hilja commented 5 months ago

It seems like .defaultNow() will be deprecated in favor of .defaultCurrentTimeStamp(), but it’s not implemented yet, so .default(sql`CURRENT_TIMESTAMP`) is the way.