import { Db, Collection } from "mongodb"; import { MongoQuery } from "./MongoQuery"; import { MongoCollection } from "../MongoCollection"; export class MongoQuerySingle extends MongoQuery { then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise { return this.exec().then( onfulfilled, onrejected ) as any; } catch(onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise { return this.exec().catch(onrejected) as any; } exec(): Promise { return new Promise( (resolve, reject) => { //this.limit(1); this.cursor({ batchSize : 1 }); let query: Promise = this.cursor().toArray().then((data:any) => { if(this.isLean || !data) { return resolve(data[0]); } let instance:any = this.collection.constructCollection((this.collection as any)); instance.hydrate(data[0]); resolve(instance); }).catch( reject ); }); } };