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)
@Controller('message')
export class MessageController extends BaseController<
Message,
AnyMessage,
MessageStub,
MessagePopulate,
MessageFull

View File

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

View File

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