forked from adeyahya/prisma-typebox-generator
-
Notifications
You must be signed in to change notification settings - Fork 6
/
plain.ts
142 lines (125 loc) · 4.36 KB
/
plain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 { processedEnums } from "./enum";
import {
type PrimitivePrismaFieldType,
isPrimitivePrismaFieldType,
stringifyPrimitiveType,
} from "./primitiveField";
import { wrapWithArray } from "./wrappers/array";
import { wrapWithNullable } from "./wrappers/nullable";
import { wrapWithOptional } from "./wrappers/optional";
export const processedPlain: ProcessedModel[] = [];
export function processPlain(models: DMMF.Model[] | Readonly<DMMF.Model[]>) {
for (const m of models) {
const o = stringifyPlain(m);
if (o) {
processedPlain.push({ name: m.name, stringRepresentation: o });
}
}
Object.freeze(processedPlain);
}
export function stringifyPlain(
data: DMMF.Model,
isInputModelCreate = false,
isInputModelUpdate = false,
) {
const annotations = extractAnnotations(data.documentation);
if (
annotations.isHidden ||
((isInputModelCreate || isInputModelUpdate) && annotations.isHiddenInput) ||
(isInputModelCreate && annotations.isHiddenInputCreate) ||
(isInputModelUpdate && annotations.isHiddenInputUpdate)
)
return undefined;
const fields = data.fields
.map((field) => {
const annotations = extractAnnotations(field.documentation);
if (
annotations.isHidden ||
((isInputModelCreate || isInputModelUpdate) &&
annotations.isHiddenInput) ||
(isInputModelCreate && annotations.isHiddenInputCreate) ||
(isInputModelUpdate && annotations.isHiddenInputUpdate)
)
return undefined;
// ===============================
// INPUT MODEL FILTERS
// ===============================
// if we generate an input model we want to omit certain fields
if (
getConfig().ignoreIdOnInputModel &&
(isInputModelCreate || isInputModelUpdate) &&
field.isId
)
return undefined;
if (
getConfig().ignoreCreatedAtOnInputModel &&
(isInputModelCreate || isInputModelUpdate) &&
field.name === "createdAt" &&
field.hasDefaultValue
)
return undefined;
if (
getConfig().ignoreUpdatedAtOnInputModel &&
(isInputModelCreate || isInputModelUpdate) &&
field.isUpdatedAt
)
return undefined;
if (
(isInputModelCreate || isInputModelUpdate) &&
(field.name.toLowerCase().endsWith("id") ||
field.name.toLowerCase().endsWith("foreign") ||
field.name.toLowerCase().endsWith("foreignkey"))
) {
return undefined;
}
// ===============================
// INPUT MODEL FILTERS END
// ===============================
let stringifiedType = "";
if (isPrimitivePrismaFieldType(field.type)) {
stringifiedType = stringifyPrimitiveType({
fieldType: field.type as PrimitivePrismaFieldType,
options: generateTypeboxOptions({ input: annotations }),
});
} else if (processedEnums.find((e) => e.name === field.type)) {
// biome-ignore lint/style/noNonNullAssertion: we checked this manually
stringifiedType = processedEnums.find(
(e) => e.name === field.type,
)!.stringRepresentation;
} else {
return undefined;
}
if (field.isList) {
stringifiedType = wrapWithArray(stringifiedType);
}
let madeOptional = false;
if (!field.isRequired) {
stringifiedType = wrapWithNullable(stringifiedType);
if (isInputModelCreate) {
stringifiedType = wrapWithOptional(stringifiedType);
madeOptional = true;
}
}
if (
!madeOptional &&
field.hasDefaultValue &&
(isInputModelCreate || isInputModelUpdate)
) {
stringifiedType = wrapWithOptional(stringifiedType);
madeOptional = true;
}
return `${field.name}: ${stringifiedType}`;
})
.filter((x) => x) as string[];
return `${getConfig().typeboxImportVariableName}.Object({${[
...fields,
!(isInputModelCreate || isInputModelUpdate)
? (getConfig().additionalFieldsPlain ?? [])
: [],
].join(",")}},${generateTypeboxOptions({ input: annotations })})\n`;
}