Skip to content
On this page

JSON Schema

json

Output one file per table or a single bundled file (default: bundled)

Example Output ​

Generated from the pet-store sample schema:

json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "Adoption": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "pet_id": {
          "type": "integer"
        },
        "owner_id": {
          "type": "integer"
        },
        "adopted_at": {
          "type": "string",
          "format": "date-time"
        },
        "adoption_fee": {
          "type": "number"
        }
      },
      "required": [
        "id",
        "pet_id",
        "owner_id",
        "adopted_at",
        "adoption_fee"
      ]
    },
    "AdoptionsAuditLog": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "table_name": {
          "type": "string"
        },
        "operation": {
          "type": "string"
        },
        "old_data": {},
        "new_data": {},
        "changed_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "table_name",
        "operation",
        "changed_at"
      ]
    },
    "Category": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "name"
      ]
    },
    "LegacyInventory": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "item_name": {
          "type": "string"
        },
        "old_sku": {
          "type": "string"
        },
        "quantity": {
          "type": "integer"
        }
      },
      "required": [
        "id"
      ]
    },
    "Location": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "zip": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "name",
        "address",
        "city"
      ]
    },
    "MedicalRecord": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "pet_id": {
          "type": "integer"
        },
        "visit_date": {
          "type": "string",
          "format": "date"
        },
        "diagnosis": {
          "type": "string"
        },
        "treatment": {
          "type": "string"
        },
        "vet_name": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "pet_id",
        "visit_date",
        "diagnosis"
      ]
    },
    "Owner": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "email": {
          "type": "string",
          "pattern": "^[^@]+@[^@]+\\.[^@]+$"
        },
        "phone": {
          "type": "string",
          "minLength": 7,
          "maxLength": 20
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "name",
        "email"
      ]
    },
    "Pet": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "category_id": {
          "type": "integer"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "sku": {
          "type": "string",
          "pattern": "^[A-Z]{3}-[0-9]{4}$"
        },
        "price": {
          "type": "number",
          "minimum": 0,
          "maximum": 99999
        },
        "internal_notes": {
          "type": "string"
        },
        "status": {
          "type": "string"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "name",
        "sku",
        "price",
        "status"
      ]
    },
    "Review": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "pet_id": {
          "type": "integer"
        },
        "owner_id": {
          "type": "integer"
        },
        "rating": {
          "type": "integer",
          "minimum": 1,
          "maximum": 5
        },
        "body": {
          "type": "string"
        },
        "location_id": {
          "type": "integer"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "pet_id",
        "owner_id",
        "rating"
      ]
    },
    "StaffAuditLog": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "table_name": {
          "type": "string"
        },
        "operation": {
          "type": "string"
        },
        "old_data": {},
        "new_data": {},
        "changed_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "table_name",
        "operation",
        "changed_at"
      ]
    }
  }
}

Usage ​

Add to your sqldoc.config.ts:

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

Then run:

bash
sqldoc codegen