@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.
@@ -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
- app: AutoBePrisma.IApplication,
8
- ): Record<string, string> {
9
- for (const file of app.files)
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
- app.files
15
+ props.application.files
14
16
  .filter((file) => file.filename !== "main.prisma")
15
- .map((file) => [file.filename, writeFile(app, file)]),
17
+ .map((file) => [
18
+ file.filename,
19
+ writeFile({
20
+ ...props,
21
+ file,
22
+ }),
23
+ ]),
16
24
  ),
17
- "main.prisma": MAIN_FILE,
25
+ "main.prisma":
26
+ props.dbms === "postgres" ? POSTGRES_MAIN_FILE : SQLITE_MAIN_FILE,
18
27
  };
19
28
  }
20
29
 
21
- function writeFile(
22
- app: AutoBePrisma.IApplication,
23
- file: AutoBePrisma.IFile,
24
- ): string {
25
- return file.models.map((model) => writeModel(app, file, model)).join("\n\n");
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
- app: AutoBePrisma.IApplication,
30
- file: AutoBePrisma.IFile,
31
- model: AutoBePrisma.IModel,
32
- ): string {
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(model), writeRelations(app, model)]).join(
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(model: AutoBePrisma.IModel): string[] {
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(model.primaryField),
75
- ...model.foreignFields.map((x) => ["", writeField(x)]).flat(),
76
- ...model.plainFields.map((x) => ["", writeField(x)]).flat(),
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(field: AutoBePrisma.IPrimaryField): string {
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 @db.Uuid`,
125
+ writeComment(props.field.description),
126
+ `${props.field.name} String @id${type ? ` ${type}` : ""}`,
84
127
  ].join("\n");
85
128
  }
86
129
 
87
- function writeField(field: AutoBePrisma.IPlainField): string {
88
- const logical: string = LOGICAL_TYPES[field.type];
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
- PHYSICAL_TYPES[field.type as keyof typeof PHYSICAL_TYPES];
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
- app: AutoBePrisma.IApplication,
106
- model: AutoBePrisma.IModel,
107
- ): string[] {
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[] = app.files
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) => otherForeign.relation.targetModel === model.name,
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
- ...model.ginIndexes.map(writeGinIndex),
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 MAIN_FILE = `
235
- generator client {
236
- provider = "prisma-client-js"
237
- previewFeatures = ["postgresqlExtensions", "views"]
238
- binaryTargets = ["native"]
239
- }
240
-
241
- datasource db {
242
- provider = "postgresql"
243
- url = env("DATABASE_URL")
244
- extensions = []
245
- }
246
-
247
- generator markdown {
248
- provider = "prisma-markdown"
249
- output = "../docs/ERD.md"
250
- }
251
- `.trim();
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
+ `;