metal-orm 1.0.98 → 1.0.100
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/README.md +724 -720
- package/dist/index.cjs +45 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -3
- package/dist/index.d.ts +26 -3
- package/dist/index.js +45 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/dialect/base/sql-dialect.ts +6 -2
- package/src/core/dialect/postgres/index.ts +60 -52
- package/src/orm/orm-session.ts +609 -562
- package/src/orm/save-graph-types.ts +56 -49
- package/src/orm/save-graph.ts +431 -405
- package/src/query-builder/select.ts +4 -4
package/src/orm/save-graph.ts
CHANGED
|
@@ -1,405 +1,431 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
EntityInstance,
|
|
3
|
-
HasManyCollection,
|
|
4
|
-
HasOneReference,
|
|
5
|
-
BelongsToReference,
|
|
6
|
-
ManyToManyCollection
|
|
7
|
-
} from '../schema/types.js';
|
|
8
|
-
import { normalizeColumnType, type ColumnDef } from '../schema/column-types.js';
|
|
9
|
-
import {
|
|
10
|
-
RelationKinds,
|
|
11
|
-
type BelongsToManyRelation,
|
|
12
|
-
type BelongsToRelation,
|
|
13
|
-
type HasManyRelation,
|
|
14
|
-
type HasOneRelation,
|
|
15
|
-
type RelationDef
|
|
16
|
-
} from '../schema/relation.js';
|
|
17
|
-
import type { TableDef } from '../schema/table.js';
|
|
18
|
-
import { findPrimaryKey } from '../query-builder/hydration-planner.js';
|
|
19
|
-
import { createEntityFromRow } from './entity.js';
|
|
20
|
-
import type { EntityConstructor } from './entity-metadata.js';
|
|
21
|
-
import { getTableDefFromEntity } from '../decorators/bootstrap.js';
|
|
22
|
-
import type { OrmSession } from './orm-session.js';
|
|
23
|
-
import type { PrimaryKey } from './entity-context.js';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Options for controlling the behavior of save graph operations.
|
|
27
|
-
*/
|
|
28
|
-
export interface SaveGraphOptions {
|
|
29
|
-
/** Remove existing collection members that are not present in the payload */
|
|
30
|
-
pruneMissing?: boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Coerce JSON-friendly input values into DB-friendly primitives.
|
|
33
|
-
* Currently:
|
|
34
|
-
* - `json`: Date -> ISO string (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
35
|
-
* - `json-in`: string/number -> Date (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
36
|
-
*/
|
|
37
|
-
coerce?: 'json' | 'json-in';
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Represents an entity object with arbitrary properties. */
|
|
41
|
-
type AnyEntity = Record<string, unknown>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
|
|
45
|
-
* Converts a value to a string key, returning an empty string for null or undefined.
|
|
46
|
-
|
|
47
|
-
* @param value - The value to convert.
|
|
48
|
-
|
|
49
|
-
* @returns The string representation or empty string.
|
|
50
|
-
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
54
|
-
|
|
55
|
-
const coerceColumnValue = (
|
|
56
|
-
table: TableDef,
|
|
57
|
-
columnName: string,
|
|
58
|
-
value: unknown,
|
|
59
|
-
options: SaveGraphOptions
|
|
60
|
-
): unknown => {
|
|
61
|
-
if (value === null || value === undefined) return value;
|
|
62
|
-
|
|
63
|
-
const column = table.columns[columnName] as unknown as ColumnDef | undefined;
|
|
64
|
-
if (!column) return value;
|
|
65
|
-
|
|
66
|
-
const normalized = normalizeColumnType(column.type);
|
|
67
|
-
|
|
68
|
-
const isDateLikeColumn =
|
|
69
|
-
normalized === 'date' ||
|
|
70
|
-
normalized === 'datetime' ||
|
|
71
|
-
normalized === 'timestamp' ||
|
|
72
|
-
normalized === 'timestamptz';
|
|
73
|
-
|
|
74
|
-
if (!isDateLikeColumn) return value;
|
|
75
|
-
|
|
76
|
-
if (options.coerce === 'json') {
|
|
77
|
-
if (value instanceof Date) {
|
|
78
|
-
return value.toISOString();
|
|
79
|
-
}
|
|
80
|
-
return value;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (options.coerce === 'json-in') {
|
|
84
|
-
if (value instanceof Date) return value;
|
|
85
|
-
if (typeof value === 'string' || typeof value === 'number') {
|
|
86
|
-
const date = new Date(value);
|
|
87
|
-
if (Number.isNaN(date.getTime())) {
|
|
88
|
-
throw new Error(`Invalid date value for column "${columnName}"`);
|
|
89
|
-
}
|
|
90
|
-
return date;
|
|
91
|
-
}
|
|
92
|
-
return value;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Future coercions can be added here based on `normalized`.
|
|
96
|
-
return value;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const pickColumns = (table: TableDef, payload: AnyEntity, options: SaveGraphOptions): Record<string, unknown> => {
|
|
100
|
-
const columns: Record<string, unknown> = {};
|
|
101
|
-
for (const key of Object.keys(table.columns)) {
|
|
102
|
-
if (payload[key] !== undefined) {
|
|
103
|
-
columns[key] = coerceColumnValue(table, key, payload[key], options);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return columns;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const ensureEntity = <TTable extends TableDef>(
|
|
110
|
-
session: OrmSession,
|
|
111
|
-
table: TTable,
|
|
112
|
-
payload: AnyEntity,
|
|
113
|
-
options: SaveGraphOptions
|
|
114
|
-
): EntityInstance<TTable> => {
|
|
115
|
-
const pk = findPrimaryKey(table);
|
|
116
|
-
const row = pickColumns(table, payload, options);
|
|
117
|
-
const pkValue = payload[pk];
|
|
118
|
-
|
|
119
|
-
if (pkValue !== undefined && pkValue !== null) {
|
|
120
|
-
const tracked = session.getEntity(table, pkValue as PrimaryKey);
|
|
121
|
-
if (tracked) {
|
|
122
|
-
return tracked as EntityInstance<TTable>;
|
|
123
|
-
}
|
|
124
|
-
// Seed the stub with PK to track a managed entity when updating.
|
|
125
|
-
if (row[pk] === undefined) {
|
|
126
|
-
row[pk] = pkValue;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return createEntityFromRow(session, table, row) as EntityInstance<TTable>;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const assignColumns = (table: TableDef, entity: AnyEntity, payload: AnyEntity, options: SaveGraphOptions): void => {
|
|
134
|
-
for (const key of Object.keys(table.columns)) {
|
|
135
|
-
if (payload[key] !== undefined) {
|
|
136
|
-
entity[key] = coerceColumnValue(table, key, payload[key], options);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const isEntityInCollection = (items: AnyEntity[], pkName: string, entity: AnyEntity): boolean => {
|
|
142
|
-
if (items.includes(entity)) return true;
|
|
143
|
-
const entityPk = entity[pkName];
|
|
144
|
-
if (entityPk === undefined || entityPk === null) return false;
|
|
145
|
-
return items.some(item => toKey(item[pkName]) === toKey(entityPk));
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const findInCollectionByPk = (items: AnyEntity[], pkName: string, pkValue: unknown): AnyEntity | undefined => {
|
|
149
|
-
if (pkValue === undefined || pkValue === null) return undefined;
|
|
150
|
-
return items.find(item => toKey(item[pkName]) === toKey(pkValue));
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const handleHasMany = async (
|
|
154
|
-
session: OrmSession,
|
|
155
|
-
root: AnyEntity,
|
|
156
|
-
relationName: string,
|
|
157
|
-
relation: HasManyRelation,
|
|
158
|
-
payload: unknown,
|
|
159
|
-
options: SaveGraphOptions
|
|
160
|
-
): Promise<void> => {
|
|
161
|
-
if (!Array.isArray(payload)) return;
|
|
162
|
-
const collection = root[relationName] as unknown as HasManyCollection<unknown>;
|
|
163
|
-
await collection.load();
|
|
164
|
-
|
|
165
|
-
const targetTable = relation.target;
|
|
166
|
-
const targetPk = findPrimaryKey(targetTable);
|
|
167
|
-
const existing = collection.getItems() as unknown as AnyEntity[];
|
|
168
|
-
const seen = new Set<string>();
|
|
169
|
-
|
|
170
|
-
for (const item of payload) {
|
|
171
|
-
if (item === null || item === undefined) continue;
|
|
172
|
-
const asObj = typeof item === 'object' ? (item as AnyEntity) : { [targetPk]: item };
|
|
173
|
-
const pkValue = asObj[targetPk];
|
|
174
|
-
|
|
175
|
-
const current =
|
|
176
|
-
findInCollectionByPk(existing, targetPk, pkValue) ??
|
|
177
|
-
(pkValue !== undefined && pkValue !== null ? session.getEntity(targetTable, pkValue as PrimaryKey) : undefined);
|
|
178
|
-
|
|
179
|
-
const entity = current ?? ensureEntity(session, targetTable, asObj, options);
|
|
180
|
-
assignColumns(targetTable, entity as AnyEntity, asObj, options);
|
|
181
|
-
await applyGraphToEntity(session, targetTable, entity as AnyEntity, asObj, options);
|
|
182
|
-
|
|
183
|
-
if (!isEntityInCollection(collection.getItems() as unknown as AnyEntity[], targetPk, entity as unknown as AnyEntity)) {
|
|
184
|
-
collection.attach(entity);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (pkValue !== undefined && pkValue !== null) {
|
|
188
|
-
seen.add(toKey(pkValue));
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (options.pruneMissing) {
|
|
193
|
-
for (const item of [...collection.getItems()]) {
|
|
194
|
-
const pkValue = item[targetPk];
|
|
195
|
-
if (pkValue !== undefined && pkValue !== null && !seen.has(toKey(pkValue))) {
|
|
196
|
-
collection.remove(item);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
const handleHasOne = async (
|
|
203
|
-
session: OrmSession,
|
|
204
|
-
root: AnyEntity,
|
|
205
|
-
relationName: string,
|
|
206
|
-
relation: HasOneRelation,
|
|
207
|
-
payload: unknown,
|
|
208
|
-
options: SaveGraphOptions
|
|
209
|
-
): Promise<void> => {
|
|
210
|
-
const ref = root[relationName] as unknown as HasOneReference<object>;
|
|
211
|
-
if (payload === undefined) return;
|
|
212
|
-
if (payload === null) {
|
|
213
|
-
ref.set(null);
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
const pk = findPrimaryKey(relation.target);
|
|
217
|
-
if (typeof payload === 'number' || typeof payload === 'string') {
|
|
218
|
-
const entity = ref.set({ [pk]: payload });
|
|
219
|
-
if (entity) {
|
|
220
|
-
await applyGraphToEntity(session, relation.target, entity as AnyEntity, { [pk]: payload as PrimaryKey }, options);
|
|
221
|
-
}
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
const attached = ref.set(payload as AnyEntity);
|
|
225
|
-
if (attached) {
|
|
226
|
-
await applyGraphToEntity(session, relation.target, attached as AnyEntity, payload as AnyEntity, options);
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
const handleBelongsTo = async (
|
|
231
|
-
session: OrmSession,
|
|
232
|
-
root: AnyEntity,
|
|
233
|
-
relationName: string,
|
|
234
|
-
relation: BelongsToRelation,
|
|
235
|
-
payload: unknown,
|
|
236
|
-
options: SaveGraphOptions
|
|
237
|
-
): Promise<void> => {
|
|
238
|
-
const ref = root[relationName] as unknown as BelongsToReference<object>;
|
|
239
|
-
if (payload === undefined) return;
|
|
240
|
-
if (payload === null) {
|
|
241
|
-
ref.set(null);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
const pk = relation.localKey || findPrimaryKey(relation.target);
|
|
245
|
-
if (typeof payload === 'number' || typeof payload === 'string') {
|
|
246
|
-
const entity = ref.set({ [pk]: payload });
|
|
247
|
-
if (entity) {
|
|
248
|
-
await applyGraphToEntity(session, relation.target, entity as AnyEntity, { [pk]: payload as PrimaryKey }, options);
|
|
249
|
-
}
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
const attached = ref.set(payload as AnyEntity);
|
|
253
|
-
if (attached) {
|
|
254
|
-
await applyGraphToEntity(session, relation.target, attached as AnyEntity, payload as AnyEntity, options);
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
const handleBelongsToMany = async (
|
|
259
|
-
session: OrmSession,
|
|
260
|
-
root: AnyEntity,
|
|
261
|
-
relationName: string,
|
|
262
|
-
relation: BelongsToManyRelation,
|
|
263
|
-
payload: unknown,
|
|
264
|
-
options: SaveGraphOptions
|
|
265
|
-
): Promise<void> => {
|
|
266
|
-
if (!Array.isArray(payload)) return;
|
|
267
|
-
const collection = root[relationName] as unknown as ManyToManyCollection<unknown>;
|
|
268
|
-
await collection.load();
|
|
269
|
-
|
|
270
|
-
const targetTable = relation.target;
|
|
271
|
-
const targetPk = relation.targetKey || findPrimaryKey(targetTable);
|
|
272
|
-
const seen = new Set<string>();
|
|
273
|
-
|
|
274
|
-
for (const item of payload) {
|
|
275
|
-
if (item === null || item === undefined) continue;
|
|
276
|
-
if (typeof item === 'number' || typeof item === 'string') {
|
|
277
|
-
const id = item;
|
|
278
|
-
collection.attach(id);
|
|
279
|
-
seen.add(toKey(id));
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const asObj = item as AnyEntity;
|
|
284
|
-
const pkValue = asObj[targetPk];
|
|
285
|
-
const entity = pkValue !== undefined && pkValue !== null
|
|
286
|
-
? session.getEntity(targetTable, pkValue as PrimaryKey) ?? ensureEntity(session, targetTable, asObj, options)
|
|
287
|
-
: ensureEntity(session, targetTable, asObj, options);
|
|
288
|
-
|
|
289
|
-
assignColumns(targetTable, entity as AnyEntity, asObj, options);
|
|
290
|
-
await applyGraphToEntity(session, targetTable, entity as AnyEntity, asObj, options);
|
|
291
|
-
|
|
292
|
-
if (!isEntityInCollection(collection.getItems() as unknown as AnyEntity[], targetPk, entity as unknown as AnyEntity)) {
|
|
293
|
-
collection.attach(entity);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (pkValue !== undefined && pkValue !== null) {
|
|
297
|
-
seen.add(toKey(pkValue));
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (options.pruneMissing) {
|
|
302
|
-
for (const item of [...collection.getItems()] as unknown as AnyEntity[]) {
|
|
303
|
-
const pkValue = item[targetPk];
|
|
304
|
-
if (pkValue !== undefined && pkValue !== null && !seen.has(toKey(pkValue))) {
|
|
305
|
-
collection.detach(item);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
const applyRelation = async (
|
|
312
|
-
session: OrmSession,
|
|
313
|
-
table: TableDef,
|
|
314
|
-
entity: AnyEntity,
|
|
315
|
-
relationName: string,
|
|
316
|
-
relation: RelationDef,
|
|
317
|
-
payload: unknown,
|
|
318
|
-
options: SaveGraphOptions
|
|
319
|
-
): Promise<void> => {
|
|
320
|
-
switch (relation.type) {
|
|
321
|
-
case RelationKinds.HasMany:
|
|
322
|
-
return handleHasMany(session, entity, relationName, relation, payload, options);
|
|
323
|
-
case RelationKinds.HasOne:
|
|
324
|
-
return handleHasOne(session, entity, relationName, relation, payload, options);
|
|
325
|
-
case RelationKinds.BelongsTo:
|
|
326
|
-
return handleBelongsTo(session, entity, relationName, relation, payload, options);
|
|
327
|
-
case RelationKinds.BelongsToMany:
|
|
328
|
-
return handleBelongsToMany(session, entity, relationName, relation, payload, options);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
const applyGraphToEntity = async (
|
|
333
|
-
session: OrmSession,
|
|
334
|
-
table: TableDef,
|
|
335
|
-
entity: AnyEntity,
|
|
336
|
-
payload: AnyEntity,
|
|
337
|
-
options: SaveGraphOptions
|
|
338
|
-
): Promise<void> => {
|
|
339
|
-
assignColumns(table, entity, payload, options);
|
|
340
|
-
|
|
341
|
-
for (const [relationName, relation] of Object.entries(table.relations)) {
|
|
342
|
-
if (!(relationName in payload)) continue;
|
|
343
|
-
await applyRelation(session, table, entity, relationName, relation as RelationDef, payload[relationName], options);
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
export const saveGraph = async <TTable extends TableDef>(
|
|
348
|
-
session: OrmSession,
|
|
349
|
-
entityClass: EntityConstructor,
|
|
350
|
-
payload: AnyEntity,
|
|
351
|
-
options: SaveGraphOptions = {}
|
|
352
|
-
): Promise<EntityInstance<TTable>> => {
|
|
353
|
-
const table = getTableDefFromEntity(entityClass);
|
|
354
|
-
if (!table) {
|
|
355
|
-
throw new Error('Entity metadata has not been bootstrapped');
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const root = ensureEntity<TTable>(session, table as TTable, payload, options);
|
|
359
|
-
await applyGraphToEntity(session, table, root as AnyEntity, payload, options);
|
|
360
|
-
return root;
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
|
|
365
|
-
* Internal version of saveGraph with typed return based on the constructor.
|
|
366
|
-
|
|
367
|
-
* @param session - The ORM session.
|
|
368
|
-
|
|
369
|
-
* @param entityClass - The entity constructor.
|
|
370
|
-
|
|
371
|
-
* @param payload - The payload data for the root entity and its relations.
|
|
372
|
-
|
|
373
|
-
* @param options - Options for the save operation.
|
|
374
|
-
|
|
375
|
-
* @returns The root entity instance.
|
|
376
|
-
|
|
377
|
-
*/
|
|
378
|
-
|
|
379
|
-
export const saveGraphInternal = async <TCtor extends EntityConstructor>(
|
|
380
|
-
|
|
381
|
-
session: OrmSession,
|
|
382
|
-
|
|
383
|
-
entityClass: TCtor,
|
|
384
|
-
|
|
385
|
-
payload: AnyEntity,
|
|
386
|
-
|
|
387
|
-
options: SaveGraphOptions = {}
|
|
388
|
-
|
|
389
|
-
): Promise<InstanceType<TCtor>> => {
|
|
390
|
-
|
|
391
|
-
const table = getTableDefFromEntity(entityClass);
|
|
392
|
-
|
|
393
|
-
if (!table) {
|
|
394
|
-
|
|
395
|
-
throw new Error('Entity metadata has not been bootstrapped');
|
|
396
|
-
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
const root = ensureEntity(session, table, payload, options);
|
|
400
|
-
|
|
401
|
-
await applyGraphToEntity(session, table, root as AnyEntity, payload, options);
|
|
402
|
-
|
|
403
|
-
return root as unknown as InstanceType<TCtor>;
|
|
404
|
-
|
|
405
|
-
};
|
|
1
|
+
import type {
|
|
2
|
+
EntityInstance,
|
|
3
|
+
HasManyCollection,
|
|
4
|
+
HasOneReference,
|
|
5
|
+
BelongsToReference,
|
|
6
|
+
ManyToManyCollection
|
|
7
|
+
} from '../schema/types.js';
|
|
8
|
+
import { normalizeColumnType, type ColumnDef } from '../schema/column-types.js';
|
|
9
|
+
import {
|
|
10
|
+
RelationKinds,
|
|
11
|
+
type BelongsToManyRelation,
|
|
12
|
+
type BelongsToRelation,
|
|
13
|
+
type HasManyRelation,
|
|
14
|
+
type HasOneRelation,
|
|
15
|
+
type RelationDef
|
|
16
|
+
} from '../schema/relation.js';
|
|
17
|
+
import type { TableDef } from '../schema/table.js';
|
|
18
|
+
import { findPrimaryKey } from '../query-builder/hydration-planner.js';
|
|
19
|
+
import { createEntityFromRow } from './entity.js';
|
|
20
|
+
import type { EntityConstructor } from './entity-metadata.js';
|
|
21
|
+
import { getTableDefFromEntity } from '../decorators/bootstrap.js';
|
|
22
|
+
import type { OrmSession } from './orm-session.js';
|
|
23
|
+
import type { PrimaryKey } from './entity-context.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for controlling the behavior of save graph operations.
|
|
27
|
+
*/
|
|
28
|
+
export interface SaveGraphOptions {
|
|
29
|
+
/** Remove existing collection members that are not present in the payload */
|
|
30
|
+
pruneMissing?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Coerce JSON-friendly input values into DB-friendly primitives.
|
|
33
|
+
* Currently:
|
|
34
|
+
* - `json`: Date -> ISO string (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
35
|
+
* - `json-in`: string/number -> Date (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
36
|
+
*/
|
|
37
|
+
coerce?: 'json' | 'json-in';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Represents an entity object with arbitrary properties. */
|
|
41
|
+
type AnyEntity = Record<string, unknown>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
|
|
45
|
+
* Converts a value to a string key, returning an empty string for null or undefined.
|
|
46
|
+
|
|
47
|
+
* @param value - The value to convert.
|
|
48
|
+
|
|
49
|
+
* @returns The string representation or empty string.
|
|
50
|
+
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
54
|
+
|
|
55
|
+
const coerceColumnValue = (
|
|
56
|
+
table: TableDef,
|
|
57
|
+
columnName: string,
|
|
58
|
+
value: unknown,
|
|
59
|
+
options: SaveGraphOptions
|
|
60
|
+
): unknown => {
|
|
61
|
+
if (value === null || value === undefined) return value;
|
|
62
|
+
|
|
63
|
+
const column = table.columns[columnName] as unknown as ColumnDef | undefined;
|
|
64
|
+
if (!column) return value;
|
|
65
|
+
|
|
66
|
+
const normalized = normalizeColumnType(column.type);
|
|
67
|
+
|
|
68
|
+
const isDateLikeColumn =
|
|
69
|
+
normalized === 'date' ||
|
|
70
|
+
normalized === 'datetime' ||
|
|
71
|
+
normalized === 'timestamp' ||
|
|
72
|
+
normalized === 'timestamptz';
|
|
73
|
+
|
|
74
|
+
if (!isDateLikeColumn) return value;
|
|
75
|
+
|
|
76
|
+
if (options.coerce === 'json') {
|
|
77
|
+
if (value instanceof Date) {
|
|
78
|
+
return value.toISOString();
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (options.coerce === 'json-in') {
|
|
84
|
+
if (value instanceof Date) return value;
|
|
85
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
86
|
+
const date = new Date(value);
|
|
87
|
+
if (Number.isNaN(date.getTime())) {
|
|
88
|
+
throw new Error(`Invalid date value for column "${columnName}"`);
|
|
89
|
+
}
|
|
90
|
+
return date;
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Future coercions can be added here based on `normalized`.
|
|
96
|
+
return value;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const pickColumns = (table: TableDef, payload: AnyEntity, options: SaveGraphOptions): Record<string, unknown> => {
|
|
100
|
+
const columns: Record<string, unknown> = {};
|
|
101
|
+
for (const key of Object.keys(table.columns)) {
|
|
102
|
+
if (payload[key] !== undefined) {
|
|
103
|
+
columns[key] = coerceColumnValue(table, key, payload[key], options);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return columns;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const ensureEntity = <TTable extends TableDef>(
|
|
110
|
+
session: OrmSession,
|
|
111
|
+
table: TTable,
|
|
112
|
+
payload: AnyEntity,
|
|
113
|
+
options: SaveGraphOptions
|
|
114
|
+
): EntityInstance<TTable> => {
|
|
115
|
+
const pk = findPrimaryKey(table);
|
|
116
|
+
const row = pickColumns(table, payload, options);
|
|
117
|
+
const pkValue = payload[pk];
|
|
118
|
+
|
|
119
|
+
if (pkValue !== undefined && pkValue !== null) {
|
|
120
|
+
const tracked = session.getEntity(table, pkValue as PrimaryKey);
|
|
121
|
+
if (tracked) {
|
|
122
|
+
return tracked as EntityInstance<TTable>;
|
|
123
|
+
}
|
|
124
|
+
// Seed the stub with PK to track a managed entity when updating.
|
|
125
|
+
if (row[pk] === undefined) {
|
|
126
|
+
row[pk] = pkValue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return createEntityFromRow(session, table, row) as EntityInstance<TTable>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const assignColumns = (table: TableDef, entity: AnyEntity, payload: AnyEntity, options: SaveGraphOptions): void => {
|
|
134
|
+
for (const key of Object.keys(table.columns)) {
|
|
135
|
+
if (payload[key] !== undefined) {
|
|
136
|
+
entity[key] = coerceColumnValue(table, key, payload[key], options);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const isEntityInCollection = (items: AnyEntity[], pkName: string, entity: AnyEntity): boolean => {
|
|
142
|
+
if (items.includes(entity)) return true;
|
|
143
|
+
const entityPk = entity[pkName];
|
|
144
|
+
if (entityPk === undefined || entityPk === null) return false;
|
|
145
|
+
return items.some(item => toKey(item[pkName]) === toKey(entityPk));
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const findInCollectionByPk = (items: AnyEntity[], pkName: string, pkValue: unknown): AnyEntity | undefined => {
|
|
149
|
+
if (pkValue === undefined || pkValue === null) return undefined;
|
|
150
|
+
return items.find(item => toKey(item[pkName]) === toKey(pkValue));
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const handleHasMany = async (
|
|
154
|
+
session: OrmSession,
|
|
155
|
+
root: AnyEntity,
|
|
156
|
+
relationName: string,
|
|
157
|
+
relation: HasManyRelation,
|
|
158
|
+
payload: unknown,
|
|
159
|
+
options: SaveGraphOptions
|
|
160
|
+
): Promise<void> => {
|
|
161
|
+
if (!Array.isArray(payload)) return;
|
|
162
|
+
const collection = root[relationName] as unknown as HasManyCollection<unknown>;
|
|
163
|
+
await collection.load();
|
|
164
|
+
|
|
165
|
+
const targetTable = relation.target;
|
|
166
|
+
const targetPk = findPrimaryKey(targetTable);
|
|
167
|
+
const existing = collection.getItems() as unknown as AnyEntity[];
|
|
168
|
+
const seen = new Set<string>();
|
|
169
|
+
|
|
170
|
+
for (const item of payload) {
|
|
171
|
+
if (item === null || item === undefined) continue;
|
|
172
|
+
const asObj = typeof item === 'object' ? (item as AnyEntity) : { [targetPk]: item };
|
|
173
|
+
const pkValue = asObj[targetPk];
|
|
174
|
+
|
|
175
|
+
const current =
|
|
176
|
+
findInCollectionByPk(existing, targetPk, pkValue) ??
|
|
177
|
+
(pkValue !== undefined && pkValue !== null ? session.getEntity(targetTable, pkValue as PrimaryKey) : undefined);
|
|
178
|
+
|
|
179
|
+
const entity = current ?? ensureEntity(session, targetTable, asObj, options);
|
|
180
|
+
assignColumns(targetTable, entity as AnyEntity, asObj, options);
|
|
181
|
+
await applyGraphToEntity(session, targetTable, entity as AnyEntity, asObj, options);
|
|
182
|
+
|
|
183
|
+
if (!isEntityInCollection(collection.getItems() as unknown as AnyEntity[], targetPk, entity as unknown as AnyEntity)) {
|
|
184
|
+
collection.attach(entity);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (pkValue !== undefined && pkValue !== null) {
|
|
188
|
+
seen.add(toKey(pkValue));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (options.pruneMissing) {
|
|
193
|
+
for (const item of [...collection.getItems()]) {
|
|
194
|
+
const pkValue = item[targetPk];
|
|
195
|
+
if (pkValue !== undefined && pkValue !== null && !seen.has(toKey(pkValue))) {
|
|
196
|
+
collection.remove(item);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const handleHasOne = async (
|
|
203
|
+
session: OrmSession,
|
|
204
|
+
root: AnyEntity,
|
|
205
|
+
relationName: string,
|
|
206
|
+
relation: HasOneRelation,
|
|
207
|
+
payload: unknown,
|
|
208
|
+
options: SaveGraphOptions
|
|
209
|
+
): Promise<void> => {
|
|
210
|
+
const ref = root[relationName] as unknown as HasOneReference<object>;
|
|
211
|
+
if (payload === undefined) return;
|
|
212
|
+
if (payload === null) {
|
|
213
|
+
ref.set(null);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const pk = findPrimaryKey(relation.target);
|
|
217
|
+
if (typeof payload === 'number' || typeof payload === 'string') {
|
|
218
|
+
const entity = ref.set({ [pk]: payload });
|
|
219
|
+
if (entity) {
|
|
220
|
+
await applyGraphToEntity(session, relation.target, entity as AnyEntity, { [pk]: payload as PrimaryKey }, options);
|
|
221
|
+
}
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const attached = ref.set(payload as AnyEntity);
|
|
225
|
+
if (attached) {
|
|
226
|
+
await applyGraphToEntity(session, relation.target, attached as AnyEntity, payload as AnyEntity, options);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const handleBelongsTo = async (
|
|
231
|
+
session: OrmSession,
|
|
232
|
+
root: AnyEntity,
|
|
233
|
+
relationName: string,
|
|
234
|
+
relation: BelongsToRelation,
|
|
235
|
+
payload: unknown,
|
|
236
|
+
options: SaveGraphOptions
|
|
237
|
+
): Promise<void> => {
|
|
238
|
+
const ref = root[relationName] as unknown as BelongsToReference<object>;
|
|
239
|
+
if (payload === undefined) return;
|
|
240
|
+
if (payload === null) {
|
|
241
|
+
ref.set(null);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const pk = relation.localKey || findPrimaryKey(relation.target);
|
|
245
|
+
if (typeof payload === 'number' || typeof payload === 'string') {
|
|
246
|
+
const entity = ref.set({ [pk]: payload });
|
|
247
|
+
if (entity) {
|
|
248
|
+
await applyGraphToEntity(session, relation.target, entity as AnyEntity, { [pk]: payload as PrimaryKey }, options);
|
|
249
|
+
}
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const attached = ref.set(payload as AnyEntity);
|
|
253
|
+
if (attached) {
|
|
254
|
+
await applyGraphToEntity(session, relation.target, attached as AnyEntity, payload as AnyEntity, options);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const handleBelongsToMany = async (
|
|
259
|
+
session: OrmSession,
|
|
260
|
+
root: AnyEntity,
|
|
261
|
+
relationName: string,
|
|
262
|
+
relation: BelongsToManyRelation,
|
|
263
|
+
payload: unknown,
|
|
264
|
+
options: SaveGraphOptions
|
|
265
|
+
): Promise<void> => {
|
|
266
|
+
if (!Array.isArray(payload)) return;
|
|
267
|
+
const collection = root[relationName] as unknown as ManyToManyCollection<unknown>;
|
|
268
|
+
await collection.load();
|
|
269
|
+
|
|
270
|
+
const targetTable = relation.target;
|
|
271
|
+
const targetPk = relation.targetKey || findPrimaryKey(targetTable);
|
|
272
|
+
const seen = new Set<string>();
|
|
273
|
+
|
|
274
|
+
for (const item of payload) {
|
|
275
|
+
if (item === null || item === undefined) continue;
|
|
276
|
+
if (typeof item === 'number' || typeof item === 'string') {
|
|
277
|
+
const id = item;
|
|
278
|
+
collection.attach(id);
|
|
279
|
+
seen.add(toKey(id));
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const asObj = item as AnyEntity;
|
|
284
|
+
const pkValue = asObj[targetPk];
|
|
285
|
+
const entity = pkValue !== undefined && pkValue !== null
|
|
286
|
+
? session.getEntity(targetTable, pkValue as PrimaryKey) ?? ensureEntity(session, targetTable, asObj, options)
|
|
287
|
+
: ensureEntity(session, targetTable, asObj, options);
|
|
288
|
+
|
|
289
|
+
assignColumns(targetTable, entity as AnyEntity, asObj, options);
|
|
290
|
+
await applyGraphToEntity(session, targetTable, entity as AnyEntity, asObj, options);
|
|
291
|
+
|
|
292
|
+
if (!isEntityInCollection(collection.getItems() as unknown as AnyEntity[], targetPk, entity as unknown as AnyEntity)) {
|
|
293
|
+
collection.attach(entity);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (pkValue !== undefined && pkValue !== null) {
|
|
297
|
+
seen.add(toKey(pkValue));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (options.pruneMissing) {
|
|
302
|
+
for (const item of [...collection.getItems()] as unknown as AnyEntity[]) {
|
|
303
|
+
const pkValue = item[targetPk];
|
|
304
|
+
if (pkValue !== undefined && pkValue !== null && !seen.has(toKey(pkValue))) {
|
|
305
|
+
collection.detach(item);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const applyRelation = async (
|
|
312
|
+
session: OrmSession,
|
|
313
|
+
table: TableDef,
|
|
314
|
+
entity: AnyEntity,
|
|
315
|
+
relationName: string,
|
|
316
|
+
relation: RelationDef,
|
|
317
|
+
payload: unknown,
|
|
318
|
+
options: SaveGraphOptions
|
|
319
|
+
): Promise<void> => {
|
|
320
|
+
switch (relation.type) {
|
|
321
|
+
case RelationKinds.HasMany:
|
|
322
|
+
return handleHasMany(session, entity, relationName, relation, payload, options);
|
|
323
|
+
case RelationKinds.HasOne:
|
|
324
|
+
return handleHasOne(session, entity, relationName, relation, payload, options);
|
|
325
|
+
case RelationKinds.BelongsTo:
|
|
326
|
+
return handleBelongsTo(session, entity, relationName, relation, payload, options);
|
|
327
|
+
case RelationKinds.BelongsToMany:
|
|
328
|
+
return handleBelongsToMany(session, entity, relationName, relation, payload, options);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const applyGraphToEntity = async (
|
|
333
|
+
session: OrmSession,
|
|
334
|
+
table: TableDef,
|
|
335
|
+
entity: AnyEntity,
|
|
336
|
+
payload: AnyEntity,
|
|
337
|
+
options: SaveGraphOptions
|
|
338
|
+
): Promise<void> => {
|
|
339
|
+
assignColumns(table, entity, payload, options);
|
|
340
|
+
|
|
341
|
+
for (const [relationName, relation] of Object.entries(table.relations)) {
|
|
342
|
+
if (!(relationName in payload)) continue;
|
|
343
|
+
await applyRelation(session, table, entity, relationName, relation as RelationDef, payload[relationName], options);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
export const saveGraph = async <TTable extends TableDef>(
|
|
348
|
+
session: OrmSession,
|
|
349
|
+
entityClass: EntityConstructor,
|
|
350
|
+
payload: AnyEntity,
|
|
351
|
+
options: SaveGraphOptions = {}
|
|
352
|
+
): Promise<EntityInstance<TTable>> => {
|
|
353
|
+
const table = getTableDefFromEntity(entityClass);
|
|
354
|
+
if (!table) {
|
|
355
|
+
throw new Error('Entity metadata has not been bootstrapped');
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const root = ensureEntity<TTable>(session, table as TTable, payload, options);
|
|
359
|
+
await applyGraphToEntity(session, table, root as AnyEntity, payload, options);
|
|
360
|
+
return root;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
|
|
365
|
+
* Internal version of saveGraph with typed return based on the constructor.
|
|
366
|
+
|
|
367
|
+
* @param session - The ORM session.
|
|
368
|
+
|
|
369
|
+
* @param entityClass - The entity constructor.
|
|
370
|
+
|
|
371
|
+
* @param payload - The payload data for the root entity and its relations.
|
|
372
|
+
|
|
373
|
+
* @param options - Options for the save operation.
|
|
374
|
+
|
|
375
|
+
* @returns The root entity instance.
|
|
376
|
+
|
|
377
|
+
*/
|
|
378
|
+
|
|
379
|
+
export const saveGraphInternal = async <TCtor extends EntityConstructor>(
|
|
380
|
+
|
|
381
|
+
session: OrmSession,
|
|
382
|
+
|
|
383
|
+
entityClass: TCtor,
|
|
384
|
+
|
|
385
|
+
payload: AnyEntity,
|
|
386
|
+
|
|
387
|
+
options: SaveGraphOptions = {}
|
|
388
|
+
|
|
389
|
+
): Promise<InstanceType<TCtor>> => {
|
|
390
|
+
|
|
391
|
+
const table = getTableDefFromEntity(entityClass);
|
|
392
|
+
|
|
393
|
+
if (!table) {
|
|
394
|
+
|
|
395
|
+
throw new Error('Entity metadata has not been bootstrapped');
|
|
396
|
+
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const root = ensureEntity(session, table, payload, options);
|
|
400
|
+
|
|
401
|
+
await applyGraphToEntity(session, table, root as AnyEntity, payload, options);
|
|
402
|
+
|
|
403
|
+
return root as unknown as InstanceType<TCtor>;
|
|
404
|
+
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Patches an existing entity by applying only the provided fields.
|
|
409
|
+
* Requires the entity to exist (fetched by primary key).
|
|
410
|
+
*
|
|
411
|
+
* @param session - The ORM session.
|
|
412
|
+
* @param entityClass - The entity constructor.
|
|
413
|
+
* @param payload - The partial payload data for the root entity and its relations.
|
|
414
|
+
* @param options - Options for the patch operation.
|
|
415
|
+
* @returns The patched entity instance.
|
|
416
|
+
*/
|
|
417
|
+
export const patchGraphInternal = async <TCtor extends EntityConstructor>(
|
|
418
|
+
session: OrmSession,
|
|
419
|
+
entityClass: TCtor,
|
|
420
|
+
existing: InstanceType<TCtor>,
|
|
421
|
+
payload: AnyEntity,
|
|
422
|
+
options: SaveGraphOptions = {}
|
|
423
|
+
): Promise<InstanceType<TCtor>> => {
|
|
424
|
+
const table = getTableDefFromEntity(entityClass);
|
|
425
|
+
if (!table) {
|
|
426
|
+
throw new Error('Entity metadata has not been bootstrapped');
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
await applyGraphToEntity(session, table, existing as AnyEntity, payload, options);
|
|
430
|
+
return existing;
|
|
431
|
+
};
|