Skip to content

Commit

Permalink
⬆️ improvement:
Browse files Browse the repository at this point in the history
  • Loading branch information
m1212e committed Mar 20, 2024
1 parent 17134a6 commit 57643cd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 93 deletions.
5 changes: 4 additions & 1 deletion src/generator/merge.ts → src/generator/composite.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Models } from "../util/modelMap";
import { parseDocumentation } from "./documentation";
import { typeboxImportVariableName } from "./typeboxImport";

export function Composite(models: Models) {
const modelList = Array.from(models.entries())
.map(([name, _content]) => name)
.join(", ");

return `${typeboxImportVariableName}.Composite([${modelList}])`;
const modelDoc = parseDocumentation(`Composition of ${modelList}`);

return `${typeboxImportVariableName}.Composite([${modelList}], ${modelDoc.options})`;
}
11 changes: 6 additions & 5 deletions src/generator/enum.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { DMMF } from "@prisma/generator-helper";
import { typeboxImportVariableName } from "./typeboxImport";
import { Annotation, parseDocumentation } from "./documentation";

export function Enum(
data: Pick<DMMF.DatamodelEnum, "values">,
options?: string
data: Pick<DMMF.DatamodelEnum, "values" | "documentation">
) {
const modelDoc = parseDocumentation(data.documentation);
if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined;

const variantsString = data.values
.map((v) => `${typeboxImportVariableName}.Literal('${v.name}')`)
.join(",");

return `${typeboxImportVariableName}.Union([${variantsString}]${
options ? "," + options : ""
});`;
return `${typeboxImportVariableName}.Union([${variantsString}],${modelDoc.options});`;
}
153 changes: 77 additions & 76 deletions src/generator/plainModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,100 @@ import type { DMMF } from "@prisma/generator-helper";
import { typeboxImportVariableName } from "./typeboxImport";
import { Annotation, parseDocumentation } from "./documentation";

