Skip to content

Commit

Permalink
Merge pull request #88 from DigiChanges/fix/LC/getOneBy
Browse files Browse the repository at this point in the history
fix: getOneBy fixed
  • Loading branch information
Murzbul authored Feb 21, 2022
2 parents 752aeca + 699b7c5 commit c3c178a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
12 changes: 5 additions & 7 deletions src/App/Infrastructure/Repositories/BaseMikroSqlRepository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,7 +12,7 @@ abstract class BaseMikroSqlRepository<T> implements IBaseRepository<T>
protected repository: EntityRepository<any>;
protected em = EntityManagerFactory.getEntityFactory();

constructor(@unmanaged() entityName: string, @unmanaged() entitySchema: any)
constructor(@unmanaged() entityName: string, @unmanaged() entitySchema: EntitySchema<any>)
{
this.entityName = entityName;
this.repository = this.em.getRepository(entitySchema);
Expand Down Expand Up @@ -56,13 +56,11 @@ abstract class BaseMikroSqlRepository<T> implements IBaseRepository<T>
return entity;
}

async getOneBy(condition: Record<string, any>, options: IByOptions = { initThrow: true }): Promise<T>
async getOneBy(condition: Record<string, any>, options: IByOptions = {}): Promise<T>
{
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<T>));

if (initThrow && !entity)
{
Expand Down
19 changes: 8 additions & 11 deletions src/App/Infrastructure/Repositories/BaseMongoRepository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,7 +27,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T

async getOne(id: string): Promise<T>
{
const entity = await this.repository.findOne({ _id: id } as any).populate(this.populate);
const entity = await this.repository.findOne({ _id: id } as FilterQuery<T>).populate(this.populate);

if (!entity)
{
Expand All @@ -39,7 +39,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T

async update(entity: T): Promise<T>
{
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<T>, { $set: entity } as UpdateQuery<T>, { new: true }).populate(this.populate);
}

async delete(id: string): Promise<T>
Expand All @@ -54,14 +54,11 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T
return entity as any;
}

async getOneBy(condition: Record<string, any>, options: IByOptions = { initThrow: true, populate: null }): Promise<T>
async getOneBy(condition: Record<string, any>, options: IByOptions = {}): Promise<T>
{
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<T>).populate(populate).exec();

if (initThrow && !entity)
{
Expand All @@ -78,7 +75,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T
initThrow = initThrow ?? false;
populate = populate ?? null;

const entities = await this.repository.find(condition as any).populate(populate).exec();
const entities = await this.repository.find(condition as FilterQuery<T>).populate(populate).exec();

if (initThrow && entities.length === 0)
{
Expand All @@ -97,7 +94,7 @@ abstract class BaseMongoRepository<T extends IBaseDomain, D extends Document & T

async exist(condition: Record<string, any>, select: string[], initThrow = false): Promise<any>
{
const exist = await this.repository.findOne(condition as any, select.join(' '));
const exist = await this.repository.findOne(condition as FilterQuery<T>, select.join(' '));

if (initThrow && !exist)
{
Expand Down
8 changes: 3 additions & 5 deletions src/App/Infrastructure/Repositories/BaseSqlRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ abstract class BaseSqlRepository<T> implements IBaseRepository<T>
return entity;
}

async getOneBy(condition: Record<string, any>, options: IByOptions = { initThrow: true }): Promise<T>
async getOneBy(condition: Record<string, any>, options: IByOptions = {}): Promise<T>
{
let { initThrow } = options;

initThrow = initThrow ?? false;
const { initThrow = true } = options;

const entity = await this.repository.findOne(condition);

Expand Down Expand Up @@ -99,7 +97,7 @@ abstract class BaseSqlRepository<T> implements IBaseRepository<T>
loadEagerRelations: false
};

const exist = await this.repository.findOne(conditionMap);
const exist = await this.repository.findOne(conditionMap as FindOneOptions<T>);

if (initThrow && !exist)
{
Expand Down
4 changes: 1 addition & 3 deletions src/App/InterfaceAdapters/IByOptions.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c3c178a

Please sign in to comment.