Skip to content

Commit 58325de

Browse files
committed
v0.0.18: add simple promise method, fix linting
1 parent 2f18cd7 commit 58325de

5 files changed

Lines changed: 108 additions & 6 deletions

File tree

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,39 @@
99
yarn add @boilingdata/node-boilingdata
1010
```
1111

12-
## Basic Example
12+
## Basic Examples
13+
14+
`execQueryPromise()` method can be used to await for the results directly.
15+
16+
```typescript
17+
import { BoilingData, isDataResponse } from "@boilingdata/node-boilingdata";
18+
19+
async function main() {
20+
const bdInstance = new BoilingData({ username: process.env["BD_USERNAME"], password: process.env["BD_PASSWORD"] });
21+
await bdInstance.connect();
22+
const sql = `SELECT 's3://KEY' AS key, COUNT(*) AS count FROM parquet_scan('s3://KEY');`;
23+
const keys = ["s3://boilingdata-demo/demo.parquet", "s3://boilingdata-demo/demo2.parquet"];
24+
const rows = await bdInstance.execQueryPromise({ sql, keys });
25+
console.log(rows);
26+
await bdInstance.close();
27+
}
28+
```
29+
30+
`execQuery()` uses callbacks.
1331

1432
```typescript
1533
import { BoilingData, isDataResponse } from "@boilingdata/node-boilingdata";
1634

1735
async function main() {
1836
const bdInstance = new BoilingData({ username: process.env["BD_USERNAME"], password: process.env["BD_PASSWORD"] });
1937
await bdInstance.connect();
38+
const sql = `SELECT 's3://KEY' AS key, COUNT(*) AS count FROM parquet_scan('s3://KEY');`;
39+
const keys = ["s3://boilingdata-demo/demo.parquet", "s3://boilingdata-demo/demo2.parquet"];
2040
const rows = await new Promise<any[]>((resolve, reject) => {
2141
let r: any[] = [];
2242
bdInstance.execQuery({
23-
sql: `SELECT 's3://KEY' AS key, COUNT(*) AS count FROM parquet_scan('s3://KEY');`,
24-
keys: ["s3://boilingdata-demo/demo.parquet", "s3://boilingdata-demo/demo2.parquet"],
43+
sql,
44+
keys,
2545
callbacks: {
2646
onData: (data: IBDDataResponse | unknown) => {
2747
if (isDataResponse(data)) data.data.map(row => r.push(row));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@boilingdata/node-boilingdata",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "BoilingData client",
55
"main": "dist/cjs/index.js",
66
"types": "dist/cjs/index.d.ts",

src/boilingdata/boilingdata.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ interface IEvent {
6767
enum ECallbackNames {
6868
REQUEST = "onRequest",
6969
ERROR = "onLogError",
70-
LOG_MESSAGE = "onLogMessage",
7170
LOG_INFO = "onLogInfo",
7271
LOG_ERROR = "onLogError",
7372
LOG_WARN = "onLogWarn",
@@ -143,8 +142,27 @@ export class BoilingData {
143142
});
144143
}
145144

145+
public execQueryPromise(params: IBDQuery): Promise<any[]> {
146+
this.logger.info("execQueryPromise:", params);
147+
const r: any[] = [];
148+
return new Promise((resolve, reject) => {
149+
this.execQuery({
150+
sql: params.sql,
151+
keys: params.keys || [],
152+
engine: params.engine ?? EEngineTypes.DUCKDB,
153+
callbacks: {
154+
onData: (data: IBDDataResponse | unknown) => {
155+
if (isDataResponse(data)) data.data.map(row => r.push(row));
156+
},
157+
onQueryFinished: () => resolve(r),
158+
onLogError: (data: any) => reject(data),
159+
},
160+
});
161+
});
162+
}
163+
146164
public execQuery(params: IBDQuery): void {
147-
this.logger.info("runQuery:", params);
165+
this.logger.info("execQuery:", params);
148166
this.socketInstance.bumpActivity();
149167
const requestId = uuidv4();
150168
const payload: IBDDataQuery = {

src/tests/__snapshots__/query.test.ts.snap

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,48 @@ Array [
645645
},
646646
]
647647
`;
648+
649+
exports[`boilingdata with promise method can run simple promise based query 1`] = `
650+
Array [
651+
Object {
652+
"DOLocationID": 145,
653+
"PULocationID": 145,
654+
"RatecodeID": 1,
655+
"VendorID": 1,
656+
"congestion_surcharge": 0,
657+
"extra": 0.5,
658+
"fare_amount": 3,
659+
"improvement_surcharge": 0.3,
660+
"mta_tax": 0.5,
661+
"passenger_count": 1,
662+
"payment_type": 2,
663+
"store_and_fwd_flag": "N",
664+
"tip_amount": 0,
665+
"tolls_amount": 0,
666+
"total_amount": 4.3,
667+
"tpep_dropoff_datetime": 1556669808000,
668+
"tpep_pickup_datetime": 1556669690000,
669+
"trip_distance": 0,
670+
},
671+
Object {
672+
"DOLocationID": 145,
673+
"PULocationID": 145,
674+
"RatecodeID": 1,
675+
"VendorID": 1,
676+
"congestion_surcharge": 0,
677+
"extra": 0.5,
678+
"fare_amount": 3,
679+
"improvement_surcharge": 0.3,
680+
"mta_tax": 0.5,
681+
"passenger_count": 1,
682+
"payment_type": 2,
683+
"store_and_fwd_flag": "N",
684+
"tip_amount": 0,
685+
"tolls_amount": 0,
686+
"total_amount": 4.3,
687+
"tpep_dropoff_datetime": 1556671047000,
688+
"tpep_pickup_datetime": 1556670954000,
689+
"trip_distance": 1.5,
690+
},
691+
]
692+
`;

src/tests/query.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ describe("boilingdata with SQLite3", () => {
155155
});
156156
});
157157

158+
describe("boilingdata with promise method", () => {
159+
beforeAll(async () => {
160+
bdInstance = new BoilingData({ username, password, globalCallbacks, logLevel });
161+
await bdInstance.connect();
162+
logger.info("connected.");
163+
});
164+
165+
afterAll(async () => {
166+
await bdInstance.close();
167+
logger.info("connection closed.");
168+
});
169+
170+
it("can run simple promise based query", async () => {
171+
const sql = `SELECT * FROM parquet_scan('s3://boilingdata-demo/demo2.parquet:m=0') LIMIT 2;`;
172+
const results = await bdInstance.execQueryPromise({ sql });
173+
expect(results).toMatchSnapshot();
174+
});
175+
});
176+
158177
describe("boilingdata with Glue Tables", () => {
159178
beforeAll(async () => {
160179
bdInstance = new BoilingData({ username, password, globalCallbacks, logLevel });

0 commit comments

Comments
 (0)