From d9a87e99bfbb9a9dc6e9d0408c8df76e424e4c67 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 6 Dec 2024 18:34:18 +0100 Subject: [PATCH] fix: base-repository.ts methods returning types --- api/src/utils/generics/base-repository.ts | 57 ++++++++++++----------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/api/src/utils/generics/base-repository.ts b/api/src/utils/generics/base-repository.ts index f3bd9fed..2b01f632 100644 --- a/api/src/utils/generics/base-repository.ts +++ b/api/src/utils/generics/base-repository.ts @@ -22,6 +22,7 @@ import { SortOrder, UpdateQuery, UpdateWithAggregationPipeline, + UpdateWriteOpResult, } from 'mongoose'; import { TFilterQuery } from '@/utils/types/filter.types'; @@ -70,7 +71,7 @@ export abstract class BaseRepository< this.registerLifeCycleHooks(); } - getPopulate() { + getPopulate(): P[] { return this.populate; } @@ -79,7 +80,7 @@ export abstract class BaseRepository< return `hook:${entity}:${suffix}` as `hook:${IHookEntities}:${TNormalizedEvents}`; } - private registerLifeCycleHooks() { + private registerLifeCycleHooks(): void { const repository = this; const hooks = LifecycleHookManager.getHooks(this.cls.name); @@ -202,7 +203,7 @@ export abstract class BaseRepository< protected async execute>( query: Query, cls: new () => R, - ) { + ): Promise { const resultSet = await query.lean(this.leanOpts).exec(); return resultSet.map((doc) => plainToClass(cls, doc, this.transformOpts)); } @@ -211,7 +212,7 @@ export abstract class BaseRepository< query: Query, cls: new () => R, options?: ClassTransformOptions, - ) { + ): Promise { const doc = await query.lean(this.leanOpts).exec(); return plainToClass(cls, doc, options ?? this.transformOpts); } @@ -219,7 +220,7 @@ export abstract class BaseRepository< protected findOneQuery( criteria: string | TFilterQuery, projection?: ProjectionType, - ) { + ): Query { if (!criteria) { // An empty criteria would return the first document that it finds throw new Error('findOneQuery() should not have an empty criteria'); @@ -247,7 +248,7 @@ export abstract class BaseRepository< async findOneAndPopulate( criteria: string | TFilterQuery, projection?: ProjectionType, - ) { + ): Promise { this.ensureCanPopulate(); const query = this.findOneQuery(criteria, projection).populate( this.populate, @@ -262,19 +263,19 @@ export abstract class BaseRepository< filter: TFilterQuery, pageQuery?: QuerySortDto, projection?: ProjectionType, - ); + ): Query; protected findQuery( filter: TFilterQuery, pageQuery?: PageQueryDto, projection?: ProjectionType, - ); + ): Query; protected findQuery( filter: TFilterQuery, pageQuery?: QuerySortDto | PageQueryDto, projection?: ProjectionType, - ) { + ): Query { if (Array.isArray(pageQuery)) { const query = this.model.find(filter, projection); return query.sort([pageQuery] as [string, SortOrder][]); @@ -321,7 +322,7 @@ export abstract class BaseRepository< return await this.execute(query, this.cls); } - private ensureCanPopulate() { + private ensureCanPopulate(): void { if (!this.populate || !this.clsPopulate) { throw new Error('Cannot populate query'); } @@ -361,15 +362,17 @@ export abstract class BaseRepository< } } - protected findAllQuery(sort?: QuerySortDto) { + protected findAllQuery( + sort?: QuerySortDto, + ): Query { return this.findQuery({}, { limit: 0, skip: 0, sort }); } - async findAll(sort?: QuerySortDto) { + async findAll(sort?: QuerySortDto): Promise { return await this.find({}, { limit: 0, skip: 0, sort }); } - async findAllAndPopulate(sort?: QuerySortDto) { + async findAllAndPopulate(sort?: QuerySortDto): Promise { this.ensureCanPopulate(); const query = this.findAllQuery(sort).populate(this.populate); return await this.execute(query, this.clsPopulate); @@ -381,7 +384,7 @@ export abstract class BaseRepository< protected findPageQuery( filters: TFilterQuery, { skip, limit, sort }: PageQueryDto, - ) { + ): Query { return this.findQuery(filters) .skip(skip) .limit(limit) @@ -405,7 +408,7 @@ export abstract class BaseRepository< async findPageAndPopulate( filters: TFilterQuery, pageQuery: PageQueryDto, - ) { + ): Promise { this.ensureCanPopulate(); const query = this.findPageQuery(filters, pageQuery).populate( this.populate, @@ -431,7 +434,7 @@ export abstract class BaseRepository< ); } - async createMany(dtoArray: U[]) { + async createMany(dtoArray: U[]): Promise { const docs = await this.model.create(dtoArray); return docs.map((doc) => @@ -460,7 +463,7 @@ export abstract class BaseRepository< async updateMany>( filter: TFilterQuery, dto: UpdateQuery, - ) { + ): Promise { return await this.model.updateMany(filter, { $set: dto, }); @@ -476,19 +479,19 @@ export abstract class BaseRepository< return await this.model.deleteMany(criteria); } - async preValidate(_doc: HydratedDocument) { + async preValidate(_doc: HydratedDocument): Promise { // Nothing ... } - async postValidate(_validated: HydratedDocument) { + async postValidate(_validated: HydratedDocument): Promise { // Nothing ... } - async preCreate(_doc: HydratedDocument) { + async preCreate(_doc: HydratedDocument): Promise { // Nothing ... } - async postCreate(_created: HydratedDocument) { + async postCreate(_created: HydratedDocument): Promise { // Nothing ... } @@ -496,7 +499,7 @@ export abstract class BaseRepository< _query: Query, _criteria: TFilterQuery, _updates: UpdateWithAggregationPipeline | UpdateQuery, - ) { + ): Promise { // Nothing ... } @@ -504,35 +507,35 @@ export abstract class BaseRepository< _query: Query, _criteria: TFilterQuery, _updates: UpdateWithAggregationPipeline | UpdateQuery, - ) { + ): Promise { // Nothing ... } async postUpdateMany( _query: Query, _updated: any, - ) { + ): Promise { // Nothing ... } async postUpdate( _query: Query, _updated: T, - ) { + ): Promise { // Nothing ... } async preDelete( _query: Query, _criteria: TFilterQuery, - ) { + ): Promise { // Nothing ... } async postDelete( _query: Query, _result: DeleteResult, - ) { + ): Promise { // Nothing ... } }