Skip to content
On this page

Knex Table Types

typescript

How to represent date/time types

Example Output ​

Generated from the pet-store sample schema:

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

import type { Knex } from 'knex'

export interface AdoptionTable {
  id: number
  pet_id: number
  owner_id: number
  adopted_at: Date
  adoption_fee: string
}

export interface AdoptionsAuditLogTable {
  id: number
  table_name: string
  operation: string
  old_data: unknown | null
  new_data: unknown | null
  changed_at: Date
}

export interface CategoryTable {
  id: number
  name: string
  description: string | null
}

export interface LegacyInventoryTable {
  id: number
  item_name: string | null
  old_sku: string | null
  quantity: number | null
}

export interface LocationTable {
  id: number
  name: string
  address: string
  city: string
  zip: string | null
}

export interface MedicalRecordTable {
  id: number
  pet_id: number
  visit_date: string
  diagnosis: string
  treatment: string | null
  vet_name: string | null
}

export interface OwnerTable {
  id: number
  name: string
  email: string
  phone: string | null
  created_at: Date | null
}

export interface PetTable {
  id: number
  category_id: number | null
  name: string
  sku: string
  price: string
  internal_notes: string | null
  status: string
  created_at: Date | null
}

export interface ReviewTable {
  id: number
  pet_id: number
  owner_id: number
  rating: number
  body: string | null
  location_id: number | null
  created_at: Date | null
}

export interface StaffAuditLogTable {
  id: number
  table_name: string
  operation: string
  old_data: unknown | null
  new_data: unknown | null
  changed_at: Date
}

declare module 'knex/types/tables' {
  interface Tables {
    adoptions: AdoptionTable
    adoptions_audit_log: AdoptionsAuditLogTable
    categories: CategoryTable
    legacy_inventory: LegacyInventoryTable
    locations: LocationTable
    medical_records: MedicalRecordTable
    owners: OwnerTable
    pets: PetTable
    reviews: ReviewTable
    staff_audit_log: StaffAuditLogTable
  }
}

Usage ​

Add to your sqldoc.config.ts:

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

Then run:

bash
sqldoc codegen