Skip to content
On this page

z.string()

typescript

How to represent date/time types

Example Output ​

Generated from the pet-store sample schema:

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

import { z } from 'zod'

export const adoptionSchema = z.object({
  id: z.number(),
  petId: z.number(),
  ownerId: z.number(),
  adoptedAt: z.date(),
  adoptionFee: z.string(),
})

export type Adoption = z.infer<typeof adoptionSchema>

export const adoptionsauditlogSchema = z.object({
  id: z.number(),
  tableName: z.string(),
  operation: z.string(),
  oldData: z.unknown().optional(),
  newData: z.unknown().optional(),
  changedAt: z.date(),
})

export type AdoptionsAuditLog = z.infer<typeof adoptionsauditlogSchema>

export const categorySchema = z.object({
  id: z.number(),
  name: z.string().min(1),
  description: z.string().optional(),
})

export type Category = z.infer<typeof categorySchema>

export const legacyinventorySchema = z.object({
  id: z.number(),
  itemName: z.string().optional(),
  oldSku: z.string().optional(),
  quantity: z.number().optional(),
})

export type LegacyInventory = z.infer<typeof legacyinventorySchema>

export const locationSchema = z.object({
  id: z.number(),
  name: z.string(),
  address: z.string(),
  city: z.string(),
  zip: z.string().optional(),
})

export type Location = z.infer<typeof locationSchema>

export const medicalrecordSchema = z.object({
  id: z.number(),
  petId: z.number(),
  visitDate: z.string(),
  diagnosis: z.string(),
  treatment: z.string().optional(),
  vetName: z.string().optional(),
})

export type MedicalRecord = z.infer<typeof medicalrecordSchema>

export const ownerSchema = z.object({
  id: z.number(),
  name: z.string().min(1),
  email: z.string().regex(/^[^@]+@[^@]+\.[^@]+$/),
  phone: z.string().min(7).max(20).optional(),
  createdAt: z.date().optional(),
})

export type Owner = z.infer<typeof ownerSchema>

export const petSchema = z.object({
  id: z.number(),
  categoryId: z.number().optional(),
  name: z.string().min(1),
  sku: z.string().regex(/^[A-Z]{3}-[0-9]{4}$/),
  price: z.string().min(0).max(99999),
  internalNotes: z.string().optional(),
  status: z.string(),
  createdAt: z.date().optional(),
})

export type Pet = z.infer<typeof petSchema>

export const reviewSchema = z.object({
  id: z.number(),
  petId: z.number(),
  ownerId: z.number(),
  rating: z.number().min(1).max(5),
  body: z.string().optional(),
  locationId: z.number().optional(),
  createdAt: z.date().optional(),
})

export type Review = z.infer<typeof reviewSchema>

export const staffauditlogSchema = z.object({
  id: z.number(),
  tableName: z.string(),
  operation: z.string(),
  oldData: z.unknown().optional(),
  newData: z.unknown().optional(),
  changedAt: z.date(),
})

export type StaffAuditLog = z.infer<typeof staffauditlogSchema>

Usage ​

Add to your sqldoc.config.ts:

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

Then run:

bash
sqldoc codegen