23 lines
		
	
	
		
			923 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			23 lines
		
	
	
		
			923 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { CommentEntity, PostEntity } from '@/modules/content/entities';
 | 
						|
import { BaseRepository } from '@/modules/database/base';
 | 
						|
import { CustomRepository } from '@/modules/database/decorators';
 | 
						|
 | 
						|
@CustomRepository(PostEntity)
 | 
						|
export class PostRepository extends BaseRepository<PostEntity> {
 | 
						|
    protected _qbName = 'post';
 | 
						|
 | 
						|
    buildBaseQB() {
 | 
						|
        // 在查询之前先查询出评论数量在添加到commentCount字段上
 | 
						|
        return this.createQueryBuilder('post')
 | 
						|
            .leftJoinAndSelect('post.category', 'category')
 | 
						|
            .leftJoinAndSelect('post.tags', 'tags')
 | 
						|
            .addSelect((subQuery) => {
 | 
						|
                return subQuery
 | 
						|
                    .select('COUNT(c.id)', 'count')
 | 
						|
                    .from(CommentEntity, 'c')
 | 
						|
                    .where('c.post.id = post.id');
 | 
						|
            }, 'commentCount')
 | 
						|
            .loadRelationCountAndMap('post.commentCount', 'post.comments');
 | 
						|
    }
 | 
						|
}
 |