Skip to content

Commit e0c0606

Browse files
authored
Add Cursor#nextBatch (#627)
* Add Cursor#nextBatch and hasNextBatch * Remove cursor.hasNextBatch The name is misleading, just use cursor.hasNext instead. * Add docs * Add tests * Fix test
1 parent 51c0a64 commit e0c0606

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
`cursor.kill` method. Note that this method has no effect if the cursor
1616
is already depleted.
1717

18+
- Added `cursor.nextBatch` method
19+
20+
Cursors normally fetch additional batches as necessary while iterating
21+
over the individual results, this method allows consuming an entire batch
22+
at a time.
23+
1824
## [6.11.1] - 2019-08-30
1925

2026
### Fixed

docs/Drivers/JS/Reference/Cursor.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ await cursor.all(); // exhausts the cursor
7272
assert.equal(cursor.hasNext(), false);
7373
```
7474

75+
## cursor.nextBatch
76+
77+
`async cursor.nextBatch(): Object`
78+
79+
Advances the cursor and returns all remaining values in the cursor's current
80+
batch. If the current batch has already been exhausted, fetches the next batch
81+
from the server and returns it. If no more batches are available, returns
82+
`undefined` instead.
83+
84+
**Examples**
85+
86+
```js
87+
const cursor = await db.query(aql`FOR i IN 1..10 RETURN i`, { batchSize: 5 });
88+
await cursor.nextBatch(); // [1, 2, 3, 4, 5]
89+
await cursor.next(); // 6
90+
await cursor.nextBatch(); // [7, 8, 9, 10]
91+
cursor.hasNext(); // false
92+
```
93+
7594
## cursor.each
7695

7796
`async cursor.each(fn): any`
@@ -165,7 +184,7 @@ const result = await cursor.every(even);
165184
assert.equal(result, false); // 3 is not even
166185
assert.equal(cursor.hasNext(), true);
167186

168-
const value = await cursor.next();
187+
const value = await cursor.nextBatch();
169188
assert.equal(value, 4); // next value after 3
170189
```
171190

src/cursor.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,20 @@ export class ArrayCursor {
6464
return this._result.shift();
6565
}
6666

67-
hasNext() {
67+
hasNext(): boolean {
6868
return Boolean(this._hasMore || this._result.length);
6969
}
7070

71+
async nextBatch(): Promise<any[] | undefined> {
72+
while (!this._result.length && this._hasMore) {
73+
await this._more();
74+
}
75+
if (!this._result.length) {
76+
return undefined;
77+
}
78+
return this._result.splice(0, this._result.length);
79+
}
80+
7181
async each(
7282
fn: (value: any, index: number, self: ArrayCursor) => boolean | void
7383
): Promise<boolean> {

src/test/08-cursors.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ describe("Cursor API", () => {
193193
expect(result).to.eql(aqlResult.reduce((a, b) => a + b));
194194
});
195195
});
196+
describe("cursor.nextBatch", () => {
197+
beforeEach(async () => {
198+
cursor = await db.query(aql`FOR i IN 1..10 RETURN i`, { batchSize: 5 });
199+
});
200+
it("fetches the next batch when empty", async () => {
201+
expect((cursor as any)._result).to.eql([1, 2, 3, 4, 5]);
202+
expect(cursor).to.have.property("_hasMore", true);
203+
(cursor as any)._result.splice(0, 5);
204+
expect(await cursor.nextBatch()).to.eql([6, 7, 8, 9, 10]);
205+
expect(cursor).to.have.property("_hasMore", false);
206+
});
207+
it("returns all fetched values", async () => {
208+
expect(await cursor.nextBatch()).to.eql([1, 2, 3, 4, 5]);
209+
expect(await cursor.next()).to.equal(6);
210+
expect(await cursor.nextBatch()).to.eql([7, 8, 9, 10]);
211+
});
212+
});
196213
describe("cursor.kill", () => {
197214
it("kills the cursor", async () => {
198215
const cursor = await db.query(aql`FOR i IN 1..5 RETURN i`, {

0 commit comments

Comments
 (0)