sonamu 0.2.31 → 0.2.33
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/.pnp.cjs +11 -10
- package/.vscode/settings.json +1 -1
- package/dist/api/sonamu.d.ts.map +1 -1
- package/dist/api/sonamu.js +3 -0
- package/dist/api/sonamu.js.map +1 -1
- package/dist/bin/cli.js +7 -4
- package/dist/bin/cli.js.map +1 -1
- package/dist/database/_batch_update.d.ts +15 -0
- package/dist/database/_batch_update.d.ts.map +1 -0
- package/dist/database/_batch_update.js +89 -0
- package/dist/database/_batch_update.js.map +1 -0
- package/dist/database/base-model.d.ts +2 -1
- package/dist/database/base-model.d.ts.map +1 -1
- package/dist/database/base-model.js +45 -31
- package/dist/database/base-model.js.map +1 -1
- package/dist/database/upsert-builder.d.ts.map +1 -1
- package/dist/database/upsert-builder.js +11 -54
- package/dist/database/upsert-builder.js.map +1 -1
- package/dist/entity/entity.js +1 -1
- package/dist/entity/entity.js.map +1 -1
- package/dist/entity/migrator.d.ts +4 -4
- package/dist/entity/migrator.d.ts.map +1 -1
- package/dist/entity/migrator.js +213 -205
- package/dist/entity/migrator.js.map +1 -1
- package/dist/syncer/syncer.d.ts.map +1 -1
- package/dist/syncer/syncer.js +26 -26
- package/dist/syncer/syncer.js.map +1 -1
- package/dist/templates/base-template.d.ts +1 -1
- package/dist/templates/base-template.d.ts.map +1 -1
- package/dist/templates/generated_http.template.d.ts +2 -2
- package/dist/templates/generated_http.template.d.ts.map +1 -1
- package/dist/templates/generated_http.template.js +49 -38
- package/dist/templates/generated_http.template.js.map +1 -1
- package/dist/templates/view_form.template.d.ts +2 -2
- package/dist/templates/view_list.template.d.ts +2 -2
- package/package.json +3 -3
- package/src/api/sonamu.ts +4 -0
- package/src/bin/cli.ts +11 -6
- package/src/database/_batch_update.ts +106 -0
- package/src/database/base-model.ts +66 -42
- package/src/database/upsert-builder.ts +16 -12
- package/src/entity/entity.ts +1 -1
- package/src/entity/migrator.ts +106 -103
- package/src/syncer/syncer.ts +58 -58
- package/src/templates/base-template.ts +1 -1
- package/src/templates/generated_http.template.ts +37 -35
package/dist/entity/migrator.js
CHANGED
|
@@ -544,12 +544,12 @@ class Migrator {
|
|
|
544
544
|
if (dbSet === null) {
|
|
545
545
|
// 기존 테이블 없음, 새로 테이블 생성
|
|
546
546
|
return [
|
|
547
|
-
this.generateCreateCode_ColumnAndIndexes(entitySet.table, entitySet.columns, entitySet.indexes),
|
|
548
|
-
...this.generateCreateCode_Foreign(entitySet.table, entitySet.foreigns),
|
|
547
|
+
yield this.generateCreateCode_ColumnAndIndexes(entitySet.table, entitySet.columns, entitySet.indexes),
|
|
548
|
+
...(yield this.generateCreateCode_Foreign(entitySet.table, entitySet.foreigns)),
|
|
549
549
|
];
|
|
550
550
|
}
|
|
551
551
|
// 기존 테이블 존재하는 케이스
|
|
552
|
-
const alterCodes = ["columnsAndIndexes", "foreigns"].map((key) => {
|
|
552
|
+
const alterCodes = yield Promise.all(["columnsAndIndexes", "foreigns"].map((key) => {
|
|
553
553
|
// 배열 원소의 순서가 달라서 불일치가 발생하는걸 방지하기 위해 각 항목별로 정렬 처리 후 비교
|
|
554
554
|
if (key === "columnsAndIndexes") {
|
|
555
555
|
const replaceColumnDefaultTo = (col) => {
|
|
@@ -569,14 +569,14 @@ class Migrator {
|
|
|
569
569
|
const entityColumns = (0, lodash_1.sortBy)(entitySet.columns, (a) => a.name).map(replaceColumnDefaultTo);
|
|
570
570
|
const dbColumns = (0, lodash_1.sortBy)(dbSet.columns, (a) => a.name).map(replaceColumnDefaultTo);
|
|
571
571
|
/* 디버깅용 코드, 특정 컬럼에서 불일치 발생할 때 확인
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
572
|
+
const entityColumn = entitySet.columns.find(
|
|
573
|
+
(col) => col.name === "price_krw"
|
|
574
|
+
);
|
|
575
|
+
const dbColumn = dbSet.columns.find(
|
|
576
|
+
(col) => col.name === "price_krw"
|
|
577
|
+
);
|
|
578
|
+
console.debug({ entityColumn, dbColumn });
|
|
579
|
+
*/
|
|
580
580
|
const entityIndexes = (0, lodash_1.sortBy)(entitySet.indexes, (a) => [
|
|
581
581
|
a.type,
|
|
582
582
|
...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1)),
|
|
@@ -610,7 +610,7 @@ class Migrator {
|
|
|
610
610
|
}
|
|
611
611
|
}
|
|
612
612
|
return null;
|
|
613
|
-
});
|
|
613
|
+
}));
|
|
614
614
|
if (alterCodes.every((alterCode) => alterCode === null)) {
|
|
615
615
|
return null;
|
|
616
616
|
}
|
|
@@ -1025,75 +1025,79 @@ class Migrator {
|
|
|
1025
1025
|
테이블 생성하는 케이스 - 컬럼/인덱스 생성
|
|
1026
1026
|
*/
|
|
1027
1027
|
generateCreateCode_ColumnAndIndexes(table, columns, indexes) {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1028
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1029
|
+
// 컬럼, 인덱스 처리
|
|
1030
|
+
const lines = [
|
|
1031
|
+
'import { Knex } from "knex";',
|
|
1032
|
+
"",
|
|
1033
|
+
"export async function up(knex: Knex): Promise<void> {",
|
|
1034
|
+
`return knex.schema.createTable("${table}", (table) => {`,
|
|
1035
|
+
"// columns",
|
|
1036
|
+
...this.genColumnDefinitions(columns),
|
|
1037
|
+
"",
|
|
1038
|
+
"// indexes",
|
|
1039
|
+
...this.genIndexDefinitions(indexes),
|
|
1040
|
+
"});",
|
|
1041
|
+
"}",
|
|
1042
|
+
"",
|
|
1043
|
+
"export async function down(knex: Knex): Promise<void> {",
|
|
1044
|
+
` return knex.schema.dropTable("${table}");`,
|
|
1045
|
+
"}",
|
|
1046
|
+
];
|
|
1047
|
+
return {
|
|
1048
|
+
table,
|
|
1049
|
+
type: "normal",
|
|
1050
|
+
title: `create__${table}`,
|
|
1051
|
+
formatted: yield prettier_1.default.format(lines.join("\n"), {
|
|
1052
|
+
parser: "typescript",
|
|
1053
|
+
}),
|
|
1054
|
+
};
|
|
1055
|
+
});
|
|
1054
1056
|
}
|
|
1055
1057
|
/*
|
|
1056
1058
|
테이블 생성하는 케이스 - FK 생성
|
|
1057
1059
|
*/
|
|
1058
1060
|
generateCreateCode_Foreign(table, foreigns) {
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1061
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1062
|
+
if (foreigns.length === 0) {
|
|
1063
|
+
return [];
|
|
1064
|
+
}
|
|
1065
|
+
const { up, down } = this.genForeignDefinitions(table, foreigns);
|
|
1066
|
+
if (up.length === 0 && down.length === 0) {
|
|
1067
|
+
console.log("fk 가 뭔가 다릅니다");
|
|
1068
|
+
return [];
|
|
1069
|
+
}
|
|
1070
|
+
const lines = [
|
|
1071
|
+
'import { Knex } from "knex";',
|
|
1072
|
+
"",
|
|
1073
|
+
"export async function up(knex: Knex): Promise<void> {",
|
|
1074
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1075
|
+
"// create fk",
|
|
1076
|
+
...up,
|
|
1077
|
+
"});",
|
|
1078
|
+
"}",
|
|
1079
|
+
"",
|
|
1080
|
+
"export async function down(knex: Knex): Promise<void> {",
|
|
1081
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1082
|
+
"// drop fk",
|
|
1083
|
+
...down,
|
|
1084
|
+
"});",
|
|
1085
|
+
"}",
|
|
1086
|
+
];
|
|
1087
|
+
const foreignKeysString = foreigns
|
|
1088
|
+
.map((foreign) => foreign.columns.join("_"))
|
|
1089
|
+
.join("_");
|
|
1090
|
+
return [
|
|
1091
|
+
{
|
|
1092
|
+
table,
|
|
1093
|
+
type: "foreign",
|
|
1094
|
+
title: `foreign__${table}__${foreignKeysString}`,
|
|
1095
|
+
formatted: yield prettier_1.default.format(lines.join("\n"), {
|
|
1096
|
+
parser: "typescript",
|
|
1097
|
+
}),
|
|
1098
|
+
},
|
|
1099
|
+
];
|
|
1100
|
+
});
|
|
1097
1101
|
}
|
|
1098
1102
|
/*
|
|
1099
1103
|
마이그레이션 컬럼 배열 비교용 코드
|
|
@@ -1137,76 +1141,78 @@ class Migrator {
|
|
|
1137
1141
|
}
|
|
1138
1142
|
}
|
|
1139
1143
|
generateAlterCode_ColumnAndIndexes(table, entityColumns, entityIndexes, dbColumns, dbIndexes) {
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
...["add", "drop", "alter"]
|
|
1193
|
-
.map((action) => {
|
|
1194
|
-
const len = alterColumnsTo[action].length;
|
|
1195
|
-
if (len > 0) {
|
|
1196
|
-
return action + len;
|
|
1197
|
-
}
|
|
1198
|
-
return null;
|
|
1199
|
-
})
|
|
1200
|
-
.filter((part) => part !== null),
|
|
1201
|
-
].join("_");
|
|
1202
|
-
return [
|
|
1203
|
-
{
|
|
1144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1145
|
+
/*
|
|
1146
|
+
세부 비교 후 다른점 찾아서 코드 생성
|
|
1147
|
+
|
|
1148
|
+
1. 컬럼갯수 다름: MD에 있으나, DB에 없다면 추가
|
|
1149
|
+
2. 컬럼갯수 다름: MD에 없으나, DB에 있다면 삭제
|
|
1150
|
+
3. 그외 컬럼(컬럼 갯수가 동일하거나, 다른 경우 동일한 컬럼끼리) => alter
|
|
1151
|
+
4. 다른거 다 동일하고 index만 변경되는 경우
|
|
1152
|
+
|
|
1153
|
+
** 컬럼명을 변경하는 경우는 따로 핸들링하지 않음
|
|
1154
|
+
=> drop/add 형태의 마이그레이션 코드가 생성되는데, 수동으로 rename 코드로 수정하여 처리
|
|
1155
|
+
*/
|
|
1156
|
+
// 각 컬럼 이름 기준으로 add, drop, alter 여부 확인
|
|
1157
|
+
const alterColumnsTo = this.getAlterColumnsTo(entityColumns, dbColumns);
|
|
1158
|
+
// 추출된 컬럼들을 기준으로 각각 라인 생성
|
|
1159
|
+
const alterColumnLinesTo = this.getAlterColumnLinesTo(alterColumnsTo, entityColumns);
|
|
1160
|
+
// 인덱스의 add, drop 여부 확인
|
|
1161
|
+
const alterIndexesTo = this.getAlterIndexesTo(entityIndexes, dbIndexes);
|
|
1162
|
+
// 추출된 인덱스들을 기준으로 각각 라인 생성
|
|
1163
|
+
const alterIndexLinesTo = this.getAlterIndexLinesTo(alterIndexesTo, alterColumnsTo);
|
|
1164
|
+
const lines = [
|
|
1165
|
+
'import { Knex } from "knex";',
|
|
1166
|
+
"",
|
|
1167
|
+
"export async function up(knex: Knex): Promise<void> {",
|
|
1168
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1169
|
+
...(alterColumnsTo.add.length > 0 ? alterColumnLinesTo.add.up : []),
|
|
1170
|
+
...(alterColumnsTo.drop.length > 0 ? alterColumnLinesTo.drop.up : []),
|
|
1171
|
+
...(alterColumnsTo.alter.length > 0 ? alterColumnLinesTo.alter.up : []),
|
|
1172
|
+
...(alterIndexesTo.add.length > 0 ? alterIndexLinesTo.add.up : []),
|
|
1173
|
+
...(alterIndexesTo.drop.length > 0 ? alterIndexLinesTo.drop.up : []),
|
|
1174
|
+
"})",
|
|
1175
|
+
"}",
|
|
1176
|
+
"",
|
|
1177
|
+
"export async function down(knex: Knex): Promise<void> {",
|
|
1178
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1179
|
+
...(alterColumnsTo.add.length > 0 ? alterColumnLinesTo.add.down : []),
|
|
1180
|
+
...(alterColumnsTo.drop.length > 0 ? alterColumnLinesTo.drop.down : []),
|
|
1181
|
+
...(alterColumnsTo.alter.length > 0 ? alterColumnLinesTo.alter.down : []),
|
|
1182
|
+
...(alterIndexLinesTo.add.down.length > 1
|
|
1183
|
+
? alterIndexLinesTo.add.down
|
|
1184
|
+
: []),
|
|
1185
|
+
...(alterIndexLinesTo.drop.down.length > 1
|
|
1186
|
+
? alterIndexLinesTo.drop.down
|
|
1187
|
+
: []),
|
|
1188
|
+
"})",
|
|
1189
|
+
"}",
|
|
1190
|
+
];
|
|
1191
|
+
const formatted = yield prettier_1.default.format(lines.join("\n"), {
|
|
1192
|
+
parser: "typescript",
|
|
1193
|
+
});
|
|
1194
|
+
const title = [
|
|
1195
|
+
"alter",
|
|
1204
1196
|
table,
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1197
|
+
...["add", "drop", "alter"]
|
|
1198
|
+
.map((action) => {
|
|
1199
|
+
const len = alterColumnsTo[action].length;
|
|
1200
|
+
if (len > 0) {
|
|
1201
|
+
return action + len;
|
|
1202
|
+
}
|
|
1203
|
+
return null;
|
|
1204
|
+
})
|
|
1205
|
+
.filter((part) => part !== null),
|
|
1206
|
+
].join("_");
|
|
1207
|
+
return [
|
|
1208
|
+
{
|
|
1209
|
+
table,
|
|
1210
|
+
title,
|
|
1211
|
+
formatted,
|
|
1212
|
+
type: "normal",
|
|
1213
|
+
},
|
|
1214
|
+
];
|
|
1215
|
+
});
|
|
1210
1216
|
}
|
|
1211
1217
|
getAlterColumnsTo(entityColumns, dbColumns) {
|
|
1212
1218
|
const columnsTo = {
|
|
@@ -1352,68 +1358,70 @@ class Migrator {
|
|
|
1352
1358
|
return linesTo;
|
|
1353
1359
|
}
|
|
1354
1360
|
generateAlterCode_Foreigns(table, entityForeigns, dbForeigns) {
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
const
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1361
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1362
|
+
// console.log({ entityForeigns, dbForeigns });
|
|
1363
|
+
const getKey = (mf) => {
|
|
1364
|
+
return [mf.columns.join("-"), mf.to].join("///");
|
|
1365
|
+
};
|
|
1366
|
+
const fkTo = entityForeigns.reduce((result, entityF) => {
|
|
1367
|
+
const matchingDbF = dbForeigns.find((dbF) => getKey(entityF) === getKey(dbF));
|
|
1368
|
+
if (!matchingDbF) {
|
|
1369
|
+
result.add.push(entityF);
|
|
1370
|
+
return result;
|
|
1371
|
+
}
|
|
1372
|
+
if ((0, fast_deep_equal_1.default)(entityF, matchingDbF) === false) {
|
|
1373
|
+
result.alterSrc.push(matchingDbF);
|
|
1374
|
+
result.alterDst.push(entityF);
|
|
1375
|
+
return result;
|
|
1376
|
+
}
|
|
1368
1377
|
return result;
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
"
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
const title = [
|
|
1404
|
-
"alter",
|
|
1405
|
-
table,
|
|
1406
|
-
"foreigns",
|
|
1407
|
-
// TODO 바뀌는 부분
|
|
1408
|
-
].join("_");
|
|
1409
|
-
return [
|
|
1410
|
-
{
|
|
1378
|
+
}, {
|
|
1379
|
+
add: [],
|
|
1380
|
+
alterSrc: [],
|
|
1381
|
+
alterDst: [],
|
|
1382
|
+
});
|
|
1383
|
+
const linesTo = {
|
|
1384
|
+
add: this.genForeignDefinitions(table, fkTo.add),
|
|
1385
|
+
alterSrc: this.genForeignDefinitions(table, fkTo.alterSrc),
|
|
1386
|
+
alterDst: this.genForeignDefinitions(table, fkTo.alterDst),
|
|
1387
|
+
};
|
|
1388
|
+
const lines = [
|
|
1389
|
+
'import { Knex } from "knex";',
|
|
1390
|
+
"",
|
|
1391
|
+
"export async function up(knex: Knex): Promise<void> {",
|
|
1392
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1393
|
+
...linesTo.add.up,
|
|
1394
|
+
...linesTo.alterSrc.down,
|
|
1395
|
+
...linesTo.alterDst.up,
|
|
1396
|
+
"})",
|
|
1397
|
+
"}",
|
|
1398
|
+
"",
|
|
1399
|
+
"export async function down(knex: Knex): Promise<void> {",
|
|
1400
|
+
`return knex.schema.alterTable("${table}", (table) => {`,
|
|
1401
|
+
...linesTo.add.down,
|
|
1402
|
+
...linesTo.alterDst.down,
|
|
1403
|
+
...linesTo.alterSrc.up,
|
|
1404
|
+
"})",
|
|
1405
|
+
"}",
|
|
1406
|
+
];
|
|
1407
|
+
const formatted = yield prettier_1.default.format(lines.join("\n"), {
|
|
1408
|
+
parser: "typescript",
|
|
1409
|
+
});
|
|
1410
|
+
const title = [
|
|
1411
|
+
"alter",
|
|
1411
1412
|
table,
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1413
|
+
"foreigns",
|
|
1414
|
+
// TODO 바뀌는 부분
|
|
1415
|
+
].join("_");
|
|
1416
|
+
return [
|
|
1417
|
+
{
|
|
1418
|
+
table,
|
|
1419
|
+
title,
|
|
1420
|
+
formatted,
|
|
1421
|
+
type: "normal",
|
|
1422
|
+
},
|
|
1423
|
+
];
|
|
1424
|
+
});
|
|
1417
1425
|
}
|
|
1418
1426
|
destroy() {
|
|
1419
1427
|
return __awaiter(this, void 0, void 0, function* () {
|