Skip to content

[Question]: Why use new Date() from JavaScript in schema.ts if sqlite has a default method for getting the epoch timestamp? #37

@tedlatentai

Description

@tedlatentai

Watched your video recently, and when you were explaining how to set up a sqlite schema for Drizzle, you mentioned that sqlite doesn’t have a built-in method for getting an epoch timestamp, which sounded off to me because I was doing just that literally a couple of days ago.

sqlite actually does have a built-in way to get the epoch time, with the only caveat being that it returns epoch seconds, not milliseconds.

This is documented in both the official sqlite docs and the Drizzle docs, so I’m not really sure why you’d use

export const tasks = sqliteTable("tasks", {
  ...,
  updatedAt: integer("updated_at", { mode: "timestamp" })
    .$defaultFn(() => new Date())
    .$onUpdate(() => new Date()),
});

instead of

export const tasks = sqliteTable("tasks", {
  ...,
  updatedAt: integer("updated_at", { mode: "timestamp" })
    .default(sql`(unixepoch() * 1000)`)
    .$onUpdate(() => sql`(unitepoch() * 1000)`),
});

Thanks for your videos in general and for this one in particular, keep on keeping on!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions