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.
- package/dist/api/code-converters.js +2 -2
- package/dist/api/config.js +2 -2
- package/dist/api/decorators.d.ts.map +1 -1
- package/dist/api/decorators.js +2 -2
- package/dist/api/sonamu.d.ts.map +1 -1
- package/dist/api/sonamu.js +3 -4
- package/dist/bin/cli.js +1 -17
- package/dist/database/base-model.types.d.ts +1 -0
- package/dist/database/base-model.types.d.ts.map +1 -1
- package/dist/database/base-model.types.js +2 -2
- package/dist/database/puri-wrapper.js +7 -3
- package/dist/database/upsert-builder.d.ts +7 -3
- package/dist/database/upsert-builder.d.ts.map +1 -1
- package/dist/database/upsert-builder.js +63 -25
- package/dist/entity/entity-manager.d.ts +1 -1
- package/dist/entity/entity.js +3 -3
- package/dist/migration/code-generation.d.ts.map +1 -1
- package/dist/migration/code-generation.js +8 -7
- package/dist/migration/migration-set.d.ts.map +1 -1
- package/dist/migration/migration-set.js +2 -25
- package/dist/migration/migrator.js +2 -2
- package/dist/migration/postgresql-schema-reader.d.ts.map +1 -1
- package/dist/migration/postgresql-schema-reader.js +2 -1
- package/dist/syncer/file-patterns.js +2 -2
- package/dist/template/implementations/service.template.d.ts.map +1 -1
- package/dist/template/implementations/service.template.js +3 -2
- package/dist/types/types.d.ts +4 -3
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/types.js +2 -2
- package/dist/utils/model.d.ts +9 -2
- package/dist/utils/model.d.ts.map +1 -1
- package/dist/utils/model.js +1 -1
- package/dist/utils/path-utils.d.ts +1 -1
- package/dist/utils/path-utils.d.ts.map +1 -1
- package/dist/utils/path-utils.js +1 -1
- package/package.json +7 -7
- package/src/api/code-converters.ts +2 -2
- package/src/api/config.ts +1 -1
- package/src/api/decorators.ts +1 -1
- package/src/api/sonamu.ts +2 -5
- package/src/bin/cli.ts +0 -17
- package/src/database/base-model.types.ts +2 -0
- package/src/database/puri-wrapper.ts +2 -2
- package/src/database/upsert-builder.ts +88 -29
- package/src/entity/entity.ts +2 -2
- package/src/migration/code-generation.ts +8 -6
- package/src/migration/migration-set.ts +0 -20
- package/src/migration/migrator.ts +1 -1
- package/src/migration/postgresql-schema-reader.ts +1 -0
- package/src/shared/web.shared.ts.txt +6 -4
- package/src/syncer/file-patterns.ts +1 -1
- package/src/template/implementations/service.template.ts +2 -1
- package/src/types/types.ts +3 -2
- package/src/utils/model.ts +10 -4
- 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(
|
|
154
|
-
|
|
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(
|
|
157
|
-
|
|
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
|
-
|
|
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(["
|
|
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
|
-
|
|
277
|
+
// uuid를 별도로 보관하고, DB에 저장할 데이터에서 제거
|
|
278
|
+
const originalUuids = dataChunk.map((r) => r.uuid as string);
|
|
279
|
+
const dataForDb = dataChunk.map(({ uuid, ...rest }) => rest);
|
|
254
280
|
|
|
255
|
-
|
|
256
|
-
// INSERT 모드
|
|
257
|
-
await wdb.insert(dataChunk).into(tableName);
|
|
281
|
+
let resultRows: { id: number; [key: string]: unknown }[];
|
|
258
282
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
|
287
|
+
// UPSERT 모드 - onConflict 사용
|
|
265
288
|
const conflictColumns = table.uniqueIndexes[0].columns;
|
|
266
|
-
const updateColumns = Object.keys(
|
|
267
|
-
(col) =>
|
|
289
|
+
const updateColumns = Object.keys(dataForDb[0]).filter(
|
|
290
|
+
(col) => !conflictColumns.includes(col),
|
|
268
291
|
);
|
|
269
292
|
|
|
270
|
-
|
|
293
|
+
// updateColumns가 비어있어도 merge()를 사용하여 모든 행이 RETURNING되도록 보장
|
|
294
|
+
const mergeColumns = updateColumns.length > 0 ? updateColumns : conflictColumns;
|
|
271
295
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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();
|
package/src/entity/entity.ts
CHANGED
|
@@ -524,8 +524,8 @@ export class Entity {
|
|
|
524
524
|
// 일반 prop 처리
|
|
525
525
|
if (key === "") {
|
|
526
526
|
return group.map((propName) => {
|
|
527
|
-
//
|
|
528
|
-
if (propName === "
|
|
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
|
-
|
|
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" ? ",
|
|
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
|
|
728
|
+
[a.type, ...a.columns].join("-"),
|
|
727
729
|
);
|
|
728
730
|
const dbIndexes = alphabetical(dbSet.indexes, (a) =>
|
|
729
|
-
[a.type, ...a.columns
|
|
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
|
|
|
@@ -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
|
-
|
|
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
|
|
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
|
);
|
package/src/types/types.ts
CHANGED
|
@@ -175,7 +175,7 @@ export type EntityProp =
|
|
|
175
175
|
export type EntityIndex = {
|
|
176
176
|
type: "index" | "unique" | "fulltext";
|
|
177
177
|
columns: string[];
|
|
178
|
-
name
|
|
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().
|
|
946
|
+
name: z.string().min(1).max(63),
|
|
946
947
|
parser: z.enum(["built-in", "ngram"]).optional(),
|
|
947
948
|
})
|
|
948
949
|
.strict();
|
package/src/utils/model.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
package/src/utils/path-utils.ts
CHANGED
|
@@ -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}
|
|
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(
|
|
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 {
|