How chrishoermann/zod-prisma-types works
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.