metal-orm 1.1.20 → 1.1.21
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/index.cjs +12 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -8
- package/dist/index.d.ts +7 -8
- package/dist/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/relation-change-processor.ts +15 -11
package/package.json
CHANGED
|
@@ -7,7 +7,6 @@ import { UpdateQueryBuilder } from '../query-builder/update.js';
|
|
|
7
7
|
import { findPrimaryKey } from '../query-builder/hydration-planner.js';
|
|
8
8
|
import type { BelongsToManyRelation, HasManyRelation, HasOneRelation, MorphOneRelation, MorphManyRelation, MorphToRelation } from '../schema/relation.js';
|
|
9
9
|
import { RelationKinds } from '../schema/relation.js';
|
|
10
|
-
import type { TableDef } from '../schema/table.js';
|
|
11
10
|
import type { DbExecutor } from '../core/execution/db-executor.js';
|
|
12
11
|
import type { RelationChangeEntry } from './runtime-types.js';
|
|
13
12
|
import { UnitOfWork } from './unit-of-work.js';
|
|
@@ -153,7 +152,10 @@ export class RelationChangeProcessor {
|
|
|
153
152
|
const rootId = entry.root[rootKey];
|
|
154
153
|
if (rootId === undefined || rootId === null) return;
|
|
155
154
|
|
|
156
|
-
const targetId = this.
|
|
155
|
+
const targetId = this.resolveBelongsToManyTargetValue(
|
|
156
|
+
entry.change.entity as Record<string, unknown>,
|
|
157
|
+
relation
|
|
158
|
+
);
|
|
157
159
|
if (targetId === null) return;
|
|
158
160
|
|
|
159
161
|
const pivotPayload = this.buildPivotPayload(relation, entry.change.pivot);
|
|
@@ -235,7 +237,7 @@ export class RelationChangeProcessor {
|
|
|
235
237
|
* Inserts a pivot row for belongs-to-many relations.
|
|
236
238
|
* @param relation - The belongs-to-many relation
|
|
237
239
|
* @param rootId - The root entity's primary key value
|
|
238
|
-
* @param targetId - The target entity's
|
|
240
|
+
* @param targetId - The target entity's relation key value
|
|
239
241
|
*/
|
|
240
242
|
private async insertPivotRow(
|
|
241
243
|
relation: BelongsToManyRelation,
|
|
@@ -258,7 +260,7 @@ export class RelationChangeProcessor {
|
|
|
258
260
|
* Updates a pivot row for belongs-to-many relations.
|
|
259
261
|
* @param relation - The belongs-to-many relation
|
|
260
262
|
* @param rootId - The root entity's primary key value
|
|
261
|
-
* @param targetId - The target entity's
|
|
263
|
+
* @param targetId - The target entity's relation key value
|
|
262
264
|
* @param pivotPayload - The pivot columns to update
|
|
263
265
|
*/
|
|
264
266
|
private async updatePivotRow(
|
|
@@ -282,7 +284,7 @@ export class RelationChangeProcessor {
|
|
|
282
284
|
* Deletes a pivot row for belongs-to-many relations.
|
|
283
285
|
* @param relation - The belongs-to-many relation
|
|
284
286
|
* @param rootId - The root entity's primary key value
|
|
285
|
-
* @param targetId - The target entity's
|
|
287
|
+
* @param targetId - The target entity's relation key value
|
|
286
288
|
*/
|
|
287
289
|
private async deletePivotRow(relation: BelongsToManyRelation, rootId: string | number, targetId: string | number): Promise<void> {
|
|
288
290
|
const rootCol = relation.pivotTable.columns[relation.pivotForeignKeyToRoot];
|
|
@@ -297,14 +299,16 @@ export class RelationChangeProcessor {
|
|
|
297
299
|
}
|
|
298
300
|
|
|
299
301
|
/**
|
|
300
|
-
* Resolves the
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
* @returns The primary key value or null
|
|
302
|
+
* Resolves the target-side key value used by a belongs-to-many relation.
|
|
303
|
+
* The declared targetKey is part of the relation contract and takes
|
|
304
|
+
* precedence over the target table primary key.
|
|
304
305
|
*/
|
|
305
|
-
private
|
|
306
|
+
private resolveBelongsToManyTargetValue(
|
|
307
|
+
entity: Record<string, unknown>,
|
|
308
|
+
relation: BelongsToManyRelation
|
|
309
|
+
): string | number | null {
|
|
306
310
|
if (!entity) return null;
|
|
307
|
-
const key = findPrimaryKey(
|
|
311
|
+
const key = relation.targetKey || findPrimaryKey(relation.target);
|
|
308
312
|
const value = entity[key];
|
|
309
313
|
if (value === undefined || value === null) return null;
|
|
310
314
|
return (value as string | number | null | undefined) ?? null;
|