/**
* Finds the first Document that matchs the params
* @param params.filter Object used to filter Documents
* @param params.fieldsToShow Object containing the fields to return from the Documents
* @param params.databaseName Set this to query on another database in the current mongo connection
* @param params.throwErrors Enable classical try/catch way of handling errors
* @returns A Promise with a single Document
*/findOne(params:IFindOneParams<Interface,false>):Promise<Either<Interface>>;findOne(params:IFindOneParams<Interface,true>):Promise<Interface>;findOne(params:IFindOneParams<Interface,boolean>):Promise<Either<Interface>|Interface>{returnnewPromise(async(resolve,reject)=>{try{const_model=this.getModel(params.databaseName);constresult=(await_model.findOne(params.filter,params.fieldsToShow).lean(true))asany;if(params.throwErrors){resolve(result);}resolve([null,result]);}catch(e){constexception=newGenericException({name:e.name,message:e.message});if(params.throwErrors){reject(exception);}resolve([exception,null]);}});}
In another place using express
/** some code **/const[insertError,insertSuccess]=awaitthis.promotionService.insert({entity:promotion,databaseName:validationSuccess._id.toString(),});res.status(insertError?insertError.statusCode:CREATED).send(insertError?insertError.formatError():insertSuccess);
My Either implementation:
Using it:
In another place using express
I didn't know you could overload your function signature like that in TypeScript... Looking dope!
In my opinion, since you need to keep track of your errors as well as your result in an Either type, it would have been like that in my understanding:
What do you think?