@@ -22,77 +22,80 @@ For more details, please, check the <a href="Sender.html">Sender</a>'s documenta
2222### Basic API usage
2323
2424``` javascript
25- const { Sender } = require (' @questdb/nodejs-client' );
25+ const { Sender } = require (" @questdb/nodejs-client" )
2626
2727async 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 ()
5147}
5248
53- run ()
54- .then (console .log )
55- .catch (console .error );
49+ run ().then (console .log ).catch (console .error )
5650```
5751
5852### Authentication and secure connection
5953
6054``` javascript
61- const { Sender } = require (' @questdb/nodejs-client' );
55+ const { Sender } = require (" @questdb/nodejs-client" )
6256
6357async function run () {
6458 // 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; " )
6660
6761 // 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 ()
7270
7371 // close the connection after all rows ingested
74- await sender .close ();
72+ await sender .close ()
7573}
7674
77- run ().catch (console .error );
75+ run ().catch (console .error )
7876```
7977
8078### TypeScript example
8179
8280``` typescript
83- import { Sender } from ' @questdb/nodejs-client' ;
81+ import { Sender } from " @questdb/nodejs-client"
8482
85- async function run(): Promise <number > {
83+ async function run(): Promise <void > {
8684 // 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; " )
8886
8987 // 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 ()
9396
9497 // close the connection after all rows ingested
95- await sender .close ();
98+ await sender .close ()
9699}
97100
98101run ().catch (console .error );
@@ -101,77 +104,79 @@ run().catch(console.error);
101104### Worker threads example
102105
103106``` 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" )
106109
107110// 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
109112function * venue (ticker ) {
110- let end = false ;
111- setTimeout (() => { end = true ; }, rndInt (5000 ));
113+ let end = false
114+ setTimeout (() => { end = true ; }, rndInt (5000 ))
112115 while (! end) {
113- yield {' ticker' : ticker, ' price ' : Math .random ()};
116+ yield {ticker, price : Math . random (), amount : Math .random ()}
114117 }
115118}
116119
117120// 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)
119122async function subscribe (ticker , onTick ) {
120- const feed = venue (workerData .ticker );
123+ const feed = venue (workerData .ticker )
121124 let tick;
122125 while (tick = feed .next ().value ) {
123- await onTick (tick);
124- await sleep (rndInt (30 ));
126+ await onTick (tick)
127+ await sleep (rndInt (30 ))
125128 }
126129}
127130
128131async function run () {
129132 if (isMainThread) {
130- const tickers = [' t1 ' , ' t2 ' , ' t3 ' , ' t4 ' ];
133+ const tickers = [" ETH-USD " , " BTC-USD " , " SOL-USD " , " DOGE-USD " ]
131134 // main thread to start a worker thread for each ticker
132- for (let ticker in tickers) {
135+ for (let ticker of tickers) {
133136 const worker = new Worker (__filename , { workerData: { ticker: ticker } })
134137 .on (' error' , (err ) => { throw err; })
135138 .on (' exit' , () => { console .log (` ${ ticker} thread exiting...` ); })
136139 .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 } ` )
138141 });
139142 }
140143 } else {
141144 // it is important that each worker has a dedicated sender object
142145 // 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" );
144147
145148 // subscribe for the market data of the ticker assigned to the worker
146149 // ingest each price update into the database using the sender
147150 let count = 0 ;
148151 await subscribe (workerData .ticker , async (tick ) => {
149152 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" )
154159 await sender .flush ();
155160 count++ ;
156161 });
157162
158163 // 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})
160165
161166 // close the connection to the database
162- await sender .close ();
167+ await sender .close ()
163168 }
164169}
165170
166171function sleep (ms ) {
167- return new Promise (resolve => setTimeout (resolve, ms));
172+ return new Promise (resolve => setTimeout (resolve, ms))
168173}
169174
170175function rndInt (limit ) {
171- return Math .floor ((Math .random () * limit) + 1 );
176+ return Math .floor ((Math .random () * limit) + 1 )
172177}
173178
174179run ()
175180 .then (console .log )
176- .catch (console .error );
181+ .catch (console .error )
177182```
0 commit comments