export class QueryResult { public data: any[] = [] public substitution: any public client: any public dbname:any public collection:any constructor(rawData: any[], substitution: any, client: any, dbname:any, collection:any){ this.data = rawData; this.substitution = substitution; this.client = client this.dbname = dbname this.collection = collection } private goThroughKeys(key: any, resultIndex: number){ return new Promise(async (resolve) => { const fields: any = {}; this.substitution[key].fields.forEach((field: string) => fields[field] = true) const change = await this.client.db(this.dbname).collection(this.substitution[key].collection).findOne({_id: this.data[resultIndex][key]}, {projection: fields}) if(change && change._id){ delete change._id } this.data[resultIndex][key] = JSON.stringify(change); resolve() }) } private goThroughResults(resultIndex: number){ return new Promise(async (resolve) => { const allKeys = Object.keys(this.substitution); const promises = allKeys.map((key) => this.goThroughKeys(key, resultIndex)) Promise.all(promises).then(() => { resolve() }) }) } public async populate(): Promise{ return new Promise(async (resolve) => { const promises = this.data.map((result, resultIndex) => this.goThroughResults(resultIndex)) Promise.all(promises).then(() => { resolve(this.data) }) }) } }