import { Db, Collection, Cursor, AggregationCursor } from "mongodb"; import { MongoQuery } from "./MongoQuery"; import { MongoCollection } from "../MongoCollection"; export class MongoQueryMulti extends MongoQuery { then(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise { return this.exec().then( onfulfilled, onrejected ); } catch(onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise { return this.exec().catch(onrejected); } exec(): Promise { return new Promise( (resolve, reject: any) => { if(this.isLean) { this.cursor().toArray().then( array => { resolve(array); }); return; } this.cursor().toArray().then( array => { resolve(array.map( document => { let instance: any = this.collection.constructCollection(this.collection); instance.hydrate(document); return instance; })); }); }); } forEach(callback, done?) { let cursor = this.cursor().toArray(); if(this.isLean) { this.cursor().toArray().then( array => { array.forEach(callback); if(done) { done(); } }) return; } cursor.then( array => { array.forEach( data => { var instance: any = this.collection.constructCollection(this.collection); instance.hydrate(data); callback(instance); }); if(done) { done(); } }); //cursor.to } };