@@ -22,77 +22,80 @@ For more details, please, check the <a href="Sender.html">Sender</a>'s documenta
22
22
### Basic API usage
23
23
24
24
``` javascript
25
- const { Sender } = require (' @questdb/nodejs-client' );
25
+ const { Sender } = require (" @questdb/nodejs-client" )
26
26
27
27
async function run () {
28
- // create a sender using HTTP protocol
29
- const sender = Sender .fromConfig (' http::addr=localhost:9000' );
30
-
31
- // add rows to the buffer of the sender
32
- await sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
33
- .floatColumn (' bid' , 1.0195 ).floatColumn (' ask' , 1.0221 )
34
- .at (Date .now (), ' ms' );
35
- await sender .table (' prices' ).symbol (' instrument' , ' GBPUSD' )
36
- .floatColumn (' bid' , 1.2076 ).floatColumn (' ask' , 1.2082 )
37
- .at (Date .now (), ' ms' );
38
-
39
- // flush the buffer of the sender, sending the data to QuestDB
40
- // the buffer is cleared after the data is sent, and the sender is ready to accept new data
41
- await sender .flush ();
42
-
43
- // add rows to the buffer again, and send it to the server
44
- await sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
45
- .floatColumn (' bid' , 1.0197 ).floatColumn (' ask' , 1.0224 )
46
- .at (Date .now (), ' ms' );
47
- await sender .flush ();
48
-
49
- // close the connection after all rows ingested
50
- await sender .close ();
28
+ // create a sender using HTTP protocol
29
+ const sender = Sender .fromConfig (" http::addr=127.0.0.1:9000" )
30
+
31
+ // add rows to the buffer of the sender
32
+ await sender
33
+ .table (" trades" )
34
+ .symbol (" symbol" , " ETH-USD" )
35
+ .symbol (" side" , " sell" )
36
+ .floatColumn (" price" , 2615.54 )
37
+ .floatColumn (" amount" , 0.00044 )
38
+ .at (Date .now (), " ms" )
39
+
40
+ // flush the buffer of the sender, sending the data to QuestDB
41
+ // the buffer is cleared after the data is sent, and the sender is ready to accept new data
42
+ await sender .flush ()
43
+
44
+ // close the connection after all rows ingested
45
+ // unflushed data will be lost
46
+ await sender .close ()
51
47
}
52
48
53
- run ()
54
- .then (console .log )
55
- .catch (console .error );
49
+ run ().then (console .log ).catch (console .error )
56
50
```
57
51
58
52
### Authentication and secure connection
59
53
60
54
``` javascript
61
- const { Sender } = require (' @questdb/nodejs-client' );
55
+ const { Sender } = require (" @questdb/nodejs-client" )
62
56
63
57
async function run () {
64
58
// create a sender using HTTPS protocol with username and password authentication
65
- const sender = Sender .fromConfig (' https::addr=localhost :9000;username=user1 ;password=pwd ' );
59
+ const sender = Sender .fromConfig (" https::addr=127.0.0.1 :9000;username=admin ;password=quest; " )
66
60
67
61
// send the data over the authenticated and secure connection
68
- await sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
69
- .floatColumn (' bid' , 1.0197 ).floatColumn (' ask' , 1.0224 )
70
- .at (Date .now (), ' ms' );
71
- await sender .flush ();
62
+ await sender
63
+ .table (" trades" )
64
+ .symbol (" symbol" , " ETH-USD" )
65
+ .symbol (" side" , " sell" )
66
+ .floatColumn (" price" , 2615.54 )
67
+ .floatColumn (" amount" , 0.00044 )
68
+ .at (Date .now (), " ms" )
69
+ await sender .flush ()
72
70
73
71
// close the connection after all rows ingested
74
- await sender .close ();
72
+ await sender .close ()
75
73
}
76
74
77
- run ().catch (console .error );
75
+ run ().catch (console .error )
78
76
```
79
77
80
78
### TypeScript example
81
79
82
80
``` typescript
83
- import { Sender } from ' @questdb/nodejs-client' ;
81
+ import { Sender } from " @questdb/nodejs-client"
84
82
85
- async function run(): Promise <number > {
83
+ async function run(): Promise <void > {
86
84
// create a sender using HTTPS protocol with bearer token authentication
87
- const sender: Sender = Sender .fromConfig (' https::addr=localhost :9000;token=Xyvd3er6GF87ysaHk' );
85
+ const sender: Sender = Sender .fromConfig (" https::addr=127.0.0.1 :9000;token=Xyvd3er6GF87ysaHk; " )
88
86
89
87
// send the data over the authenticated and secure connection
90
- sender .table (' prices' ).symbol (' instrument' , ' EURUSD' )
91
- .floatColumn (' bid' , 1.0197 ).floatColumn (' ask' , 1.0224 ).at (Date .now (), ' ms' );
92
- await sender .flush ();
88
+ await sender
89
+ .table (" trades" )
90
+ .symbol (" symbol" , " ETH-USD" )
91
+ .symbol (" side" , " sell" )
92
+ .floatColumn (" price" , 2615.54 )
93
+ .floatColumn (" amount" , 0.00044 )
94
+ .at (Date .now (), " ms" )
95
+ await sender .flush ()
93
96
94
97
// close the connection after all rows ingested
95
- await sender .close ();
98
+ await sender .close ()
96
99
}
97
100
98
101
run ().catch (console .error );
@@ -101,77 +104,79 @@ run().catch(console.error);
101
104
### Worker threads example
102
105
103
106
``` javascript
104
- const { Sender } = require (' @questdb/nodejs-client' );
105
- const { Worker , isMainThread , parentPort , workerData } = require (' worker_threads' );
107
+ const { Sender } = require (" @questdb/nodejs-client" )
108
+ const { Worker , isMainThread , parentPort , workerData } = require (" worker_threads" )
106
109
107
110
// fake venue
108
- // generates random prices for a ticker for max 5 seconds, then the feed closes
111
+ // generates random prices and amounts for a ticker for max 5 seconds, then the feed closes
109
112
function * venue (ticker ) {
110
- let end = false ;
111
- setTimeout (() => { end = true ; }, rndInt (5000 ));
113
+ let end = false
114
+ setTimeout (() => { end = true ; }, rndInt (5000 ))
112
115
while (! end) {
113
- yield {' ticker' : ticker, ' price ' : Math .random ()};
116
+ yield {ticker, price : Math . random (), amount : Math .random ()}
114
117
}
115
118
}
116
119
117
120
// market data feed simulator
118
- // uses the fake venue to deliver price updates to the feed handler (onTick() callback)
121
+ // uses the fake venue to deliver price and amount updates to the feed handler (onTick() callback)
119
122
async function subscribe (ticker , onTick ) {
120
- const feed = venue (workerData .ticker );
123
+ const feed = venue (workerData .ticker )
121
124
let tick;
122
125
while (tick = feed .next ().value ) {
123
- await onTick (tick);
124
- await sleep (rndInt (30 ));
126
+ await onTick (tick)
127
+ await sleep (rndInt (30 ))
125
128
}
126
129
}
127
130
128
131
async function run () {
129
132
if (isMainThread) {
130
- const tickers = [' t1 ' , ' t2 ' , ' t3 ' , ' t4 ' ];
133
+ const tickers = [" ETH-USD " , " BTC-USD " , " SOL-USD " , " DOGE-USD " ]
131
134
// main thread to start a worker thread for each ticker
132
- for (let ticker in tickers) {
135
+ for (let ticker of tickers) {
133
136
const worker = new Worker (__filename , { workerData: { ticker: ticker } })
134
137
.on (' error' , (err ) => { throw err; })
135
138
.on (' exit' , () => { console .log (` ${ ticker} thread exiting...` ); })
136
139
.on (' message' , (msg ) => {
137
- console .log (` Ingested ${ msg .count } prices for ticker ${ msg .ticker } ` );
140
+ console .log (` Ingested ${ msg .count } prices for ticker ${ msg .ticker } ` )
138
141
});
139
142
}
140
143
} else {
141
144
// it is important that each worker has a dedicated sender object
142
145
// threads cannot share the sender because they would write into the same buffer
143
- const sender = Sender .fromConfig (' http::addr=localhost :9000' );
146
+ const sender = Sender .fromConfig (" http::addr=127.0.0.1 :9000" );
144
147
145
148
// subscribe for the market data of the ticker assigned to the worker
146
149
// ingest each price update into the database using the sender
147
150
let count = 0 ;
148
151
await subscribe (workerData .ticker , async (tick ) => {
149
152
await sender
150
- .table (' prices' )
151
- .symbol (' ticker' , tick .ticker )
152
- .floatColumn (' price' , tick .price )
153
- .at (Date .now (), ' ms' );
153
+ .table (" trades" )
154
+ .symbol (" symbol" , tick .ticker )
155
+ .symbol (" side" , " sell" )
156
+ .floatColumn (" price" , tick .price )
157
+ .floatColumn (" amount" , tick .amount )
158
+ .at (Date .now (), " ms" )
154
159
await sender .flush ();
155
160
count++ ;
156
161
});
157
162
158
163
// let the main thread know how many prices were ingested
159
- parentPort .postMessage ({' ticker' : workerData .ticker , ' count' : count});
164
+ parentPort .postMessage ({ticker: workerData .ticker , count})
160
165
161
166
// close the connection to the database
162
- await sender .close ();
167
+ await sender .close ()
163
168
}
164
169
}
165
170
166
171
function sleep (ms ) {
167
- return new Promise (resolve => setTimeout (resolve, ms));
172
+ return new Promise (resolve => setTimeout (resolve, ms))
168
173
}
169
174
170
175
function rndInt (limit ) {
171
- return Math .floor ((Math .random () * limit) + 1 );
176
+ return Math .floor ((Math .random () * limit) + 1 )
172
177
}
173
178
174
179
run ()
175
180
.then (console .log )
176
- .catch (console .error );
181
+ .catch (console .error )
177
182
```
0 commit comments