sonamu 0.2.32 → 0.2.34

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.
Files changed (37) hide show
  1. package/.pnp.cjs +11 -10
  2. package/.vscode/settings.json +1 -1
  3. package/dist/api/sonamu.d.ts.map +1 -1
  4. package/dist/api/sonamu.js +3 -0
  5. package/dist/api/sonamu.js.map +1 -1
  6. package/dist/bin/cli.js +6 -3
  7. package/dist/bin/cli.js.map +1 -1
  8. package/dist/database/base-model.d.ts +2 -1
  9. package/dist/database/base-model.d.ts.map +1 -1
  10. package/dist/database/base-model.js +46 -32
  11. package/dist/database/base-model.js.map +1 -1
  12. package/dist/entity/entity.js +1 -1
  13. package/dist/entity/entity.js.map +1 -1
  14. package/dist/entity/migrator.d.ts +4 -4
  15. package/dist/entity/migrator.d.ts.map +1 -1
  16. package/dist/entity/migrator.js +213 -205
  17. package/dist/entity/migrator.js.map +1 -1
  18. package/dist/syncer/syncer.d.ts.map +1 -1
  19. package/dist/syncer/syncer.js +26 -26
  20. package/dist/syncer/syncer.js.map +1 -1
  21. package/dist/templates/base-template.d.ts +1 -1
  22. package/dist/templates/base-template.d.ts.map +1 -1
  23. package/dist/templates/generated_http.template.d.ts +2 -2
  24. package/dist/templates/generated_http.template.d.ts.map +1 -1
  25. package/dist/templates/generated_http.template.js +49 -38
  26. package/dist/templates/generated_http.template.js.map +1 -1
  27. package/dist/templates/view_form.template.d.ts +2 -2
  28. package/dist/templates/view_list.template.d.ts +2 -2
  29. package/package.json +3 -3
  30. package/src/api/sonamu.ts +4 -0
  31. package/src/bin/cli.ts +10 -5
  32. package/src/database/base-model.ts +67 -42
  33. package/src/entity/entity.ts +1 -1
  34. package/src/entity/migrator.ts +106 -103
  35. package/src/syncer/syncer.ts +58 -58
  36. package/src/templates/base-template.ts +1 -1
  37. package/src/templates/generated_http.template.ts +37 -35
