sonamu 0.7.2 → 0.7.3

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 (55) hide show
  1. package/dist/api/code-converters.js +2 -2
  2. package/dist/api/config.js +2 -2
  3. package/dist/api/decorators.d.ts.map +1 -1
  4. package/dist/api/decorators.js +2 -2
  5. package/dist/api/sonamu.d.ts.map +1 -1
  6. package/dist/api/sonamu.js +3 -4
  7. package/dist/bin/cli.js +1 -17
  8. package/dist/database/base-model.types.d.ts +1 -0
  9. package/dist/database/base-model.types.d.ts.map +1 -1
  10. package/dist/database/base-model.types.js +2 -2
  11. package/dist/database/puri-wrapper.js +7 -3
  12. package/dist/database/upsert-builder.d.ts +7 -3
  13. package/dist/database/upsert-builder.d.ts.map +1 -1
  14. package/dist/database/upsert-builder.js +63 -25
  15. package/dist/entity/entity-manager.d.ts +1 -1
  16. package/dist/entity/entity.js +3 -3
  17. package/dist/migration/code-generation.d.ts.map +1 -1
  18. package/dist/migration/code-generation.js +8 -7
  19. package/dist/migration/migration-set.d.ts.map +1 -1
  20. package/dist/migration/migration-set.js +2 -25
  21. package/dist/migration/migrator.js +2 -2
  22. package/dist/migration/postgresql-schema-reader.d.ts.map +1 -1
  23. package/dist/migration/postgresql-schema-reader.js +2 -1
  24. package/dist/syncer/file-patterns.js +2 -2
  25. package/dist/template/implementations/service.template.d.ts.map +1 -1
  26. package/dist/template/implementations/service.template.js +3 -2
  27. package/dist/types/types.d.ts +4 -3
  28. package/dist/types/types.d.ts.map +1 -1
  29. package/dist/types/types.js +2 -2
  30. package/dist/utils/model.d.ts +9 -2
  31. package/dist/utils/model.d.ts.map +1 -1
  32. package/dist/utils/model.js +1 -1
  33. package/dist/utils/path-utils.d.ts +1 -1
  34. package/dist/utils/path-utils.d.ts.map +1 -1
  35. package/dist/utils/path-utils.js +1 -1
  36. package/package.json +7 -7
  37. package/src/api/code-converters.ts +2 -2
  38. package/src/api/config.ts +1 -1
  39. package/src/api/decorators.ts +1 -1
  40. package/src/api/sonamu.ts +2 -5
  41. package/src/bin/cli.ts +0 -17
  42. package/src/database/base-model.types.ts +2 -0
  43. package/src/database/puri-wrapper.ts +2 -2
  44. package/src/database/upsert-builder.ts +88 -29
  45. package/src/entity/entity.ts +2 -2
  46. package/src/migration/code-generation.ts +8 -6
  47. package/src/migration/migration-set.ts +0 -20
  48. package/src/migration/migrator.ts +1 -1
  49. package/src/migration/postgresql-schema-reader.ts +1 -0
  50. package/src/shared/web.shared.ts.txt +6 -4
  51. package/src/syncer/file-patterns.ts +1 -1
  52. package/src/template/implementations/service.template.ts +2 -1
  53. package/src/types/types.ts +3 -2
  54. package/src/utils/model.ts +10 -4
  55. package/src/utils/path-utils.ts +5 -2
@@ -1,6 +1,6 @@
1
1
  import { randomUUID } from "crypto";
2
2
  import type { Knex } from "knex";
3
- import { unique } from "radashi";
3
+ import { isArray, unique } from "radashi";
4
4
  import { EntityManager } from "../entity/entity-manager";
5
5
  import { Naite } from "../naite/naite";
6
6
  import { assertDefined, chunk, nonNullable } from "../utils/utils";
@@ -17,6 +17,10 @@ export type UBRef = {
17
17
  of: string;
18
18
  use?: string;
19
19
  };
