Skip to content

Commit 97a5f9e

Browse files
glasstigernwoolmerpuzpuzpuz
authoredApr 29, 2024
feat(nodejs): HTTP transport, configuration string, new config options (#26)
* feat(nodejs): HTTP transport, configuration string, new config options * feat(nodejs): HTTP transport, configuration string, new config options * Update src/sender.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/sender.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update types/src/sender.d.ts Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * Update src/options.js Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> * use sender's logger * destroy request before retry * fix test to work with different node versions * custom http agent option * custom http agent options * destroy default http agents when last sender is closed * buffer size comment + jsdoc fix * tsc + jsdoc generated * fixing tests * fix test depends on node version * Update src/options.js Co-authored-by: Andrei Pechkurov <37772591+puzpuzpuz@users.noreply.github.com> * separate counters for http and tcp * examples * examples * examples * make protocol option mandatory * remove sender instance counters * improve docs --------- Co-authored-by: Nick Woolmer <29717167+nwoolmer@users.noreply.github.com> Co-authored-by: Andrei Pechkurov <37772591+puzpuzpuz@users.noreply.github.com>
1 parent f8c221e commit 97a5f9e

32 files changed

+6409
-826
lines changed
 

‎README.md

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,45 @@ The client requires Node.js v16 or newer version.
99
npm i -s @questdb/nodejs-client
1010
```
1111

12+
## Configuration options
13+
14+
Detailed description of the client's configuration options can be found in
15+
the <a href="SenderOptions.html">SenderOptions</a> documentation.
16+
1217
## Examples
1318

19+
The examples below demonstrate how to use the client. <br>
20+
For more details, please, check the <a href="Sender.html">Sender</a>'s documentation.
21+
1422
### Basic API usage
1523

1624
```javascript
1725
const { Sender } = require('@questdb/nodejs-client');
1826

1927
async function run() {
20-
const sender = new Sender();
21-
22-
// connect to QuestDB
23-
// host and port are required in connect options
24-
await sender.connect({port: 9009, host: 'localhost'});
28+
// create a sender using HTTP protocol
29+
const sender = Sender.fromConfig('http::addr=localhost:9000');
2530

2631
// add rows to the buffer of the sender
27-
sender.table('prices').symbol('instrument', 'EURUSD')
32+
await sender.table('prices').symbol('instrument', 'EURUSD')
2833
.floatColumn('bid', 1.0195).floatColumn('ask', 1.0221)
2934
.at(Date.now(), 'ms');
30-
sender.table('prices').symbol('instrument', 'GBPUSD')
35+
await sender.table('prices').symbol('instrument', 'GBPUSD')
3136
.floatColumn('bid', 1.2076).floatColumn('ask', 1.2082)
3237
.at(Date.now(), 'ms');
3338

3439
// flush the buffer of the sender, sending the data to QuestDB
35-
// the buffer is cleared after the data is sent and the sender is ready to accept new data
40+
// the buffer is cleared after the data is sent, and the sender is ready to accept new data
3641
await sender.flush();
3742

38-
// add rows to the buffer again and send it to the server
39-
sender.table('prices').symbol('instrument', 'EURUSD')
43+
// add rows to the buffer again, and send it to the server
44+
await sender.table('prices').symbol('instrument', 'EURUSD')
4045
.floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
4146
.at(Date.now(), 'ms');
4247
await sender.flush();
4348

4449
// close the connection after all rows ingested
4550
await sender.close();
46-
return new Promise(resolve => resolve(0));
4751
}
4852

4953
run()
@@ -57,23 +61,11 @@ run()
5761
const { Sender } = require('@questdb/nodejs-client');
5862

5963
async function run() {
60-
// authentication details
61-
const CLIENT_ID = 'testapp';
62-
const PRIVATE_KEY = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
63-
const AUTH = {
64-
keyId: CLIENT_ID,
65-
token: PRIVATE_KEY
66-
};
67-
68-
// pass the authentication details to the sender
69-
const sender = new Sender({auth: AUTH});
70-
71-
// connect() takes an optional second argument
72-
// if 'true' passed the connection is secured with TLS encryption
73-
await sender.connect({port: 9009, host: 'localhost'}, true);
64+
// create a sender using HTTPS protocol with username and password authentication
65+
const sender = Sender.fromConfig('https::addr=localhost:9000;username=user1;password=pwd');
7466

7567
// send the data over the authenticated and secure connection
76-
sender.table('prices').symbol('instrument', 'EURUSD')
68+
await sender.table('prices').symbol('instrument', 'EURUSD')
7769
.floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
7870
.at(Date.now(), 'ms');
7971
await sender.flush();
@@ -91,20 +83,8 @@ run().catch(console.error);
9183
import { Sender } from '@questdb/nodejs-client';
9284

9385
async function run(): Promise<number> {
94-
// authentication details
95-
const CLIENT_ID: string = 'testapp';
96-
const PRIVATE_KEY: string = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
97-
const AUTH: { keyId: string, token: string } = {
98-
keyId: CLIENT_ID,
99-
token: PRIVATE_KEY
100-
};
101-
102-
// pass the authentication details to the sender
103-
const sender: Sender = new Sender({auth: AUTH});
104-
105-
// connect() takes an optional second argument
106-
// if 'true' passed the connection is secured with TLS encryption
107-
await sender.connect({port: 9009, host: 'localhost'}, true);
86+
// create a sender using HTTPS protocol with bearer token authentication
87+
const sender: Sender = Sender.fromConfig('https::addr=localhost:9000;token=Xyvd3er6GF87ysaHk');
10888

10989
// send the data over the authenticated and secure connection
11090
sender.table('prices').symbol('instrument', 'EURUSD')
@@ -160,14 +140,13 @@ async function run() {
160140
} else {
161141
// it is important that each worker has a dedicated sender object
162142
// threads cannot share the sender because they would write into the same buffer
163-
const sender = new Sender();
164-
await sender.connect({ port: 9009, host: 'localhost' });
143+
const sender = Sender.fromConfig('http::addr=localhost:9000');
165144

166145
// subscribe for the market data of the ticker assigned to the worker
167146
// ingest each price update into the database using the sender
168147
let count = 0;
169148
await subscribe(workerData.ticker, async (tick) => {
170-
sender
149+
await sender
171150
.table('prices')
172151
.symbol('ticker', tick.ticker)
173152
.floatColumn('price', tick.price)

0 commit comments

Comments
 (0)