@autobe/compiler 0.28.1 → 0.29.1
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/prisma/AutoBePrismaCompiler.js +2 -2
- package/lib/prisma/AutoBePrismaCompiler.js.map +1 -1
- package/lib/utils/FilePrinter.js +9 -1
- package/lib/utils/FilePrinter.js.map +1 -1
- package/package.json +6 -6
- package/src/prisma/AutoBePrismaCompiler.ts +1 -1
- package/src/utils/FilePrinter.ts +12 -1
- package/lib/prisma/writePrismaApplication.d.ts +0 -5
- package/lib/prisma/writePrismaApplication.js +0 -341
- package/lib/prisma/writePrismaApplication.js.map +0 -1
- package/src/prisma/writePrismaApplication.ts +0 -438
|
@@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.AutoBePrismaCompiler = void 0;
|
|
13
|
+
const utils_1 = require("@autobe/utils");
|
|
13
14
|
const embed_prisma_1 = require("embed-prisma");
|
|
14
15
|
const validatePrismaApplication_1 = require("./validatePrismaApplication");
|
|
15
|
-
const writePrismaApplication_1 = require("./writePrismaApplication");
|
|
16
16
|
class AutoBePrismaCompiler {
|
|
17
17
|
compile(props) {
|
|
18
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -27,7 +27,7 @@ class AutoBePrismaCompiler {
|
|
|
27
27
|
}
|
|
28
28
|
write(application_1) {
|
|
29
29
|
return __awaiter(this, arguments, void 0, function* (application, dbms = "postgres") {
|
|
30
|
-
return (0,
|
|
30
|
+
return (0, utils_1.writePrismaApplication)({
|
|
31
31
|
application,
|
|
32
32
|
dbms,
|
|
33
33
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AutoBePrismaCompiler.js","sourceRoot":"","sources":["../../src/prisma/AutoBePrismaCompiler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,+CAA2C;AAE3C,2EAAwE;
|
|
1
|
+
{"version":3,"file":"AutoBePrismaCompiler.js","sourceRoot":"","sources":["../../src/prisma/AutoBePrismaCompiler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,yCAAuD;AACvD,+CAA2C;AAE3C,2EAAwE;AAExE,MAAa,oBAAoB;IAClB,OAAO,CAClB,KAAiC;;YAEjC,MAAM,QAAQ,GAAgB,IAAI,0BAAW,EAAE,CAAC;YAChD,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;KAAA;IAEY,QAAQ,CACnB,WAAsC;;YAEtC,OAAO,IAAA,qDAAyB,EAAC,WAAW,CAAC,CAAC;QAChD,CAAC;KAAA;IAEY,KAAK;6DAChB,WAAsC,EACtC,OAA8B,UAAU;YAExC,OAAO,IAAA,8BAAsB,EAAC;gBAC5B,WAAW;gBACX,IAAI;aACL,CAAC,CAAC;QACL,CAAC;KAAA;CACF;AAvBD,oDAuBC"}
|
package/lib/utils/FilePrinter.js
CHANGED
|
@@ -22,7 +22,15 @@ var FilePrinter;
|
|
|
22
22
|
FilePrinter.description = (node, comment) => {
|
|
23
23
|
if (comment.length === 0)
|
|
24
24
|
return node;
|
|
25
|
-
typescript_1.default.addSyntheticLeadingComment(node, typescript_1.default.SyntaxKind.MultiLineCommentTrivia, [
|
|
25
|
+
typescript_1.default.addSyntheticLeadingComment(node, typescript_1.default.SyntaxKind.MultiLineCommentTrivia, [
|
|
26
|
+
"*",
|
|
27
|
+
...comment
|
|
28
|
+
.split("\r\n")
|
|
29
|
+
.join("\n")
|
|
30
|
+
.split("\n")
|
|
31
|
+
.map((str) => ` * ${str.split("*/").join("*\\\\/").split("*\\/").join("*\\\\/")}`),
|
|
32
|
+
"",
|
|
33
|
+
].join("\n"), true);
|
|
26
34
|
return node;
|
|
27
35
|
};
|
|
28
36
|
FilePrinter.newLine = () => typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createIdentifier("\n"));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FilePrinter.js","sourceRoot":"","sources":["../../src/utils/FilePrinter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,yGAA+D;AAC/D,sDAA8B;AAC9B,uCAAkC;AAClC,4DAA4B;AAE5B,IAAiB,WAAW,
|
|
1
|
+
{"version":3,"file":"FilePrinter.js","sourceRoot":"","sources":["../../src/utils/FilePrinter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,yGAA+D;AAC/D,sDAA8B;AAC9B,uCAAkC;AAClC,4DAA4B;AAE5B,IAAiB,WAAW,CA6D3B;AA7DD,WAAiB,WAAW;IACb,uBAAW,GAAG,CACzB,IAAU,EACV,OAAe,EACT,EAAE;QACR,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACtC,oBAAE,CAAC,0BAA0B,CAC3B,IAAI,EACJ,oBAAE,CAAC,UAAU,CAAC,sBAAsB,EACpC;YACE,GAAG;YACH,GAAG,OAAO;iBACP,KAAK,CAAC,MAAM,CAAC;iBACb,IAAI,CAAC,IAAI,CAAC;iBACV,KAAK,CAAC,IAAI,CAAC;iBACX,GAAG,CACF,CAAC,GAAG,EAAE,EAAE,CACN,MAAM,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CACtE;YACH,EAAE;SACH,CAAC,IAAI,CAAC,IAAI,CAAC,EACZ,IAAI,CACL,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEW,mBAAO,GAAG,GAAG,EAAE,CAC1B,oBAAE,CAAC,OAAO,CAAC,yBAAyB,CAAC,oBAAE,CAAC,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IAE7D,iBAAK,GAAG,CAAC,KAGrB,EAAU,EAAE;;QACX,MAAM,MAAM,GAAW,oBAAE;aACtB,aAAa,CAAC;YACb,OAAO,EAAE,oBAAE,CAAC,WAAW,CAAC,QAAQ;SACjC,CAAC;aACD,SAAS,CACR,oBAAE,CAAC,OAAO,CAAC,gBAAgB,CACzB,KAAK,CAAC,UAAU,EAChB,oBAAE,CAAC,OAAO,CAAC,WAAW,CAAC,oBAAE,CAAC,UAAU,CAAC,cAAc,CAAC,EACpD,oBAAE,CAAC,SAAS,CAAC,IAAI,CAClB,CACF,CAAC;QACJ,OAAO,CAAC,MAAA,KAAK,CAAC,GAAG,mCAAI,EAAE,CAAC,GAAG,MAAM,CAAC;IACpC,CAAC,CAAC;IAEW,oBAAQ,GAAG,CAAO,MAAc,EAAmB,EAAE;QAChE,IAAI,CAAC;YACH,OAAO,MAAM,IAAA,iBAAM,EAAC,MAAM,EAAE;gBAC1B,MAAM,EAAE,YAAY;gBACpB,OAAO,EAAE,CAAC,sCAAU,EAAE,MAAM,IAAA,iBAAO,EAAC,uBAAuB,CAAC,CAAC;gBAC7D,WAAW,EAAE,CAAC,uBAAuB,EAAE,OAAO,CAAC;gBAC/C,qBAAqB,EAAE,IAAI;gBAC3B,yBAAyB,EAAE,IAAI;gBAC/B,wBAAwB,EAAE,CAAC,mBAAmB,EAAE,YAAY,EAAE,KAAK,CAAC;aACrE,CAAC,CAAC;QACL,CAAC;QAAC,WAAM,CAAC;YACP,OAAO,MAAM,CAAC;QAChB,CAAC;IACH,CAAC,CAAA,CAAC;AACJ,CAAC,EA7DgB,WAAW,2BAAX,WAAW,QA6D3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autobe/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.1",
|
|
4
4
|
"description": "AI backend server code generator",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"author": "Wrtn Technologies",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"url": "https://github.com/wrtnlabs/autobe/issues"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@nestia/core": "^9.0.
|
|
17
|
-
"@nestia/migrate": "^9.0.
|
|
16
|
+
"@nestia/core": "^9.0.3",
|
|
17
|
+
"@nestia/migrate": "^9.0.3",
|
|
18
18
|
"@samchon/openapi": "^5.0.1",
|
|
19
19
|
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
|
|
20
20
|
"@typescript-eslint/eslint-plugin": "8.40.0",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"tgrid": "^1.2.0",
|
|
31
31
|
"tstl": "^3.0.0",
|
|
32
32
|
"typia": "^10.0.2",
|
|
33
|
-
"@autobe/filesystem": "^0.
|
|
34
|
-
"@autobe/interface": "^0.
|
|
35
|
-
"@autobe/utils": "^0.
|
|
33
|
+
"@autobe/filesystem": "^0.29.1",
|
|
34
|
+
"@autobe/interface": "^0.29.1",
|
|
35
|
+
"@autobe/utils": "^0.29.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^22.15.3",
|
|
@@ -5,10 +5,10 @@ import {
|
|
|
5
5
|
IAutoBePrismaCompilerProps,
|
|
6
6
|
IAutoBePrismaValidation,
|
|
7
7
|
} from "@autobe/interface";
|
|
8
|
+
import { writePrismaApplication } from "@autobe/utils";
|
|
8
9
|
import { EmbedPrisma } from "embed-prisma";
|
|
9
10
|
|
|
10
11
|
import { validatePrismaApplication } from "./validatePrismaApplication";
|
|
11
|
-
import { writePrismaApplication } from "./writePrismaApplication";
|
|
12
12
|
|
|
13
13
|
export class AutoBePrismaCompiler implements IAutoBePrismaCompiler {
|
|
14
14
|
public async compile(
|
package/src/utils/FilePrinter.ts
CHANGED
|
@@ -12,7 +12,18 @@ export namespace FilePrinter {
|
|
|
12
12
|
ts.addSyntheticLeadingComment(
|
|
13
13
|
node,
|
|
14
14
|
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
15
|
-
[
|
|
15
|
+
[
|
|
16
|
+
"*",
|
|
17
|
+
...comment
|
|
18
|
+
.split("\r\n")
|
|
19
|
+
.join("\n")
|
|
20
|
+
.split("\n")
|
|
21
|
+
.map(
|
|
22
|
+
(str) =>
|
|
23
|
+
` * ${str.split("*/").join("*\\\\/").split("*\\/").join("*\\\\/")}`,
|
|
24
|
+
),
|
|
25
|
+
"",
|
|
26
|
+
].join("\n"),
|
|
16
27
|
true,
|
|
17
28
|
);
|
|
18
29
|
return node;
|
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.writePrismaApplication = writePrismaApplication;
|
|
7
|
-
const utils_1 = require("@autobe/utils");
|
|
8
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
-
const ArrayUtil_1 = require("../utils/ArrayUtil");
|
|
10
|
-
function writePrismaApplication(props) {
|
|
11
|
-
for (const file of props.application.files)
|
|
12
|
-
for (const model of file.models)
|
|
13
|
-
fillMappingName(model);
|
|
14
|
-
return Object.assign(Object.assign({}, Object.fromEntries(props.application.files
|
|
15
|
-
.filter((file) => file.filename !== "main.prisma")
|
|
16
|
-
.map((file) => [
|
|
17
|
-
file.filename,
|
|
18
|
-
writeFile(Object.assign(Object.assign({}, props), { file })),
|
|
19
|
-
]))), { "main.prisma": props.dbms === "postgres" ? POSTGRES_MAIN_FILE : SQLITE_MAIN_FILE });
|
|
20
|
-
}
|
|
21
|
-
function writeFile(props) {
|
|
22
|
-
return props.file.models
|
|
23
|
-
.map((model) => writeModel(Object.assign(Object.assign({}, props), { model })))
|
|
24
|
-
.join("\n\n");
|
|
25
|
-
}
|
|
26
|
-
function writeModel(props) {
|
|
27
|
-
return [
|
|
28
|
-
writeComment([
|
|
29
|
-
props.model.description,
|
|
30
|
-
"",
|
|
31
|
-
...(props.model.material ? [] : [`@namespace ${props.file.namespace}`]),
|
|
32
|
-
"@author AutoBE - https://github.com/wrtnlabs/autobe",
|
|
33
|
-
].join("\n"), 80),
|
|
34
|
-
`model ${props.model.name} {`,
|
|
35
|
-
addIndent(ArrayUtil_1.ArrayUtil.paddle([writeColumns(props), writeRelations(props)]).join("\n")),
|
|
36
|
-
"}",
|
|
37
|
-
].join("\n");
|
|
38
|
-
}
|
|
39
|
-
function fillMappingName(model) {
|
|
40
|
-
const group = new Map();
|
|
41
|
-
for (const ff of model.foreignFields) {
|
|
42
|
-
utils_1.MapUtil.take(group, ff.relation.targetModel, () => []).push(ff);
|
|
43
|
-
if (ff.relation.targetModel == model.name)
|
|
44
|
-
ff.relation.mappingName = "recursive";
|
|
45
|
-
}
|
|
46
|
-
for (const array of group.values())
|
|
47
|
-
if (array.length !== 1)
|
|
48
|
-
for (const ff of array)
|
|
49
|
-
ff.relation.mappingName = shortName(`${model.name}_of_${ff.name}`);
|
|
50
|
-
}
|
|
51
|
-
/* -----------------------------------------------------------
|
|
52
|
-
COLUMNS
|
|
53
|
-
----------------------------------------------------------- */
|
|
54
|
-
function writeColumns(props) {
|
|
55
|
-
return [
|
|
56
|
-
"//----",
|
|
57
|
-
"// COLUMNS",
|
|
58
|
-
"//----",
|
|
59
|
-
writePrimary({
|
|
60
|
-
dbms: props.dbms,
|
|
61
|
-
model: props.model,
|
|
62
|
-
field: props.model.primaryField,
|
|
63
|
-
}),
|
|
64
|
-
...props.model.foreignFields
|
|
65
|
-
.map((x) => [
|
|
66
|
-
"",
|
|
67
|
-
writeField({
|
|
68
|
-
dbms: props.dbms,
|
|
69
|
-
field: x,
|
|
70
|
-
}),
|
|
71
|
-
])
|
|
72
|
-
.flat(),
|
|
73
|
-
...props.model.plainFields
|
|
74
|
-
.map((x) => [
|
|
75
|
-
"",
|
|
76
|
-
writeField({
|
|
77
|
-
dbms: props.dbms,
|
|
78
|
-
field: x,
|
|
79
|
-
}),
|
|
80
|
-
])
|
|
81
|
-
.flat(),
|
|
82
|
-
];
|
|
83
|
-
}
|
|
84
|
-
function writePrimary(props) {
|
|
85
|
-
const type = props.dbms === "postgres" ? POSTGRES_PHYSICAL_TYPES.uuid : undefined;
|
|
86
|
-
const pkeyName = `${props.model.name}__pkey`;
|
|
87
|
-
const signature = pkeyName.length <= MAX_IDENTIFIER_LENGTH
|
|
88
|
-
? "@id"
|
|
89
|
-
: `@id(map: "${shortName(pkeyName)}")`;
|
|
90
|
-
return [
|
|
91
|
-
writeComment(props.field.description, 78),
|
|
92
|
-
`${props.field.name} String ${signature}${type ? ` ${type}` : ""}`,
|
|
93
|
-
].join("\n");
|
|
94
|
-
}
|
|
95
|
-
function writeField(props) {
|
|
96
|
-
const logical = LOGICAL_TYPES[props.field.type];
|
|
97
|
-
const physical = props.dbms === "postgres"
|
|
98
|
-
? POSTGRES_PHYSICAL_TYPES[props.field.type]
|
|
99
|
-
: undefined;
|
|
100
|
-
return [
|
|
101
|
-
writeComment(props.field.description, 78),
|
|
102
|
-
[
|
|
103
|
-
props.field.name,
|
|
104
|
-
`${logical}${props.field.nullable ? "?" : ""}`,
|
|
105
|
-
...(physical ? [physical] : []),
|
|
106
|
-
].join(" "),
|
|
107
|
-
].join("\n");
|
|
108
|
-
}
|
|
109
|
-
/* -----------------------------------------------------------
|
|
110
|
-
RELATIONS
|
|
111
|
-
----------------------------------------------------------- */
|
|
112
|
-
function writeRelations(props) {
|
|
113
|
-
const hasRelationships = props.application.files
|
|
114
|
-
.map((otherFile) => otherFile.models.map((otherModel) => otherModel.foreignFields
|
|
115
|
-
.filter((otherForeign) => otherForeign.relation.targetModel === props.model.name)
|
|
116
|
-
.map((otherForeign) => ({
|
|
117
|
-
modelName: otherModel.name,
|
|
118
|
-
unique: otherForeign.unique,
|
|
119
|
-
mappingName: otherForeign.relation.mappingName,
|
|
120
|
-
}))))
|
|
121
|
-
.flat(2);
|
|
122
|
-
const foreignIndexes = props.model.foreignFields.filter((f) => {
|
|
123
|
-
if (f.unique === true)
|
|
124
|
-
return props.model.uniqueIndexes.every((u) => u.fieldNames.length !== 1 || u.fieldNames[0] !== f.name);
|
|
125
|
-
return (props.model.uniqueIndexes.every((u) => u.fieldNames[0] !== f.name) &&
|
|
126
|
-
props.model.plainIndexes.every((p) => p.fieldNames[0] !== f.name));
|
|
127
|
-
});
|
|
128
|
-
const contents = [
|
|
129
|
-
props.model.foreignFields.map((foreign) => writeConstraint({
|
|
130
|
-
dbms: props.dbms,
|
|
131
|
-
model: props.model,
|
|
132
|
-
foreign,
|
|
133
|
-
})),
|
|
134
|
-
hasRelationships.map((r) => {
|
|
135
|
-
var _a;
|
|
136
|
-
return [
|
|
137
|
-
(_a = r.mappingName) !== null && _a !== void 0 ? _a : r.modelName,
|
|
138
|
-
`${r.modelName}${r.unique ? "?" : "[]"}`,
|
|
139
|
-
...(r.mappingName ? [`@relation("${r.mappingName}")`] : []),
|
|
140
|
-
].join(" ");
|
|
141
|
-
}),
|
|
142
|
-
foreignIndexes.map((field) => writeForeignIndex({
|
|
143
|
-
model: props.model,
|
|
144
|
-
field,
|
|
145
|
-
})),
|
|
146
|
-
[
|
|
147
|
-
...props.model.uniqueIndexes.map((unique) => writeUniqueIndex({
|
|
148
|
-
model: props.model,
|
|
149
|
-
unique,
|
|
150
|
-
})),
|
|
151
|
-
...props.model.plainIndexes.map((plain) => writePlainIndex({
|
|
152
|
-
model: props.model,
|
|
153
|
-
plain,
|
|
154
|
-
})),
|
|
155
|
-
...(props.dbms === "postgres"
|
|
156
|
-
? props.model.ginIndexes.map((gin) => writeGinIndex({
|
|
157
|
-
model: props.model,
|
|
158
|
-
gin,
|
|
159
|
-
}))
|
|
160
|
-
: []),
|
|
161
|
-
],
|
|
162
|
-
];
|
|
163
|
-
if (contents.every((c) => c.length === 0))
|
|
164
|
-
return [];
|
|
165
|
-
return [
|
|
166
|
-
"//----",
|
|
167
|
-
"// RELATIONS",
|
|
168
|
-
"//----",
|
|
169
|
-
// paddled content
|
|
170
|
-
...ArrayUtil_1.ArrayUtil.paddle(contents),
|
|
171
|
-
];
|
|
172
|
-
}
|
|
173
|
-
function writeConstraint(props) {
|
|
174
|
-
// spellchecker:ignore-next-line
|
|
175
|
-
const name = `${props.model.name}_${props.foreign.name}_rela`;
|
|
176
|
-
const tooMuchLong = props.dbms === "postgres" && name.length > MAX_IDENTIFIER_LENGTH;
|
|
177
|
-
const body = [
|
|
178
|
-
props.foreign.relation.name,
|
|
179
|
-
`${props.foreign.relation.targetModel}${props.foreign.nullable ? "?" : ""}`,
|
|
180
|
-
`@relation(${[
|
|
181
|
-
...(props.foreign.relation.mappingName
|
|
182
|
-
? [`"${props.foreign.relation.mappingName}"`]
|
|
183
|
-
: []),
|
|
184
|
-
`fields: [${props.foreign.name}]`,
|
|
185
|
-
`references: [id]`,
|
|
186
|
-
`onDelete: Cascade`,
|
|
187
|
-
...(tooMuchLong ? [`map: "${shortName(name)}"`] : []),
|
|
188
|
-
].join(", ")})`,
|
|
189
|
-
].join(" ");
|
|
190
|
-
return tooMuchLong
|
|
191
|
-
? utils_1.StringUtil.trim `
|
|
192
|
-
// spellchecker: ignore-next-line
|
|
193
|
-
${body}
|
|
194
|
-
`
|
|
195
|
-
: body;
|
|
196
|
-
}
|
|
197
|
-
function writeForeignIndex(props) {
|
|
198
|
-
const name = `${props.model.name}_${props.field.name}_fkey`;
|
|
199
|
-
const prefix = `@@${props.field.unique === true ? "unique" : "index"}([${props.field.name}]`;
|
|
200
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH)
|
|
201
|
-
return `${prefix})`;
|
|
202
|
-
return utils_1.StringUtil.trim `
|
|
203
|
-
// spellchecker: ignore-next-line
|
|
204
|
-
${prefix}, map: "${shortName(name)}")
|
|
205
|
-
`;
|
|
206
|
-
}
|
|
207
|
-
function writeUniqueIndex(props) {
|
|
208
|
-
const name = `${props.model.name}_${props.unique.fieldNames.join("_")}_key`;
|
|
209
|
-
const prefix = `@@unique([${props.unique.fieldNames.join(", ")}]`;
|
|
210
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH)
|
|
211
|
-
return `${prefix})`;
|
|
212
|
-
return utils_1.StringUtil.trim `
|
|
213
|
-
// spellchecker: ignore-next-line
|
|
214
|
-
${prefix}, map: "${shortName(name)}")
|
|
215
|
-
`;
|
|
216
|
-
}
|
|
217
|
-
function writePlainIndex(props) {
|
|
218
|
-
const name = `${props.model.name}_${props.plain.fieldNames.join("_")}_idx`;
|
|
219
|
-
const prefix = `@@index([${props.plain.fieldNames.join(", ")}]`;
|
|
220
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH)
|
|
221
|
-
return `${prefix})`;
|
|
222
|
-
return utils_1.StringUtil.trim `
|
|
223
|
-
// spellchecker: ignore-next-line
|
|
224
|
-
${prefix}, map: "${shortName(name)}")
|
|
225
|
-
`;
|
|
226
|
-
}
|
|
227
|
-
function writeGinIndex(props) {
|
|
228
|
-
const name = `${props.model.name}_${props.gin.fieldName}_idx`;
|
|
229
|
-
const prefix = `@@index([${props.gin.fieldName}(ops: raw("gin_trgm_ops"))], type: Gin`;
|
|
230
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH)
|
|
231
|
-
return `${prefix})`;
|
|
232
|
-
return utils_1.StringUtil.trim `
|
|
233
|
-
// spellchecker: ignore-next-line
|
|
234
|
-
${prefix}, map: "${shortName(name)}")
|
|
235
|
-
`;
|
|
236
|
-
}
|
|
237
|
-
/* -----------------------------------------------------------
|
|
238
|
-
BACKGROUND
|
|
239
|
-
----------------------------------------------------------- */
|
|
240
|
-
function writeComment(content, length) {
|
|
241
|
-
return content
|
|
242
|
-
.split("\r\n")
|
|
243
|
-
.join("\n")
|
|
244
|
-
.split("\n")
|
|
245
|
-
.map((line) => line.trim())
|
|
246
|
-
.map((line) => {
|
|
247
|
-
// 77자에서 "/// " 4자를 뺀 73자가 실제 컨텐츠 최대 길이
|
|
248
|
-
if (line.length <= length - 4)
|
|
249
|
-
return [line];
|
|
250
|
-
const words = line.split(" ");
|
|
251
|
-
const result = [];
|
|
252
|
-
let currentLine = "";
|
|
253
|
-
for (const word of words) {
|
|
254
|
-
const potentialLine = currentLine ? `${currentLine} ${word}` : word;
|
|
255
|
-
if (potentialLine.length <= 73) {
|
|
256
|
-
currentLine = potentialLine;
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
if (currentLine)
|
|
260
|
-
result.push(currentLine);
|
|
261
|
-
currentLine = word;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
if (currentLine)
|
|
265
|
-
result.push(currentLine);
|
|
266
|
-
return result;
|
|
267
|
-
})
|
|
268
|
-
.flat()
|
|
269
|
-
.map((str) => `///${str.length ? ` ${str}` : ""}`)
|
|
270
|
-
.join("\n")
|
|
271
|
-
.trim();
|
|
272
|
-
}
|
|
273
|
-
function addIndent(content) {
|
|
274
|
-
return content
|
|
275
|
-
.split("\r\n")
|
|
276
|
-
.join("\n")
|
|
277
|
-
.split("\n")
|
|
278
|
-
.map((str) => ` ${str}`)
|
|
279
|
-
.join("\n");
|
|
280
|
-
}
|
|
281
|
-
function shortName(name) {
|
|
282
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH)
|
|
283
|
-
return name;
|
|
284
|
-
const hash = crypto_1.default
|
|
285
|
-
.createHash("md5")
|
|
286
|
-
.update(name)
|
|
287
|
-
.digest("hex")
|
|
288
|
-
.substring(0, HASH_TRUNCATION_LENGTH);
|
|
289
|
-
return `${name.substring(0, MAX_IDENTIFIER_LENGTH - HASH_TRUNCATION_LENGTH - 1)}_${hash}`;
|
|
290
|
-
}
|
|
291
|
-
const LOGICAL_TYPES = {
|
|
292
|
-
// native types
|
|
293
|
-
boolean: "Boolean",
|
|
294
|
-
int: "Int",
|
|
295
|
-
double: "Float",
|
|
296
|
-
string: "String",
|
|
297
|
-
// formats
|
|
298
|
-
datetime: "DateTime",
|
|
299
|
-
uuid: "String",
|
|
300
|
-
uri: "String",
|
|
301
|
-
};
|
|
302
|
-
const POSTGRES_PHYSICAL_TYPES = {
|
|
303
|
-
int: "@db.Integer",
|
|
304
|
-
double: "@db.DoublePrecision",
|
|
305
|
-
uuid: "@db.Uuid",
|
|
306
|
-
datetime: "@db.Timestamptz",
|
|
307
|
-
uri: "@db.VarChar(80000)",
|
|
308
|
-
};
|
|
309
|
-
const POSTGRES_MAIN_FILE = utils_1.StringUtil.trim `
|
|
310
|
-
generator client {
|
|
311
|
-
provider = "prisma-client-js"
|
|
312
|
-
engineType = "client"
|
|
313
|
-
previewFeatures = ["postgresqlExtensions", "views"]
|
|
314
|
-
}
|
|
315
|
-
datasource db {
|
|
316
|
-
provider = "postgresql"
|
|
317
|
-
url = env("DATABASE_URL")
|
|
318
|
-
extensions = [pg_trgm]
|
|
319
|
-
}
|
|
320
|
-
generator markdown {
|
|
321
|
-
provider = "prisma-markdown"
|
|
322
|
-
output = "../../docs/ERD.md"
|
|
323
|
-
}
|
|
324
|
-
`;
|
|
325
|
-
const SQLITE_MAIN_FILE = utils_1.StringUtil.trim `
|
|
326
|
-
generator client {
|
|
327
|
-
provider = "prisma-client-js"
|
|
328
|
-
engineType = "client"
|
|
329
|
-
}
|
|
330
|
-
datasource db {
|
|
331
|
-
provider = "sqlite"
|
|
332
|
-
url = "file:../db.sqlite"
|
|
333
|
-
}
|
|
334
|
-
generator markdown {
|
|
335
|
-
provider = "prisma-markdown"
|
|
336
|
-
output = "../../docs/ERD.md"
|
|
337
|
-
}
|
|
338
|
-
`;
|
|
339
|
-
const MAX_IDENTIFIER_LENGTH = 63;
|
|
340
|
-
const HASH_TRUNCATION_LENGTH = 8;
|
|
341
|
-
//# sourceMappingURL=writePrismaApplication.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"writePrismaApplication.js","sourceRoot":"","sources":["../../src/prisma/writePrismaApplication.ts"],"names":[],"mappings":";;;;;AAMA,wDAqBC;AA1BD,yCAAoD;AACpD,oDAA4B;AAE5B,kDAA+C;AAE/C,SAAg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ACF,MAAM,qBAAqB,GAAG,EAAE,CAAC;AACjC,MAAM,sBAAsB,GAAG,CAAC,CAAC"}
|
|
@@ -1,438 +0,0 @@
|
|
|
1
|
-
import { AutoBePrisma } from "@autobe/interface";
|
|
2
|
-
import { MapUtil, StringUtil } from "@autobe/utils";
|
|
3
|
-
import crypto from "crypto";
|
|
4
|
-
|
|
5
|
-
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
6
|
-
|
|
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)
|
|
12
|
-
for (const model of file.models) fillMappingName(model);
|
|
13
|
-
return {
|
|
14
|
-
...Object.fromEntries(
|
|
15
|
-
props.application.files
|
|
16
|
-
.filter((file) => file.filename !== "main.prisma")
|
|
17
|
-
.map((file) => [
|
|
18
|
-
file.filename,
|
|
19
|
-
writeFile({
|
|
20
|
-
...props,
|
|
21
|
-
file,
|
|
22
|
-
}),
|
|
23
|
-
]),
|
|
24
|
-
),
|
|
25
|
-
"main.prisma":
|
|
26
|
-
props.dbms === "postgres" ? POSTGRES_MAIN_FILE : SQLITE_MAIN_FILE,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
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");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function writeModel(props: {
|
|
46
|
-
dbms: "postgres" | "sqlite";
|
|
47
|
-
application: AutoBePrisma.IApplication;
|
|
48
|
-
file: AutoBePrisma.IFile;
|
|
49
|
-
model: AutoBePrisma.IModel;
|
|
50
|
-
}): string {
|
|
51
|
-
return [
|
|
52
|
-
writeComment(
|
|
53
|
-
[
|
|
54
|
-
props.model.description,
|
|
55
|
-
"",
|
|
56
|
-
...(props.model.material ? [] : [`@namespace ${props.file.namespace}`]),
|
|
57
|
-
"@author AutoBE - https://github.com/wrtnlabs/autobe",
|
|
58
|
-
].join("\n"),
|
|
59
|
-
80,
|
|
60
|
-
),
|
|
61
|
-
`model ${props.model.name} {`,
|
|
62
|
-
addIndent(
|
|
63
|
-
ArrayUtil.paddle([writeColumns(props), writeRelations(props)]).join("\n"),
|
|
64
|
-
),
|
|
65
|
-
"}",
|
|
66
|
-
].join("\n");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function fillMappingName(model: AutoBePrisma.IModel): void {
|
|
70
|
-
const group: Map<string, AutoBePrisma.IForeignField[]> = new Map();
|
|
71
|
-
for (const ff of model.foreignFields) {
|
|
72
|
-
MapUtil.take(group, ff.relation.targetModel, () => []).push(ff);
|
|
73
|
-
if (ff.relation.targetModel == model.name)
|
|
74
|
-
ff.relation.mappingName = "recursive";
|
|
75
|
-
}
|
|
76
|
-
for (const array of group.values())
|
|
77
|
-
if (array.length !== 1)
|
|
78
|
-
for (const ff of array)
|
|
79
|
-
ff.relation.mappingName = shortName(`${model.name}_of_${ff.name}`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/* -----------------------------------------------------------
|
|
83
|
-
COLUMNS
|
|
84
|
-
----------------------------------------------------------- */
|
|
85
|
-
function writeColumns(props: {
|
|
86
|
-
dbms: "postgres" | "sqlite";
|
|
87
|
-
model: AutoBePrisma.IModel;
|
|
88
|
-
}): string[] {
|
|
89
|
-
return [
|
|
90
|
-
"//----",
|
|
91
|
-
"// COLUMNS",
|
|
92
|
-
"//----",
|
|
93
|
-
writePrimary({
|
|
94
|
-
dbms: props.dbms,
|
|
95
|
-
model: props.model,
|
|
96
|
-
field: props.model.primaryField,
|
|
97
|
-
}),
|
|
98
|
-
...props.model.foreignFields
|
|
99
|
-
.map((x) => [
|
|
100
|
-
"",
|
|
101
|
-
writeField({
|
|
102
|
-
dbms: props.dbms,
|
|
103
|
-
field: x,
|
|
104
|
-
}),
|
|
105
|
-
])
|
|
106
|
-
.flat(),
|
|
107
|
-
...props.model.plainFields
|
|
108
|
-
.map((x) => [
|
|
109
|
-
"",
|
|
110
|
-
writeField({
|
|
111
|
-
dbms: props.dbms,
|
|
112
|
-
field: x,
|
|
113
|
-
}),
|
|
114
|
-
])
|
|
115
|
-
.flat(),
|
|
116
|
-
];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function writePrimary(props: {
|
|
120
|
-
dbms: "postgres" | "sqlite";
|
|
121
|
-
model: AutoBePrisma.IModel;
|
|
122
|
-
field: AutoBePrisma.IPrimaryField;
|
|
123
|
-
}): string {
|
|
124
|
-
const type: string | undefined =
|
|
125
|
-
props.dbms === "postgres" ? POSTGRES_PHYSICAL_TYPES.uuid : undefined;
|
|
126
|
-
const pkeyName: string = `${props.model.name}__pkey`;
|
|
127
|
-
const signature: string =
|
|
128
|
-
pkeyName.length <= MAX_IDENTIFIER_LENGTH
|
|
129
|
-
? "@id"
|
|
130
|
-
: `@id(map: "${shortName(pkeyName)}")`;
|
|
131
|
-
return [
|
|
132
|
-
writeComment(props.field.description, 78),
|
|
133
|
-
`${props.field.name} String ${signature}${type ? ` ${type}` : ""}`,
|
|
134
|
-
].join("\n");
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function writeField(props: {
|
|
138
|
-
dbms: "postgres" | "sqlite";
|
|
139
|
-
field: AutoBePrisma.IPlainField;
|
|
140
|
-
}): string {
|
|
141
|
-
const logical: string = LOGICAL_TYPES[props.field.type];
|
|
142
|
-
const physical: string | undefined =
|
|
143
|
-
props.dbms === "postgres"
|
|
144
|
-
? POSTGRES_PHYSICAL_TYPES[
|
|
145
|
-
props.field.type as keyof typeof POSTGRES_PHYSICAL_TYPES
|
|
146
|
-
]
|
|
147
|
-
: undefined;
|
|
148
|
-
return [
|
|
149
|
-
writeComment(props.field.description, 78),
|
|
150
|
-
[
|
|
151
|
-
props.field.name,
|
|
152
|
-
`${logical}${props.field.nullable ? "?" : ""}`,
|
|
153
|
-
...(physical ? [physical] : []),
|
|
154
|
-
].join(" "),
|
|
155
|
-
].join("\n");
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/* -----------------------------------------------------------
|
|
159
|
-
RELATIONS
|
|
160
|
-
----------------------------------------------------------- */
|
|
161
|
-
function writeRelations(props: {
|
|
162
|
-
dbms: "postgres" | "sqlite";
|
|
163
|
-
application: AutoBePrisma.IApplication;
|
|
164
|
-
model: AutoBePrisma.IModel;
|
|
165
|
-
}): string[] {
|
|
166
|
-
interface IHasRelationship {
|
|
167
|
-
modelName: string;
|
|
168
|
-
unique: boolean;
|
|
169
|
-
mappingName?: string;
|
|
170
|
-
}
|
|
171
|
-
const hasRelationships: IHasRelationship[] = props.application.files
|
|
172
|
-
.map((otherFile) =>
|
|
173
|
-
otherFile.models.map((otherModel) =>
|
|
174
|
-
otherModel.foreignFields
|
|
175
|
-
.filter(
|
|
176
|
-
(otherForeign) =>
|
|
177
|
-
otherForeign.relation.targetModel === props.model.name,
|
|
178
|
-
)
|
|
179
|
-
.map((otherForeign) => ({
|
|
180
|
-
modelName: otherModel.name,
|
|
181
|
-
unique: otherForeign.unique,
|
|
182
|
-
mappingName: otherForeign.relation.mappingName,
|
|
183
|
-
})),
|
|
184
|
-
),
|
|
185
|
-
)
|
|
186
|
-
.flat(2);
|
|
187
|
-
const foreignIndexes: AutoBePrisma.IForeignField[] =
|
|
188
|
-
props.model.foreignFields.filter((f) => {
|
|
189
|
-
if (f.unique === true)
|
|
190
|
-
return props.model.uniqueIndexes.every(
|
|
191
|
-
(u) => u.fieldNames.length !== 1 || u.fieldNames[0] !== f.name,
|
|
192
|
-
);
|
|
193
|
-
return (
|
|
194
|
-
props.model.uniqueIndexes.every((u) => u.fieldNames[0] !== f.name) &&
|
|
195
|
-
props.model.plainIndexes.every((p) => p.fieldNames[0] !== f.name)
|
|
196
|
-
);
|
|
197
|
-
});
|
|
198
|
-
const contents: string[][] = [
|
|
199
|
-
props.model.foreignFields.map((foreign) =>
|
|
200
|
-
writeConstraint({
|
|
201
|
-
dbms: props.dbms,
|
|
202
|
-
model: props.model,
|
|
203
|
-
foreign,
|
|
204
|
-
}),
|
|
205
|
-
),
|
|
206
|
-
hasRelationships.map((r) =>
|
|
207
|
-
[
|
|
208
|
-
r.mappingName ?? r.modelName,
|
|
209
|
-
`${r.modelName}${r.unique ? "?" : "[]"}`,
|
|
210
|
-
...(r.mappingName ? [`@relation("${r.mappingName}")`] : []),
|
|
211
|
-
].join(" "),
|
|
212
|
-
),
|
|
213
|
-
foreignIndexes.map((field) =>
|
|
214
|
-
writeForeignIndex({
|
|
215
|
-
model: props.model,
|
|
216
|
-
field,
|
|
217
|
-
}),
|
|
218
|
-
),
|
|
219
|
-
[
|
|
220
|
-
...props.model.uniqueIndexes.map((unique) =>
|
|
221
|
-
writeUniqueIndex({
|
|
222
|
-
model: props.model,
|
|
223
|
-
unique,
|
|
224
|
-
}),
|
|
225
|
-
),
|
|
226
|
-
...props.model.plainIndexes.map((plain) =>
|
|
227
|
-
writePlainIndex({
|
|
228
|
-
model: props.model,
|
|
229
|
-
plain,
|
|
230
|
-
}),
|
|
231
|
-
),
|
|
232
|
-
...(props.dbms === "postgres"
|
|
233
|
-
? props.model.ginIndexes.map((gin) =>
|
|
234
|
-
writeGinIndex({
|
|
235
|
-
model: props.model,
|
|
236
|
-
gin,
|
|
237
|
-
}),
|
|
238
|
-
)
|
|
239
|
-
: []),
|
|
240
|
-
],
|
|
241
|
-
];
|
|
242
|
-
if (contents.every((c) => c.length === 0)) return [];
|
|
243
|
-
return [
|
|
244
|
-
"//----",
|
|
245
|
-
"// RELATIONS",
|
|
246
|
-
"//----",
|
|
247
|
-
// paddled content
|
|
248
|
-
...ArrayUtil.paddle(contents),
|
|
249
|
-
];
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function writeConstraint(props: {
|
|
253
|
-
dbms: "postgres" | "sqlite";
|
|
254
|
-
model: AutoBePrisma.IModel;
|
|
255
|
-
foreign: AutoBePrisma.IForeignField;
|
|
256
|
-
}): string {
|
|
257
|
-
// spellchecker:ignore-next-line
|
|
258
|
-
const name: string = `${props.model.name}_${props.foreign.name}_rela`;
|
|
259
|
-
const tooMuchLong: boolean =
|
|
260
|
-
props.dbms === "postgres" && name.length > MAX_IDENTIFIER_LENGTH;
|
|
261
|
-
const body: string = [
|
|
262
|
-
props.foreign.relation.name,
|
|
263
|
-
`${props.foreign.relation.targetModel}${props.foreign.nullable ? "?" : ""}`,
|
|
264
|
-
`@relation(${[
|
|
265
|
-
...(props.foreign.relation.mappingName
|
|
266
|
-
? [`"${props.foreign.relation.mappingName}"`]
|
|
267
|
-
: []),
|
|
268
|
-
`fields: [${props.foreign.name}]`,
|
|
269
|
-
`references: [id]`,
|
|
270
|
-
`onDelete: Cascade`,
|
|
271
|
-
...(tooMuchLong ? [`map: "${shortName(name)}"`] : []),
|
|
272
|
-
].join(", ")})`,
|
|
273
|
-
].join(" ");
|
|
274
|
-
return tooMuchLong
|
|
275
|
-
? StringUtil.trim`
|
|
276
|
-
// spellchecker: ignore-next-line
|
|
277
|
-
${body}
|
|
278
|
-
`
|
|
279
|
-
: body;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function writeForeignIndex(props: {
|
|
283
|
-
model: AutoBePrisma.IModel;
|
|
284
|
-
field: AutoBePrisma.IForeignField;
|
|
285
|
-
}): string {
|
|
286
|
-
const name: string = `${props.model.name}_${props.field.name}_fkey`;
|
|
287
|
-
const prefix: string = `@@${props.field.unique === true ? "unique" : "index"}([${props.field.name}]`;
|
|
288
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH) return `${prefix})`;
|
|
289
|
-
return StringUtil.trim`
|
|
290
|
-
// spellchecker: ignore-next-line
|
|
291
|
-
${prefix}, map: "${shortName(name)}")
|
|
292
|
-
`;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function writeUniqueIndex(props: {
|
|
296
|
-
model: AutoBePrisma.IModel;
|
|
297
|
-
unique: AutoBePrisma.IUniqueIndex;
|
|
298
|
-
}): string {
|
|
299
|
-
const name: string = `${props.model.name}_${props.unique.fieldNames.join("_")}_key`;
|
|
300
|
-
const prefix: string = `@@unique([${props.unique.fieldNames.join(", ")}]`;
|
|
301
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH) return `${prefix})`;
|
|
302
|
-
return StringUtil.trim`
|
|
303
|
-
// spellchecker: ignore-next-line
|
|
304
|
-
${prefix}, map: "${shortName(name)}")
|
|
305
|
-
`;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function writePlainIndex(props: {
|
|
309
|
-
model: AutoBePrisma.IModel;
|
|
310
|
-
plain: AutoBePrisma.IPlainIndex;
|
|
311
|
-
}): string {
|
|
312
|
-
const name: string = `${props.model.name}_${props.plain.fieldNames.join("_")}_idx`;
|
|
313
|
-
const prefix: string = `@@index([${props.plain.fieldNames.join(", ")}]`;
|
|
314
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH) return `${prefix})`;
|
|
315
|
-
return StringUtil.trim`
|
|
316
|
-
// spellchecker: ignore-next-line
|
|
317
|
-
${prefix}, map: "${shortName(name)}")
|
|
318
|
-
`;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function writeGinIndex(props: {
|
|
322
|
-
model: AutoBePrisma.IModel;
|
|
323
|
-
gin: AutoBePrisma.IGinIndex;
|
|
324
|
-
}): string {
|
|
325
|
-
const name: string = `${props.model.name}_${props.gin.fieldName}_idx`;
|
|
326
|
-
const prefix: string = `@@index([${props.gin.fieldName}(ops: raw("gin_trgm_ops"))], type: Gin`;
|
|
327
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH) return `${prefix})`;
|
|
328
|
-
return StringUtil.trim`
|
|
329
|
-
// spellchecker: ignore-next-line
|
|
330
|
-
${prefix}, map: "${shortName(name)}")
|
|
331
|
-
`;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/* -----------------------------------------------------------
|
|
335
|
-
BACKGROUND
|
|
336
|
-
----------------------------------------------------------- */
|
|
337
|
-
function writeComment(content: string, length: number): string {
|
|
338
|
-
return content
|
|
339
|
-
.split("\r\n")
|
|
340
|
-
.join("\n")
|
|
341
|
-
.split("\n")
|
|
342
|
-
.map((line) => line.trim())
|
|
343
|
-
.map((line) => {
|
|
344
|
-
// 77자에서 "/// " 4자를 뺀 73자가 실제 컨텐츠 최대 길이
|
|
345
|
-
if (line.length <= length - 4) return [line];
|
|
346
|
-
const words: string[] = line.split(" ");
|
|
347
|
-
const result: string[] = [];
|
|
348
|
-
let currentLine = "";
|
|
349
|
-
|
|
350
|
-
for (const word of words) {
|
|
351
|
-
const potentialLine = currentLine ? `${currentLine} ${word}` : word;
|
|
352
|
-
if (potentialLine.length <= 73) {
|
|
353
|
-
currentLine = potentialLine;
|
|
354
|
-
} else {
|
|
355
|
-
if (currentLine) result.push(currentLine);
|
|
356
|
-
currentLine = word;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
if (currentLine) result.push(currentLine);
|
|
361
|
-
return result;
|
|
362
|
-
})
|
|
363
|
-
.flat()
|
|
364
|
-
.map((str) => `///${str.length ? ` ${str}` : ""}`)
|
|
365
|
-
.join("\n")
|
|
366
|
-
.trim();
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function addIndent(content: string): string {
|
|
370
|
-
return content
|
|
371
|
-
.split("\r\n")
|
|
372
|
-
.join("\n")
|
|
373
|
-
.split("\n")
|
|
374
|
-
.map((str) => ` ${str}`)
|
|
375
|
-
.join("\n");
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
function shortName(name: string): string {
|
|
379
|
-
if (name.length <= MAX_IDENTIFIER_LENGTH) return name;
|
|
380
|
-
const hash: string = crypto
|
|
381
|
-
.createHash("md5")
|
|
382
|
-
.update(name)
|
|
383
|
-
.digest("hex")
|
|
384
|
-
.substring(0, HASH_TRUNCATION_LENGTH);
|
|
385
|
-
return `${name.substring(0, MAX_IDENTIFIER_LENGTH - HASH_TRUNCATION_LENGTH - 1)}_${hash}`;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
const LOGICAL_TYPES = {
|
|
389
|
-
// native types
|
|
390
|
-
boolean: "Boolean",
|
|
391
|
-
int: "Int",
|
|
392
|
-
double: "Float",
|
|
393
|
-
string: "String",
|
|
394
|
-
// formats
|
|
395
|
-
datetime: "DateTime",
|
|
396
|
-
uuid: "String",
|
|
397
|
-
uri: "String",
|
|
398
|
-
};
|
|
399
|
-
const POSTGRES_PHYSICAL_TYPES = {
|
|
400
|
-
int: "@db.Integer",
|
|
401
|
-
double: "@db.DoublePrecision",
|
|
402
|
-
uuid: "@db.Uuid",
|
|
403
|
-
datetime: "@db.Timestamptz",
|
|
404
|
-
uri: "@db.VarChar(80000)",
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
const POSTGRES_MAIN_FILE = StringUtil.trim`
|
|
408
|
-
generator client {
|
|
409
|
-
provider = "prisma-client-js"
|
|
410
|
-
engineType = "client"
|
|
411
|
-
previewFeatures = ["postgresqlExtensions", "views"]
|
|
412
|
-
}
|
|
413
|
-
datasource db {
|
|
414
|
-
provider = "postgresql"
|
|
415
|
-
url = env("DATABASE_URL")
|
|
416
|
-
extensions = [pg_trgm]
|
|
417
|
-
}
|
|
418
|
-
generator markdown {
|
|
419
|
-
provider = "prisma-markdown"
|
|
420
|
-
output = "../../docs/ERD.md"
|
|
421
|
-
}
|
|
422
|
-
`;
|
|
423
|
-
const SQLITE_MAIN_FILE = StringUtil.trim`
|
|
424
|
-
generator client {
|
|
425
|
-
provider = "prisma-client-js"
|
|
426
|
-
engineType = "client"
|
|
427
|
-
}
|
|
428
|
-
datasource db {
|
|
429
|
-
provider = "sqlite"
|
|
430
|
-
url = "file:../db.sqlite"
|
|
431
|
-
}
|
|
432
|
-
generator markdown {
|
|
433
|
-
provider = "prisma-markdown"
|
|
434
|
-
output = "../../docs/ERD.md"
|
|
435
|
-
}
|
|
436
|
-
`;
|
|
437
|
-
const MAX_IDENTIFIER_LENGTH = 63;
|
|
438
|
-
const HASH_TRUNCATION_LENGTH = 8;
|