Skip to content

Commit 1116c84

Browse files
authored
Merge pull request #5 from classmethod/use-delegated-yield
2 parents 9ea8271 + e2b6f70 commit 1116c84

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import AthenaQuery from "athena-query";
3434
const athena = new Athena({});
3535
const athenaQuery = new AthenaQuery(athena);
3636

37-
for await (const items of athenaQuery.query("SELECT * FROM waf_logs;")) {
38-
console.log(items); // You can get all items with pagination by query.
37+
for await (const item of athenaQuery.query("SELECT * FROM waf_logs;")) {
38+
console.log(item); // You can get all items across pagination.
3939
}
4040
```
4141

lib/athena-query.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export class AthenaQuery {
1616
async *query(
1717
sql: string,
1818
options?: { executionParameters?: string[]; maxResults: number }
19-
): AsyncGenerator<Record<string, string | number | BigInt | null>[]> {
19+
): AsyncGenerator<
20+
Record<string, string | number | BigInt | null>,
21+
void,
22+
undefined
23+
> {
2024
const QueryExecutionId = await helpers.startQueryExecution({
2125
athena: this.athena,
2226
sql,
@@ -39,7 +43,7 @@ export class AthenaQuery {
3943
QueryExecutionId,
4044
});
4145

42-
yield queryResults.items;
46+
yield* queryResults.items;
4347

4448
nextToken = queryResults.nextToken;
4549
} while (nextToken);

test/index.test.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,17 @@ test("parse to json following ColumnInfo", async () => {
6262

6363
const res1 = await resultGen.next();
6464

65-
expect(res1.value).toEqual([
66-
{
67-
name: "test-name-1",
68-
disabled: true,
69-
timestamp: 1669718600001n,
70-
score1: 101,
71-
score2: 102,
72-
score3: 103,
73-
score4: 104,
74-
rate1: 1.01,
75-
rate2: 1.02,
76-
},
77-
]);
65+
expect(res1.value).toEqual({
66+
name: "test-name-1",
67+
disabled: true,
68+
timestamp: 1669718600001n,
69+
score1: 101,
70+
score2: 102,
71+
score3: 103,
72+
score4: 104,
73+
rate1: 1.01,
74+
rate2: 1.02,
75+
});
7876
});
7977

8078
test("wait query completed", async () => {
@@ -91,10 +89,7 @@ test("wait query completed", async () => {
9189
ResultSetMetadata: {
9290
ColumnInfo: [{ Name: "name", Type: "varchar" }],
9391
},
94-
Rows: [
95-
{ Data: [{ VarCharValue: "test-name-1" }] },
96-
{ Data: [{ VarCharValue: "test-name-2" }] },
97-
],
92+
Rows: [{ Data: [{ VarCharValue: "test-name-1" }] }],
9893
},
9994
});
10095

@@ -103,10 +98,7 @@ test("wait query completed", async () => {
10398
const res1 = await resultGen.next();
10499

105100
expect(res1.done).toBe(false);
106-
expect(res1.value).toEqual([
107-
{ name: "test-name-1" },
108-
{ name: "test-name-2" },
109-
]);
101+
expect(res1.value).toEqual({ name: "test-name-1" });
110102
});
111103

112104
test("get items with generator", async () => {
@@ -141,18 +133,19 @@ test("get items with generator", async () => {
141133

142134
const res1 = await queryResultGen.next();
143135
expect(res1.done).toBe(false);
144-
expect(res1.value).toEqual([
145-
{ name: "test-name-1" },
146-
{ name: "test-name-2" },
147-
]);
136+
expect(res1.value).toEqual({ name: "test-name-1" });
148137

149138
const res2 = await queryResultGen.next();
150139
expect(res2.done).toBe(false);
151-
expect(res2.value).toEqual([{ name: "test-name-3" }]);
140+
expect(res2.value).toEqual({ name: "test-name-2" });
152141

153142
const res3 = await queryResultGen.next();
154-
expect(res3.done).toBe(true);
155-
expect(res3.value).toBe(undefined);
143+
expect(res3.done).toBe(false);
144+
expect(res3.value).toEqual({ name: "test-name-3" });
145+
146+
const res4 = await queryResultGen.next();
147+
expect(res4.done).toBe(true);
148+
expect(res4.value).toBe(undefined);
156149
});
157150

158151
test("get all item with generator", async () => {
@@ -197,8 +190,8 @@ test("get all item with generator", async () => {
197190

198191
const allItems = [];
199192

200-
for await (const items of athenaQuery.query("")) {
201-
allItems.push(...items);
193+
for await (const item of athenaQuery.query("")) {
194+
allItems.push(item);
202195
}
203196

204197
expect(allItems).toEqual([

0 commit comments

Comments
 (0)