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.d.cts CHANGED
@@ -4230,14 +4230,14 @@ declare class RelationChangeProcessor {
4230
4230
  * Inserts a pivot row for belongs-to-many relations.
4231
4231
  * @param relation - The belongs-to-many relation
4232
4232
  * @param rootId - The root entity's primary key value
4233
- * @param targetId - The target entity's primary key value
4233
+ * @param targetId - The target entity's relation key value
4234
4234
  */
4235
4235
  private insertPivotRow;
4236
4236
  /**
4237
4237
  * Updates a pivot row for belongs-to-many relations.
4238
4238
  * @param relation - The belongs-to-many relation
4239
4239
  * @param rootId - The root entity's primary key value
4240
- * @param targetId - The target entity's primary key value
4240
+ * @param targetId - The target entity's relation key value
4241
4241
  * @param pivotPayload - The pivot columns to update
4242
4242
  */
4243
4243
  private updatePivotRow;
@@ -4245,16 +4245,15 @@ declare class RelationChangeProcessor {
4245
4245
  * Deletes a pivot row for belongs-to-many relations.
4246
4246
  * @param relation - The belongs-to-many relation
4247
4247
  * @param rootId - The root entity's primary key value
4248
- * @param targetId - The target entity's primary key value
4248
+ * @param targetId - The target entity's relation key value
4249
4249
  */
4250
4250
  private deletePivotRow;
4251
4251
  /**
4252
- * Resolves the primary key value from an entity.
4253
- * @param entity - The entity
4254
- * @param table - The table definition
4255
- * @returns The primary key value or null
4252
+ * Resolves the target-side key value used by a belongs-to-many relation.
4253
+ * The declared targetKey is part of the relation contract and takes
4254
+ * precedence over the target table primary key.
4256
4255
  */
4257
- private resolvePrimaryKeyValue;
4256
+ private resolveBelongsToManyTargetValue;
4258
4257
  private buildPivotPayload;
4259
4258
  private handleMorphOneChange;
4260
4259
  private handleMorphManyChange;
package/dist/index.d.ts CHANGED
@@ -4230,14 +4230,14 @@ declare class RelationChangeProcessor {
4230
4230
  * Inserts a pivot row for belongs-to-many relations.
4231
4231
  * @param relation - The belongs-to-many relation
4232
4232
  * @param rootId - The root entity's primary key value
4233
- * @param targetId - The target entity's primary key value
4233
+ * @param targetId - The target entity's relation key value
4234
4234
  */
4235
4235
  private insertPivotRow;
4236
4236
  /**
4237
4237
  * Updates a pivot row for belongs-to-many relations.
4238
4238
  * @param relation - The belongs-to-many relation
4239
4239
  * @param rootId - The root entity's primary key value
4240
- * @param targetId - The target entity's primary key value
4240
+ * @param targetId - The target entity's relation key value
4241
4241
  * @param pivotPayload - The pivot columns to update
4242
4242
  */
4243
4243
  private updatePivotRow;
@@ -4245,16 +4245,15 @@ declare class RelationChangeProcessor {
4245
4245
  * Deletes a pivot row for belongs-to-many relations.
4246
4246
  * @param relation - The belongs-to-many relation
4247
4247
  * @param rootId - The root entity's primary key value
4248
- * @param targetId - The target entity's primary key value
4248
+ * @param targetId - The target entity's relation key value
4249
4249
  */
4250
4250
  private deletePivotRow;
4251
4251
  /**
4252
- * Resolves the primary key value from an entity.
4253
- * @param entity - The entity
4254
- * @param table - The table definition
4255
- * @returns The primary key value or null
4252
+ * Resolves the target-side key value used by a belongs-to-many relation.
4253
+ * The declared targetKey is part of the relation contract and takes
4254
+ * precedence over the target table primary key.
4256
4255
  */
4257
- private resolvePrimaryKeyValue;
4256
+ private resolveBelongsToManyTargetValue;
4258
4257
  private buildPivotPayload;
4259
4258
  private handleMorphOneChange;
4260
4259
  private handleMorphManyChange;
package/dist/index.js CHANGED
@@ -14629,7 +14629,10 @@ var RelationChangeProcessor = class {
14629
14629
  const rootKey = relation.localKey || findPrimaryKey(entry.rootTable);
14630
14630
  const rootId = entry.root[rootKey];
14631
14631
  if (rootId === void 0 || rootId === null) return;
14632
- const targetId = this.resolvePrimaryKeyValue(entry.change.entity, relation.target);
14632
+ const targetId = this.resolveBelongsToManyTargetValue(
14633
+ entry.change.entity,
14634
+ relation
14635
+ );
14633
14636
  if (targetId === null) return;
14634
14637
  const pivotPayload = this.buildPivotPayload(relation, entry.change.pivot);
14635
14638
  if (entry.change.kind === "update") {
@@ -14701,7 +14704,7 @@ var RelationChangeProcessor = class {
14701
14704
  * Inserts a pivot row for belongs-to-many relations.
14702
14705
  * @param relation - The belongs-to-many relation
14703
14706
  * @param rootId - The root entity's primary key value
14704
- * @param targetId - The target entity's primary key value
14707
+ * @param targetId - The target entity's relation key value
14705
14708
  */
14706
14709
  async insertPivotRow(relation, rootId, targetId, pivotPayload) {
14707
14710
  const payload = {
@@ -14717,7 +14720,7 @@ var RelationChangeProcessor = class {
14717
14720
  * Updates a pivot row for belongs-to-many relations.
14718
14721
  * @param relation - The belongs-to-many relation
14719
14722
  * @param rootId - The root entity's primary key value
14720
- * @param targetId - The target entity's primary key value
14723
+ * @param targetId - The target entity's relation key value
14721
14724
  * @param pivotPayload - The pivot columns to update
14722
14725
  */
14723
14726
  async updatePivotRow(relation, rootId, targetId, pivotPayload) {
@@ -14732,7 +14735,7 @@ var RelationChangeProcessor = class {
14732
14735
  * Deletes a pivot row for belongs-to-many relations.
14733
14736
  * @param relation - The belongs-to-many relation
14734
14737
  * @param rootId - The root entity's primary key value
14735
- * @param targetId - The target entity's primary key value
14738
+ * @param targetId - The target entity's relation key value
14736
14739
  */
14737
14740
  async deletePivotRow(relation, rootId, targetId) {
14738
14741
  const rootCol = relation.pivotTable.columns[relation.pivotForeignKeyToRoot];
@@ -14745,14 +14748,13 @@ var RelationChangeProcessor = class {
14745
14748
  await this.executor.executeSql(compiled.sql, compiled.params);
14746
14749
  }
14747
14750
  /**
14748
- * Resolves the primary key value from an entity.
14749
- * @param entity - The entity
14750
- * @param table - The table definition
14751
- * @returns The primary key value or null
14751
+ * Resolves the target-side key value used by a belongs-to-many relation.
14752
+ * The declared targetKey is part of the relation contract and takes
14753
+ * precedence over the target table primary key.
14752
14754
  */
14753
- resolvePrimaryKeyValue(entity, table) {
14755
+ resolveBelongsToManyTargetValue(entity, relation) {
14754
14756
  if (!entity) return null;
14755
- const key = findPrimaryKey(table);
14757
+ const key = relation.targetKey || findPrimaryKey(relation.target);
14756
14758
  const value = entity[key];
14757
14759
  if (value === void 0 || value === null) return null;
14758
14760
  return value ?? null;