smartdata/ts/smartdata.classes.cursor.ts

39 lines
947 B
TypeScript
Raw Permalink 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
2021-11-12 16:22:31 +00:00
public mongodbCursor: plugins.mongodb.FindCursor<T>;
constructor(cursorArg: plugins.mongodb.FindCursor<T>) {
2021-11-12 15:23:26 +00:00
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();
}
}