@@ -9,41 +9,45 @@ The client requires Node.js v16 or newer version.
9
9
npm i -s @questdb/nodejs-client
10
10
```
11
11
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
+
12
17
## Examples
13
18
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
+
14
22
### Basic API usage
15
23
16
24
``` javascript
17
25
const { Sender } = require (' @questdb/nodejs-client' );
18
26
19
27
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' );
25
30
26
31
// add rows to the buffer of the sender
27
- sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
32
+ await sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
28
33
.floatColumn (' bid' , 1.0195 ).floatColumn (' ask' , 1.0221 )
29
34
.at (Date .now (), ' ms' );
30
- sender .table (' prices' ).symbol (' instrument' , ' GBPUSD' )
35
+ await sender .table (' prices' ).symbol (' instrument' , ' GBPUSD' )
31
36
.floatColumn (' bid' , 1.2076 ).floatColumn (' ask' , 1.2082 )
32
37
.at (Date .now (), ' ms' );
33
38
34
39
// 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
36
41
await sender .flush ();
37
42
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' )
40
45
.floatColumn (' bid' , 1.0197 ).floatColumn (' ask' , 1.0224 )
41
46
.at (Date .now (), ' ms' );
42
47
await sender .flush ();
43
48
44
49
// close the connection after all rows ingested
45
50
await sender .close ();
46
- return new Promise (resolve => resolve (0 ));
47
51
}
48
52
49
53
run ()
@@ -57,23 +61,11 @@ run()
57
61
const { Sender } = require (' @questdb/nodejs-client' );
58
62
59
63
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' );
74
66
75
67
// send the data over the authenticated and secure connection
76
- sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
68
+ await sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
77
69
.floatColumn (' bid' , 1.0197 ).floatColumn (' ask' , 1.0224 )
78
70
.at (Date .now (), ' ms' );
79
71
await sender .flush ();
@@ -91,20 +83,8 @@ run().catch(console.error);
91
83
import { Sender } from ' @questdb/nodejs-client' ;
92
84
93
85
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' );
108
88
109
89
// send the data over the authenticated and secure connection
110
90
sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
@@ -160,14 +140,13 @@ async function run() {
160
140
} else {
161
141
// it is important that each worker has a dedicated sender object
162
142
// 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' );
165
144
166
145
// subscribe for the market data of the ticker assigned to the worker
167
146
// ingest each price update into the database using the sender
168
147
let count = 0 ;
169
148
await subscribe (workerData .ticker , async (tick ) => {
170
- sender
149
+ await sender
171
150
.table (' prices' )
172
151
.symbol (' ticker' , tick .ticker )
173
152
.floatColumn (' price' , tick .price )
0 commit comments