Skip to content

Instantly share code, notes, and snippets.

@Ozerich
Created June 8, 2023 11:41
Show Gist options
  • Save Ozerich/3b2a570668768bd899617d7814f01567 to your computer and use it in GitHub Desktop.
Save Ozerich/3b2a570668768bd899617d7814f01567 to your computer and use it in GitHub Desktop.
async findOneGameByUrlAlias(urlAlias: string): Promise<Game | null> {
const relations = [
"gamePlatforms",
"gamePlatforms.developer",
"gamePlatforms.developer.icon",
"gamePlatforms.developer.cover",
"reviews",
"reviews.user",
"gameSimilar",
"gameSimilar.gameSimilar",
"gameSimilar.gameSimilar.gamePlatforms",
"gameSimilar.gameSimilar.gamePlatforms.developer",
"gameTags",
"gameTags.tag",
"gameCategories",
"gameCategories.category",
"videoStreams"
];
const [someBaseEntity, someBaseEntityWithScreenshots] = await Promise.all([
this.gameRepository.findOne({
where: {
url_alias: urlAlias
}
}),
this.gameRepository.findOne({
where: {
url_alias: urlAlias
},
relations: ["gamePlatforms", "gamePlatforms.screenshots"]
})
]);
if (someBaseEntity) {
const someBaseEntityRelationObjects = await Promise.all(
relations.map(relation => {
return this.gameRepository.findOne({
where: {
url_alias: urlAlias
},
relations: [relation]
});
})
);
for (const relationObj of someBaseEntityRelationObjects) {
const { id, ...rest } = relationObj;
const key = Object.keys(rest)[Object.keys(rest).length - 1];
const val = rest[Object.keys(rest)[Object.keys(rest).length - 1]];
Object.assign(someBaseEntity, { [key]: val });
}
}
if (someBaseEntityWithScreenshots) {
someBaseEntityWithScreenshots.gamePlatforms.forEach((gamePlatform, ind) => {
someBaseEntity.gamePlatforms[ind].screenshots = gamePlatform.screenshots;
});
}
return someBaseEntity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment