Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jul 31, 2024
1 parent 62d1acc commit 2b82d72
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ export abstract class AbstractCursor<
}

/** Returns current buffered documents */
readBufferedDocuments(number?: number): TSchema[] {
const bufferedDocs: TSchema[] = [];
readBufferedDocuments(number?: number): NonNullable<TSchema>[] {
const bufferedDocs: NonNullable<TSchema>[] = [];
const documentsToRead = Math.min(
number ?? this.documents?.length ?? 0,
this.documents?.length ?? 0
Expand Down Expand Up @@ -457,29 +457,33 @@ export abstract class AbstractCursor<
* results when this cursor had been previously accessed. In that case,
* cursor.rewind() can be used to reset the cursor.
*/
async toArray(): Promise<TSchema[]> {
async toArray(transform_temp?: (doc: TSchema) => any): Promise<TSchema[]> {
const array: TSchema[] = [];

// when each loop iteration ends,documents will be empty and a 'await (const document of this)' will run a getMore operation
this.transform = transform_temp;
// at the end of the loop (since readBufferedDocuments is called) the buffer will be empty
// then, the 'await of' syntax will run a getMore call
for await (const document of this) {
array.push(document);
let docs = this.readBufferedDocuments();
const docs = this.readBufferedDocuments();
if (this.transform != null) {
docs = await Promise.all(
docs.map(async doc => {
if (doc != null) {
return await this.transformDocument(doc);
} else {
throw Error;
}
})
);
for (const doc of docs) {
array.push(await this.transformDocument(doc));
}
} else {
array.push(...docs);
}
array.push(...docs);
}
return array;
}

async toArrayOld(): Promise<TSchema[]> {
const array = [];
for await (const document of this) {
array.push(document);
}
return array;
}
/**
* Add a cursor flag to the cursor
*
Expand Down Expand Up @@ -820,7 +824,7 @@ export abstract class AbstractCursor<
}

/** @internal */
private async transformDocument(document: NonNullable<TSchema>): Promise<TSchema> {
private async transformDocument(document: NonNullable<TSchema>): Promise<NonNullable<TSchema>> {
if (this.transform == null) return document;

try {
Expand Down

0 comments on commit 2b82d72

Please sign in to comment.