Skip to content

Instantly share code, notes, and snippets.

@Ozerich
Created June 8, 2023 11:41
Show Gist options
  • Save Ozerich/54d20b4a8127630a47a047be43825d74 to your computer and use it in GitHub Desktop.
Save Ozerich/54d20b4a8127630a47a047be43825d74 to your computer and use it in GitHub Desktop.
async loadRelationshipsForEntity(entity: BaseEntity, repo: Repository<any>, relationships: Array<string | [string, string[]]>) {
// @ts-ignore
const id = entity.id;
const { ...res } = await Promise.all(
relationships.map((relation) => {
// @ts-ignore
const relationsArray: string[] = Array.isArray(relation) ? [relation[0], ...relation[1].map(item => relation[0] + "." + item)] : [relation];
return repo.findOne(
{
where: { id },
select: ["id"],
relations: relationsArray
}
);
})
);
relationships.forEach((relation, i) => {
const param = String(Array.isArray(relation) ? relation[0] : relation);
entity[param] = res[i][param];
});
}
async findOneGameById(id: number, platform?: Platform): Promise<Game | null> {
const relationsExtended: Array<string | [string, string[]]> = [
"gameStats",
["gamePlatforms", ["videos", "screenshots", "developer"]],
["gameTags", ["tag"]],
["gameCategories", ["category"]],
"videoStreams"
];
const game = await this.gameRepository.findOne({
where: platform ? this.addPlatformToWhere(platform, { id }) : { id }
});
if (!game) return null;
await this.loadRelationshipsForEntity(game, this.gameRepository, relationsExtended);
return game;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment