metal-orm 1.0.5 → 1.0.7

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 (36) hide show
  1. package/README.md +299 -113
  2. package/docs/CHANGES.md +104 -0
  3. package/docs/advanced-features.md +92 -1
  4. package/docs/api-reference.md +13 -4
  5. package/docs/dml-operations.md +156 -0
  6. package/docs/getting-started.md +122 -55
  7. package/docs/hydration.md +78 -13
  8. package/docs/index.md +19 -14
  9. package/docs/multi-dialect-support.md +25 -0
  10. package/docs/query-builder.md +60 -0
  11. package/docs/runtime.md +105 -0
  12. package/docs/schema-definition.md +52 -1
  13. package/package.json +1 -1
  14. package/src/ast/expression.ts +38 -18
  15. package/src/builder/hydration-planner.ts +74 -74
  16. package/src/builder/select.ts +427 -395
  17. package/src/constants/sql-operator-config.ts +3 -0
  18. package/src/constants/sql.ts +38 -32
  19. package/src/index.ts +16 -8
  20. package/src/playground/features/playground/data/scenarios/types.ts +18 -15
  21. package/src/playground/features/playground/data/schema.ts +10 -10
  22. package/src/playground/features/playground/services/QueryExecutionService.ts +2 -1
  23. package/src/runtime/entity-meta.ts +52 -0
  24. package/src/runtime/entity.ts +252 -0
  25. package/src/runtime/execute.ts +36 -0
  26. package/src/runtime/hydration.ts +99 -49
  27. package/src/runtime/lazy-batch.ts +205 -0
  28. package/src/runtime/orm-context.ts +539 -0
  29. package/src/runtime/relations/belongs-to.ts +92 -0
  30. package/src/runtime/relations/has-many.ts +111 -0
  31. package/src/runtime/relations/many-to-many.ts +149 -0
  32. package/src/schema/column.ts +15 -1
  33. package/src/schema/relation.ts +82 -58
  34. package/src/schema/table.ts +34 -22
  35. package/src/schema/types.ts +76 -0
  36. package/tests/orm-runtime.test.ts +254 -0
@@ -0,0 +1,539 @@
1
+ import { Dialect, CompiledQuery } from '../dialect/abstract';
2
+ import { findPrimaryKey } from '../builder/hydration-planner';
3
+ import { TableDef, TableHooks } from '../schema/table';
4
+ import {
5
+ RelationDef,
6
+ HasManyRelation,
7
+ BelongsToRelation,
8
+ BelongsToManyRelation,
9
+ RelationKinds
10
+ } from '../schema/relation';
11
+ import { InsertQueryBuilder } from '../builder/insert';
12
+ import { UpdateQueryBuilder } from '../builder/update';
13
+ import { DeleteQueryBuilder } from '../builder/delete';
14
+ import { and, eq } from '../ast/expression';
15
+
16
+ export type QueryResult = {
17
+ columns: string[];
18
+ values: unknown[][];
19
+ };
20
+
21
+ export interface DbExecutor {
22
+ executeSql(sql: string, params?: unknown[]): Promise<QueryResult[]>;
23
+ beginTransaction?(): Promise<void>;
24
+ commitTransaction?(): Promise<void>;
25
+ rollbackTransaction?(): Promise<void>;
26
+ }
27
+
28
+ export interface OrmInterceptor {
29
+ beforeFlush?(ctx: OrmContext): Promise<void> | void;
30
+ afterFlush?(ctx: OrmContext): Promise<void> | void;
31
+ }
32
+
33
+ export interface DomainEventHandler {
34
+ (event: any, ctx: OrmContext): Promise<void> | void;
35
+ }
36
+
37
+ export interface HasDomainEvents {
38
+ domainEvents?: any[];
39
+ }
40
+
41
+ export type RelationKey = string;
42
+
43
+ export type RelationChange<T> =
44
+ | { kind: 'add'; entity: T }
45
+ | { kind: 'attach'; entity: T }
46
+ | { kind: 'remove'; entity: T }
47
+ | { kind: 'detach'; entity: T };
48
+
49
+ export interface RelationChangeEntry {
50
+ root: any;
51
+ relationKey: RelationKey;
52
+ rootTable: TableDef;
53
+ relationName: string;
54
+ relation: RelationDef;
55
+ change: RelationChange<any>;
56
+ }
57
+
58
+ export enum EntityStatus {
59
+ New = 'new',
60
+ Managed = 'managed',
61
+ Dirty = 'dirty',
62
+ Removed = 'removed',
63
+ Detached = 'detached'
64
+ }
65
+
66
+ interface TrackedEntity {
67
+ table: TableDef;
68
+ entity: any;
69
+ pk: string | number | null;
70
+ status: EntityStatus;
71
+ original: Record<string, any> | null;
72
+ }
73
+
74
+ export interface OrmContextOptions {
75
+ dialect: Dialect;
76
+ executor: DbExecutor;
77
+ interceptors?: OrmInterceptor[];
78
+ domainEventHandlers?: Record<string, DomainEventHandler[]>;
79
+ }
80
+
81
+ export class OrmContext {
82
+ private readonly identityMap = new Map<string, Map<string, TrackedEntity>>();
83
+ private readonly trackedEntities = new Map<any, TrackedEntity>();
84
+ private readonly relationChanges: RelationChangeEntry[] = [];
85
+ private readonly interceptors: OrmInterceptor[];
86
+ private readonly domainEventHandlers = new Map<string, DomainEventHandler[]>();
87
+
88
+ constructor(private readonly options: OrmContextOptions) {
89
+ this.interceptors = [...(options.interceptors ?? [])];
90
+ const handlers = options.domainEventHandlers ?? {};
91
+ Object.entries(handlers).forEach(([name, list]) => {
92
+ this.domainEventHandlers.set(name, [...list]);
93
+ });
94
+ }
95
+
96
+ get dialect(): Dialect {
97
+ return this.options.dialect;
98
+ }
99
+
100
+ get executor(): DbExecutor {
101
+ return this.options.executor;
102
+ }
103
+
104
+ get identityBuckets(): Map<string, Map<string, TrackedEntity>> {
105
+ return this.identityMap;
106
+ }
107
+
108
+ get tracked(): TrackedEntity[] {
109
+ return Array.from(this.trackedEntities.values());
110
+ }
111
+
112
+ getEntity(table: TableDef, pk: string | number): any | undefined {
113
+ const bucket = this.identityMap.get(table.name);
114
+ return bucket?.get(this.toIdentityKey(pk))?.entity;
115
+ }
116
+
117
+ setEntity(table: TableDef, pk: string | number, entity: any): void {
118
+ if (pk === null || pk === undefined) return;
119
+ let tracked = this.trackedEntities.get(entity);
120
+ if (!tracked) {
121
+ tracked = {
122
+ table,
123
+ entity,
124
+ pk,
125
+ status: EntityStatus.Managed,
126
+ original: this.createSnapshot(table, entity)
127
+ };
128
+ this.trackedEntities.set(entity, tracked);
129
+ } else {
130
+ tracked.pk = pk;
131
+ }
132
+
133
+ this.registerIdentity(tracked);
134
+ }
135
+
136
+ trackNew(table: TableDef, entity: any, pk?: string | number): void {
137
+ const tracked: TrackedEntity = {
138
+ table,
139
+ entity,
140
+ pk: pk ?? null,
141
+ status: EntityStatus.New,
142
+ original: null
143
+ };
144
+ this.trackedEntities.set(entity, tracked);
145
+ if (pk != null) {
146
+ this.registerIdentity(tracked);
147
+ }
148
+ }
149
+
150
+ trackManaged(table: TableDef, pk: string | number, entity: any): void {
151
+ const tracked: TrackedEntity = {
152
+ table,
153
+ entity,
154
+ pk,
155
+ status: EntityStatus.Managed,
156
+ original: this.createSnapshot(table, entity)
157
+ };
158
+ this.trackedEntities.set(entity, tracked);
159
+ this.registerIdentity(tracked);
160
+ }
161
+
162
+ markDirty(entity: any): void {
163
+ const tracked = this.trackedEntities.get(entity);
164
+ if (!tracked) return;
165
+ if (tracked.status === EntityStatus.New || tracked.status === EntityStatus.Removed) return;
166
+ tracked.status = EntityStatus.Dirty;
167
+ }
168
+
169
+ markRemoved(entity: any): void {
170
+ const tracked = this.trackedEntities.get(entity);
171
+ if (!tracked) return;
172
+ tracked.status = EntityStatus.Removed;
173
+ }
174
+
175
+ registerRelationChange(
176
+ root: any,
177
+ relationKey: RelationKey,
178
+ rootTable: TableDef,
179
+ relationName: string,
180
+ relation: RelationDef,
181
+ change: RelationChange<any>
182
+ ): void {
183
+ this.relationChanges.push({
184
+ root,
185
+ relationKey,
186
+ rootTable,
187
+ relationName,
188
+ relation,
189
+ change
190
+ });
191
+ }
192
+
193
+ registerInterceptor(interceptor: OrmInterceptor): void {
194
+ this.interceptors.push(interceptor);
195
+ }
196
+
197
+ registerDomainEventHandler(name: string, handler: DomainEventHandler): void {
198
+ const existing = this.domainEventHandlers.get(name) ?? [];
199
+ existing.push(handler);
200
+ this.domainEventHandlers.set(name, existing);
201
+ }
202
+
203
+ async saveChanges(): Promise<void> {
204
+ await this.runInTransaction(async () => {
205
+ for (const interceptor of this.interceptors) {
206
+ await interceptor.beforeFlush?.(this);
207
+ }
208
+
209
+ await this.flushEntities();
210
+ await this.processRelationChanges();
211
+ await this.flushEntities();
212
+
213
+ for (const interceptor of this.interceptors) {
214
+ await interceptor.afterFlush?.(this);
215
+ }
216
+ });
217
+
218
+ await this.dispatchDomainEvents();
219
+ }
220
+
221
+ getEntitiesForTable(table: TableDef): TrackedEntity[] {
222
+ const bucket = this.identityMap.get(table.name);
223
+ return bucket ? Array.from(bucket.values()) : [];
224
+ }
225
+
226
+ protected async flushEntities(): Promise<void> {
227
+ const toFlush = Array.from(this.trackedEntities.values());
228
+ for (const tracked of toFlush) {
229
+ switch (tracked.status) {
230
+ case EntityStatus.New:
231
+ await this.flushInsert(tracked);
232
+ break;
233
+ case EntityStatus.Dirty:
234
+ await this.flushUpdate(tracked);
235
+ break;
236
+ case EntityStatus.Removed:
237
+ await this.flushDelete(tracked);
238
+ break;
239
+ default:
240
+ break;
241
+ }
242
+ }
243
+ }
244
+
245
+ private async flushInsert(tracked: TrackedEntity): Promise<void> {
246
+ await this.runHook(tracked.table.hooks?.beforeInsert, tracked);
247
+
248
+ const payload = this.extractColumns(tracked.table, tracked.entity);
249
+ const builder = new InsertQueryBuilder(tracked.table).values(payload);
250
+ const compiled = builder.compile(this.dialect);
251
+ await this.executeCompiled(compiled);
252
+
253
+ tracked.status = EntityStatus.Managed;
254
+ tracked.original = this.createSnapshot(tracked.table, tracked.entity);
255
+ tracked.pk = this.getPrimaryKeyValue(tracked);
256
+ this.registerIdentity(tracked);
257
+
258
+ await this.runHook(tracked.table.hooks?.afterInsert, tracked);
259
+ }
260
+
261
+ private async flushUpdate(tracked: TrackedEntity): Promise<void> {
262
+ if (tracked.pk == null) return;
263
+ const changes = this.computeChanges(tracked);
264
+ if (!Object.keys(changes).length) {
265
+ tracked.status = EntityStatus.Managed;
266
+ return;
267
+ }
268
+
269
+ await this.runHook(tracked.table.hooks?.beforeUpdate, tracked);
270
+
271
+ const pkColumn = tracked.table.columns[findPrimaryKey(tracked.table)];
272
+ if (!pkColumn) return;
273
+
274
+ const builder = new UpdateQueryBuilder(tracked.table)
275
+ .set(changes)
276
+ .where(eq(pkColumn, tracked.pk));
277
+
278
+ const compiled = builder.compile(this.dialect);
279
+ await this.executeCompiled(compiled);
280
+
281
+ tracked.status = EntityStatus.Managed;
282
+ tracked.original = this.createSnapshot(tracked.table, tracked.entity);
283
+ this.registerIdentity(tracked);
284
+
285
+ await this.runHook(tracked.table.hooks?.afterUpdate, tracked);
286
+ }
287
+
288
+ private async flushDelete(tracked: TrackedEntity): Promise<void> {
289
+ if (tracked.pk == null) return;
290
+ await this.runHook(tracked.table.hooks?.beforeDelete, tracked);
291
+
292
+ const pkColumn = tracked.table.columns[findPrimaryKey(tracked.table)];
293
+ if (!pkColumn) return;
294
+
295
+ const builder = new DeleteQueryBuilder(tracked.table).where(
296
+ eq(pkColumn, tracked.pk)
297
+ );
298
+ const compiled = builder.compile(this.dialect);
299
+ await this.executeCompiled(compiled);
300
+
301
+ tracked.status = EntityStatus.Detached;
302
+ this.trackedEntities.delete(tracked.entity);
303
+ this.removeIdentity(tracked);
304
+
305
+ await this.runHook(tracked.table.hooks?.afterDelete, tracked);
306
+ }
307
+
308
+ private async processRelationChanges(): Promise<void> {
309
+ if (!this.relationChanges.length) return;
310
+ const entries = [...this.relationChanges];
311
+ this.relationChanges.length = 0;
312
+
313
+ for (const entry of entries) {
314
+ switch (entry.relation.type) {
315
+ case RelationKinds.HasMany:
316
+ await this.handleHasManyChange(entry);
317
+ break;
318
+ case RelationKinds.BelongsToMany:
319
+ await this.handleBelongsToManyChange(entry);
320
+ break;
321
+ case RelationKinds.BelongsTo:
322
+ await this.handleBelongsToChange(entry);
323
+ break;
324
+ }
325
+ }
326
+ }
327
+
328
+ private async handleHasManyChange(entry: RelationChangeEntry): Promise<void> {
329
+ const relation = entry.relation as HasManyRelation;
330
+ const target = entry.change.entity;
331
+ if (!target) return;
332
+
333
+ const tracked = this.trackedEntities.get(target);
334
+ if (!tracked) return;
335
+
336
+ const localKey = relation.localKey || findPrimaryKey(entry.rootTable);
337
+ const rootValue = entry.root[localKey];
338
+ if (rootValue === undefined || rootValue === null) return;
339
+
340
+ if (entry.change.kind === 'add' || entry.change.kind === 'attach') {
341
+ this.assignHasManyForeignKey(tracked, relation, rootValue);
342
+ return;
343
+ }
344
+
345
+ if (entry.change.kind === 'remove') {
346
+ this.detachHasManyChild(tracked, relation);
347
+ }
348
+ }
349
+
350
+ private async handleBelongsToChange(_entry: RelationChangeEntry): Promise<void> {
351
+ // Reserved for future cascade/persist behaviors for belongs-to relations.
352
+ }
353
+
354
+ private async handleBelongsToManyChange(entry: RelationChangeEntry): Promise<void> {
355
+ const relation = entry.relation as BelongsToManyRelation;
356
+ const rootKey = relation.localKey || findPrimaryKey(entry.rootTable);
357
+ const rootId = entry.root[rootKey];
358
+ if (rootId === undefined || rootId === null) return;
359
+
360
+ const targetId = this.resolvePrimaryKeyValue(entry.change.entity, relation.target);
361
+ if (targetId === null) return;
362
+
363
+ if (entry.change.kind === 'attach' || entry.change.kind === 'add') {
364
+ await this.insertPivotRow(relation, rootId, targetId);
365
+ return;
366
+ }
367
+
368
+ if (entry.change.kind === 'detach' || entry.change.kind === 'remove') {
369
+ await this.deletePivotRow(relation, rootId, targetId);
370
+
371
+ if (relation.cascade === 'all' || relation.cascade === 'remove') {
372
+ this.markRemoved(entry.change.entity);
373
+ }
374
+ }
375
+ }
376
+
377
+ private assignHasManyForeignKey(
378
+ tracked: TrackedEntity,
379
+ relation: HasManyRelation,
380
+ rootValue: unknown
381
+ ): void {
382
+ const child = tracked.entity;
383
+ const current = child[relation.foreignKey];
384
+ if (current === rootValue) return;
385
+ child[relation.foreignKey] = rootValue;
386
+ this.markDirty(child);
387
+ }
388
+
389
+ private detachHasManyChild(tracked: TrackedEntity, relation: HasManyRelation): void {
390
+ const child = tracked.entity;
391
+ if (relation.cascade === 'all' || relation.cascade === 'remove') {
392
+ this.markRemoved(child);
393
+ return;
394
+ }
395
+ child[relation.foreignKey] = null;
396
+ this.markDirty(child);
397
+ }
398
+
399
+ private async insertPivotRow(relation: BelongsToManyRelation, rootId: string | number, targetId: string | number): Promise<void> {
400
+ const payload = {
401
+ [relation.pivotForeignKeyToRoot]: rootId,
402
+ [relation.pivotForeignKeyToTarget]: targetId
403
+ };
404
+ const builder = new InsertQueryBuilder(relation.pivotTable).values(payload);
405
+ await this.executeCompiled(builder.compile(this.dialect));
406
+ }
407
+
408
+ private async deletePivotRow(relation: BelongsToManyRelation, rootId: string | number, targetId: string | number): Promise<void> {
409
+ const rootCol = relation.pivotTable.columns[relation.pivotForeignKeyToRoot];
410
+ const targetCol = relation.pivotTable.columns[relation.pivotForeignKeyToTarget];
411
+ if (!rootCol || !targetCol) return;
412
+
413
+ const builder = new DeleteQueryBuilder(relation.pivotTable).where(
414
+ and(eq(rootCol, rootId), eq(targetCol, targetId))
415
+ );
416
+ await this.executeCompiled(builder.compile(this.dialect));
417
+ }
418
+
419
+ private resolvePrimaryKeyValue(entity: any, table: TableDef): string | number | null {
420
+ if (!entity) return null;
421
+ const key = findPrimaryKey(table);
422
+ const value = entity[key];
423
+ if (value === undefined || value === null) return null;
424
+ return value;
425
+ }
426
+
427
+ private async dispatchDomainEvents(): Promise<void> {
428
+ for (const tracked of this.trackedEntities.values()) {
429
+ const entity = tracked.entity as HasDomainEvents;
430
+ if (!entity.domainEvents || !entity.domainEvents.length) continue;
431
+
432
+ for (const event of entity.domainEvents) {
433
+ const eventName = this.getEventName(event);
434
+ const handlers = this.domainEventHandlers.get(eventName);
435
+ if (!handlers) continue;
436
+
437
+ for (const handler of handlers) {
438
+ await handler(event, this);
439
+ }
440
+ }
441
+
442
+ entity.domainEvents = [];
443
+ }
444
+ }
445
+
446
+ private async runHook(
447
+ hook: TableHooks[keyof TableHooks] | undefined,
448
+ tracked: TrackedEntity
449
+ ): Promise<void> {
450
+ if (!hook) return;
451
+ await hook(this, tracked.entity);
452
+ }
453
+
454
+ private computeChanges(tracked: TrackedEntity): Record<string, unknown> {
455
+ const snapshot = tracked.original ?? {};
456
+ const changes: Record<string, unknown> = {};
457
+ for (const column of Object.keys(tracked.table.columns)) {
458
+ const current = tracked.entity[column];
459
+ if (snapshot[column] !== current) {
460
+ changes[column] = current;
461
+ }
462
+ }
463
+ return changes;
464
+ }
465
+
466
+ private extractColumns(table: TableDef, entity: any): Record<string, unknown> {
467
+ const payload: Record<string, unknown> = {};
468
+ for (const column of Object.keys(table.columns)) {
469
+ payload[column] = entity[column];
470
+ }
471
+ return payload;
472
+ }
473
+
474
+ private async executeCompiled(compiled: CompiledQuery): Promise<void> {
475
+ await this.executor.executeSql(compiled.sql, compiled.params);
476
+ }
477
+
478
+ private registerIdentity(tracked: TrackedEntity): void {
479
+ if (tracked.pk == null) return;
480
+ const bucket = this.identityMap.get(tracked.table.name) ?? new Map<string, TrackedEntity>();
481
+ bucket.set(this.toIdentityKey(tracked.pk), tracked);
482
+ this.identityMap.set(tracked.table.name, bucket);
483
+ }
484
+
485
+ private removeIdentity(tracked: TrackedEntity): void {
486
+ if (tracked.pk == null) return;
487
+ const bucket = this.identityMap.get(tracked.table.name);
488
+ bucket?.delete(this.toIdentityKey(tracked.pk));
489
+ }
490
+
491
+ private createSnapshot(table: TableDef, entity: any): Record<string, any> {
492
+ const snapshot: Record<string, any> = {};
493
+ for (const column of Object.keys(table.columns)) {
494
+ snapshot[column] = entity[column];
495
+ }
496
+ return snapshot;
497
+ }
498
+
499
+ private getPrimaryKeyValue(tracked: TrackedEntity): string | number | null {
500
+ const key = findPrimaryKey(tracked.table);
501
+ const val = tracked.entity[key];
502
+ if (val === undefined || val === null) return null;
503
+ return val;
504
+ }
505
+
506
+ private async runInTransaction(action: () => Promise<void>): Promise<void> {
507
+ const executor = this.executor;
508
+ if (!executor.beginTransaction) {
509
+ await action();
510
+ return;
511
+ }
512
+
513
+ await executor.beginTransaction();
514
+ try {
515
+ await action();
516
+ await executor.commitTransaction?.();
517
+ } catch (error) {
518
+ await executor.rollbackTransaction?.();
519
+ throw error;
520
+ }
521
+ }
522
+
523
+ private toIdentityKey(pk: string | number): string {
524
+ return String(pk);
525
+ }
526
+
527
+ private getEventName(event: any): string {
528
+ if (!event) return 'Unknown';
529
+ if (typeof event === 'string') return event;
530
+ return event.constructor?.name ?? 'Unknown';
531
+ }
532
+ }
533
+
534
+ export const addDomainEvent = (entity: HasDomainEvents, event: any): void => {
535
+ if (!entity.domainEvents) {
536
+ entity.domainEvents = [];
537
+ }
538
+ entity.domainEvents.push(event);
539
+ };
@@ -0,0 +1,92 @@
1
+ import { BelongsToReference } from '../../schema/types';
2
+ import { OrmContext, RelationKey } from '../orm-context';
3
+ import { BelongsToRelation } from '../../schema/relation';
4
+ import { TableDef } from '../../schema/table';
5
+ import { EntityMeta, getHydrationRecord, hasEntityMeta } from '../entity-meta';
6
+
7
+ type Rows = Record<string, any>;
8
+
9
+ const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
10
+
11
+ export class DefaultBelongsToReference<TParent> implements BelongsToReference<TParent> {
12
+ private loaded = false;
13
+ private current: TParent | null = null;
14
+
15
+ constructor(
16
+ private readonly ctx: OrmContext,
17
+ private readonly meta: EntityMeta<any>,
18
+ private readonly root: any,
19
+ private readonly relationName: string,
20
+ private readonly relation: BelongsToRelation,
21
+ private readonly rootTable: TableDef,
22
+ private readonly loader: () => Promise<Map<string, Rows>>,
23
+ private readonly createEntity: (row: Record<string, any>) => TParent,
24
+ private readonly targetKey: string
25
+ ) {
26
+ this.populateFromHydrationCache();
27
+ }
28
+
29
+ async load(): Promise<TParent | null> {
30
+ if (this.loaded) return this.current;
31
+ const map = await this.loader();
32
+ const fkValue = this.root[this.relation.foreignKey];
33
+ if (fkValue === null || fkValue === undefined) {
34
+ this.current = null;
35
+ } else {
36
+ const row = map.get(toKey(fkValue));
37
+ this.current = row ? this.createEntity(row) : null;
38
+ }
39
+ this.loaded = true;
40
+ return this.current;
41
+ }
42
+
43
+ get(): TParent | null {
44
+ return this.current;
45
+ }
46
+
47
+ set(data: Partial<TParent> | TParent | null): TParent | null {
48
+ if (data === null) {
49
+ const previous = this.current;
50
+ this.root[this.relation.foreignKey] = null;
51
+ this.current = null;
52
+ this.ctx.registerRelationChange(
53
+ this.root,
54
+ this.relationKey,
55
+ this.rootTable,
56
+ this.relationName,
57
+ this.relation,
58
+ { kind: 'remove', entity: previous }
59
+ );
60
+ return null;
61
+ }
62
+
63
+ const entity = hasEntityMeta(data) ? (data as TParent) : this.createEntity(data as Record<string, any>);
64
+ const pkValue = (entity as any)[this.targetKey];
65
+ if (pkValue !== undefined) {
66
+ this.root[this.relation.foreignKey] = pkValue;
67
+ }
68
+ this.current = entity;
69
+ this.ctx.registerRelationChange(
70
+ this.root,
71
+ this.relationKey,
72
+ this.rootTable,
73
+ this.relationName,
74
+ this.relation,
75
+ { kind: 'attach', entity }
76
+ );
77
+ return entity;
78
+ }
79
+
80
+ private get relationKey(): RelationKey {
81
+ return `${this.rootTable.name}.${this.relationName}`;
82
+ }
83
+
84
+ private populateFromHydrationCache(): void {
85
+ const fkValue = this.root[this.relation.foreignKey];
86
+ if (fkValue === undefined || fkValue === null) return;
87
+ const row = getHydrationRecord(this.meta, this.relationName, fkValue);
88
+ if (!row) return;
89
+ this.current = this.createEntity(row);
90
+ this.loaded = true;
91
+ }
92
+ }