Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating queryIterable method #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ main();

```

###### Query to fetch results (rows) for an async iterable of pages

You can also receive an async iterable that yields all result pages, seamless.

```javascript
async function main() {
const results = athenaExpress.queryIterable("SELECT * from students LIMIT 100", 10);
for await (const page of results) {
console.log(page);
}
}
main();

```

##### UTILITY queries
###### Show Tables (single column result)
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare module 'athena-express' {

class AthenaExpress<T> {
public new: (config: Partial<ConnectionConfigInterface>) => any;
public queryIterable(sql: string, pagination?: number): AsyncIterable<QueryResult<T>>;
public query: QueryFunc<T>;
constructor(config: Partial<ConnectionConfigInterface>);
}
Expand Down
40 changes: 40 additions & 0 deletions lib/athenaExpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ const COST_PER_MB = 0.000004768, //Based on $5/TB

const helpers = require("./helpers.js");

function getNext(query, instance) {
let previous;
return async () => {
if (previous) {
if (!previous.NextToken) {
return {
done: true
};
}
const { NextToken, QueryExecutionId } = previous;
query = { ...query, NextToken, QueryExecutionId };
}
previous = await instance.query(query);

return {
value: previous,
done: !previous.Items || !previous.Items.length,
};
}
}

module.exports = class AthenaExpress {
constructor(init) {
helpers.validateConstructor(init);
Expand Down Expand Up @@ -39,6 +60,25 @@ module.exports = class AthenaExpress {
};
}

queryIterable(sql, pagination = 999) {
if (typeof sql !== 'string') {
throw new TypeError('sql must be a SQL Statement');
}
if (typeof pagination !== 'number' || pagination <= 0) {
throw new TypeError('pagination must be a positive number');
}

return {
[Symbol.asyncIterator]: () => {
const query = { sql, pagination };

return {
next: getNext(query, this),
}
}
}
}

async query(query) {
const config = this.config;

Expand Down
Loading