Created
June 8, 2023 11:41
-
-
Save Ozerich/54d20b4a8127630a47a047be43825d74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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