@@ -56,149 +56,154 @@ <h2>Examples</h2>
5656< p > The examples below demonstrate how to use the client. < br >
5757For more details, please, check the < a href ="Sender.html "> Sender</ a > 's documentation.</ p >
5858< h3 > Basic API usage</ h3 >
59- < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(' @questdb/nodejs-client');
59+ < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(" @questdb/nodejs-client")
6060
6161async function run() {
62- // create a sender using HTTP protocol
63- const sender = Sender.fromConfig('http::addr=localhost:9000');
64-
65- // add rows to the buffer of the sender
66- await sender.table('prices').symbol('instrument', 'EURUSD')
67- .floatColumn('bid', 1.0195).floatColumn('ask', 1.0221)
68- .at(Date.now(), 'ms');
69- await sender.table('prices').symbol('instrument', 'GBPUSD')
70- .floatColumn('bid', 1.2076).floatColumn('ask', 1.2082)
71- .at(Date.now(), 'ms');
72-
73- // flush the buffer of the sender, sending the data to QuestDB
74- // the buffer is cleared after the data is sent, and the sender is ready to accept new data
75- await sender.flush();
76-
77- // add rows to the buffer again, and send it to the server
78- await sender.table('prices').symbol('instrument', 'EURUSD')
79- .floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
80- .at(Date.now(), 'ms');
81- await sender.flush();
82-
83- // close the connection after all rows ingested
84- await sender.close();
62+ // create a sender using HTTP protocol
63+ const sender = Sender.fromConfig("http::addr=127.0.0.1:9000")
64+
65+ // add rows to the buffer of the sender
66+ await sender
67+ .table("trades")
68+ .symbol("symbol", "ETH-USD")
69+ .symbol("side", "sell")
70+ .floatColumn("price", 2615.54)
71+ .floatColumn("amount", 0.00044)
72+ .at(Date.now(), "ms")
73+
74+ // flush the buffer of the sender, sending the data to QuestDB
75+ // the buffer is cleared after the data is sent, and the sender is ready to accept new data
76+ await sender.flush()
77+
78+ // close the connection after all rows ingested
79+ // unflushed data will be lost
80+ await sender.close()
8581}
8682
87- run()
88- .then(console.log)
89- .catch(console.error);
83+ run().then(console.log).catch(console.error)
9084</ code > </ pre >
9185< h3 > Authentication and secure connection</ h3 >
92- < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(' @questdb/nodejs-client');
86+ < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(" @questdb/nodejs-client")
9387
9488async function run() {
9589 // create a sender using HTTPS protocol with username and password authentication
96- const sender = Sender.fromConfig(' https::addr=localhost :9000;username=user1 ;password=pwd');
90+ const sender = Sender.fromConfig(" https::addr=127.0.0.1 :9000;username=admin ;password=quest;")
9791
9892 // send the data over the authenticated and secure connection
99- await sender.table('prices').symbol('instrument', 'EURUSD')
100- .floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
101- .at(Date.now(), 'ms');
102- await sender.flush();
93+ await sender
94+ .table("trades")
95+ .symbol("symbol", "ETH-USD")
96+ .symbol("side", "sell")
97+ .floatColumn("price", 2615.54)
98+ .floatColumn("amount", 0.00044)
99+ .at(Date.now(), "ms")
100+ await sender.flush()
103101
104102 // close the connection after all rows ingested
105- await sender.close();
103+ await sender.close()
106104}
107105
108- run().catch(console.error);
106+ run().catch(console.error)
109107</ code > </ pre >
110108< h3 > TypeScript example</ h3 >
111- < pre class ="prettyprint source lang-typescript "> < code > import { Sender } from ' @questdb/nodejs-client' ;
109+ < pre class ="prettyprint source lang-typescript "> < code > import { Sender } from " @questdb/nodejs-client" ;
112110
113- async function run(): Promise<number > {
111+ async function run(): Promise<void > {
114112 // create a sender using HTTPS protocol with bearer token authentication
115- const sender: Sender = Sender.fromConfig(' https::addr=localhost :9000;token=Xyvd3er6GF87ysaHk');
113+ const sender: Sender = Sender.fromConfig(" https::addr=127.0.0.1 :9000;token=Xyvd3er6GF87ysaHk;")
116114
117115 // send the data over the authenticated and secure connection
118- sender.table('prices').symbol('instrument', 'EURUSD')
119- .floatColumn('bid', 1.0197).floatColumn('ask', 1.0224).at(Date.now(), 'ms');
120- await sender.flush();
116+ await sender
117+ .table("trades")
118+ .symbol("symbol", "ETH-USD")
119+ .symbol("side", "sell")
120+ .floatColumn("price", 2615.54)
121+ .floatColumn("amount", 0.00044)
122+ .at(Date.now(), "ms")
123+ await sender.flush()
121124
122125 // close the connection after all rows ingested
123- await sender.close();
126+ await sender.close()
124127}
125128
126129run().catch(console.error);
127130</ code > </ pre >
128131< h3 > Worker threads example</ h3 >
129- < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(' @questdb/nodejs-client');
130- const { Worker, isMainThread, parentPort, workerData } = require(' worker_threads');
132+ < pre class ="prettyprint source lang-javascript "> < code > const { Sender } = require(" @questdb/nodejs-client")
133+ const { Worker, isMainThread, parentPort, workerData } = require(" worker_threads")
131134
132135// fake venue
133- // generates random prices for a ticker for max 5 seconds, then the feed closes
136+ // generates random prices and amounts for a ticker for max 5 seconds, then the feed closes
134137function* venue(ticker) {
135- let end = false;
136- setTimeout(() => { end = true; }, rndInt(5000));
138+ let end = false
139+ setTimeout(() => { end = true; }, rndInt(5000))
137140 while (!end) {
138- yield {' ticker': ticker, 'price' : Math.random()};
141+ yield {ticker, price: Math.random(), amount : Math.random()}
139142 }
140143}
141144
142145// market data feed simulator
143- // uses the fake venue to deliver price updates to the feed handler (onTick() callback)
146+ // uses the fake venue to deliver price and amount updates to the feed handler (onTick() callback)
144147async function subscribe(ticker, onTick) {
145- const feed = venue(workerData.ticker);
148+ const feed = venue(workerData.ticker)
146149 let tick;
147150 while (tick = feed.next().value) {
148- await onTick(tick);
149- await sleep(rndInt(30));
151+ await onTick(tick)
152+ await sleep(rndInt(30))
150153 }
151154}
152155
153156async function run() {
154157 if (isMainThread) {
155- const tickers = ['t1', 't2', 't3', 't4'];
158+ const tickers = ["ETH-USD", "BTC-USD", "SOL-USD", "DOGE-USD"]
156159 // main thread to start a worker thread for each ticker
157- for (let ticker in tickers) {
160+ for (let ticker of tickers) {
158161 const worker = new Worker(__filename, { workerData: { ticker: ticker } })
159162 .on('error', (err) => { throw err; })
160163 .on('exit', () => { console.log(`${ticker} thread exiting...`); })
161164 .on('message', (msg) => {
162- console.log(`Ingested ${msg.count} prices for ticker ${msg.ticker}`);
165+ console.log(`Ingested ${msg.count} prices for ticker ${msg.ticker}`)
163166 });
164167 }
165168 } else {
166169 // it is important that each worker has a dedicated sender object
167170 // threads cannot share the sender because they would write into the same buffer
168- const sender = Sender.fromConfig(' http::addr=localhost :9000' );
171+ const sender = Sender.fromConfig(" http::addr=127.0.0.1 :9000" );
169172
170173 // subscribe for the market data of the ticker assigned to the worker
171174 // ingest each price update into the database using the sender
172175 let count = 0;
173176 await subscribe(workerData.ticker, async (tick) => {
174177 await sender
175- .table('prices')
176- .symbol('ticker', tick.ticker)
177- .floatColumn('price', tick.price)
178- .at(Date.now(), 'ms');
178+ .table("trades")
179+ .symbol("symbol", tick.ticker)
180+ .symbol("side", "sell")
181+ .floatColumn("price", tick.price)
182+ .floatColumn("amount", tick.amount)
183+ .at(Date.now(), "ms")
179184 await sender.flush();
180185 count++;
181186 });
182187
183188 // let the main thread know how many prices were ingested
184- parentPort.postMessage({' ticker' : workerData.ticker, ' count': count});
189+ parentPort.postMessage({ticker: workerData.ticker, count})
185190
186191 // close the connection to the database
187- await sender.close();
192+ await sender.close()
188193 }
189194}
190195
191196function sleep(ms) {
192- return new Promise(resolve => setTimeout(resolve, ms));
197+ return new Promise(resolve => setTimeout(resolve, ms))
193198}
194199
195200function rndInt(limit) {
196- return Math.floor((Math.random() * limit) + 1);
201+ return Math.floor((Math.random() * limit) + 1)
197202}
198203
199204run()
200205 .then(console.log)
201- .catch(console.error);
206+ .catch(console.error)
202207</ code > </ pre > </ article >
203208 </ section >
204209
@@ -216,7 +221,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sender.ht
216221< br class ="clear ">
217222
218223< footer >
219- Documentation generated by < a href ="https://github.com/jsdoc/jsdoc "> JSDoc 4.0.2</ a > on Mon Apr 29 2024 20:05:17 GMT+0100 (British Summer Time)
224+ Documentation generated by < a href ="https://github.com/jsdoc/jsdoc "> JSDoc 4.0.2</ a > on Tue Aug 13 2024 14:27:30 GMT+0300 (Eastern European Summer Time)
220225</ footer >
221226
222227< script > prettyPrint ( ) ; </ script >
0 commit comments