UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

162 lines (161 loc) 4.39 kB
/** * Result Schema Generator * Generates Zod schemas for Prisma operation return values to enable validation of API responses and operation results */ import { DMMF } from '@prisma/generator-helper'; import { GeneratorConfig } from '../config/parser'; /** * Prisma operation types that return results */ export declare enum OperationType { FIND_UNIQUE = "findUnique", FIND_FIRST = "findFirst", FIND_MANY = "findMany", CREATE = "create", CREATE_MANY = "createMany", UPDATE = "update", UPDATE_MANY = "updateMany", UPSERT = "upsert", DELETE = "delete", DELETE_MANY = "deleteMany", AGGREGATE = "aggregate", GROUP_BY = "groupBy", COUNT = "count" } /** * Result schema generation options */ export interface ResultSchemaOptions { modelName: string; operationType: OperationType; includeRelations?: string[]; excludeFields?: string[]; paginationSupport?: boolean; nullableResult?: boolean; customValidations?: Record<string, string>; } /** * Pagination schema configuration */ export interface PaginationConfig { includeCursor?: boolean; includeCount?: boolean; includePageInfo?: boolean; customFields?: Record<string, string>; } /** * Aggregate operation configuration */ export interface AggregateConfig { includeCount?: boolean; includeSum?: string[]; includeAvg?: string[]; includeMin?: string[]; includeMax?: string[]; customAggregates?: Record<string, string>; } /** * Generated result schema information */ export interface GeneratedResultSchema { operationType: OperationType; schemaName: string; zodSchema: string; typeDefinition: string; imports: Set<string>; exports: Set<string>; dependencies: string[]; documentation: string; examples?: string[]; } /** * Result schema generation context */ export interface ResultGenerationContext { model: DMMF.Model; options: ResultSchemaOptions; baseModelSchema?: string; relatedModels: Map<string, DMMF.Model>; fieldTypeMap: Map<string, string>; } /** * Result Schema Generator * Main class for generating Zod schemas for Prisma operation results */ export declare class ResultSchemaGenerator { private generatedSchemas; private baseModelSchemas; private config; private isJsonSchemaModeEnabled; private getJsonSchemaOptions; constructor(config?: GeneratorConfig); /** * Generate result schema for a specific operation */ generateResultSchema(model: DMMF.Model, options: ResultSchemaOptions): GeneratedResultSchema; /** * Generate schemas for all operations of a model */ generateAllResultSchemas(model: DMMF.Model, operationTypes?: OperationType[]): GeneratedResultSchema[]; /** * Build generation context */ private buildGenerationContext; /** * Generate single result schema (for operations returning one model or null) */ private generateSingleResultSchema; /** * Generate array result schema (for findMany operations) */ private generateArrayResultSchema; /** * Generate batch operation result schema */ private generateBatchResultSchema; /** * Generate aggregate result schema */ private generateAggregateResultSchema; /** * Generate groupBy result schema */ private generateGroupByResultSchema; /** * Generate count result schema */ private generateCountResultSchema; /** * Helper methods */ private generateSchemaName; private operationTypeToSuffix; private isNullableOperation; private buildBaseResultSchema; private buildRelationSchema; private buildAggregateFields; private buildGroupByFields; private generatePaginationSchema; private mapPrismaTypeToZod; private getBaseModelSchema; private extractDependencies; private generateDocumentation; private generateExamples; private generateCacheKey; /** * Public utility methods */ /** * Clear generated schema cache */ clearCache(): void; /** * Get all generated schemas */ getAllGeneratedSchemas(): GeneratedResultSchema[]; /** * Register base model schema */ registerBaseModelSchema(modelName: string, schema: string): void; } export default ResultSchemaGenerator;