export function PlainModel(data: Pick<DMMF.Model, "fields">, options?: string) {
const fields = data.fields
.map((field) => {
if (!isPrimitivePrismaFieldType(field.type)) return undefined;
const doc = parseDocumentation(field.documentation);
if (doc.annotations.includes(Annotation.HIDDEN)) return undefined;
export function PlainModel(data: Pick<DMMF.Model, "fields" | "documentation">) {
const modelDoc = parseDocumentation(data.documentation);
if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined;

return PrimitiveField({
name: field.name,
fieldType: field.type,
list: field.isList,
optional: !field.isRequired,
options: doc.options,
});
})
.filter((x) => x) as string[];
const fields = data.fields
.map((field) => {
if (!isPrimitivePrismaFieldType(field.type)) return undefined;
const doc = parseDocumentation(field.documentation);
if (doc.annotations.includes(Annotation.HIDDEN)) return undefined;

return `${typeboxImportVariableName}.Object({${fields.join(",")}}${
options ? "," + options : ""
})\n`;
return PrimitiveField({
name: field.name,
fieldType: field.type,
list: field.isList,
optional: !field.isRequired,
options: doc.options,
});
})
.filter((x) => x) as string[];

return `${typeboxImportVariableName}.Object({${fields.join(",")}},${modelDoc.options})\n`;
}

type PrimitivePrismaFieldType =
| "Int"
| "Float"
| "Decimal"
| "BigInt"
| "String"
| "DateTime"
| "Json"
| "Date"
| "Boolean";
| "Int"
| "Float"
| "Decimal"
| "BigInt"
| "String"
| "DateTime"
| "Json"
| "Date"
| "Boolean";

export function isPrimitivePrismaFieldType(
str: string,
str: string
): str is PrimitivePrismaFieldType {
return [
"Int",
"BigInt",
"Float",
"Decimal",
"String",
"DateTime",
"Date",
"Json",
"Boolean",
].includes(str);
return [
"Int",
"BigInt",
"Float",
"Decimal",
"String",
"DateTime",
"Date",
"Json",
"Boolean",
].includes(str);
}

function PrimitiveField({
name,
fieldType,
options,
optional,
list,
name,
fieldType,
options,
optional,
list,
}: {
fieldType: PrimitivePrismaFieldType;
options: string;
name: string;
optional: boolean;
list: boolean;
fieldType: PrimitivePrismaFieldType;
options: string;
name: string;
optional: boolean;
list: boolean;
}) {
let ret = `${name}: `;
let ret = `${name}: `;

if (optional) {
ret += `${typeboxImportVariableName}.Optional(`;
}
if (optional) {
ret += `${typeboxImportVariableName}.Optional(`;
}

if (list) {
ret += `${typeboxImportVariableName}.Array(`;
}
if (list) {
ret += `${typeboxImportVariableName}.Array(`;
}

if (["Int", "BigInt"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Integer(${options})`;
} else if (["Float", "Decimal"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Number(${options})`;
} else if (fieldType === "String") {
ret += `${typeboxImportVariableName}.String(${options})`;
} else if (["DateTime", "Date"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Date(${options})`;
} else if (fieldType === "Json") {
ret += `${typeboxImportVariableName}.Any(${options})`;
} else if (fieldType === "Boolean") {
ret += `${typeboxImportVariableName}.Boolean(${options})`;
} else throw new Error("Invalid type for primitive generation");
if (["Int", "BigInt"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Integer(${options})`;
} else if (["Float", "Decimal"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Number(${options})`;
} else if (fieldType === "String") {
ret += `${typeboxImportVariableName}.String(${options})`;
} else if (["DateTime", "Date"].includes(fieldType)) {
ret += `${typeboxImportVariableName}.Date(${options})`;
} else if (fieldType === "Json") {
ret += `${typeboxImportVariableName}.Any(${options})`;
} else if (fieldType === "Boolean") {
ret += `${typeboxImportVariableName}.Boolean(${options})`;
} else throw new Error("Invalid type for primitive generation");

if (optional) {
ret += ")";
}
if (optional) {
ret += ")";
}

if (list) {
ret += ")";
}
if (list) {
ret += ")";
}

return ret;
return ret;
}
16 changes: 9 additions & 7 deletions src/generator/relationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import type { Models } from "../util/modelMap";
import { isPrimitivePrismaFieldType } from "./plainModel";

export function RelationModel(
data: Pick<DMMF.Model, "fields">,
referenceablePlainModels: Models,
options?: string
data: Pick<DMMF.Model, "fields" | "documentation">,
referenceablePlainModels: Models
) {
const modelDoc = parseDocumentation(data.documentation);
if (modelDoc.annotations.includes(Annotation.HIDDEN)) return undefined;

const fields = data.fields
.map((field) => {
if (isPrimitivePrismaFieldType(field.type)) return undefined;
Expand All @@ -26,9 +28,7 @@ export function RelationModel(
})
.filter((x) => x) as string[];

return `${typeboxImportVariableName}.Object({${fields.join(",")}}${
options ? "," + options : ""
})\n`;
return `${typeboxImportVariableName}.Object({${fields.join(",")}},${modelDoc.options})\n`;
}

function RelationField({
Expand Down Expand Up @@ -64,7 +64,9 @@ function RelationField({

const referencedFieldModel = referenceablePlainModels.get(fieldType);
if (!referencedFieldModel) {
console.warn(`Could not find model for field type: ${fieldType}. It may annotated as hidden. Ignoring field: ${name} (${fieldType})`);
console.warn(
`Could not find model for field type: ${fieldType}. It may annotated as hidden. Ignoring field: ${name} (${fieldType})`
);
return undefined;
}
ret += referencedFieldModel;
Expand Down
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Models } from "./util/modelMap";
import { Compose } from "./generator/composer";
import { RelationModel } from "./generator/relationModel";
import { setAdditionalProperties } from "./generator/documentation";
import { Composite } from "./generator/merge";
import { Composite } from "./generator/composite";

generatorHandler({
onManifest() {
Expand Down Expand Up @@ -58,13 +58,19 @@ generatorHandler({

plainTasks.push(
...options.dmmf.datamodel.enums.map(async (e) => {
plainTypes.set(e.name, Enum(e));
const en = Enum(e);
if (en) {
plainTypes.set(e.name, en);
}
})
);

plainTasks.push(
...options.dmmf.datamodel.models.map(async (e) => {
plainTypes.set(e.name, PlainModel(e));
const model = PlainModel(e);
if (model) {
plainTypes.set(e.name, model);
}
})
);
await Promise.all(plainTasks);
Expand All @@ -74,7 +80,10 @@ generatorHandler({

relationTasks.push(
...options.dmmf.datamodel.models.map(async (e) => {
relationTypes.set(e.name, RelationModel(e, plainTypes));
const model = RelationModel(e, plainTypes);
if (model) {
relationTypes.set(e.name, model);
}
})
);
await Promise.all(relationTasks);
Expand Down

0 comments on commit 57643cd

Please sign in to comment.