Skip to content

Commit

Permalink
use getAssociationKey in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
nils committed Sep 17, 2024
1 parent a20aeb4 commit b217bc2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/change-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
const {
getKey,
flattenKey,
getAssociationKey,
} = require("./keys")
const { localizeLogFields } = require("./localization")
const isRoot = "change-tracking-isRootEntity"
Expand Down Expand Up @@ -374,7 +375,7 @@ const _prepareChangeLogForComposition = async function (entity, entityKey, chang

async function generatePathAndParams (req, entityKey) {
const { target, data } = req;
const { ID, foreignKey, parentEntity } = getAssociationDetails(target);
const { foreignKey, parentEntity, assoc } = getAssociationDetails(target);
const hasParentAndForeignKey = parentEntity && data[foreignKey];
const targetEntity = hasParentAndForeignKey ? parentEntity : target;
const targetKey = hasParentAndForeignKey ? {ID: data[foreignKey]} : entityKey;
Expand All @@ -384,7 +385,7 @@ async function generatePathAndParams (req, entityKey) {
? [{target: parentEntity.name}, {target: target.name}]
: [{target: target.name}],
params: hasParentAndForeignKey
? [{ [ID]: data[foreignKey] }, entityKey]
? [ getAssociationKey(assoc, data), entityKey]
: [ entityKey],
hasComp: true
};
Expand All @@ -405,17 +406,16 @@ async function generatePathAndParams (req, entityKey) {
}

async function processEntity (entity, entityKey, compContext) {
const { ID, foreignKey, parentEntity } = getAssociationDetails(entity);
const { foreignKey, parentEntity, assoc } = getAssociationDetails(entity);

if (foreignKey && parentEntity) {
const parentResult =
(await SELECT.one
.from(entity.name)
.where(entityKey)
.columns(foreignKey)) || {};
const hasForeignKey = parentResult[foreignKey];
if (!hasForeignKey) return;
const key = { [ID]: parentResult[foreignKey] };
const key = getAssociationKey(assoc, parentResult)
if (!key) return;
compContext.path = [{target: parentEntity.name, key}, ...compContext.path];
compContext.params.unshift(key);
return {
Expand All @@ -431,8 +431,7 @@ function getAssociationDetails (entity) {
const assoc = entity.elements[assocName];
const parentEntity = assoc?._target;
const foreignKey = assoc?.keys?.[0]?.$generatedFieldName;
const ID = assoc?.keys?.[0]?.ref[0] || 'ID';
return { ID, foreignKey, parentEntity };
return { foreignKey, parentEntity, assoc };
}

async function track_changes (req) {
Expand Down

0 comments on commit b217bc2

Please sign in to comment.