sonamu 0.13.9 → 0.13.10
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/database/base-model.d.ts +5 -8
- package/dist/database/base-model.d.ts.map +1 -1
- package/dist/database/base-model.js +56 -24
- package/dist/database/puri.d.ts +1 -2
- package/dist/database/puri.d.ts.map +1 -1
- package/dist/database/puri.js +65 -58
- package/dist/entity/entity.d.ts +13 -1
- package/dist/entity/entity.d.ts.map +1 -1
- package/dist/entity/entity.js +41 -3
- package/package.json +1 -1
- package/src/database/__tests__/puri-hydration.test.ts +113 -76
- package/src/database/base-model.ts +75 -43
- package/src/database/puri.ts +84 -106
- package/src/entity/__tests__/entity-subset-query.test.ts +35 -0
- package/src/entity/entity.ts +58 -2
|
@@ -9,35 +9,10 @@ const query = () => new Puri<Schema, Schema, Schema["users"]>(db, "users");
|
|
|
9
9
|
|
|
10
10
|
type RawRow = { [column: string]: string | number | boolean | null | RawRow[] };
|
|
11
11
|
|
|
12
|
-
function result(
|
|
13
|
-
builder
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
) {
|
|
17
|
-
if (vi.isMockFunction(builder.clone)) builder.clone.mockRestore();
|
|
18
|
-
const clone = builder.clone.bind(builder);
|
|
19
|
-
vi.spyOn(builder, "clone").mockImplementation(() => {
|
|
20
|
-
const cloned = clone();
|
|
21
|
-
result(cloned, rows, presence);
|
|
22
|
-
return cloned;
|
|
23
|
-
});
|
|
24
|
-
vi.spyOn(builder, "then").mockImplementation((fulfilled, rejected) => {
|
|
25
|
-
const markers = [
|
|
26
|
-
...builder.toQuery().matchAll(/"([^"]+)"\.\* IS DISTINCT FROM NULL AS "([^"]+)"/g),
|
|
27
|
-
];
|
|
28
|
-
const withPresence = (row: RawRow, index: number) => ({
|
|
29
|
-
...row,
|
|
30
|
-
...Object.fromEntries(
|
|
31
|
-
markers.map(([, alias, column]) => [column, presence[alias]?.[index] ?? true]),
|
|
32
|
-
),
|
|
33
|
-
});
|
|
34
|
-
const output = Array.isArray(rows)
|
|
35
|
-
? rows.map(withPresence)
|
|
36
|
-
: rows
|
|
37
|
-
? withPresence(rows, 0)
|
|
38
|
-
: rows;
|
|
39
|
-
return Promise.resolve(output).then(fulfilled, rejected);
|
|
40
|
-
});
|
|
12
|
+
function result(builder: Knex.QueryBuilder, rows: RawRow | RawRow[] | undefined) {
|
|
13
|
+
vi.spyOn(builder, "then").mockImplementation((fulfilled, rejected) =>
|
|
14
|
+
Promise.resolve(rows).then(fulfilled, rejected),
|
|
15
|
+
);
|
|
41
16
|
}
|
|
42
17
|
|
|
43
18
|
afterAll(() => db.destroy());
|
|
@@ -64,19 +39,17 @@ describe("Puri 중첩 select 결과", () => {
|
|
|
64
39
|
expect(await selected).toEqual([{ user: { data: [] } }]);
|
|
65
40
|
});
|
|
66
41
|
|
|
67
|
-
it("중첩 단계마다 null 관계를
|
|
42
|
+
it("중첩 단계마다 id로 null 관계를 복원한다", async () => {
|
|
68
43
|
const selected = query()
|
|
69
44
|
.leftJoin({ user: "users" }, (join) => join.on("users.id", "user.id"))
|
|
70
45
|
.leftJoin({ user__detail: "users" }, (join) => join.on("users.id", "user__detail.id"))
|
|
71
|
-
.select({
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
{ user: [true, false], user__detail: [false, true] },
|
|
79
|
-
);
|
|
46
|
+
.select({
|
|
47
|
+
user: { id: "user.id", detail: { id: "user__detail.id", name: "user__detail.name" } },
|
|
48
|
+
});
|
|
49
|
+
result(selected.rawQuery(), [
|
|
50
|
+
{ user__id: 1, user__detail__id: null, user__detail__name: null },
|
|
51
|
+
{ user__id: null, user__detail__id: 2, user__detail__name: "무시할 값" },
|
|
52
|
+
]);
|
|
80
53
|
expect(await selected).toEqual([{ user: { id: 1, detail: null } }, { user: null }]);
|
|
81
54
|
});
|
|
82
55
|
|
|
@@ -86,17 +59,30 @@ describe("Puri 중첩 select 결과", () => {
|
|
|
86
59
|
expect(await selected).toEqual([{ profile: { nickname: null } }]);
|
|
87
60
|
});
|
|
88
61
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
62
|
+
const leftJoined = () =>
|
|
63
|
+
query().leftJoin({ colleague: "users" }, (join) => join.on("users.id", "colleague.id"));
|
|
64
|
+
it.each([
|
|
65
|
+
[
|
|
66
|
+
"LEFT JOIN 별칭과 같은 그룹",
|
|
67
|
+
() => leftJoined().select({ colleague: { nickname: "colleague.nickname" } }),
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
"LEFT JOIN 별칭을 다른 이름으로 묶은 그룹",
|
|
71
|
+
() => leftJoined().select({ profile: { nickname: "colleague.nickname" } }),
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
"칸 없이 하위 그룹만 가진 LEFT JOIN 그룹",
|
|
75
|
+
() => leftJoined().select({ colleague: { detail: { name: "users.name" } } }),
|
|
76
|
+
],
|
|
77
|
+
])("%s에 id가 없으면 실행할 때 오류를 낸다", async (_, build) => {
|
|
78
|
+
const selected = build();
|
|
79
|
+
result(selected.rawQuery(), []);
|
|
80
|
+
await expect(selected).rejects.toThrow(
|
|
81
|
+
/needs an "id" field because it reads LEFT JOIN "colleague"/,
|
|
82
|
+
);
|
|
83
|
+
// rawQuery와 toQuery는 검사하지 않습니다.
|
|
84
|
+
expect(selected.toQuery()).toContain("left join");
|
|
85
|
+
expect(await selected.rawQuery()).toEqual([]);
|
|
100
86
|
});
|
|
101
87
|
|
|
102
88
|
it("INNER JOIN 별칭과 같은 그룹도 nullable 필드를 보존한다", async () => {
|
|
@@ -107,28 +93,85 @@ describe("Puri 중첩 select 결과", () => {
|
|
|
107
93
|
expect(await selected).toEqual([{ colleague: { nickname: null } }]);
|
|
108
94
|
});
|
|
109
95
|
|
|
110
|
-
it("선택한 값이 모두 null이어도
|
|
96
|
+
it("선택한 값이 모두 null이어도 LEFT JOIN 행의 id가 있으면 객체를 유지한다", async () => {
|
|
111
97
|
const selected = query()
|
|
112
98
|
.leftJoin({ colleague: "users" }, (join) => join.on("users.id", "colleague.id"))
|
|
113
|
-
.select({ colleague: { nickname: "colleague.nickname" } });
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
expect(await selected
|
|
120
|
-
|
|
121
|
-
|
|
99
|
+
.select({ colleague: { id: "colleague.id", nickname: "colleague.nickname" } });
|
|
100
|
+
const rows = [
|
|
101
|
+
{ colleague__id: 1, colleague__nickname: null },
|
|
102
|
+
{ colleague__id: null, colleague__nickname: null },
|
|
103
|
+
];
|
|
104
|
+
result(selected.rawQuery(), rows);
|
|
105
|
+
expect(await selected).toEqual([{ colleague: { id: 1, nickname: null } }, { colleague: null }]);
|
|
106
|
+
expect(await selected.rawQuery()).toEqual(rows);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("GROUP BY에 id를 넣은 LEFT JOIN 그룹도 id로 판정한다", async () => {
|
|
110
|
+
const selected = query()
|
|
111
|
+
.leftJoin({ colleague: "users" }, (join) => join.on("users.id", "colleague.id"))
|
|
112
|
+
.select({ colleague: { id: "colleague.id", nickname: "colleague.nickname" } })
|
|
113
|
+
.groupBy("colleague.id", "colleague.nickname");
|
|
114
|
+
result(selected.rawQuery(), [
|
|
115
|
+
{ colleague__id: 1, colleague__nickname: null },
|
|
116
|
+
{ colleague__id: null, colleague__nickname: null },
|
|
122
117
|
]);
|
|
118
|
+
expect(await selected).toEqual([{ colleague: { id: 1, nickname: null } }, { colleague: null }]);
|
|
123
119
|
});
|
|
124
120
|
|
|
125
|
-
it("
|
|
121
|
+
it("여러 별칭이 섞인 그룹은 id 하나로 통과하고 그 id로만 판정한다", async () => {
|
|
126
122
|
const selected = query()
|
|
123
|
+
.join({ staffs: "users" }, "users.id", "staffs.id")
|
|
127
124
|
.leftJoin({ colleague: "users" }, (join) => join.on("users.id", "colleague.id"))
|
|
128
|
-
.select({
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
.select({ manager: { id: "staffs.id", name: "colleague.name" } });
|
|
126
|
+
result(selected.rawQuery(), [{ manager__id: 1, manager__name: null }]);
|
|
127
|
+
expect(await selected).toEqual([{ manager: { id: 1, name: null } }]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("raw 식만 든 그룹은 LEFT JOIN 별칭과 같아도 검사하지 않고 객체로 둔다", async () => {
|
|
131
|
+
const selected = query()
|
|
132
|
+
.leftJoin({ colleague: "users" }, (join) => join.on("users.id", "colleague.id"))
|
|
133
|
+
.select({ colleague: { total: Puri.rawNumber("COUNT(colleague.id)") } });
|
|
134
|
+
result(selected.rawQuery(), [{ colleague__total: null }]);
|
|
135
|
+
expect(await selected).toEqual([{ colleague: { total: null } }]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("서브쿼리를 LEFT JOIN한 그룹도 id가 없으면 오류를 내고 id가 있으면 id로 판정한다", async () => {
|
|
139
|
+
const sub = query().select({ id: "users.id", name: "users.name" });
|
|
140
|
+
const missing = query()
|
|
141
|
+
.leftJoin({ sq: sub }, "users.id", "sq.id")
|
|
142
|
+
.select({ sq: { name: "sq.name" } });
|
|
143
|
+
result(missing.rawQuery(), []);
|
|
144
|
+
await expect(missing).rejects.toThrow(/needs an "id" field because it reads LEFT JOIN "sq"/);
|
|
145
|
+
const selected = query()
|
|
146
|
+
.leftJoin({ sq: sub }, "users.id", "sq.id")
|
|
147
|
+
.select({ sq: { id: "sq.id", name: "sq.name" } });
|
|
148
|
+
result(selected.rawQuery(), [
|
|
149
|
+
{ sq__id: 1, sq__name: null },
|
|
150
|
+
{ sq__id: null, sq__name: null },
|
|
151
|
+
]);
|
|
152
|
+
expect(await selected).toEqual([{ sq: { id: 1, name: null } }, { sq: null }]);
|
|
153
|
+
const flat = query().leftJoin({ sq: sub }, "users.id", "sq.id").select({ sq__name: "sq.name" });
|
|
154
|
+
result(flat.rawQuery(), [{ sq__name: null }]);
|
|
155
|
+
expect(await flat).toEqual([{ sq__name: null }]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("Puri가 모르게 joinRaw로 붙인 별칭은 id가 있으면 id로 판정한다", async () => {
|
|
159
|
+
// medpath의 마지막 메시지처럼 rawQuery().joinRaw("LEFT JOIN LATERAL …")로 붙인 별칭입니다.
|
|
160
|
+
const selected = query().select({
|
|
161
|
+
// @ts-expect-error joinRaw로 붙인 별칭은 Puri 타입이 모릅니다.
|
|
162
|
+
last_message: { id: "last_message.id", body: "last_message.body" },
|
|
163
|
+
});
|
|
164
|
+
selected
|
|
165
|
+
.rawQuery()
|
|
166
|
+
.joinRaw("LEFT JOIN LATERAL (SELECT 1 AS id, '' AS body) AS last_message ON TRUE");
|
|
167
|
+
result(selected.rawQuery(), [
|
|
168
|
+
{ last_message__id: 1, last_message__body: null },
|
|
169
|
+
{ last_message__id: null, last_message__body: null },
|
|
170
|
+
]);
|
|
171
|
+
expect(await selected).toEqual([
|
|
172
|
+
{ last_message: { id: 1, body: null } },
|
|
173
|
+
{ last_message: null },
|
|
174
|
+
]);
|
|
132
175
|
});
|
|
133
176
|
|
|
134
177
|
it.each([false, true])(
|
|
@@ -138,18 +181,12 @@ describe("Puri 중첩 select 결과", () => {
|
|
|
138
181
|
.leftJoin({ staffs: "users" }, (join) => join.on("users.id", "staffs.id"))
|
|
139
182
|
.select({ manager: { id: "staffs.id", name: "staffs.name" } });
|
|
140
183
|
if (raw) selected.appendSelect({ total: Puri.rawNumber("(SELECT COUNT(*) FROM users)") });
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
{ manager__id: null, manager__name: null },
|
|
146
|
-
],
|
|
147
|
-
{ staffs: [true, false] },
|
|
148
|
-
);
|
|
149
|
-
expect(await selected.clone()).toEqual([
|
|
150
|
-
{ manager: { id: 1, name: "담당자" } },
|
|
151
|
-
{ manager: null },
|
|
184
|
+
const cloned = selected.clone();
|
|
185
|
+
result(cloned.rawQuery(), [
|
|
186
|
+
{ manager__id: 1, manager__name: "담당자" },
|
|
187
|
+
{ manager__id: null, manager__name: null },
|
|
152
188
|
]);
|
|
189
|
+
expect(await cloned).toEqual([{ manager: { id: 1, name: "담당자" } }, { manager: null }]);
|
|
153
190
|
},
|
|
154
191
|
);
|
|
155
192
|
|
|
@@ -9,7 +9,6 @@ import { EntityManager } from "../entity/entity-manager";
|
|
|
9
9
|
import { type FilterOperator, type FilterQuery } from "../filter/types";
|
|
10
10
|
import { normalizeFilterQuery, validateSonamuFilters } from "../filter/utils";
|
|
11
11
|
import { convertDomainToCategory } from "../logger/category";
|
|
12
|
-
import { Naite } from "../naite/naite";
|
|
13
12
|
import { type DatabaseSchemaExtend, type SonamuQueryMode } from "../types/types";
|
|
14
13
|
import { type ListResult } from "../utils/model";
|
|
15
14
|
import { isObjectValue } from "../utils/runtime-value";
|
|
@@ -48,6 +47,30 @@ type DatabaseRecord = Record<string, DatabaseRecordValue | undefined>;
|
|
|
48
47
|
* @template TSubsetQueries - 서브셋 쿼리 함수 객체
|
|
49
48
|
* @template TLoaderQueries - 서브셋별 로더 쿼리 배열 객체
|
|
50
49
|
*/
|
|
50
|
+
// 로더 정의의 refId(부모 줄에서 꺼낼 값)와 as(결과를 넣을 자리)는 생성 코드에서
|
|
51
|
+
// "content__ref"처럼 __로 이은 문자열입니다. 본 쿼리와 로더 쿼리는 Puri가 이미 접은
|
|
52
|
+
// 객체를 돌려주므로, 여기서 __를 객체 경계로 보고 그 객체 속 위치로 번역해 읽고 씁니다.
|
|
53
|
+
// 중간 관계가 null이면(관계가 비었으면) 읽기는 null, 쓰기는 건너뜁니다.
|
|
54
|
+
function readLoaderPath(row: any, path: string): any {
|
|
55
|
+
let value = row;
|
|
56
|
+
for (const key of path.split("__")) {
|
|
57
|
+
if (!isObjectValue(value) || Array.isArray(value)) return null;
|
|
58
|
+
value = value[key];
|
|
59
|
+
}
|
|
60
|
+
return value ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeLoaderPath(row: any, path: string, value: any[]): void {
|
|
64
|
+
const keys = path.split("__");
|
|
65
|
+
const last = keys.pop();
|
|
66
|
+
let parent = row;
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
if (!isObjectValue(parent) || Array.isArray(parent)) return;
|
|
69
|
+
parent = parent[key];
|
|
70
|
+
}
|
|
71
|
+
if (last !== undefined && isObjectValue(parent) && !Array.isArray(parent)) parent[last] = value;
|
|
72
|
+
}
|
|
73
|
+
|
|
51
74
|
export class BaseModelClass<
|
|
52
75
|
TSubsetKey extends string = never,
|
|
53
76
|
TSubsetMapping extends { [K in TSubsetKey]: TSubsetMapping[K] } = never,
|
|
@@ -263,7 +286,10 @@ export class BaseModelClass<
|
|
|
263
286
|
|
|
264
287
|
// Internal 필드 제거
|
|
265
288
|
const entity = EntityManager.get(this.modelName);
|
|
266
|
-
const internalFields =
|
|
289
|
+
const internalFields = [
|
|
290
|
+
...(entity.subsetsInternal[subset] ?? []),
|
|
291
|
+
...entity.getSubsetAutoInternalFields(subset),
|
|
292
|
+
];
|
|
267
293
|
const rows =
|
|
268
294
|
internalFields.length > 0
|
|
269
295
|
? enhancedRows.map((row) => this.omitInternalFields(row, internalFields))
|
|
@@ -518,9 +544,7 @@ export class BaseModelClass<
|
|
|
518
544
|
return qb.limit(num).offset(num * (page - 1));
|
|
519
545
|
}
|
|
520
546
|
})();
|
|
521
|
-
|
|
522
|
-
Naite.t("puri:executed-query", limitedQb.toQuery());
|
|
523
|
-
let unloadedRows = await limitedQb.rawQuery();
|
|
547
|
+
let rows: any[] = await limitedQb;
|
|
524
548
|
|
|
525
549
|
if (debug) {
|
|
526
550
|
qb.debug();
|
|
@@ -530,10 +554,10 @@ export class BaseModelClass<
|
|
|
530
554
|
// SAFETY: 쿼리 빌더의 제네릭 계약과 선행 검증이 이 타입을 보장합니다.
|
|
531
555
|
const loaders = (this.loaderQueries as any)[subset];
|
|
532
556
|
if (loaders && Array.isArray(loaders)) {
|
|
533
|
-
|
|
557
|
+
rows = await this.processLoaders(rows, loaders, debug, puriWrapper);
|
|
534
558
|
}
|
|
535
559
|
|
|
536
|
-
return
|
|
560
|
+
return rows;
|
|
537
561
|
}
|
|
538
562
|
|
|
539
563
|
/**
|
|
@@ -550,16 +574,14 @@ export class BaseModelClass<
|
|
|
550
574
|
|
|
551
575
|
const resolveLoaderQb = resolveLoaderQbFn(
|
|
552
576
|
puriWrapper,
|
|
553
|
-
rows.map((row) => row
|
|
577
|
+
rows.map((row) => readLoaderPath(row, refId)).filter((id) => id !== null),
|
|
554
578
|
);
|
|
555
579
|
|
|
556
580
|
if (debug) {
|
|
557
581
|
resolveLoaderQb.debug();
|
|
558
582
|
}
|
|
559
583
|
|
|
560
|
-
|
|
561
|
-
// SAFETY: 로더의 select 쿼리는 평평한 레코드 배열을 반환합니다.
|
|
562
|
-
let loadedRows = (await resolveLoaderQb.rawQuery()) as any[];
|
|
584
|
+
let loadedRows: any[] = await resolveLoaderQb;
|
|
563
585
|
|
|
564
586
|
// 중첩 loaders가 있으면 재귀 처리
|
|
565
587
|
if (nestedLoaders && nestedLoaders.length > 0) {
|
|
@@ -569,7 +591,12 @@ export class BaseModelClass<
|
|
|
569
591
|
const subRowGroups = group(loadedRows, (row) => row.refId);
|
|
570
592
|
|
|
571
593
|
rows = rows.map((row) => {
|
|
572
|
-
|
|
594
|
+
const refValue = readLoaderPath(row, refId);
|
|
595
|
+
writeLoaderPath(
|
|
596
|
+
row,
|
|
597
|
+
as,
|
|
598
|
+
(refValue === null ? [] : (subRowGroups[refValue] ?? [])).map((r) => omit(r, ["refId"])),
|
|
599
|
+
);
|
|
573
600
|
return row;
|
|
574
601
|
});
|
|
575
602
|
}
|
|
@@ -582,34 +609,45 @@ export class BaseModelClass<
|
|
|
582
609
|
*
|
|
583
610
|
* - `user__name` → `{ user: { name } }`
|
|
584
611
|
* - nullable relation의 경우 id 필드가 null이면 객체 자체를 null로
|
|
612
|
+
*
|
|
613
|
+
* @deprecated 키 이름의 `__`와 id·칸 값으로 관계가 있는지 추측해 접습니다. 그래서 id 없는
|
|
614
|
+
* 중첩 그룹은 비어 있어도 null이 아니라 칸이 전부 null인 객체가 되고, jsonb 값이나 raw
|
|
615
|
+
* 별칭에 든 `__`도 접어 버립니다. Puri 쿼리를 직접 `await`하면 select에 적은 중첩 모양
|
|
616
|
+
* 그대로 접힌 결과를 받습니다.
|
|
585
617
|
*/
|
|
586
618
|
hydrate<T extends object>(rows: T[]): T[] {
|
|
619
|
+
const toObjPath = (key: string) => {
|
|
620
|
+
const [head, ...rest] = key.split("__");
|
|
621
|
+
return head + rest.map((part) => `[${part}]`).join("");
|
|
622
|
+
};
|
|
587
623
|
// SAFETY: 쿼리 빌더의 제네릭 계약과 선행 검증이 이 타입을 보장합니다.
|
|
588
624
|
return rows.map((row: T) => {
|
|
589
625
|
const rowValues = new Map(Object.entries(row));
|
|
590
|
-
// nullable relation 처리: 그룹의 id 필드가 null이면 객체 전체를 null로
|
|
591
626
|
const nestedKeys = Object.keys(row).filter((key) => key.includes("__"));
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
627
|
+
// 중첩 관계마다 접두사를 모읍니다 (예: shop__country__id → shop, shop__country)
|
|
628
|
+
const prefixes = new Set(
|
|
629
|
+
nestedKeys.flatMap((key) => {
|
|
630
|
+
const parts = key.split("__");
|
|
631
|
+
return parts.slice(1).map((_, i) => parts.slice(0, i + 1).join("__"));
|
|
632
|
+
}),
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
// 관계의 id 필드가 null이면 그 관계 객체 전체를 null로 (예: shop__country__id → shop.country)
|
|
636
|
+
const nullKeys = [...prefixes].filter((prefix) => {
|
|
637
|
+
const idField = `${prefix}__id`;
|
|
638
|
+
if (idField in row) {
|
|
639
|
+
return rowValues.get(idField) === null;
|
|
640
|
+
}
|
|
605
641
|
|
|
606
|
-
|
|
607
|
-
|
|
642
|
+
// 첫 단계 관계에 id 필드가 없으면 기존 로직: 모든 필드가 null인지 확인
|
|
643
|
+
if (prefix.includes("__")) return false;
|
|
644
|
+
return nestedKeys
|
|
645
|
+
.filter((key) => key.startsWith(`${prefix}__`))
|
|
646
|
+
.every((field) => {
|
|
608
647
|
const fieldValue = rowValues.get(field);
|
|
609
648
|
return fieldValue === null || (Array.isArray(fieldValue) && fieldValue.length === 0);
|
|
610
649
|
});
|
|
611
|
-
|
|
612
|
-
.map(([key]) => key);
|
|
650
|
+
});
|
|
613
651
|
|
|
614
652
|
let hydrated: DatabaseRecord = Object.fromEntries([]);
|
|
615
653
|
for (const field of Object.keys(row)) {
|
|
@@ -624,27 +662,21 @@ export class BaseModelClass<
|
|
|
624
662
|
}
|
|
625
663
|
|
|
626
664
|
// 중첩 필드 처리: user__name → user[name]
|
|
627
|
-
const parts = field.split("__");
|
|
628
|
-
const objPath =
|
|
629
|
-
parts[0] +
|
|
630
|
-
parts
|
|
631
|
-
.slice(1)
|
|
632
|
-
.map((part) => `[${part}]`)
|
|
633
|
-
.join("");
|
|
634
|
-
|
|
635
665
|
hydrated = set(
|
|
636
666
|
hydrated,
|
|
637
|
-
|
|
667
|
+
toObjPath(field),
|
|
638
668
|
fieldValue && Array.isArray(fieldValue) && isObject(fieldValue[0])
|
|
639
669
|
? this.hydrate(fieldValue)
|
|
640
670
|
: fieldValue,
|
|
641
671
|
);
|
|
642
672
|
}
|
|
643
673
|
|
|
644
|
-
// null relation
|
|
645
|
-
nullKeys
|
|
646
|
-
|
|
647
|
-
|
|
674
|
+
// null relation 처리: 안쪽부터 적용해야 바깥 null이 안쪽 경로에 덮여 객체로 되살아나지 않습니다.
|
|
675
|
+
nullKeys
|
|
676
|
+
.sort((a, b) => b.split("__").length - a.split("__").length)
|
|
677
|
+
.forEach((nullKey) => {
|
|
678
|
+
hydrated = set(hydrated, toObjPath(nullKey), null);
|
|
679
|
+
});
|
|
648
680
|
|
|
649
681
|
return hydrated;
|
|
650
682
|
}) as T[];
|