Refactor child character loading in FalukantService: Update logic to load child relations using separate queries, improving performance and preventing EagerLoadingError. Simplify data retrieval by eliminating unnecessary eager loading and enhancing clarity in the code structure.
This commit is contained in:
@@ -2701,50 +2701,45 @@ class FalukantService extends BaseService {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const charsWithChildren = await FalukantCharacter.findAll({
|
// Load child relations without FalukantCharacter includes to avoid EagerLoadingError
|
||||||
|
const userCharacterIds = (await FalukantCharacter.findAll({
|
||||||
where: { userId: user.id },
|
where: { userId: user.id },
|
||||||
include: [
|
attributes: ['id']
|
||||||
{
|
})).map(c => c.id);
|
||||||
model: ChildRelation,
|
const childRels = userCharacterIds.length
|
||||||
as: 'childrenFather',
|
? await ChildRelation.findAll({
|
||||||
include: [{
|
where: {
|
||||||
model: FalukantCharacter,
|
[Op.or]: [
|
||||||
as: 'child',
|
{ fatherCharacterId: { [Op.in]: userCharacterIds } },
|
||||||
include: [{ model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }]
|
{ motherCharacterId: { [Op.in]: userCharacterIds } }
|
||||||
}]
|
]
|
||||||
},
|
},
|
||||||
{
|
attributes: ['childCharacterId', 'nameSet', 'isHeir', 'createdAt']
|
||||||
model: ChildRelation,
|
})
|
||||||
as: 'childrenMother',
|
: [];
|
||||||
include: [{
|
const childCharIds = [...new Set(childRels.map(r => r.childCharacterId))];
|
||||||
model: FalukantCharacter,
|
const childChars = childCharIds.length
|
||||||
as: 'child',
|
? await FalukantCharacter.findAll({
|
||||||
include: [{ model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }]
|
where: { id: childCharIds },
|
||||||
}]
|
attributes: ['id', 'birthdate', 'gender'],
|
||||||
}
|
include: [{ model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }]
|
||||||
]
|
})
|
||||||
|
: [];
|
||||||
|
const childCharMap = Object.fromEntries(childChars.map(c => [c.id, c]));
|
||||||
|
const children = childRels.map(rel => {
|
||||||
|
const kid = childCharMap[rel.childCharacterId];
|
||||||
|
return {
|
||||||
|
childCharacterId: rel.childCharacterId,
|
||||||
|
name: kid?.definedFirstName?.name || 'Unknown',
|
||||||
|
gender: kid?.gender,
|
||||||
|
age: kid?.birthdate ? calcAge(kid.birthdate) : null,
|
||||||
|
hasName: rel.nameSet,
|
||||||
|
isHeir: rel.isHeir || false,
|
||||||
|
_createdAt: rel.createdAt,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
const children = [];
|
// Sort children globally by relation createdAt ascending (older first)
|
||||||
for (const parentChar of charsWithChildren) {
|
children.sort((a, b) => new Date(a._createdAt) - new Date(b._createdAt));
|
||||||
const allRels = [
|
|
||||||
...(parentChar.childrenFather || []),
|
|
||||||
...(parentChar.childrenMother || [])
|
|
||||||
];
|
|
||||||
for (const rel of allRels) {
|
|
||||||
const kid = rel.child;
|
|
||||||
children.push({
|
|
||||||
childCharacterId: kid.id,
|
|
||||||
name: kid.definedFirstName?.name || 'Unknown',
|
|
||||||
gender: kid.gender,
|
|
||||||
age: calcAge(kid.birthdate),
|
|
||||||
hasName: rel.nameSet,
|
|
||||||
isHeir: rel.isHeir || false,
|
|
||||||
_createdAt: rel.createdAt,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Sort children globally by relation createdAt ascending (older first)
|
|
||||||
children.sort((a, b) => new Date(a._createdAt) - new Date(b._createdAt));
|
|
||||||
const inProgress = ['wooing', 'engaged', 'married'];
|
const inProgress = ['wooing', 'engaged', 'married'];
|
||||||
const family = {
|
const family = {
|
||||||
relationships: relationships.filter(r => inProgress.includes(r.relationshipType)),
|
relationships: relationships.filter(r => inProgress.includes(r.relationshipType)),
|
||||||
@@ -2968,7 +2963,7 @@ class FalukantService extends BaseService {
|
|||||||
},
|
},
|
||||||
attributes: ['character1Id', 'character2Id']
|
attributes: ['character1Id', 'character2Id']
|
||||||
});
|
});
|
||||||
if (!rel) throw new Error('Beziehung nicht gefunden');
|
if (!rel) return [];
|
||||||
|
|
||||||
const relatedCharId = rel.character1Id === myChar.id ? rel.character2Id : rel.character1Id;
|
const relatedCharId = rel.character1Id === myChar.id ? rel.character2Id : rel.character1Id;
|
||||||
const relatedChar = await FalukantCharacter.findOne({
|
const relatedChar = await FalukantCharacter.findOne({
|
||||||
|
|||||||
Reference in New Issue
Block a user