import * as plugins from './smartdata.plugins'; /** * a wrapper for the native mongodb cursor. Exposes better */ export class SmartdataDbCursor { // STATIC // INSTANCE public mongodbCursor: plugins.mongodb.FindCursor; constructor(cursorArg: plugins.mongodb.FindCursor) { this.mongodbCursor = cursorArg; } public async next(closeAtEnd = true) { const result = await this.mongodbCursor.next(); if (!result && closeAtEnd) { await this.close(); } return result; } public async forEach(forEachFuncArg: (itemArg: T) => Promise, closeCursorAtEnd = true) { let currentValue: T; do { currentValue = await this.mongodbCursor.next(); if (currentValue) { await forEachFuncArg(currentValue); } } while (currentValue); if (closeCursorAtEnd) { await this.close(); } } public async close() { await this.mongodbCursor.close(); } }