Backend: Optimized entry credits creation on create and refrech

This commit is contained in:
X4Lo committed 2025-07-12 00:11:59 +01:00
1 parent f1cd3f9167
commit 1224d48c5c
2 files changed
+90 -136

No files matched your search

+46 -73
View File
@@ -255,90 +255,63 @@ class EntryManager {
}
// * Credits
if (!oldEntry.cast_fetched || oldEntry.credits.length == 0) {
if (!oldEntry.cast_fetched || oldEntry.credits.length === 0) {
const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
if (!creditData) {
throw new InternalServerError(`Couldn't fetch entry crew data`);
}
// ? loop creditData.crew and creditData.cast and create a people for each if missing
// ? then create a cast/crew for each people if missing
for (const cast of creditData.cast) {
// ? check if the cast already exists
let existingPeople = await People.findOne({
where: { tmdb_id: cast.id }
const allCredits = [...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' }))];
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
const existingPeopleList = await People.findAll({ where: { tmdb_id: tmdbIds } });
const existingPeopleMap = Object.fromEntries(existingPeopleList.map(p => [p.tmdb_id, p]));
const newPeopleData = tmdbIds
.filter(id => !existingPeopleMap[id])
.map(id => {
const credit = allCredits.find(c => c.id === id);
return {
tmdb_id: credit.id,
name: credit.name,
gender: credit.gender || null,
popularity: credit.popularity,
known_for_department: credit.known_for_department || null,
profile_path: credit.profile_path,
adult: credit.adult || false,
last_fetch: new Date()
};
});
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: cast.id,
name: cast.name,
gender: cast.gender || null,
popularity: cast.popularity,
known_for_department: cast.known_for_department || null,
profile_path: cast.profile_path,
adult: cast.adult || false,
last_fetch: new Date()
});
}
// ? check if the cast already exists for this entry
let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id);
// ? if not, create a new cast
if (!existingCast) {
await Credit.create({
entry_id: oldEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: cast.credit_id,
character: cast.character,
order: cast.order,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'cast'
});
}
const createdPeople = await People.bulkCreate(newPeopleData);
for (const person of createdPeople) {
existingPeopleMap[person.tmdb_id] = person;
}
for (const crew of creditData.crew) {
// ? check if the crew already exists
let existingPeople = await People.findOne({
where: { tmdb_id: crew.id }
});
const existingPeopleIds = new Set(oldEntry.credits.map(c => c.people_id));
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: crew.id,
name: crew.name,
gender: crew.gender || null,
popularity: crew.popularity,
known_for_department: crew.known_for_department || null,
profile_path: crew.profile_path,
adult: crew.adult || false,
last_fetch: new Date()
});
}
// ? check if the crew already exists for this entry
let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id);
// ? if not, create a new cast
if (!existingCast) {
await Credit.create({
const newCredits = allCredits
.map(c => {
const person = existingPeopleMap[c.id];
if (existingPeopleIds.has(person.id)) return null;
return {
entry_id: oldEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: crew.credit_id,
character: null,
order: null,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'crew',
department: crew.department || null,
job: crew.job || null
});
}
people_id: person.id,
tmdb_credit_id: c.credit_id,
character: c.cast_type === 'cast' ? c.character : null,
order: c.cast_type === 'cast' ? c.order : null,
profile_path: person.profile_path,
adult: person.adult,
cast_type: c.cast_type,
department: c.cast_type === 'crew' ? c.department || null : null,
job: c.cast_type === 'crew' ? c.job || null : null
};
})
.filter(Boolean);
if (newCredits.length) {
await Credit.bulkCreate(newCredits);
}
}
+44 -63
View File
@@ -248,75 +248,56 @@ class EntryService {
throw new InternalServerError(`Couldn't fetch entry credits data`);
}
// ? loop creditData.crew and creditData.cast and create a people for each if missing
// ? then create a cast/crew for each people if missing
for (const cast of creditData.cast) {
// ? check if the cast already exists
let existingPeople = await People.findOne({
where: { tmdb_id: cast.id }
});
const allCredits = [
...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' })),
];
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: cast.id,
name: cast.name,
gender: cast.gender || null,
popularity: cast.popularity,
known_for_department: cast.known_for_department || null,
profile_path: cast.profile_path,
adult: cast.adult || false,
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
const existingPeopleList = await People.findAll({
where: { tmdb_id: tmdbIds },
});
const existingPeopleMap = Object.fromEntries(existingPeopleList.map(p => [p.tmdb_id, p]));
const newPeopleData = tmdbIds
.filter(id => !existingPeopleMap[id])
.map(id => {
const credit = allCredits.find(c => c.id === id);
return {
tmdb_id: credit.id,
name: credit.name,
gender: credit.gender || null,
popularity: credit.popularity,
known_for_department: credit.known_for_department || null,
profile_path: credit.profile_path,
adult: credit.adult || false,
last_fetch: new Date()
});
}
// ? create a new cast
await Credit.create({
entry_id: savedEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: cast.credit_id,
character: cast.character,
order: cast.order,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'cast'
};
});
const createdPeople = await People.bulkCreate(newPeopleData);
for (const person of createdPeople) {
existingPeopleMap[person.tmdb_id] = person;
}
for (const crew of creditData.crew) {
// ? check if the crew already exists
let existingPeople = await People.findOne({
where: { tmdb_id: crew.id }
});
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: crew.id,
name: crew.name,
gender: crew.gender,
popularity: crew.popularity,
known_for_department: crew.known_for_department || null,
profile_path: crew.profile_path,
adult: crew.adult || false,
last_fetch: new Date()
});
}
// ? create a new cast
await Credit.create({
const newCredits = allCredits.map(c => {
const person = existingPeopleMap[c.id];
return {
entry_id: savedEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: crew.credit_id,
character: null,
order: null,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'crew',
department: crew.department || null,
job: crew.job || null
});
}
people_id: person.id,
tmdb_credit_id: c.credit_id,
character: c.cast_type === 'cast' ? c.character : null,
order: c.cast_type === 'cast' ? c.order : null,
profile_path: person.profile_path || null,
adult: person.adult || false,
cast_type: c.cast_type,
department: c.cast_type === 'crew' ? c.department || null : null,
job: c.cast_type === 'crew' ? c.job || null : null,
};
});
await Credit.bulkCreate(newCredits);
savedEntry.credit_fetched = true;
await savedEntry.save();