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,48 +2701,43 @@ 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 } }
|
||||||
}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: ChildRelation,
|
|
||||||
as: 'childrenMother',
|
|
||||||
include: [{
|
|
||||||
model: FalukantCharacter,
|
|
||||||
as: 'child',
|
|
||||||
include: [{ model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }]
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
});
|
},
|
||||||
const children = [];
|
attributes: ['childCharacterId', 'nameSet', 'isHeir', 'createdAt']
|
||||||
for (const parentChar of charsWithChildren) {
|
})
|
||||||
const allRels = [
|
: [];
|
||||||
...(parentChar.childrenFather || []),
|
const childCharIds = [...new Set(childRels.map(r => r.childCharacterId))];
|
||||||
...(parentChar.childrenMother || [])
|
const childChars = childCharIds.length
|
||||||
];
|
? await FalukantCharacter.findAll({
|
||||||
for (const rel of allRels) {
|
where: { id: childCharIds },
|
||||||
const kid = rel.child;
|
attributes: ['id', 'birthdate', 'gender'],
|
||||||
children.push({
|
include: [{ model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }]
|
||||||
childCharacterId: kid.id,
|
})
|
||||||
name: kid.definedFirstName?.name || 'Unknown',
|
: [];
|
||||||
gender: kid.gender,
|
const childCharMap = Object.fromEntries(childChars.map(c => [c.id, c]));
|
||||||
age: calcAge(kid.birthdate),
|
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,
|
hasName: rel.nameSet,
|
||||||
isHeir: rel.isHeir || false,
|
isHeir: rel.isHeir || false,
|
||||||
_createdAt: rel.createdAt,
|
_createdAt: rel.createdAt,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
// Sort children globally by relation createdAt ascending (older first)
|
// Sort children globally by relation createdAt ascending (older first)
|
||||||
children.sort((a, b) => new Date(a._createdAt) - new Date(b._createdAt));
|
children.sort((a, b) => new Date(a._createdAt) - new Date(b._createdAt));
|
||||||
const inProgress = ['wooing', 'engaged', 'married'];
|
const inProgress = ['wooing', 'engaged', 'married'];
|
||||||
@@ -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