My goal is to implement a 'rating' field in my User Entity. Within the User Entity, there exists a relationship with the Rating Entity, where the User has a field called ratingsReceived that eagerly loads all Ratings assigned to that User.
The 'rating' field on the User Entity should be a mean calculation of all rating values from the Rating Entity, specifically the 'ratingValue' field.
To achieve this, the calculation for every User's 'rating' field should be as follows:
ratingsReceived.reduce((acc, curr) => acc + curr.ratingValue, 0) / ratingsReceived.length
The key fields involved here are 'ratingsReceived' on User:
@OneToMany(
() => Rating,
rating => rating.ratingTo
)
ratingsReceived: Rating[];
And 'ratingValue' on Rating:
@Column('decimal')
@Min(0)
@Max(5)
ratingValue: number;