sonamu 0.2.34 → 0.2.36

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 (40) hide show
  1. package/.pnp.cjs +31 -0
  2. package/dist/api/sonamu.d.ts.map +1 -1
  3. package/dist/api/sonamu.js +2 -2
  4. package/dist/api/sonamu.js.map +1 -1
  5. package/dist/bin/cli.js +1 -1
  6. package/dist/bin/cli.js.map +1 -1
  7. package/dist/database/base-model.d.ts.map +1 -1
  8. package/dist/database/base-model.js +17 -13
  9. package/dist/database/base-model.js.map +1 -1
  10. package/dist/database/upsert-builder.d.ts.map +1 -1
  11. package/dist/database/upsert-builder.js +4 -5
  12. package/dist/database/upsert-builder.js.map +1 -1
  13. package/dist/entity/migrator.d.ts.map +1 -1
  14. package/dist/entity/migrator.js +19 -24
  15. package/dist/entity/migrator.js.map +1 -1
  16. package/dist/templates/generated.template.d.ts.map +1 -1
  17. package/dist/templates/generated.template.js +1 -0
  18. package/dist/templates/generated.template.js.map +1 -1
  19. package/dist/templates/service.template.js +2 -2
  20. package/dist/templates/service.template.js.map +1 -1
  21. package/dist/templates/view_form.template.d.ts +2 -2
  22. package/dist/templates/view_list.template.d.ts +2 -2
  23. package/dist/utils/lodash-able.d.ts +0 -1
  24. package/dist/utils/lodash-able.d.ts.map +1 -1
  25. package/dist/utils/lodash-able.js +1 -5
  26. package/dist/utils/lodash-able.js.map +1 -1
  27. package/dist/utils/sql-parser.d.ts +4 -0
  28. package/dist/utils/sql-parser.d.ts.map +1 -0
  29. package/dist/utils/sql-parser.js +37 -0
  30. package/dist/utils/sql-parser.js.map +1 -0
  31. package/package.json +2 -1
  32. package/src/api/sonamu.ts +4 -2
  33. package/src/bin/cli.ts +1 -1
  34. package/src/database/base-model.ts +27 -17
  35. package/src/database/upsert-builder.ts +8 -10
  36. package/src/entity/migrator.ts +18 -26
  37. package/src/templates/generated.template.ts +1 -0
  38. package/src/templates/service.template.ts +2 -2
  39. package/src/utils/lodash-able.ts +0 -4
  40. package/src/utils/sql-parser.ts +40 -0
@@ -52,7 +52,6 @@ import {
52
52
  isKnexError,
53
53
  RelationOn,
54
54
  } from "../types/types";
55
- import { propIf } from "../utils/lodash-able";
56
55
  import { EntityManager } from "./entity-manager";
57
56
  import { Entity } from "./entity";
58
57
  import { Sonamu } from "../api";
