diff --git a/api/src/utils/generics/base-repository.ts b/api/src/utils/generics/base-repository.ts index 00203f99..e2bc6f68 100644 --- a/api/src/utils/generics/base-repository.ts +++ b/api/src/utils/generics/base-repository.ts @@ -17,6 +17,7 @@ import { FlattenMaps, HydratedDocument, Model, + ProjectionType, Query, SortOrder, UpdateQuery, @@ -215,47 +216,64 @@ export abstract class BaseRepository< return plainToClass(cls, doc, options ?? this.transformOpts); } - protected findOneQuery(criteria: string | TFilterQuery) { + protected findOneQuery( + criteria: string | TFilterQuery, + projection?: ProjectionType, + ) { if (!criteria) { // An empty criteria would return the first document that it finds throw new Error('findOneQuery() should not have an empty criteria'); } return typeof criteria === 'string' - ? this.model.findById(criteria) - : this.model.findOne(criteria); + ? this.model.findById(criteria, projection) + : this.model.findOne(criteria, projection); } async findOne( criteria: string | TFilterQuery, options?: ClassTransformOptions, + projection?: ProjectionType, ) { if (!criteria) { // @TODO : Issue a warning ? return Promise.resolve(undefined); } - const query = this.findOneQuery(criteria); + const query = this.findOneQuery(criteria, projection); return await this.executeOne(query, this.cls, options); } - async findOneAndPopulate(criteria: string | TFilterQuery) { + async findOneAndPopulate( + criteria: string | TFilterQuery, + projection?: ProjectionType, + ) { this.ensureCanPopulate(); - const query = this.findOneQuery(criteria).populate(this.populate); + const query = this.findOneQuery(criteria, projection).populate( + this.populate, + ); return await this.executeOne(query, this.clsPopulate); } - protected findQuery(filter: TFilterQuery, pageQuery?: PageQueryDto) { + protected findQuery( + filter: TFilterQuery, + pageQuery?: PageQueryDto, + projection?: ProjectionType, + ) { const { skip = 0, limit, sort = ['createdAt', 'asc'] } = pageQuery || {}; - const query = this.model.find(filter); + const query = this.model.find(filter, projection); return query .skip(skip) .limit(limit) .sort([sort] as [string, SortOrder][]); } - async find(filter: TFilterQuery, pageQuery?: PageQueryDto) { - const query = this.findQuery(filter, pageQuery); + async find( + filter: TFilterQuery, + pageQuery?: PageQueryDto, + projection?: ProjectionType, + ) { + const query = this.findQuery(filter, pageQuery, projection); return await this.execute(query, this.cls); } @@ -265,9 +283,15 @@ export abstract class BaseRepository< } } - async findAndPopulate(filters: TFilterQuery, pageQuery?: PageQueryDto) { + async findAndPopulate( + filters: TFilterQuery, + pageQuery?: PageQueryDto, + projection?: ProjectionType, + ) { this.ensureCanPopulate(); - const query = this.findQuery(filters, pageQuery).populate(this.populate); + const query = this.findQuery(filters, pageQuery, projection).populate( + this.populate, + ); return await this.execute(query, this.clsPopulate); } diff --git a/api/src/utils/generics/base-service.ts b/api/src/utils/generics/base-service.ts index b924f0af..dd2b6d9b 100644 --- a/api/src/utils/generics/base-service.ts +++ b/api/src/utils/generics/base-service.ts @@ -9,6 +9,7 @@ import { ConflictException } from '@nestjs/common'; import { ClassTransformOptions } from 'class-transformer'; import { MongoError } from 'mongodb'; +import { ProjectionType } from 'mongoose'; import { TFilterQuery } from '@/utils/types/filter.types'; @@ -31,23 +32,36 @@ export abstract class BaseService< async findOne( criteria: string | TFilterQuery, options?: ClassTransformOptions, + projection?: ProjectionType, ): Promise { - return await this.repository.findOne(criteria, options); + return await this.repository.findOne(criteria, options, projection); } - async findOneAndPopulate(criteria: string | TFilterQuery) { - return await this.repository.findOneAndPopulate(criteria); + async findOneAndPopulate( + criteria: string | TFilterQuery, + projection?: ProjectionType, + ) { + return await this.repository.findOneAndPopulate(criteria, projection); } async find( filter: TFilterQuery, pageQuery?: PageQueryDto, + projection?: ProjectionType, ): Promise { - return await this.repository.find(filter, pageQuery); + return await this.repository.find(filter, pageQuery, projection); } - async findAndPopulate(filters: TFilterQuery, pageQuery?: PageQueryDto) { - return await this.repository.findAndPopulate(filters, pageQuery); + async findAndPopulate( + filters: TFilterQuery, + pageQuery?: PageQueryDto, + projection?: ProjectionType, + ) { + return await this.repository.findAndPopulate( + filters, + pageQuery, + projection, + ); } async findAll(sort?: QuerySortDto): Promise { @@ -58,20 +72,6 @@ export abstract class BaseService< return await this.repository.findAllAndPopulate(sort); } - async findPage( - filters: TFilterQuery, - pageQueryDto: PageQueryDto, - ): Promise { - return await this.repository.findPage(filters, pageQueryDto); - } - - async findPageAndPopulate( - filters: TFilterQuery, - pageQueryDto: PageQueryDto, - ): Promise { - return await this.repository.findPageAndPopulate(filters, pageQueryDto); - } - async countAll(): Promise { return await this.repository.countAll(); }