Skip to content
On this page

Drizzle Schema

typescript

PostgreSQL schema name override

Example Output ​

Generated from the pet-store sample schema:

typescript
// Generated by @sqldoc/templates/drizzle -- DO NOT EDIT

import { bigserial, date, integer, jsonb, numeric, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'

export const adoption = pgTable('adoptions', {
  id: serial('id').primaryKey(),
  petId: integer('pet_id').notNull().references(() => pet.id),
  ownerId: integer('owner_id').notNull().references(() => owner.id),
  adoptedAt: timestamp('adopted_at').notNull().default(sql`now()`),
  adoptionFee: numeric('adoption_fee').notNull().default(0),
})

export const adoptionsauditlog = pgTable('adoptions_audit_log', {
  id: bigserial('id', { mode: 'number' }).primaryKey(),
  tableName: text('table_name').notNull(),
  operation: text('operation').notNull(),
  oldData: jsonb('old_data'),
  newData: jsonb('new_data'),
  changedAt: timestamp('changed_at').notNull().default(sql`now()`),
})

export const category = pgTable('categories', {
  id: serial('id').primaryKey(),
  name: varchar('name').notNull(),
  description: text('description'),
})

export const legacyinventory = pgTable('legacy_inventory', {
  id: serial('id').primaryKey(),
  itemName: varchar('item_name'),
  oldSku: varchar('old_sku'),
  quantity: integer('quantity').default(0),
})

export const location = pgTable('locations', {
  id: serial('id').primaryKey(),
  name: varchar('name').notNull(),
  address: text('address').notNull(),
  city: varchar('city').notNull(),
  zip: varchar('zip'),
})

export const medicalrecord = pgTable('medical_records', {
  id: serial('id').primaryKey(),
  petId: integer('pet_id').notNull().references(() => pet.id),
  visitDate: date('visit_date').notNull().default(sql`CURRENT_DATE`),
  diagnosis: text('diagnosis').notNull(),
  treatment: text('treatment'),
  vetName: varchar('vet_name'),
})

export const owner = pgTable('owners', {
  id: serial('id').primaryKey(),
  name: varchar('name').notNull(),
  email: varchar('email').notNull(),
  phone: varchar('phone'),
  createdAt: timestamp('created_at').default(sql`now()`),
})

export const pet = pgTable('pets', {
  id: serial('id').primaryKey(),
  categoryId: integer('category_id').references(() => category.id),
  name: varchar('name').notNull(),
  sku: varchar('sku').notNull(),
  price: numeric('price').notNull().default(0),
  internalNotes: text('internal_notes'),
  status: varchar('status').notNull().default('available'),
  createdAt: timestamp('created_at').default(sql`now()`),
})

export const review = pgTable('reviews', {
  id: serial('id').primaryKey(),
  petId: integer('pet_id').notNull(),
  ownerId: integer('owner_id').notNull(),
  rating: integer('rating').notNull(),
  body: text('body'),
  locationId: integer('location_id').references(() => location.id),
  createdAt: timestamp('created_at').default(sql`now()`),
})

export const staffauditlog = pgTable('staff_audit_log', {
  id: bigserial('id', { mode: 'number' }).primaryKey(),
  tableName: text('table_name').notNull(),
  operation: text('operation').notNull(),
  oldData: jsonb('old_data'),
  newData: jsonb('new_data'),
  changedAt: timestamp('changed_at').notNull().default(sql`now()`),
})

Usage ​

Add to your sqldoc.config.ts:

typescript
export default {
  dialect: 'postgres',
  schema: ['./schema.sql'],
  codegen: [
    {
      template: 'drizzle',
      output: './generated/types.ts',
      config: {
        // See configuration options below
      },
    },
  ],
}

Then run:

bash
sqldoc codegen