# chrishoermann/zod-prisma-types architecture

> Architecture diagram of the GitHub repository chrishoermann/zod-prisma-types, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chrishoermann/zod-prisma-types
- Repository: https://github.com/chrishoermann/zod-prisma-types

## Overview

##### Main Components
- CLI / Entry Point:
– The generator starts at the CLI (packages/generator/src/bin.ts). This file is invoked when “prisma generate” or the equivalent command runs.
– It is responsible for reading input (a Prisma schema/DMMF) and invoking the generator workflow.
- Generator Handler:
– packages/generator/src/generatorHandler.ts serves as the coordinator that bridges the CLI, processing modules, and the output writing functions.
- Core Processing Modules:
– The “classes” folder (packages/generator/src/classes) contains modules that extend the DMMF data (e.g. extendedDMMF.ts, extendedDMMFModel, extendedDMMFField, etc.). These classes encapsulate parsing and transforming Prisma’s internal model descriptions.
– The “functions” folder (packages/generator/src/functions) holds many helper functions to write parts of the generated code. There are two subcategories here:
▪ Content writers (e.g., writeArgs.ts, writePrismaEnum.ts, writeSingleFileModelStatements.ts) that assemble strings and build complete schema definitions.
▪ Field writers (e.g., writeJsDoc.ts, writeModelCustomValidator.ts) that are responsible for writing field-level validations.
– The “schemas” folder (packages/generator/src/schemas) validates the generator configuration using Zod.
- Utilities, Constants, and Helpers:
– The “constants” folder holds configuration items such as regular expressions and object maps.
– In the “utils” folder, helper functions for DMMF processing and other tasks (for example, getAllBoolCombinations, getPrismaVersion, transformImportStringToList) are gathered.
- File Writing and Output:
– The class “fileWriter.ts” (within classes) abstracts writing the generated content to disk. This module is the endpoint for outputting the Zod schemas.
- Testing and Examples:
– The project contains __tests__ inside both the “classes”, “functions”, and “utils” directories to verify the logic.
– The “usage” package (packages/usage) contains sample Prisma schemas (in its prisma folder) along with tests and example integration (e.g., trpc examples) illustrating how the generated schemas are consumed in a real application.
- Build and Workspace Configuration:
– Files like pnpm-lock.yaml, pnpm-workspace.yaml, turbo.json, and vite.config.ts suggest a monorepo setup with tooling for development, building, and testing.

##### Relationships & Data Flow
- The CLI launches the Generator Handler, which in turn calls core modules (classes and functions) to parse and extend the DMMF.
- The extendedDMMF classes transform the raw Prisma schema data into enriched metadata.
- Subsequently, “Content Writers” and “Field Writers” use this processed data to generate code strings for Zod validation schemas.
- These generated strings are then sent to the “File Writer” to output to designated files (possibly as a single file or multiple files, depending on the configuration).
- Finally, the “Usage” package demonstrates how these generated files are imported and used, and the tests in various __tests__ folders validate correctness.

##### Architectural Patterns & Design Principles
- The project uses a modular, layered design pattern:
– Separation of concerns: Each folder has a clear purpose (classes to transform/prune DMMF, functions to write file content, and utilities for recurring tasks).
– Dependency injection through a central generator handler, which calls on different modules.
- Proper error handling especially in the validation of Zod validators integrated via Prisma rich-comments.
- The monorepo structure using pnpm-workspace.yaml indicates that the generator and its usage examples (as well as tests) are managed together for easy development and integration.
- The test suites in the __tests__ directories help ensure that each module is working as expected.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input & CLI Layer"
        A["Prisma Schema/DMMF Input"]:::input
        B["CLI Entry Point (bin.ts)"]:::cli
    end

    subgraph "Core Generation Layer"
        C["Generator Handler (generatorHandler.ts)"]:::processing
        D["Core Processing – Classes"]:::processing
        E["Content Writers"]:::helper
        F["Field Writers"]:::helper
        G["File Writer"]:::output
    end

    subgraph "Configuration & Utilities"
        H["Configuration & Schemas"]:::config
        I["Constants"]:::utils
        J["Utilities"]:::utils
    end

    subgraph "Usage & Testing"
        K["Usage / Integration Examples"]:::external
        L["Generator Tests"]:::testing
        M["Usage/Integration Tests"]:::testing
    end

    %% Data Flow Connections
    A -->|"parsesDMMF"| B
    B -->|"invokes"| C
    C -->|"calls_transformation"| D
    C -->|"calls_helpers"| E
    C -->|"calls_helpers"| F
    D -->|"transformsData"| E
    D -->|"transformsData"| F
    E -->|"generatesOutput"| G
    F -->|"generatesOutput"| G
    G -->|"outputsSchemas"| K

    %% Inter-module interactions
    C -->|"validatesConfig"| H
    D -->|"usesUtilities"| J
    E -->|"usesConstants"| I
    F -->|"usesConstants"| I

    %% Testing connections
    D -->|"testedBy"| L
    E -->|"testedBy"| L
    F -->|"testedBy"| L
    I -->|"testedBy"| L
    J -->|"testedBy"| L
    K -->|"hasTests"| M

    %% Click Events
    click B "https://github.com/chrishoermann/zod-prisma-types/blob/master/packages/generator/src/bin.ts"
    click C "https://github.com/chrishoermann/zod-prisma-types/blob/master/packages/generator/src/generatorHandler.ts"
    click D "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/classes"
    click E "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/functions/contentWriters"
    click F "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/functions/fieldWriters"
    click H "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/schemas"
    click I "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/constants"
    click J "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/utils"
    click G "https://github.com/chrishoermann/zod-prisma-types/blob/master/packages/generator/src/classes/fileWriter.ts"
    click L "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/generator/src/__tests__"
    click M "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/usage/tests"
    click K "https://github.com/chrishoermann/zod-prisma-types/tree/master/packages/usage"

    %% Styles
    classDef input fill:#f9e79f,stroke:#333,stroke-width:2px
    classDef cli fill:#aed6f1,stroke:#333,stroke-width:2px
    classDef processing fill:#a9dfbf,stroke:#333,stroke-width:2px
    classDef helper fill:#f7dc6f,stroke:#333,stroke-width:2px
    classDef config fill:#f5b7b1,stroke:#333,stroke-width:2px
    classDef utils fill:#d6eaf8,stroke:#333,stroke-width:2px
    classDef output fill:#fad7a0,stroke:#333,stroke-width:2px
    classDef external fill:#f5cba7,stroke:#333,stroke-width:2px
    classDef testing fill:#d2b4de,stroke:#333,stroke-width:2px
```
