diff --git a/src/generators/include.ts b/src/generators/include.ts new file mode 100644 index 0000000..724f9cc --- /dev/null +++ b/src/generators/include.ts @@ -0,0 +1,44 @@ +import type { DMMF } from "@prisma/generator-helper"; +import { extractAnnotations } from "../annotations/annotations"; +import { generateTypeboxOptions } from "../annotations/options"; +import { getConfig } from "../config"; +import type { ProcessedModel } from "../model"; +import { wrapWithPartial } from "./wrappers/partial"; +import { isPrimitivePrismaFieldType } from "./primitiveField"; + +export const processedInclude: ProcessedModel[] = []; + +export function processInclude(models: DMMF.Model[] | Readonly) { + for (const m of models) { + const o = stringifyInclude(m); + if (o) { + processedInclude.push({ name: m.name, stringRepresentation: o }); + } + } + Object.freeze(processedInclude); +} + +export function stringifyInclude(data: DMMF.Model) { + const annotations = extractAnnotations(data.documentation); + + if (annotations.isHidden) return undefined; + + const fields = data.fields + .map((field) => { + const annotations = extractAnnotations(field.documentation); + if (annotations.isHidden) return undefined; + + if (isPrimitivePrismaFieldType(field.type)) return undefined; + + return `${field.name}: ${getConfig().typeboxImportVariableName}.Boolean()`; + }) + .filter((x) => x) as string[]; + + fields.push(`_count: ${getConfig().typeboxImportVariableName}.Boolean()`); + + const ret = `${getConfig().typeboxImportVariableName}.Object({${[ + ...fields, + ].join(",")}},${generateTypeboxOptions({ input: annotations })})\n`; + + return wrapWithPartial(ret); +} diff --git a/src/index.ts b/src/index.ts index e075245..ab6dbed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import { write } from "./writer"; import { processPlainInputCreate } from "./generators/plainInputCreate"; import { processPlainInputUpdate } from "./generators/plainInputUpdate"; import { processSelect } from "./generators/select"; +import { processInclude } from "./generators/include"; generatorHandler({ onManifest() { @@ -47,6 +48,7 @@ generatorHandler({ processRelationsInputUpdate(options.dmmf.datamodel.models); } processSelect(options.dmmf.datamodel.models); + processInclude(options.dmmf.datamodel.models); await write(); }, diff --git a/src/model.ts b/src/model.ts index 41e0981..ba92d9f 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,5 +1,6 @@ import { getConfig } from "./config"; import { processedEnums } from "./generators/enum"; +import { processedInclude } from "./generators/include"; import { processedPlain } from "./generators/plain"; import { processedPlainInputCreate } from "./generators/plainInputCreate"; import { processedPlainInputUpdate } from "./generators/plainInputUpdate"; @@ -61,6 +62,7 @@ export function mapAllModelsForWrite() { process(processedWhere, "Where"); process(processedWhereUnique, "WhereUnique"); process(processedSelect, "Select"); + process(processedInclude, "Include"); for (const [key, value] of modelsPerName) { const plain = processedPlain.find((e) => e.name === key);