@@ -993,7 +992,6 @@ export class Migrator {
993
992
  if (rawType === "char" && colField === "uuid") {
994
993
  return {
995
994
  type: "uuid",
996
- ...propIf(length !== undefined, {}),
997
995
  };
998
996
  }
999
997
 
@@ -1007,7 +1005,7 @@ export class Migrator {
1007
1005
  // case "char":
1008
1006
  return {
1009
1007
  type: "string",
1010
- ...propIf(length !== undefined, {
1008
+ ...(length !== undefined && {
1011
1009
  length,
1012
1010
  }),
1013
1011
  };
@@ -1038,7 +1036,7 @@ export class Migrator {
1038
1036
  type: "decimal",
1039
1037
  precision: parseInt(precision),
1040
1038
  scale: parseInt(scale),
1041
- ...propIf(unsigned === "unsigned", {
1039
+ ...(unsigned === "unsigned" && {
1042
1040
  unsigned: true,
1043
1041
  }),
1044
1042
  };
@@ -1049,7 +1047,7 @@ export class Migrator {
1049
1047
  type: "float",
1050
1048
  precision: parseInt(precision),
1051
1049
  scale: parseInt(scale),
1052
- ...propIf(unsigned === "unsigned", {
1050
+ ...(unsigned === "unsigned" && {
1053
1051
  unsigned: true,
1054
1052
  }),
1055
1053
  };
@@ -1073,15 +1071,13 @@ export class Migrator {
1073
1071
  const cols = _cols.map((col) => ({
1074
1072
  ...col,
1075
1073
  // Default 값은 숫자나 MySQL Expression이 아닌 경우 ""로 감싸줌
1076
- ...(col.Default !== null
1077
- ? {
1078
- Default:
1079
- col.Default.replace(/[0-9]+/g, "").length > 0 &&
1080
- col.Extra !== "DEFAULT_GENERATED"
1081
- ? `"${col.Default}"`
1082
- : col.Default,
1083
- }
1084
- : {}),
1074
+ ...(col.Default !== null && {
1075
+ Default:
1076
+ col.Default.replace(/[0-9]+/g, "").length > 0 &&
1077
+ col.Extra !== "DEFAULT_GENERATED"
1078
+ ? `"${col.Default}"`
1079
+ : col.Default,
1080
+ }),
1085
1081
  }));
1086
1082
 
1087
1083
  const [indexes] = await compareDB.raw(`SHOW INDEX FROM ${tableName}`);
@@ -1154,12 +1150,10 @@ export class Migrator {
1154
1150
  const column = {
1155
1151
  name: prop.name,
1156
1152
  type,
1157
- ...(isIntegerProp(prop)
1158
- ? { unsigned: prop.unsigned === true }
1159
- : {}),
1160
- ...(isStringProp(prop) || isEnumProp(prop)
1161
- ? { length: prop.length }
1162
- : {}),
1153
+ ...(isIntegerProp(prop) && { unsigned: prop.unsigned === true }),
1154
+ ...((isStringProp(prop) || isEnumProp(prop)) && {
1155
+ length: prop.length,
1156
+ }),
1163
1157
  nullable: prop.nullable === true,
1164
1158
  ...(() => {
1165
1159
  if (prop.dbDefault !== undefined) {
@@ -1170,12 +1164,10 @@ export class Migrator {
1170
1164
  return {};
1171
1165
  })(),
1172
1166
  // Decimal, Float 타입의 경우 precision, scale 추가
1173
- ...(isDecimalProp(prop) || isFloatProp(prop)
1174
- ? {
1175
- precision: prop.precision ?? 8,
1176
- scale: prop.scale ?? 2,
1177
- }
1178
- : {}),
1167
+ ...((isDecimalProp(prop) || isFloatProp(prop)) && {
1168
+ precision: prop.precision ?? 8,
1169
+ scale: prop.scale ?? 2,
1170
+ }),
1179
1171
  };
1180
1172
 
1181
1173
  r.columns.push(column);
@@ -140,6 +140,7 @@ export class Template__generated extends Template {
140
140
  label: `Enums: ${entity.id}`,
141
141
  lines: [
142
142
  ...Object.entries(entity.enumLabels)
143
+ .filter(([_, enumLabel]) => Object.keys(enumLabel).length > 0)
143
144
  .map(([enumId, enumLabel]) => [
144
145
  `export const ${enumId} = z.enum([${Object.keys(enumLabel).map(
145
146
  (el) => `"${el}"`
@@ -241,13 +241,13 @@ export async function ${api.methodName}${typeParamsDef}(
241
241
  return ` export function ${camelize(
242
242
  methodNameSwr,
243
243
  true
244
- )}${typeParamsDef}(${[paramsDef, "options?: SwrOptions"]
244
+ )}${typeParamsDef}(${[paramsDef, "swrOptions?: SwrOptions"]
245
245
  .filter((p) => p !== "")
246
246
  .join(",")}, ): SWRResponse<${returnTypeDef}, SWRError> {
247
247
  return useSWR(handleConditional([
248
248
  \`${apiBaseUrl}\`,
249
249
  ${payloadDef},
250
- ], options?.conditional)${
250
+ ], swrOptions?.conditional)${
251
251
  api.options.httpMethod === "POST" ? ", swrPostFetcher" : ""
252
252
  });
253
253
  }`;
@@ -5,7 +5,3 @@ export function wrapIf(
5
5
  const [ok, wrapped] = predicate(source);
6
6
  return ok ? wrapped : source;
7
7
  }
8
-
9
- export function propIf(expression: boolean, obj: any) {
10
- return expression ? obj : {};
11
- }
@@ -0,0 +1,40 @@
1
+ import { uniq } from "lodash";
2
+ import { AST, ColumnRef, Expr, ExpressionValue, Select } from "node-sql-parser";
3
+
4
+ export function getTableName(expr: ColumnRef) {
5
+ if ("table" in expr && expr.table !== null) {
6
+ return typeof expr.table === "string"
7
+ ? expr.table
8
+ : (expr.table as { type: string; value: string }).value;
9
+ }
10
+ return null;
11
+ }
12
+
13
+ // where 조건에 사용된 테이블명을 추출
14
+ export function getTableNamesFromWhere(ast: AST | AST[]): string[] {
15
+ const extractTableNames = (where: Select["where"]): string[] => {
16
+ if (where === null || !(where.type === "binary_expr" && "left" in where)) {
17
+ return [];
18
+ }
19
+
20
+ const extractTableName = (expr: Expr | ExpressionValue): string[] => {
21
+ if (expr.type === "column_ref") {
22
+ const table = getTableName(expr as ColumnRef);
23
+ return table ? [table] : [];
24
+ } else if (expr.type === "binary_expr" && "left" in expr) {
25
+ return extractTableNames(expr);
26
+ }
27
+ return [];
28
+ };
29
+
30
+ return [...extractTableName(where.left), ...extractTableName(where.right)];
31
+ };
32
+
33
+ return uniq(
34
+ (Array.isArray(ast) ? ast : [ast]).flatMap((a) =>
35
+ a.type === "select" || a.type === "update" || a.type === "delete"
36
+ ? extractTableNames(a.where)
37
+ : []
38
+ )
39
+ );
40
+ }