forked from adeyahya/prisma-typebox-generator
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmodel.ts
More file actions
157 lines (137 loc) · 4.75 KB
/
model.ts
File metadata and controls
157 lines (137 loc) · 4.75 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { getConfig } from "./config";
import { processedEnums } from "./generators/enum";
import { processedInclude } from "./generators/include";
import { processedOrderBy } from "./generators/orderBy";
import { processedPlain } from "./generators/plain";
import { processedPlainInputCreate } from "./generators/plainInputCreate";
import { processedPlainInputUpdate } from "./generators/plainInputUpdate";
import {
processedRelations,
processedRelationsInputCreate,
processedRelationsInputUpdate,
} from "./generators/relations";
import { processedSelect } from "./generators/select";
import {
transformDateImportStatement,
transformDateType,
} from "./generators/transformDate";
import { processedWhere, processedWhereUnique } from "./generators/where";
import { makeComposite } from "./generators/wrappers/composite";
import { nullableImport, nullableType } from "./generators/wrappers/nullable";
export type ProcessedModel = {
name: string;
stringRepresentation: string;
};
function convertModelToStandalone(
input: Pick<ProcessedModel, "name" | "stringRepresentation">,
) {
const generatedName = `${getConfig().exportedTypePrefix}${input.name}`;
let exportStr = `export const ${generatedName} = ${input.stringRepresentation}\n`;
if (getConfig().generateTsTypes) {
exportStr += `export type ${generatedName} = ${getConfig().unwrapSchemaImportName}<typeof ${generatedName}>\n`
}
return exportStr;
}
function typepoxImportStatement() {
let imports = getConfig().typeboxImportVariableName
if (getConfig().generateTsTypes) {
imports += `, type ${getConfig().unwrapSchemaImportName}`;
}
return `import { ${imports} } from "${
getConfig().typeboxImportDependencyName
}"\n`;
}
export function mapAllModelsForWrite() {
const modelsPerName = new Map<
ProcessedModel["name"],
ProcessedModel["stringRepresentation"]
>();
const process = (models: ProcessedModel[], suffix: string) => {
for (const processedModel of models) {
const standalone = convertModelToStandalone({
...processedModel,
name: `${processedModel.name}${suffix}`,
});
const current = modelsPerName.get(processedModel.name);
if (current) {
modelsPerName.set(processedModel.name, `${current}\n${standalone}`);
} else {
modelsPerName.set(processedModel.name, standalone);
}
}
};
process(processedEnums, "");
process(processedPlain, "Plain");
process(processedRelations, "Relations");
process(processedPlainInputCreate, "PlainInputCreate");
process(processedPlainInputUpdate, "PlainInputUpdate");
process(processedRelationsInputCreate, "RelationsInputCreate");
process(processedRelationsInputUpdate, "RelationsInputUpdate");
process(processedWhere, "Where");
process(processedWhereUnique, "WhereUnique");
process(processedSelect, "Select");
process(processedInclude, "Include");
process(processedOrderBy, "OrderBy");
for (const [key, value] of modelsPerName) {
const plain = processedPlain.find((e) => e.name === key);
const relations = processedRelations.find((e) => e.name === key);
let composite: string;
if (plain && relations) {
composite = makeComposite([`${key}Plain`, `${key}Relations`]);
} else if (plain) {
composite = `${key}Plain`;
} else if (relations) {
composite = `${key}Relations`;
} else {
continue;
}
modelsPerName.set(
key,
`${value}\n${convertModelToStandalone({
name: key,
stringRepresentation: composite,
})}`,
);
}
for (const [key, value] of modelsPerName) {
const create = processedRelationsInputCreate.find((e) => e.name === key);
if (create) {
const composite = makeComposite([
`${key}PlainInputCreate`,
`${key}RelationsInputCreate`,
]);
modelsPerName.set(
key,
`${value}\n${convertModelToStandalone({
name: `${key}InputCreate`,
stringRepresentation: composite,
})}`,
);
}
}
for (const [key, value] of modelsPerName) {
const update = processedRelationsInputUpdate.find((e) => e.name === key);
if (update) {
const composite = makeComposite([
`${key}PlainInputUpdate`,
`${key}RelationsInputUpdate`,
]);
modelsPerName.set(
key,
`${value}\n${convertModelToStandalone({
name: `${key}InputUpdate`,
stringRepresentation: composite,
})}`,
);
}
}
for (const [key, value] of modelsPerName) {
modelsPerName.set(
key,
`${typepoxImportStatement()}\n${transformDateImportStatement()}\n${nullableImport()}\n${value}`,
);
}
modelsPerName.set(getConfig().nullableName, nullableType());
modelsPerName.set(getConfig().transformDateName, transformDateType());
return modelsPerName;
}