add base repository
This commit is contained in:
parent
823046e94a
commit
68b06801d3
@ -1,21 +1,22 @@
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { CommentEntity } from '@/modules/content/entities/comment.entity';
|
||||
import { PostEntity } from '@/modules/content/entities/post.entity';
|
||||
import { BaseRepository } from '@/modules/database/base/repository';
|
||||
import { CustomRepository } from '@/modules/database/decorators/repository.decorator';
|
||||
|
||||
@CustomRepository(PostEntity)
|
||||
export class PostRepository extends Repository<PostEntity> {
|
||||
export class PostRepository extends BaseRepository<PostEntity> {
|
||||
protected _qbName = 'post';
|
||||
|
||||
buildBaseQB() {
|
||||
return this.createQueryBuilder('post')
|
||||
.leftJoinAndSelect('post.category', 'category')
|
||||
.leftJoinAndSelect('post.tags', 'tags')
|
||||
return this.createQueryBuilder(this.qbName)
|
||||
.leftJoinAndSelect(`${this.qbName}.category`, 'category')
|
||||
.leftJoinAndSelect(`${this.qbName}.tags`, 'tags')
|
||||
.addSelect((query) => {
|
||||
return query
|
||||
.select('COUNT(c.id)', 'count')
|
||||
.from(CommentEntity, 'c')
|
||||
.where('c.post.id = post.id');
|
||||
.where(`c.post.id = ${this.qbName}.id`);
|
||||
}, 'commentCount')
|
||||
.loadRelationCountAndMap('post.commentCOunt', 'post.comments');
|
||||
.loadRelationCountAndMap(`${this.qbName}.commentCOunt`, `${this.qbName}.comments`);
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,21 @@
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { PostEntity } from '@/modules/content/entities/post.entity';
|
||||
import { TagEntity } from '@/modules/content/entities/tag.entity';
|
||||
import { CustomRepository } from '@/modules/database/decorators/repository.decorator';
|
||||
|
||||
import { BaseRepository } from '../../database/base/repository';
|
||||
|
||||
@CustomRepository(TagEntity)
|
||||
export class TagRepository extends Repository<TagEntity> {
|
||||
export class TagRepository extends BaseRepository<TagEntity> {
|
||||
protected _qbName = 'tag';
|
||||
|
||||
buildBaseQB() {
|
||||
return this.createQueryBuilder('tag')
|
||||
.leftJoinAndSelect('tag.posts', 'posts')
|
||||
return this.createQueryBuilder(this.qbName)
|
||||
.leftJoinAndSelect(`${this.qbName}.posts`, 'posts')
|
||||
.addSelect(
|
||||
(query) => query.select('COUNT(p.id)', 'count').from(PostEntity, 'p'),
|
||||
'postCount',
|
||||
)
|
||||
.orderBy('postCount', 'DESC')
|
||||
.loadRelationCountAndMap('tag.postCount', 'tag.posts');
|
||||
.loadRelationCountAndMap(`${this.qbName}.postCount`, `${this.qbName}.posts`);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { isArray, isNil } from 'lodash';
|
||||
import { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
import { PaginateOptions, PaginateReturn } from '@/modules/database/types';
|
||||
import { OrderQueryType, PaginateOptions, PaginateReturn } from '@/modules/database/types';
|
||||
|
||||
export const paginate = async <T extends ObjectLiteral>(
|
||||
qb: SelectQueryBuilder<T>,
|
||||
|
Loading…
Reference in New Issue
Block a user