From 699b7c5db982122e1b0ef327488021b3e7379407 Mon Sep 17 00:00:00 2001 From: Leonardo Casales Date: Mon, 21 Feb 2022 12:29:28 -0300 Subject: [PATCH] fix: getOneBy fixed --- .../Repositories/BaseMikroSqlRepository.ts | 12 +++++------- .../Repositories/BaseMongoRepository.ts | 19 ++++++++----------- .../Repositories/BaseSqlRepository.ts | 8 +++----- src/App/InterfaceAdapters/IByOptions.ts | 4 +--- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/App/Infrastructure/Repositories/BaseMikroSqlRepository.ts b/src/App/Infrastructure/Repositories/BaseMikroSqlRepository.ts index 8f63ad1d..b0fdbf6d 100644 --- a/src/App/Infrastructure/Repositories/BaseMikroSqlRepository.ts +++ b/src/App/Infrastructure/Repositories/BaseMikroSqlRepository.ts @@ -1,5 +1,5 @@ import { injectable, unmanaged } from 'inversify'; -import { EntityRepository } from '@mikro-orm/core'; +import { EntityRepository, EntitySchema, FindOneOptions } from '@mikro-orm/core'; import NotFoundException from '../../../Shared/Exceptions/NotFoundException'; import IByOptions from '../../InterfaceAdapters/IByOptions'; import IBaseRepository from '../../InterfaceAdapters/IBaseRepository'; @@ -12,7 +12,7 @@ abstract class BaseMikroSqlRepository implements IBaseRepository protected repository: EntityRepository; protected em = EntityManagerFactory.getEntityFactory(); - constructor(@unmanaged() entityName: string, @unmanaged() entitySchema: any) + constructor(@unmanaged() entityName: string, @unmanaged() entitySchema: EntitySchema) { this.entityName = entityName; this.repository = this.em.getRepository(entitySchema); @@ -56,13 +56,11 @@ abstract class BaseMikroSqlRepository implements IBaseRepository return entity; } - async getOneBy(condition: Record, options: IByOptions = { initThrow: true }): Promise + async getOneBy(condition: Record, options: IByOptions = {}): Promise { - let { initThrow } = options; - - initThrow = initThrow ?? false; + const { initThrow = true, populate = [] } = options; - const entity = await this.repository.findOne(condition); + const entity = await this.repository.findOne(condition, ({ populate } as FindOneOptions)); if (initThrow && !entity) { diff --git a/src/App/Infrastructure/Repositories/BaseMongoRepository.ts b/src/App/Infrastructure/Repositories/BaseMongoRepository.ts index 0c259541..52b9d413 100644 --- a/src/App/Infrastructure/Repositories/BaseMongoRepository.ts +++ b/src/App/Infrastructure/Repositories/BaseMongoRepository.ts @@ -1,4 +1,4 @@ -import { Document, Model } from 'mongoose'; +import { Document, FilterQuery, Model, UpdateQuery } from 'mongoose'; import { injectable, unmanaged } from 'inversify'; import { connection } from '../../../Shared/Database/MongooseCreateConnection'; import NotFoundException from '../../../Shared/Exceptions/NotFoundException'; @@ -27,7 +27,7 @@ abstract class BaseMongoRepository { - const entity = await this.repository.findOne({ _id: id } as any).populate(this.populate); + const entity = await this.repository.findOne({ _id: id } as FilterQuery).populate(this.populate); if (!entity) { @@ -39,7 +39,7 @@ abstract class BaseMongoRepository { - return this.repository.findOneAndUpdate({ _id: entity.getId() } as any, { $set: entity } as any, { new: true }).populate(this.populate); + return this.repository.findOneAndUpdate({ _id: entity.getId() } as FilterQuery, { $set: entity } as UpdateQuery, { new: true }).populate(this.populate); } async delete(id: string): Promise @@ -54,14 +54,11 @@ abstract class BaseMongoRepository, options: IByOptions = { initThrow: true, populate: null }): Promise + async getOneBy(condition: Record, options: IByOptions = {}): Promise { - let { initThrow, populate } = options; - - initThrow = initThrow ?? false; - populate = populate ?? null; + const { initThrow = true, populate = null } = options; - const entity = await this.repository.findOne(condition as any).populate(populate).exec(); + const entity = await this.repository.findOne(condition as FilterQuery).populate(populate).exec(); if (initThrow && !entity) { @@ -78,7 +75,7 @@ abstract class BaseMongoRepository).populate(populate).exec(); if (initThrow && entities.length === 0) { @@ -97,7 +94,7 @@ abstract class BaseMongoRepository, select: string[], initThrow = false): Promise { - const exist = await this.repository.findOne(condition as any, select.join(' ')); + const exist = await this.repository.findOne(condition as FilterQuery, select.join(' ')); if (initThrow && !exist) { diff --git a/src/App/Infrastructure/Repositories/BaseSqlRepository.ts b/src/App/Infrastructure/Repositories/BaseSqlRepository.ts index 999f176b..06819bcf 100644 --- a/src/App/Infrastructure/Repositories/BaseSqlRepository.ts +++ b/src/App/Infrastructure/Repositories/BaseSqlRepository.ts @@ -52,11 +52,9 @@ abstract class BaseSqlRepository implements IBaseRepository return entity; } - async getOneBy(condition: Record, options: IByOptions = { initThrow: true }): Promise + async getOneBy(condition: Record, options: IByOptions = {}): Promise { - let { initThrow } = options; - - initThrow = initThrow ?? false; + const { initThrow = true } = options; const entity = await this.repository.findOne(condition); @@ -99,7 +97,7 @@ abstract class BaseSqlRepository implements IBaseRepository loadEagerRelations: false }; - const exist = await this.repository.findOne(conditionMap); + const exist = await this.repository.findOne(conditionMap as FindOneOptions); if (initThrow && !exist) { diff --git a/src/App/InterfaceAdapters/IByOptions.ts b/src/App/InterfaceAdapters/IByOptions.ts index 1d53cf16..22bab5a4 100644 --- a/src/App/InterfaceAdapters/IByOptions.ts +++ b/src/App/InterfaceAdapters/IByOptions.ts @@ -1,9 +1,7 @@ -import { PopulateOptions } from 'mongoose'; - interface IByOptions { initThrow?: boolean | undefined; - populate?: string | PopulateOptions | PopulateOptions[] | undefined; + populate?: string | string[] | boolean | undefined; } export default IByOptions;