@@ -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
- 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
- */
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
- const lines = [
1030
- 'import { Knex } from "knex";',
1031
- "",
1032
- "export async function up(knex: Knex): Promise<void> {",
1033
- `return knex.schema.createTable("${table}", (table) => {`,
1034
- "// columns",
1035
- ...this.genColumnDefinitions(columns),
1036
- "",
1037
- "// indexes",
1038
- ...this.genIndexDefinitions(indexes),
1039
- "});",
1040
- "}",
1041
- "",
1042
- "export async function down(knex: Knex): Promise<void> {",
1043
- ` return knex.schema.dropTable("${table}");`,
1044
- "}",
1045
- ];
1046
- return {
1047
- table,
1048
- type: "normal",
1049
- title: `create__${table}`,
1050
- formatted: prettier_1.default.format(lines.join("\n"), {
1051
- parser: "typescript",
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
- if (foreigns.length === 0) {
1060
- return [];
1061
- }
1062
- const { up, down } = this.genForeignDefinitions(table, foreigns);
1063
- if (up.length === 0 && down.length === 0) {
1064
- console.log("fk 뭔가 다릅니다");
1065
- return [];
1066
- }
1067
- const lines = [
1068
- 'import { Knex } from "knex";',
1069
- "",
1070
- "export async function up(knex: Knex): Promise<void> {",
1071
- `return knex.schema.alterTable("${table}", (table) => {`,
1072
- "// create fk",
1073
- ...up,
1074
- "});",
1075
- "}",
1076
- "",
1077
- "export async function down(knex: Knex): Promise<void> {",
1078
- `return knex.schema.alterTable("${table}", (table) => {`,
1079
- "// drop fk",
1080
- ...down,
1081
- "});",
1082
- "}",
1083
- ];
1084
- const foreignKeysString = foreigns
1085
- .map((foreign) => foreign.columns.join("_"))
1086
- .join("_");
1087
- return [
1088
- {
1089
- table,
1090
- type: "foreign",
1091
- title: `foreign__${table}__${foreignKeysString}`,
1092
- formatted: prettier_1.default.format(lines.join("\n"), {
1093
- parser: "typescript",
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
- 1. 컬럼갯수 다름: MD에 있으나, DB에 없다면 추가
1144
- 2. 컬럼갯수 다름: MD에 없으나, DB에 있다면 삭제
1145
- 3. 그외 컬럼(컬럼 갯수가 동일하거나, 다른 경우 동일한 컬럼끼리) => alter
1146
- 4. 다른거 동일하고 index만 변경되는 경우
1147
-
1148
- ** 컬럼명을 변경하는 경우는 따로 핸들링하지 않음
1149
- => drop/add 형태의 마이그레이션 코드가 생성되는데, 수동으로 rename 코드로 수정하여 처리
1150
- */
1151
- // 각 컬럼 이름 기준으로 add, drop, alter 여부 확인
1152
- const alterColumnsTo = this.getAlterColumnsTo(entityColumns, dbColumns);
1153
- // 추출된 컬럼들을 기준으로 각각 라인 생성
1154
- const alterColumnLinesTo = this.getAlterColumnLinesTo(alterColumnsTo, entityColumns);
1155
- // 인덱스의 add, drop 여부 확인
1156
- const alterIndexesTo = this.getAlterIndexesTo(entityIndexes, dbIndexes);
1157
- // 추출된 인덱스들을 기준으로 각각 라인 생성
1158
- const alterIndexLinesTo = this.getAlterIndexLinesTo(alterIndexesTo, alterColumnsTo);
1159
- const lines = [
1160
- 'import { Knex } from "knex";',
1161
- "",
1162
- "export async function up(knex: Knex): Promise<void> {",
1163
- `return knex.schema.alterTable("${table}", (table) => {`,
1164
- ...(alterColumnsTo.add.length > 0 ? alterColumnLinesTo.add.up : []),
1165
- ...(alterColumnsTo.drop.length > 0 ? alterColumnLinesTo.drop.up : []),
1166
- ...(alterColumnsTo.alter.length > 0 ? alterColumnLinesTo.alter.up : []),
1167
- ...(alterIndexesTo.add.length > 0 ? alterIndexLinesTo.add.up : []),
1168
- ...(alterIndexesTo.drop.length > 0 ? alterIndexLinesTo.drop.up : []),
1169
- "})",
1170
- "}",
1171
- "",
1172
- "export async function down(knex: Knex): Promise<void> {",
1173
- `return knex.schema.alterTable("${table}", (table) => {`,
1174
- ...(alterColumnsTo.add.length > 0 ? alterColumnLinesTo.add.down : []),
1175
- ...(alterColumnsTo.drop.length > 0 ? alterColumnLinesTo.drop.down : []),
1176
- ...(alterColumnsTo.alter.length > 0 ? alterColumnLinesTo.alter.down : []),
1177
- ...(alterIndexLinesTo.add.down.length > 1
1178
- ? alterIndexLinesTo.add.down
1179
- : []),
1180
- ...(alterIndexLinesTo.drop.down.length > 1
1181
- ? alterIndexLinesTo.drop.down
1182
- : []),
1183
- "})",
1184
- "}",
1185
- ];
1186
- const formatted = prettier_1.default.format(lines.join("\n"), {
1187
- parser: "typescript",
1188
- });
1189
- const title = [
1190
- "alter",
1191
- table,
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
- title,
1206
- formatted,
1207
- type: "normal",
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
- // console.log({ entityForeigns, dbForeigns });
1356
- const getKey = (mf) => {
1357
- return [mf.columns.join("-"), mf.to].join("///");
1358
- };
1359
- const fkTo = entityForeigns.reduce((result, entityF) => {
1360
- const matchingDbF = dbForeigns.find((dbF) => getKey(entityF) === getKey(dbF));
1361
- if (!matchingDbF) {
1362
- result.add.push(entityF);
1363
- return result;
1364
- }
1365
- if ((0, fast_deep_equal_1.default)(entityF, matchingDbF) === false) {
1366
- result.alterSrc.push(matchingDbF);
1367
- result.alterDst.push(entityF);
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
- return result;
1371
- }, {
1372
- add: [],
1373
- alterSrc: [],
1374
- alterDst: [],
1375
- });
1376
- const linesTo = {
1377
- add: this.genForeignDefinitions(table, fkTo.add),
1378
- alterSrc: this.genForeignDefinitions(table, fkTo.alterSrc),
1379
- alterDst: this.genForeignDefinitions(table, fkTo.alterDst),
1380
- };
1381
- const lines = [
1382
- 'import { Knex } from "knex";',
1383
- "",
1384
- "export async function up(knex: Knex): Promise<void> {",
1385
- `return knex.schema.alterTable("${table}", (table) => {`,
1386
- ...linesTo.add.up,
1387
- ...linesTo.alterSrc.down,
1388
- ...linesTo.alterDst.up,
1389
- "})",
1390
- "}",
1391
- "",
1392
- "export async function down(knex: Knex): Promise<void> {",
1393
- `return knex.schema.alterTable("${table}", (table) => {`,
1394
- ...linesTo.add.down,
1395
- ...linesTo.alterDst.down,
1396
- ...linesTo.alterSrc.up,
1397
- "})",
1398
- "}",
1399
- ];
1400
- const formatted = prettier_1.default.format(lines.join("\n"), {
1401
- parser: "typescript",
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
- title,
1413
- formatted,
1414
- type: "normal",
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* () {