fix: add retro compatibility for find and findAndPopulate methods

This commit is contained in:
yassinedorbozgithub 2024-12-06 16:26:54 +01:00
parent 62e59069dd
commit d3b5070407
3 changed files with 23 additions and 12 deletions

View File

@ -55,7 +55,7 @@ import { SubscriberService } from '../services/subscriber.service';
@UseInterceptors(CsrfInterceptor) @UseInterceptors(CsrfInterceptor)
@Controller('message') @Controller('message')
export class MessageController extends BaseController< export class MessageController extends BaseController<
Message, AnyMessage,
MessageStub, MessageStub,
MessagePopulate, MessagePopulate,
MessageFull MessageFull

View File

@ -257,20 +257,28 @@ export abstract class BaseRepository<
protected findQuery( protected findQuery(
filter: TFilterQuery<T>, filter: TFilterQuery<T>,
pageQuery?: PageQueryDto<T>, // TODO: QuerySortDto<T> type need to be removed
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
) { ) {
const { skip = 0, limit, sort = ['createdAt', 'asc'] } = pageQuery || {}; // TODO: current block need to be removed
const query = this.model.find<T>(filter, projection); if (Array.isArray(pageQuery)) {
return query const query = this.model.find<T>(filter, projection);
.skip(skip) return query.sort([pageQuery] as [string, SortOrder][]);
.limit(limit) } else {
.sort([sort] as [string, SortOrder][]); const { skip = 0, limit, sort = ['createdAt', 'asc'] } = pageQuery || {};
const query = this.model.find<T>(filter, projection);
return query
.skip(skip)
.limit(limit)
.sort([sort] as [string, SortOrder][]);
}
} }
async find( async find(
filter: TFilterQuery<T>, filter: TFilterQuery<T>,
pageQuery?: PageQueryDto<T>, // TODO: QuerySortDto<T> type need to be removed
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
) { ) {
const query = this.findQuery(filter, pageQuery, projection); const query = this.findQuery(filter, pageQuery, projection);
@ -285,7 +293,8 @@ export abstract class BaseRepository<
async findAndPopulate( async findAndPopulate(
filters: TFilterQuery<T>, filters: TFilterQuery<T>,
pageQuery?: PageQueryDto<T>, // TODO: QuerySortDto<T> need to be removed
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
) { ) {
this.ensureCanPopulate(); this.ensureCanPopulate();

View File

@ -46,7 +46,8 @@ export abstract class BaseService<
async find( async find(
filter: TFilterQuery<T>, filter: TFilterQuery<T>,
pageQuery?: PageQueryDto<T>, // TODO: QuerySortDto<T> type need to be removed
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
): Promise<T[]> { ): Promise<T[]> {
return await this.repository.find(filter, pageQuery, projection); return await this.repository.find(filter, pageQuery, projection);
@ -54,7 +55,8 @@ export abstract class BaseService<
async findAndPopulate( async findAndPopulate(
filters: TFilterQuery<T>, filters: TFilterQuery<T>,
pageQuery?: PageQueryDto<T>, // TODO: QuerySortDto<T> type need to be removed
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
) { ) {
return await this.repository.findAndPopulate( return await this.repository.findAndPopulate(