Skip to content

Commit

Permalink
✨ feat: add include models
Browse files Browse the repository at this point in the history
  • Loading branch information
m1212e committed Sep 18, 2024
1 parent f164ac6 commit 297e5ab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/generators/include.ts
Original file line number Diff line number Diff line change
@@ -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<DMMF.Model[]>) {
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);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -47,6 +48,7 @@ generatorHandler({
processRelationsInputUpdate(options.dmmf.datamodel.models);
}
processSelect(options.dmmf.datamodel.models);
processInclude(options.dmmf.datamodel.models);

await write();
},
Expand Down
2 changes: 2 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 297e5ab

Please sign in to comment.