smartdata/ts/smartdata.classes.cursor.ts

39 lines
939 B
TypeScript
Raw Normal View History

2021-11-12 15:23:26 +00:00
import * as plugins from './smartdata.plugins';
/**
* a wrapper for the native mongodb cursor. Exposes better
*/
export class SmartdataDbCursor<T = any> {
// STATIC
// INSTANCE
public mongodbCursor: plugins.mongodb.Cursor<T>;
constructor(cursorArg: plugins.mongodb.Cursor<T>) {
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<any>, 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();
}
}