20
+ type UpsertOptions = {
21
+ chunkSize?: number;
22
+ cleanOrphans?: string | string[]; // FK 컬럼명(들)
23
+ };
20
24
  export function isRefField(field: unknown): field is UBRef {
21
25
  return (
22
26
  field !== undefined &&
@@ -150,18 +154,37 @@ export class UpsertBuilder {
150
154
  return result;
151
155
  }
152
156
 
153
- async upsert(wdb: Knex, tableName: string, chunkSize?: number): Promise<number[]> {
154
- return this.upsertOrInsert(wdb, tableName, "upsert", chunkSize);
157
+ async upsert(
158
+ wdb: Knex,
159
+ tableName: string,
160
+ optionsOrChunkSize?: UpsertOptions,
161
+ ): Promise<number[]> {
162
+ // 숫자면 { chunkSize: n } 으로 변환
163
+ const options =
164
+ typeof optionsOrChunkSize === "number"
165
+ ? { chunkSize: optionsOrChunkSize }
166
+ : optionsOrChunkSize;
167
+
168
+ return this.upsertOrInsert(wdb, tableName, "upsert", options);
155
169
  }
156
- async insertOnly(wdb: Knex, tableName: string, chunkSize?: number): Promise<number[]> {
157
- return this.upsertOrInsert(wdb, tableName, "insert", chunkSize);
170
+ async insertOnly(
171
+ wdb: Knex,
172
+ tableName: string,
173
+ optionsOrChunkSize?: UpsertOptions | number,
174
+ ): Promise<number[]> {
175
+ const options =
176
+ typeof optionsOrChunkSize === "number"
177
+ ? { chunkSize: optionsOrChunkSize }
178
+ : optionsOrChunkSize;
179
+
180
+ return this.upsertOrInsert(wdb, tableName, "insert", options);
158
181
  }
159
182
 
160
183
  async upsertOrInsert(
161
184
  wdb: Knex,
162
185
  tableName: string,
163
186
  mode: "upsert" | "insert",
164
- chunkSize?: number,
187
+ options?: UpsertOptions,
165
188
  ): Promise<number[]> {
166
189
  if (this.hasTable(tableName) === false) {
167
190
  return [];
@@ -244,43 +267,47 @@ export class UpsertBuilder {
244
267
  });
245
268
 
246
269
  // 현재 레벨 upsert
270
+ const chunkSize = options?.chunkSize;
247
271
  const levelChunks = chunkSize ? chunk(resolvedRows, chunkSize) : [resolvedRows];
248
- const selectFields = unique(["uuid", "id", ...extractFields]);
272
+ const selectFields = unique(["id", ...extractFields]);
249
273
 
250
274
  for (const dataChunk of levelChunks) {
251
275
  if (dataChunk.length === 0) continue;
252
276
 
253
- let resultRows: { uuid: string; id: number; [key: string]: unknown }[];
277
+ // uuid 별도로 보관하고, DB에 저장할 데이터에서 제거
278
+ const originalUuids = dataChunk.map((r) => r.uuid as string);
279
+ const dataForDb = dataChunk.map(({ uuid, ...rest }) => rest);
254
280
 
255
- if (mode === "insert") {
256
- // INSERT 모드
257
- await wdb.insert(dataChunk).into(tableName);
281
+ let resultRows: { id: number; [key: string]: unknown }[];
258
282
 
259
- const uuids = dataChunk.map((r) => r.uuid);
260
- resultRows = await wdb(tableName)
261
- .select(selectFields)
262
- .whereIn("uuid", uuids as readonly string[]);
283
+ if (mode === "insert") {
284
+ // INSERT 모드 - RETURNING 사용
285
+ resultRows = await wdb.insert(dataForDb).into(tableName).returning(selectFields);
263
286
  } else {
264
- // UPSERT 모드: onConflict 중복 처리
287
+ // UPSERT 모드 - onConflict 사용
265
288
  const conflictColumns = table.uniqueIndexes[0].columns;
266
- const updateColumns = Object.keys(dataChunk[0]).filter(
267
- (col) => col !== "uuid" && !conflictColumns.includes(col),
289
+ const updateColumns = Object.keys(dataForDb[0]).filter(
290
+ (col) => !conflictColumns.includes(col),
268
291
  );
269
292
 
270
- const query = wdb.insert(dataChunk).into(tableName).onConflict(conflictColumns);
293
+ // updateColumns가 비어있어도 merge()를 사용하여 모든 행이 RETURNING되도록 보장
294
+ const mergeColumns = updateColumns.length > 0 ? updateColumns : conflictColumns;
271
295
 
272
- // updateColumns 유무에 따라 ignore/merge 선택하고 RETURNING으로 결과 받기
273
- if (updateColumns.length === 0) {
274
- resultRows = await query.ignore().returning(selectFields);
275
- } else {
276
- resultRows = await query.merge(updateColumns).returning(selectFields);
277
- }
296
+ resultRows = await wdb
297
+ .insert(dataForDb)
298
+ .into(tableName)
299
+ .onConflict(conflictColumns)
300
+ .merge(mergeColumns)
301
+ .returning(selectFields);
278
302
  }
279
303
 
280
- // 양쪽 모드 공통 처리
281
- for (const row of resultRows) {
282
- uuidMap.set(row.uuid, row);
283
- allIds.push(row.id);
304
+ if (originalUuids.length !== resultRows.length) {
305
+ throw new Error(`${tableName}: register/returning 불일치`);
306
+ }
307
+
308
+ for (let i = 0; i < resultRows.length; i++) {
309
+ uuidMap.set(originalUuids[i], resultRows[i]);
310
+ allIds.push(resultRows[i].id);
284
311
  }
285
312
  }
286
313
  }
@@ -311,6 +338,38 @@ export class UpsertBuilder {
311
338
  });
312
339
  }
313
340
 
341
+ if (options?.cleanOrphans) {
342
+ const cleanOrphans = options.cleanOrphans;
343
+ const fkColumns = isArray(cleanOrphans) ? cleanOrphans : [cleanOrphans];
344
+
345
+ // 현재 register된 레코드들의 FK 값들 추출
346
+ const fkConditions = fkColumns.map((fkCol) => {
347
+ const fkValues = [...new Set(table.rows.map((row) => row[fkCol]).filter((v) => v != null))];
348
+ return { column: fkCol, values: fkValues };
349
+ });
350
+
351
+ // 모든 FK 컬럼에 값이 있는 경우에만 삭제 실행
352
+ if (fkConditions.every((fc) => fc.values.length > 0)) {
353
+ let deleteQuery = wdb(tableName);
354
+
355
+ // 각 FK 컬럼에 대한 WHERE IN 조건 추가
356
+ for (const { column, values } of fkConditions) {
357
+ deleteQuery = deleteQuery.whereIn(column, values);
358
+ }
359
+
360
+ // 방금 upsert한 ID는 제외
361
+ deleteQuery = deleteQuery.whereNotIn("id", allIds);
362
+
363
+ const deletedCount = await deleteQuery.delete();
364
+
365
+ Naite.t("puri:ub-clean-orphans", {
366
+ tableName,
367
+ cleanOrphans: fkColumns,
368
+ deletedCount,
369
+ });
370
+ }
371
+ }
372
+
314
373
  // 해당 테이블의 데이터 초기화
315
374
  table.rows = [];
316
375
  table.references.clear();
@@ -524,8 +524,8 @@ export class Entity {
524
524
  // 일반 prop 처리
525
525
  if (key === "") {
526
526
  return group.map((propName) => {
527
- // uuid 개별 처리
528
- if (propName === "uuid") {
527
+ // FIXME: 이거 나중에 없애야함
528
+ if (propName === "말도안되는프롭명__이거왜타입처리가꼬여서이러지?") {
529
529
  return {
530
530
  nodeType: "plain" as const,
531
531
  prop: {
@@ -146,15 +146,15 @@ function genIndexDefinition(index: MigrationIndex, table: string) {
146
146
  };
147
147
 
148
148
  if (index.type === "fulltext" && index.parser === "ngram") {
149
- const indexName = `${table}_${index.columns.join("_")}_index`;
150
- return `await knex.raw(\`ALTER TABLE ${table} ADD FULLTEXT INDEX ${indexName} (${index.columns.join(
149
+ return `await knex.raw(\`ALTER TABLE ${table} ADD FULLTEXT INDEX ${index.name} (${index.columns.join(
151
150
  ", ",
152
151
  )}) WITH PARSER ngram\`);`;
153
152
  }
154
153
 
155
154
  return `table.${methodMap[index.type]}([${index.columns
156
155
  .map((col) => `'${col}'`)
157
- .join(",")}]${index.type === "fulltext" ? ", undefined, 'FULLTEXT'" : ""})`;
156
+ .join(",")}], '${index.name}'${index.type === "fulltext" ? ", 'FULLTEXT'" : ""}
157
+ );`;
158
158
  }
159
159
 
160
160
  /**
@@ -309,6 +309,8 @@ async function generateAlterCode_ColumnAndIndexes(
309
309
  });
310
310
  // Naite.t("migrator:generateAlterCode_ColumnAndIndexes:alterColumnsTo", alterColumnsTo);
311
311
 
312
+ // TODO: 인덱스명 변경된 경우 처리
313
+
312
314
  const lines: string[] = [
313
315
  'import { Knex } from "knex";',
314
316
  "",
@@ -536,7 +538,7 @@ function genIndexDropDefinition(index: MigrationIndex) {
536
538
 
537
539
  return `table.drop${methodMap[index.type]}([${index.columns
538
540
  .map((columnName) => `'${columnName}'`)
539
- .join(",")}])`;
541
+ .join(",")}], '${index.name}')`;
540
542
  }
541
543
 
542
544
  /**
@@ -723,10 +725,10 @@ export async function generateAlterCode(
723
725
  */
724
726
 
725
727
  const entityIndexes = alphabetical(entitySet.indexes, (a) =>
726
- [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-"),
728
+ [a.type, ...a.columns].join("-"),
727
729
  );
728
730
  const dbIndexes = alphabetical(dbSet.indexes, (a) =>
729
- [a.type, ...a.columns.sort((c1, c2) => (c1 > c2 ? 1 : -1))].join("-"),
731
+ [a.type, ...a.columns].join("-"),
730
732
  );
731
733
 
732
734
  const replaceNoActionOnMySQL = (f: MigrationForeign) => {
@@ -88,10 +88,6 @@ export function getMigrationSetFromEntity(entity: Entity): MigrationSetAndJoinTa
88
88
  r.joinTables.push({
89
89
  table: through.from.split(".")[0],
90
90
  indexes: [
91
- {
92
- type: "unique",
93
- columns: ["uuid"],
94
- },
95
91
  // 조인 테이블에 걸린 인덱스 찾아와서 연결
96
92
  ...entity.indexes
97
93
  .filter((index) => index.columns.find((col) => col.includes(`${prop.joinTable}.`)))
@@ -113,11 +109,6 @@ export function getMigrationSetFromEntity(entity: Entity): MigrationSetAndJoinTa
113
109
  nullable: false,
114
110
  } as MigrationColumn;
115
111
  }),
116
- {
117
- name: "uuid",
118
- nullable: true,
119
- type: "uuid",
120
- },
121
112
  ],
122
113
  foreigns: fields.map((field) => {
123
114
  // 현재 필드가 어떤 테이블에 속하는지 판단
@@ -175,17 +166,6 @@ export function getMigrationSetFromEntity(entity: Entity): MigrationSetAndJoinTa
175
166
  index.columns.find((col) => col.includes(".") === false),
176
167
  );
177
168
 
178
- // uuid
179
- migrationSet.columns = migrationSet.columns.concat({
180
- name: "uuid",
181
- nullable: true,
182
- type: "uuid",
183
- } as MigrationColumn);
184
- migrationSet.indexes = migrationSet.indexes.concat({
185
- type: "unique",
186
- columns: ["uuid"],
187
- } as MigrationIndex);
188
-
189
169
  return migrationSet;
190
170
  }
191
171
 
@@ -341,7 +341,7 @@ export class Migrator {
341
341
  ...tables[0],
342
342
  indexes: unique(
343
343
  tables.flatMap((t) => t.indexes),
344
- (index) => [index.type, ...index.columns.sort()].join("-"),
344
+ (index) => [index.type, ...index.columns].join("-"),
345
345
  ),
346
346
  };
347
347
  });
@@ -109,6 +109,7 @@ class PostgreSQLSchemaReaderClass {
109
109
 
110
110
  return {
111
111
  type,
112
+ name: indexName,
112
113
  columns: currentIndexes.map((idx) => idx.column_name),
113
114
  };
114
115
  });
@@ -86,10 +86,12 @@ export function defaultCatch(e: any) {
86
86
  /*
87
87
  Isomorphic Types
88
88
  */
89
- export type ListResult<T> = {
90
- rows: T[];
91
- total?: number;
92
- };
89
+ export type ListResult<LP extends { queryMode?: SonamuQueryMode }, T> = LP["queryMode"] extends "list"
90
+ ? { rows: T[] }
91
+ : LP["queryMode"] extends "count"
92
+ ? { total: number }
93
+ : { rows: T[]; total: number };
94
+
93
95
  export const SonamuQueryMode = z.enum(["both", "list", "count"]);
94
96
  export type SonamuQueryMode = z.infer<typeof SonamuQueryMode>;
95
97
 
@@ -30,7 +30,7 @@ export const checksumPatternGroup: GlobPattern<ApiRelativePath> = {
30
30
  model: "src/application/**/*.model.ts",
31
31
  frame: "src/application/**/*.frame.ts",
32
32
  functions: "src/application/**/*.functions.ts",
33
- config: "sonamu.config.ts",
33
+ config: "src/sonamu.config.ts",
34
34
  };
35
35
 
36
36
  /**
@@ -85,11 +85,12 @@ export class Template__service extends Template {
85
85
  );
86
86
 
87
87
  // 파라미터 타입 정의
88
- const typeParamsDef = api.typeParameters
88
+ const typeParametersAsTsType = api.typeParameters
89
89
  .map((typeParam) => {
90
90
  return apiParamTypeToTsType(typeParam, importKeys);
91
91
  })
92
92
  .join(", ");
93
+ const typeParamsDef = typeParametersAsTsType ? `<${typeParametersAsTsType}>` : "";
93
94
  typeParamNames = typeParamNames.concat(
94
95
  api.typeParameters.map((typeParam) => typeParam.id),
95
96
  );
@@ -175,7 +175,7 @@ export type EntityProp =
175
175
  export type EntityIndex = {
176
176
  type: "index" | "unique" | "fulltext";
177
177
  columns: string[];
178
- name?: string;
178
+ name: string;
179
179
  parser?: "built-in" | "ngram";
180
180
  };
181
181
  export type EntityJson = {
@@ -451,6 +451,7 @@ export type MigrationColumn = {
451
451
  scale?: number;
452
452
  };
453
453
  export type MigrationIndex = {
454
+ name: string;
454
455
  columns: string[];
455
456
  type: "unique" | "index" | "fulltext";
456
457
  parser?: "built-in" | "ngram";
@@ -942,7 +943,7 @@ const EntityIndexSchema = z
942
943
  .object({
943
944
  type: z.enum(["index", "unique", "fulltext"]),
944
945
  columns: z.array(z.string()),
945
- name: z.string().optional(),
946
+ name: z.string().min(1).max(63),
946
947
  parser: z.enum(["built-in", "ngram"]).optional(),
947
948
  })
948
949
  .strict();
@@ -1,7 +1,13 @@
1
- export type ListResult<T> = {
2
- rows: T[];
3
- total?: number;
4
- };
1
+ import type { SonamuQueryMode } from "..";
2
+
3
+ export type ListResult<
4
+ LP extends { queryMode?: SonamuQueryMode },
5
+ T,
6
+ > = LP["queryMode"] extends "list"
7
+ ? { rows: T[] }
8
+ : LP["queryMode"] extends "count"
9
+ ? { total: number }
10
+ : { rows: T[]; total: number };
5
11
 
6
12
  export type ArrayOr<T> = T | T[];
7
13
 
@@ -10,7 +10,7 @@ import { isHotReloadServer, isTest } from "./controller.js";
10
10
  *
11
11
  * **기준점**: `Sonamu.apiRootPath` (일반적으로 프로젝트의 `/api` 디렉토리)
12
12
  */
13
- export type ApiRelativePath = `${"src" | "dist"}/${string}` | "sonamu.config.ts";
13
+ export type ApiRelativePath = `${"src" | "dist"}/${string}`;
14
14
 
15
15
  /**
16
16
  * 앱 루트 기준 상대 경로 (api/, web/ 등 타겟 디렉토리로 시작)
@@ -90,7 +90,10 @@ export type AbsolutePath = `/${string}`;
90
90
  * @param anyPath
91
91
  * @returns
92
92
  */
93
- export function runtimePath(anyPath: string, isDev: boolean = isHotReloadServer() || isTest()): string {
93
+ export function runtimePath(
94
+ anyPath: string,
95
+ isDev: boolean = isHotReloadServer() || isTest(),
96
+ ): string {
94
97
  if (isDev) {
95
98
  return anyPath.replace(/dist\//, "src/").replace(/\.js/, ".ts");
96
99
  } else {