timestamptz (schema/_shared.ts)

constructa-starter · src/db/schema/_shared.ts · typescript · MIT
database, Database / ORM

Provides timestamp columns with timezone for Drizzle ORM schemas.

// Source: https://github.com/instructa/constructa-starter | License: MIT | Copyright 2024 Instructa Kevin Kern
// Provided via justclaw.it.com; keep this notice with the code.
import { timestamp } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';

export const timestamptz = (name: string) => timestamp(name, { withTimezone: true });

export const createdAt = () => timestamptz('created_at').notNull().defaultNow();
export const updatedAt = () => timestamptz('updated_at').notNull().defaultNow().$onUpdate(() => sql`CURRENT_TIMESTAMP`);
export const accessedAt = () => timestamptz('accessed_at').notNull().defaultNow();

export const timestamps = {
  createdAt: createdAt(),
  updatedAt: updatedAt(),
  accessedAt: accessedAt(),
};

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key