@@ -56,149 +56,154 @@ <h2>Examples</h2>
56
56
< p > The examples below demonstrate how to use the client. < br >
57
57
For more details, please, check the < a href ="Sender.html "> Sender</ a > 's documentation.</ p >
58
58
< 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")
60
60
61
61
async 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()
85
81
}
86
82
87
- run()
88
- .then(console.log)
89
- .catch(console.error);
83
+ run().then(console.log).catch(console.error)
90
84
</ code > </ pre >
91
85
< 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")
93
87
94
88
async function run() {
95
89
// 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;")
97
91
98
92
// 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()
103
101
104
102
// close the connection after all rows ingested
105
- await sender.close();
103
+ await sender.close()
106
104
}
107
105
108
- run().catch(console.error);
106
+ run().catch(console.error)
109
107
</ code > </ pre >
110
108
< 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" ;
112
110
113
- async function run(): Promise<number > {
111
+ async function run(): Promise<void > {
114
112
// 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;")
116
114
117
115
// 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()
121
124
122
125
// close the connection after all rows ingested
123
- await sender.close();
126
+ await sender.close()
124
127
}
125
128
126
129
run().catch(console.error);
127
130
</ code > </ pre >
128
131
< 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")
131
134
132
135
// 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
134
137
function* venue(ticker) {
135
- let end = false;
136
- setTimeout(() => { end = true; }, rndInt(5000));
138
+ let end = false
139
+ setTimeout(() => { end = true; }, rndInt(5000))
137
140
while (!end) {
138
- yield {' ticker': ticker, 'price' : Math.random()};
141
+ yield {ticker, price: Math.random(), amount : Math.random()}
139
142
}
140
143
}
141
144
142
145
// 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)
144
147
async function subscribe(ticker, onTick) {
145
- const feed = venue(workerData.ticker);
148
+ const feed = venue(workerData.ticker)
146
149
let tick;
147
150
while (tick = feed.next().value) {
148
- await onTick(tick);
149
- await sleep(rndInt(30));
151
+ await onTick(tick)
152
+ await sleep(rndInt(30))
150
153
}
151
154
}
152
155
153
156
async function run() {
154
157
if (isMainThread) {
155
- const tickers = ['t1', 't2', 't3', 't4'];
158
+ const tickers = ["ETH-USD", "BTC-USD", "SOL-USD", "DOGE-USD"]
156
159
// main thread to start a worker thread for each ticker
157
- for (let ticker in tickers) {
160
+ for (let ticker of tickers) {
158
161
const worker = new Worker(__filename, { workerData: { ticker: ticker } })
159
162
.on('error', (err) => { throw err; })
160
163
.on('exit', () => { console.log(`${ticker} thread exiting...`); })
161
164
.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}`)
163
166
});
164
167
}
165
168
} else {
166
169
// it is important that each worker has a dedicated sender object
167
170
// 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" );
169
172
170
173
// subscribe for the market data of the ticker assigned to the worker
171
174
// ingest each price update into the database using the sender
172
175
let count = 0;
173
176
await subscribe(workerData.ticker, async (tick) => {
174
177
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")
179
184
await sender.flush();
180
185
count++;
181
186
});
182
187
183
188
// 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})
185
190
186
191
// close the connection to the database
187
- await sender.close();
192
+ await sender.close()
188
193
}
189
194
}
190
195
191
196
function sleep(ms) {
192
- return new Promise(resolve => setTimeout(resolve, ms));
197
+ return new Promise(resolve => setTimeout(resolve, ms))
193
198
}
194
199
195
200
function rndInt(limit) {
196
- return Math.floor((Math.random() * limit) + 1);
201
+ return Math.floor((Math.random() * limit) + 1)
197
202
}
198
203
199
204
run()
200
205
.then(console.log)
201
- .catch(console.error);
206
+ .catch(console.error)
202
207
</ code > </ pre > </ article >
203
208
</ section >
204
209
@@ -216,7 +221,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sender.ht
216
221
< br class ="clear ">
217
222
218
223
< 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)
220
225
</ footer >
221
226
222
227
< script > prettyPrint ( ) ; </ script >
0 commit comments