@autobe/compiler 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/AutoBeCompiler.d.ts +25 -5
- package/lib/AutoBeCompiler.js +24 -8
- package/lib/AutoBeCompiler.js.map +1 -1
- package/lib/AutoBeInterfaceCompiler.d.ts +26 -0
- package/lib/AutoBeInterfaceCompiler.js +81 -2
- package/lib/AutoBeInterfaceCompiler.js.map +1 -1
- package/lib/AutoBePrismaCompiler.d.ts +27 -2
- package/lib/AutoBePrismaCompiler.js +33 -5
- package/lib/AutoBePrismaCompiler.js.map +1 -1
- package/lib/AutoBeTypeScriptCompiler.d.ts +25 -0
- package/lib/AutoBeTypeScriptCompiler.js +26 -1
- package/lib/AutoBeTypeScriptCompiler.js.map +1 -1
- package/lib/prisma/writePrismaApplication.d.ts +4 -1
- package/lib/prisma/writePrismaApplication.js +93 -52
- package/lib/prisma/writePrismaApplication.js.map +1 -1
- package/package.json +2 -2
- package/src/AutoBeCompiler.ts +24 -17
- package/src/AutoBeInterfaceCompiler.ts +98 -5
- package/src/AutoBePrismaCompiler.ts +33 -4
- package/src/AutoBeTypeScriptCompiler.ts +26 -1
- package/src/prisma/writePrismaApplication.ts +133 -69
|
@@ -2,48 +2,64 @@ import { AutoBePrisma } from "@autobe/interface";
|
|
|
2
2
|
|
|
3
3
|
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
4
4
|
import { MapUtil } from "../utils/MapUtil";
|
|
5
|
+
import { StringUtil } from "../utils/StringUtil";
|
|
5
6
|
|
|
6
|
-
export function writePrismaApplication(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
export function writePrismaApplication(props: {
|
|
8
|
+
dbms: "postgres" | "sqlite";
|
|
9
|
+
application: AutoBePrisma.IApplication;
|
|
10
|
+
}): Record<string, string> {
|
|
11
|
+
for (const file of props.application.files)
|
|
10
12
|
for (const model of file.models) fillMappingName(model);
|
|
11
13
|
return {
|
|
12
14
|
...Object.fromEntries(
|
|
13
|
-
|
|
15
|
+
props.application.files
|
|
14
16
|
.filter((file) => file.filename !== "main.prisma")
|
|
15
|
-
.map((file) => [
|
|
17
|
+
.map((file) => [
|
|
18
|
+
file.filename,
|
|
19
|
+
writeFile({
|
|
20
|
+
...props,
|
|
21
|
+
file,
|
|
22
|
+
}),
|
|
23
|
+
]),
|
|
16
24
|
),
|
|
17
|
-
"main.prisma":
|
|
25
|
+
"main.prisma":
|
|
26
|
+
props.dbms === "postgres" ? POSTGRES_MAIN_FILE : SQLITE_MAIN_FILE,
|
|
18
27
|
};
|
|
19
28
|
}
|
|
20
29
|
|
|
21
|
-
function writeFile(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
function writeFile(props: {
|
|
31
|
+
dbms: "postgres" | "sqlite";
|
|
32
|
+
application: AutoBePrisma.IApplication;
|
|
33
|
+
file: AutoBePrisma.IFile;
|
|
34
|
+
}): string {
|
|
35
|
+
return props.file.models
|
|
36
|
+
.map((model) =>
|
|
37
|
+
writeModel({
|
|
38
|
+
...props,
|
|
39
|
+
model,
|
|
40
|
+
}),
|
|
41
|
+
)
|
|
42
|
+
.join("\n\n");
|
|
26
43
|
}
|
|
27
44
|
|
|
28
|
-
function writeModel(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
45
|
+
function writeModel(props: {
|
|
46
|
+
dbms: "postgres" | "sqlite";
|
|
47
|
+
application: AutoBePrisma.IApplication;
|
|
48
|
+
file: AutoBePrisma.IFile;
|
|
49
|
+
model: AutoBePrisma.IModel;
|
|
50
|
+
}): string {
|
|
33
51
|
return [
|
|
34
52
|
writeComment(
|
|
35
53
|
[
|
|
36
|
-
model.description,
|
|
54
|
+
props.model.description,
|
|
37
55
|
"",
|
|
38
|
-
...(model.material ? [] : [`@namespace ${file.namespace}`]),
|
|
56
|
+
...(props.model.material ? [] : [`@namespace ${props.file.namespace}`]),
|
|
39
57
|
"@author AutoBE - https://github.com/wrtnlabs/autobe",
|
|
40
58
|
].join("\n"),
|
|
41
59
|
),
|
|
42
|
-
`model ${model.name} {`,
|
|
60
|
+
`model ${props.model.name} {`,
|
|
43
61
|
indent(
|
|
44
|
-
ArrayUtil.paddle([writeColumns(
|
|
45
|
-
"\n",
|
|
46
|
-
),
|
|
62
|
+
ArrayUtil.paddle([writeColumns(props), writeRelations(props)]).join("\n"),
|
|
47
63
|
),
|
|
48
64
|
"}",
|
|
49
65
|
].join("\n");
|
|
@@ -66,33 +82,67 @@ function fillMappingName(model: AutoBePrisma.IModel): void {
|
|
|
66
82
|
/* -----------------------------------------------------------
|
|
67
83
|
COLUMNS
|
|
68
84
|
----------------------------------------------------------- */
|
|
69
|
-
function writeColumns(
|
|
85
|
+
function writeColumns(props: {
|
|
86
|
+
dbms: "postgres" | "sqlite";
|
|
87
|
+
model: AutoBePrisma.IModel;
|
|
88
|
+
}): string[] {
|
|
70
89
|
return [
|
|
71
90
|
"//----",
|
|
72
91
|
"// COLUMNS",
|
|
73
92
|
"//----",
|
|
74
|
-
writePrimary(
|
|
75
|
-
|
|
76
|
-
|
|
93
|
+
writePrimary({
|
|
94
|
+
dbms: props.dbms,
|
|
95
|
+
field: props.model.primaryField,
|
|
96
|
+
}),
|
|
97
|
+
...props.model.foreignFields
|
|
98
|
+
.map((x) => [
|
|
99
|
+
"",
|
|
100
|
+
writeField({
|
|
101
|
+
dbms: props.dbms,
|
|
102
|
+
field: x,
|
|
103
|
+
}),
|
|
104
|
+
])
|
|
105
|
+
.flat(),
|
|
106
|
+
...props.model.plainFields
|
|
107
|
+
.map((x) => [
|
|
108
|
+
"",
|
|
109
|
+
writeField({
|
|
110
|
+
dbms: props.dbms,
|
|
111
|
+
field: x,
|
|
112
|
+
}),
|
|
113
|
+
])
|
|
114
|
+
.flat(),
|
|
77
115
|
];
|
|
78
116
|
}
|
|
79
117
|
|
|
80
|
-
function writePrimary(
|
|
118
|
+
function writePrimary(props: {
|
|
119
|
+
dbms: "postgres" | "sqlite";
|
|
120
|
+
field: AutoBePrisma.IPrimaryField;
|
|
121
|
+
}): string {
|
|
122
|
+
const type: string | undefined =
|
|
123
|
+
props.dbms === "postgres" ? POSTGRES_PHYSICAL_TYPES.uuid : undefined;
|
|
81
124
|
return [
|
|
82
|
-
writeComment(field.description),
|
|
83
|
-
`${field.name} String @id
|
|
125
|
+
writeComment(props.field.description),
|
|
126
|
+
`${props.field.name} String @id${type ? ` ${type}` : ""}`,
|
|
84
127
|
].join("\n");
|
|
85
128
|
}
|
|
86
129
|
|
|
87
|
-
function writeField(
|
|
88
|
-
|
|
130
|
+
function writeField(props: {
|
|
131
|
+
dbms: "postgres" | "sqlite";
|
|
132
|
+
field: AutoBePrisma.IPlainField;
|
|
133
|
+
}): string {
|
|
134
|
+
const logical: string = LOGICAL_TYPES[props.field.type];
|
|
89
135
|
const physical: string | undefined =
|
|
90
|
-
|
|
136
|
+
props.dbms === "postgres"
|
|
137
|
+
? POSTGRES_PHYSICAL_TYPES[
|
|
138
|
+
props.field.type as keyof typeof POSTGRES_PHYSICAL_TYPES
|
|
139
|
+
]
|
|
140
|
+
: undefined;
|
|
91
141
|
return [
|
|
92
|
-
writeComment(field.description),
|
|
142
|
+
writeComment(props.field.description),
|
|
93
143
|
[
|
|
94
|
-
field.name,
|
|
95
|
-
`${logical}${field.nullable ? "?" : ""}`,
|
|
144
|
+
props.field.name,
|
|
145
|
+
`${logical}${props.field.nullable ? "?" : ""}`,
|
|
96
146
|
...(physical ? [physical] : []),
|
|
97
147
|
].join(" "),
|
|
98
148
|
].join("\n");
|
|
@@ -101,21 +151,23 @@ function writeField(field: AutoBePrisma.IPlainField): string {
|
|
|
101
151
|
/* -----------------------------------------------------------
|
|
102
152
|
RELATIONS
|
|
103
153
|
----------------------------------------------------------- */
|
|
104
|
-
function writeRelations(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
154
|
+
function writeRelations(props: {
|
|
155
|
+
dbms: "postgres" | "sqlite";
|
|
156
|
+
application: AutoBePrisma.IApplication;
|
|
157
|
+
model: AutoBePrisma.IModel;
|
|
158
|
+
}): string[] {
|
|
108
159
|
interface IHasRelationship {
|
|
109
160
|
modelName: string;
|
|
110
161
|
unique: boolean;
|
|
111
162
|
mappingName?: string;
|
|
112
163
|
}
|
|
113
|
-
const hasRelationships: IHasRelationship[] =
|
|
164
|
+
const hasRelationships: IHasRelationship[] = props.application.files
|
|
114
165
|
.map((otherFile) =>
|
|
115
166
|
otherFile.models.map((otherModel) =>
|
|
116
167
|
otherModel.foreignFields
|
|
117
168
|
.filter(
|
|
118
|
-
(otherForeign) =>
|
|
169
|
+
(otherForeign) =>
|
|
170
|
+
otherForeign.relation.targetModel === props.model.name,
|
|
119
171
|
)
|
|
120
172
|
.map((otherForeign) => ({
|
|
121
173
|
modelName: otherModel.name,
|
|
@@ -126,14 +178,14 @@ function writeRelations(
|
|
|
126
178
|
)
|
|
127
179
|
.flat(2);
|
|
128
180
|
const foreignIndexes: AutoBePrisma.IForeignField[] =
|
|
129
|
-
model.foreignFields.filter(
|
|
181
|
+
props.model.foreignFields.filter(
|
|
130
182
|
(f) =>
|
|
131
|
-
model.uniqueIndexes.every((u) => u.fieldNames[0] !== f.name) &&
|
|
183
|
+
props.model.uniqueIndexes.every((u) => u.fieldNames[0] !== f.name) &&
|
|
132
184
|
(f.unique === true ||
|
|
133
|
-
model.plainIndexes.every((p) => p.fieldNames[0] !== f.name)),
|
|
185
|
+
props.model.plainIndexes.every((p) => p.fieldNames[0] !== f.name)),
|
|
134
186
|
);
|
|
135
187
|
const contents: string[][] = [
|
|
136
|
-
model.foreignFields.map(writeConstraint),
|
|
188
|
+
props.model.foreignFields.map(writeConstraint),
|
|
137
189
|
hasRelationships.map((r) =>
|
|
138
190
|
[
|
|
139
191
|
r.mappingName ?? r.modelName,
|
|
@@ -143,9 +195,11 @@ function writeRelations(
|
|
|
143
195
|
),
|
|
144
196
|
foreignIndexes.map(writeForeignIndex),
|
|
145
197
|
[
|
|
146
|
-
...model.uniqueIndexes.map(writeUniqueIndex),
|
|
147
|
-
...model.plainIndexes.map(writePlainIndex),
|
|
148
|
-
...
|
|
198
|
+
...props.model.uniqueIndexes.map(writeUniqueIndex),
|
|
199
|
+
...props.model.plainIndexes.map(writePlainIndex),
|
|
200
|
+
...(props.dbms === "postgres"
|
|
201
|
+
? props.model.ginIndexes.map(writeGinIndex)
|
|
202
|
+
: []),
|
|
149
203
|
],
|
|
150
204
|
];
|
|
151
205
|
if (contents.every((c) => c.length === 0)) return [];
|
|
@@ -222,8 +276,7 @@ const LOGICAL_TYPES = {
|
|
|
222
276
|
uuid: "String",
|
|
223
277
|
uri: "String",
|
|
224
278
|
};
|
|
225
|
-
|
|
226
|
-
const PHYSICAL_TYPES = {
|
|
279
|
+
const POSTGRES_PHYSICAL_TYPES = {
|
|
227
280
|
int: "@db.Integer",
|
|
228
281
|
double: "@db.DoublePrecision",
|
|
229
282
|
uuid: "@db.Uuid",
|
|
@@ -231,21 +284,32 @@ const PHYSICAL_TYPES = {
|
|
|
231
284
|
uri: "@db.VarChar(80000)",
|
|
232
285
|
};
|
|
233
286
|
|
|
234
|
-
const
|
|
235
|
-
generator client {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
287
|
+
const POSTGRES_MAIN_FILE = StringUtil.trim`
|
|
288
|
+
generator client {
|
|
289
|
+
provider = "prisma-client-js"
|
|
290
|
+
previewFeatures = ["postgresqlExtensions", "views"]
|
|
291
|
+
binaryTargets = ["native"]
|
|
292
|
+
}
|
|
293
|
+
datasource db {
|
|
294
|
+
provider = "postgresql"
|
|
295
|
+
url = env("DATABASE_URL")
|
|
296
|
+
extensions = []
|
|
297
|
+
}
|
|
298
|
+
generator markdown {
|
|
299
|
+
provider = "prisma-markdown"
|
|
300
|
+
output = "../docs/ERD.md"
|
|
301
|
+
}
|
|
302
|
+
`;
|
|
303
|
+
const SQLITE_MAIN_FILE = StringUtil.trim`
|
|
304
|
+
generator client {
|
|
305
|
+
provider = "prisma-client-js"
|
|
306
|
+
}
|
|
307
|
+
datasource db {
|
|
308
|
+
provider = "sqlite"
|
|
309
|
+
url = "file:./prisma.db"
|
|
310
|
+
}
|
|
311
|
+
generator markdown {
|
|
312
|
+
provider = "prisma-markdown"
|
|
313
|
+
output = "../docs/ERD.md"
|
|
314
|
+
}
|
|
315
|
+
`;
|