98
rated 0 times
[
101]
[
3]
/ answers: 1 / hits: 6211
/ 4 Years ago, mon, march 30, 2020, 12:00:00
I want to make a 'rating' field on my User Entity.
The User Entity has a relationship to the Rating Entity, on User there is a field called ratingsReceived, which is an eager load of all Ratings assigned to that User.
I want the 'rating' field on User to be a mean calculation of all rating values which is a field on Rating Entity called 'ratingValue'.
So essentially I want this calculation to be the value of every User 'rating' field:
ratingsReceived.reduce((acc, curr) => acc + curr.ratingValue, 0) / ratingsReceived.length
The fields in question are 'ratingsReceived' on User:
@OneToMany(
() => Rating,
rating => rating.ratingTo
)
ratingsReceived: Rating[];
And 'ratingValue' on Rating:
@Column('decimal')
@Min(0)
@Max(5)
ratingValue: number;
More From » sql