diff --git a/src/generator/plainModel.ts b/src/generator/plainModel.ts index 4c65e59..bc01acb 100644 --- a/src/generator/plainModel.ts +++ b/src/generator/plainModel.ts @@ -2,12 +2,14 @@ import type { DMMF } from "@prisma/generator-helper"; import { typeboxImportVariableName } from "./typeboxImport"; import { Annotation, parseDocumentation } from "./documentation"; import { NullableVariant, nullableVariableName } from "./nullable"; +import type { Models } from "../util/modelMap"; /** * @param allowUndefinedFields In case we want to create input schemes, we want to allow fields to be undefined (just not set) if they are optional */ export function PlainModel( data: Pick, + referenceableEnums: Models, allowUndefinedFields = false, additionalFields: DMMF.Model["fields"][number][] = [], ) { @@ -15,23 +17,30 @@ export function PlainModel( if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined; - const fields = data.fields.concat(additionalFields) + const fields = data.fields + .concat(additionalFields) .map((field) => { - if (!isPrimitivePrismaFieldType(field.type)) return undefined; const doc = parseDocumentation(field.documentation); if (doc.annotations.includes(Annotation.HIDDEN)) return undefined; + if (isPrimitivePrismaFieldType(field.type)) { + return PrimitiveField({ + name: field.name, + fieldType: field.type, + list: field.isList, + optional: field.isRequired + ? NullableVariant.REQUIRED + : allowUndefinedFields + ? NullableVariant.OPTIONAL_NULLABLE + : NullableVariant.NULLABLE, + options: doc.options, + }); + } - return PrimitiveField({ - name: field.name, - fieldType: field.type, - list: field.isList, - optional: field.isRequired - ? NullableVariant.REQUIRED - : allowUndefinedFields - ? NullableVariant.OPTIONAL_NULLABLE - : NullableVariant.NULLABLE, - options: doc.options, - }); + if (referenceableEnums.has(field.type)) { + return `${field.name}: ${referenceableEnums.get(field.type)}`; + } + + return undefined; }) .filter((x) => x) as string[]; diff --git a/src/generator/relationModel.ts b/src/generator/relationModel.ts index 98e408c..83bcb8f 100644 --- a/src/generator/relationModel.ts +++ b/src/generator/relationModel.ts @@ -7,6 +7,7 @@ import { NullableVariant, nullableVariableName } from "./nullable"; export function RelationModel( data: Pick, + referenceableEnums: Models, referenceableModels: Models, additionalFields: DMMF.Model["fields"][number][] = [], ) { @@ -15,7 +16,7 @@ export function RelationModel( const fields = data.fields.concat(additionalFields) .map((field) => { - if (isPrimitivePrismaFieldType(field.type)) return undefined; + if (isPrimitivePrismaFieldType(field.type) || referenceableEnums.has(field.type)) return undefined; const doc = parseDocumentation(field.documentation); if (doc.annotations.includes(Annotation.HIDDEN)) return undefined; diff --git a/src/generator/whereModel.ts b/src/generator/whereModel.ts index b30634a..3729b6e 100644 --- a/src/generator/whereModel.ts +++ b/src/generator/whereModel.ts @@ -2,9 +2,11 @@ import type { DMMF } from "@prisma/generator-helper"; import { typeboxImportVariableName } from "./typeboxImport"; import { Annotation, parseDocumentation } from "./documentation"; import { PlainModel } from "./plainModel"; +import type { Models } from "../util/modelMap"; export function WhereModel( - data: Pick + data: Pick, + referenceableEnums: Models ) { const modelDoc = parseDocumentation(data.documentation); if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined; @@ -30,7 +32,7 @@ export function WhereModel( const fields = data.fields.filter((field) => uniqueFieldFields.includes(field.name) ); - return `${uniqueFieldFields.join("_")}: ${PlainModel({ fields }, true)}`; + return `${uniqueFieldFields.join("_")}: ${PlainModel({ fields }, referenceableEnums, true)}`; }) .filter((x) => x) as string[]; diff --git a/src/index.ts b/src/index.ts index efc8ac3..19d5734 100644 --- a/src/index.ts +++ b/src/index.ts @@ -77,18 +77,6 @@ generatorHandler({ await mkdir(outputDirectory, { recursive: true }); - const plainTasks: Promise[] = []; - const plainTypes: Models = new Map(); - - plainTasks.push( - ...options.dmmf.datamodel.models.map(async (e) => { - const model = PlainModel(e, false, plainAdditionalFields); - if (model) { - plainTypes.set(e.name, model); - } - }), - ); - const enumTasks: Promise[] = []; const enumTypes: Models = new Map(); @@ -101,12 +89,26 @@ generatorHandler({ }), ); + await Promise.all(enumTasks); + + const plainTasks: Promise[] = []; + const plainTypes: Models = new Map(); + + plainTasks.push( + ...options.dmmf.datamodel.models.map(async (e) => { + const model = PlainModel(e, enumTypes, false); + if (model) { + plainTypes.set(e.name, model); + } + }), + ); + const whereTasks: Promise[] = []; const whereTypes: Models = new Map(); whereTasks.push( ...options.dmmf.datamodel.models.map(async (e) => { - const model = WhereModel(e); + const model = WhereModel(e, enumTypes); if (model) { whereTypes.set(e.name, model); } @@ -178,7 +180,6 @@ generatorHandler({ ...optionalDataPlainTasks, ...dataRelationTasks, ...optionalDataRelationTasks, - ...enumTasks, ]); const relationTasks: Promise[] = []; @@ -188,7 +189,8 @@ generatorHandler({ ...options.dmmf.datamodel.models.map(async (e) => { const model = RelationModel( e, - mergeModels(plainTypes, enumTypes), + enumTypes, + plainTypes, relationsAdditionalFields, ); if (model) {