UNPKG

prisma-zod-generator

Version:

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

154 lines (153 loc) 5.82 kB
/** * Pro Feature Base Class * * Abstract base class for all Pro features with DMMF access and common functionality */ import { DMMF } from '@prisma/generator-helper'; import { ProGeneratorContext, ProContextUtils } from './ProGeneratorContext'; import { DMMFHelpers } from './DMMFHelpers'; export declare abstract class ProFeatureBase { protected context: ProGeneratorContext; protected utils: ProContextUtils; protected dmmfHelpers: DMMFHelpers; constructor(context: ProGeneratorContext); /** * Validate license for this feature before generation * Override in subclasses to specify feature name */ protected abstract getFeatureName(): string; /** * Main generation method - implement in subclasses */ protected abstract generateFeature(): Promise<void>; /** * Public generate method with license validation */ generate(): Promise<void>; /** * Models this feature should generate for. * * Every pack routes its model loop through here, so honouring the config in * this one place is what makes exclusion work across all of them — otherwise * join tables and secret-bearing models get endpoints, forms and SDK methods * whether you asked for them or not. */ protected getEnabledModels(): DMMF.Model[]; /** * Whether a model should be included. * * Uses the same `models: { <Name>: { enabled: false } }` shape as the core * generator's configuration, so one config governs both. A model with no * entry is included, which keeps existing setups generating exactly what they * generated before. */ protected isModelEnabled(modelName: string): boolean; /** * Get field documentation/comments from DMMF */ protected getFieldDocumentation(model: DMMF.Model, fieldName: string): string | undefined; /** * Get model documentation/comments from DMMF */ protected getModelDocumentation(model: DMMF.Model): string | undefined; /** * Get all fields of a specific kind from a model */ protected getFieldsByKind(model: DMMF.Model, kind: DMMF.FieldKind): DMMF.Field[]; /** * Get all scalar fields from a model */ protected getScalarFields(model: DMMF.Model): DMMF.Field[]; /** * Get all relation fields from a model */ protected getRelationFields(model: DMMF.Model): DMMF.Field[]; /** * Get all enum fields from a model */ protected getEnumFields(model: DMMF.Model): DMMF.Field[]; /** * Check if a field is required (not optional and no default value) */ protected isFieldRequired(field: DMMF.Field): boolean; /** * Check if a field is unique */ protected isFieldUnique(model: DMMF.Model, fieldName: string): boolean; /** * Get field type as string for code generation */ protected getFieldTypeString(field: DMMF.Field): string; /** * Write file safely with proper directory creation */ protected writeFile(filePath: string, content: string): Promise<void>; /** * Log info message */ protected logInfo(message: string): void; /** * Report options that will not do what the caller expects. * * Silence was the worst possible response here: a mistyped key was discarded * without a word, and several documented options were accepted while having no * effect, so a configuration that did nothing looked exactly like one that * worked. `notImplemented` names options that are read but not yet acted on; * anything outside both lists is reported as unrecognised. */ protected warnAboutOptions(config: Record<string, unknown> | undefined, spec: { supported: string[]; notImplemented?: string[]; }): void; /** * Log warning message. * * Warnings go to stdout, not stderr: Prisma runs generators as child processes * over JSON-RPC and does not relay their stderr, so a console.warn here is * invisible to the person running `prisma generate` — which silently defeated * every diagnostic the packs emit. */ protected logWarning(message: string): void; /** * Log error message. * * Stdout, for the same reason as logWarning: Prisma does not relay a * generator's stderr, so an error written there never reaches the person * running `prisma generate`. An invisible error is worse than an invisible * warning — it is the one message they most need. */ protected logError(message: string): void; /** * Extract comments that match a specific pattern * Useful for parsing special annotations like @policy, @pii, etc. */ protected extractAnnotationsFromComment(comment: string | undefined, annotationType: string): string[]; /** * Parse field-level annotations from documentation */ protected parseFieldAnnotations(model: DMMF.Model, fieldName: string, annotationType: string): string[]; /** * Parse model-level annotations from documentation */ protected parseModelAnnotations(model: DMMF.Model, annotationType: string): string[]; /** * Get all models that have a specific annotation */ protected getModelsWithAnnotation(annotationType: string): DMMF.Model[]; /** * Get all fields from a model that have a specific annotation */ protected getFieldsWithAnnotation(model: DMMF.Model, annotationType: string): DMMF.Field[]; /** * Generate TypeScript import statements */ protected generateImports(imports: Array<{ from: string; imports: string[]; typeOnly?: boolean; }>): string; /** * Generate export statements */ protected generateExports(exports: string[]): string; }