Skip to content

Commit

Permalink
⬆️ improvement: switch enum fields to plain models instead of relatio…
Browse files Browse the repository at this point in the history
…n schemes
  • Loading branch information
m1212e committed Apr 29, 2024
1 parent 7951c13 commit 945efde
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
35 changes: 22 additions & 13 deletions src/generator/plainModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@ 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<DMMF.Model, "fields" | "documentation">,
referenceableEnums: Models,
allowUndefinedFields = false,
additionalFields: DMMF.Model["fields"][number][] = [],
) {
const modelDoc = parseDocumentation(data.documentation);

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[];

Expand Down
3 changes: 2 additions & 1 deletion src/generator/relationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NullableVariant, nullableVariableName } from "./nullable";

export function RelationModel(
data: Pick<DMMF.Model, "fields" | "documentation">,
referenceableEnums: Models,
referenceableModels: Models,
additionalFields: DMMF.Model["fields"][number][] = [],
) {
Expand All @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions src/generator/whereModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DMMF.Model, "fields" | "documentation" | "name" | "uniqueFields">
data: Pick<DMMF.Model, "fields" | "documentation" | "name" | "uniqueFields">,
referenceableEnums: Models
) {
const modelDoc = parseDocumentation(data.documentation);
if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined;
Expand All @@ -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[];

Expand Down
32 changes: 17 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ generatorHandler({

await mkdir(outputDirectory, { recursive: true });

const plainTasks: Promise<void>[] = [];
const plainTypes: Models = new Map<string, string>();

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<void>[] = [];
const enumTypes: Models = new Map<string, string>();

Expand All @@ -101,12 +89,26 @@ generatorHandler({
}),
);

await Promise.all(enumTasks);

const plainTasks: Promise<void>[] = [];
const plainTypes: Models = new Map<string, string>();

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<void>[] = [];
const whereTypes: Models = new Map<string, string>();

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);
}
Expand Down Expand Up @@ -178,7 +180,6 @@ generatorHandler({
...optionalDataPlainTasks,
...dataRelationTasks,
...optionalDataRelationTasks,
...enumTasks,
]);

const relationTasks: Promise<void>[] = [];
Expand All @@ -188,7 +189,8 @@ generatorHandler({
...options.dmmf.datamodel.models.map(async (e) => {
const model = RelationModel(
e,
mergeModels(plainTypes, enumTypes),
enumTypes,
plainTypes,
relationsAdditionalFields,
);
if (model) {
Expand Down

0 comments on commit 945efde

Please sign in to comment.