I want to compute directly in the entities :
- the globalRating of an user based on the rating where he’s tenant of the associated contract
- the globalRating of a property based on the ratings where it’s the property associated to the contract
Something like this with queryBuilder
<code>const userGlobalRating = await this.userRepo.createQueryBuilder(User, 'user')
.leftJoin('user.tenantships', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('user.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('user.id');
const propertyGlobalRAting = await this.propertyRepo.createQueryBuilder(Property, 'property')
.leftJoin('property.contracts', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('property.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('property.id');
</code>
<code>const userGlobalRating = await this.userRepo.createQueryBuilder(User, 'user')
.leftJoin('user.tenantships', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('user.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('user.id');
const propertyGlobalRAting = await this.propertyRepo.createQueryBuilder(Property, 'property')
.leftJoin('property.contracts', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('property.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('property.id');
</code>
const userGlobalRating = await this.userRepo.createQueryBuilder(User, 'user')
.leftJoin('user.tenantships', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('user.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('user.id');
const propertyGlobalRAting = await this.propertyRepo.createQueryBuilder(Property, 'property')
.leftJoin('property.contracts', 'contract')
.leftJoin('contract.ratings', 'rating')
.select('property.id', 'id')
.addSelect('COALESCE(AVG(rating.stars), 0)', 'rating')
.groupBy('property.id');
Here the entities :
<code>@Entity()
export class Rating {
@ManyToOne(() => Contract, c => c.ratings)
@JoinColumn()
@Index()
contract!: Contract;
}
</code>
<code>@Entity()
export class Rating {
@ManyToOne(() => Contract, c => c.ratings)
@JoinColumn()
@Index()
contract!: Contract;
}
</code>
@Entity()
export class Rating {
@ManyToOne(() => Contract, c => c.ratings)
@JoinColumn()
@Index()
contract!: Contract;
}
<code>@Entity()
export class Contract {
@OneToMany(() => Rating, rating => rating.contract)
ratings!: Rating[];
@ManyToOne(() => Property, property => property.contracts)
property!: Property | undefined;
@ManyToOne(() => User, user => user.tenantships)
tenant?: User | undefined;
}
</code>
<code>@Entity()
export class Contract {
@OneToMany(() => Rating, rating => rating.contract)
ratings!: Rating[];
@ManyToOne(() => Property, property => property.contracts)
property!: Property | undefined;
@ManyToOne(() => User, user => user.tenantships)
tenant?: User | undefined;
}
</code>
@Entity()
export class Contract {
@OneToMany(() => Rating, rating => rating.contract)
ratings!: Rating[];
@ManyToOne(() => Property, property => property.contracts)
property!: Property | undefined;
@ManyToOne(() => User, user => user.tenantships)
tenant?: User | undefined;
}
<code>@Entity()
export class Property{
@OneToMany(() => Contract, contract => contract.property)
contracts!: Contract[];
@ManyToOne(() => User, user => user.properties)
@JoinColumn()
@Index()
owner!: User | undefined;
globalRating!:number
}
</code>
<code>@Entity()
export class Property{
@OneToMany(() => Contract, contract => contract.property)
contracts!: Contract[];
@ManyToOne(() => User, user => user.properties)
@JoinColumn()
@Index()
owner!: User | undefined;
globalRating!:number
}
</code>
@Entity()
export class Property{
@OneToMany(() => Contract, contract => contract.property)
contracts!: Contract[];
@ManyToOne(() => User, user => user.properties)
@JoinColumn()
@Index()
owner!: User | undefined;
globalRating!:number
}
<code>@Entity()
export class User {
@OneToMany(() => Property, property => property.owner)
properties: Property[] | undefined;
@OneToMany(() => Contract, contract => contract.tenant)
tenantships: Contract[];
globalRating!: number;
}
</code>
<code>@Entity()
export class User {
@OneToMany(() => Property, property => property.owner)
properties: Property[] | undefined;
@OneToMany(() => Contract, contract => contract.tenant)
tenantships: Contract[];
globalRating!: number;
}
</code>
@Entity()
export class User {
@OneToMany(() => Property, property => property.owner)
properties: Property[] | undefined;
@OneToMany(() => Contract, contract => contract.tenant)
tenantships: Contract[];
globalRating!: number;
}
So is that possible using postgres ?
Is that possible without injecting the repository and just add raw sql ?