Skip to content

Commit 92fd173

Browse files
javierpuzpuzpuz
andauthored
[docs] Changed code examples and bumped dev dependency (#35)
* changed code examples * changing examples to use at with a date. Bumped dependency for audit * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update examples/workers.js consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update examples/workers.js consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> * Update README.md consistent quotes Co-authored-by: Andrei Pechkurov <[email protected]> --------- Co-authored-by: Andrei Pechkurov <[email protected]>
1 parent d4fc1bf commit 92fd173

File tree

7 files changed

+218
-218
lines changed

7 files changed

+218
-218
lines changed

README.md

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2727
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()
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

6357
async 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

98101
run().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
109112
function* 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)
119122
async 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

128131
async 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

166171
function sleep(ms) {
167-
return new Promise(resolve => setTimeout(resolve, ms));
172+
return new Promise(resolve => setTimeout(resolve, ms))
168173
}
169174

170175
function rndInt(limit) {
171-
return Math.floor((Math.random() * limit) + 1);
176+
return Math.floor((Math.random() * limit) + 1)
172177
}
173178

174179
run()
175180
.then(console.log)
176-
.catch(console.error);
181+
.catch(console.error)
177182
```

examples/auth.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ const { Sender } = require('@questdb/nodejs-client');
22

33
async function run() {
44
// authentication details
5-
const CLIENT_ID = 'testapp';
6-
const PRIVATE_KEY = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
5+
const CLIENT_ID = 'admin'
6+
const PRIVATE_KEY = 'ZRxmCOQBpZoj2fZ-lEtqzVDkCre_ouF3ePpaQNDwoQk'
77
const AUTH = {
88
keyId: CLIENT_ID,
99
token: PRIVATE_KEY
10-
};
10+
}
1111

1212
// pass the authentication details to the sender
13-
const sender = new Sender({ protocol: 'tcp', host: 'localhost', port: 9009, bufferSize: 4096, auth: AUTH });
14-
await sender.connect();
13+
const sender = new Sender({ protocol: 'tcp', host: '127.0.0.1', port: 9009, bufferSize: 4096, auth: AUTH })
14+
await sender.connect()
1515

1616
// send the data over the authenticated connection
17-
let bday = Date.parse('1856-07-10');
1817
await sender
19-
.table('inventors_nodejs')
20-
.symbol('born', 'Austrian Empire')
21-
.timestampColumn('birthday', bday, 'ms') // epoch in millis
22-
.intColumn('id', 0)
23-
.stringColumn('name', 'Nicola Tesla')
24-
.at(Date.now(), 'ms'); // epoch in millis
25-
bday = Date.parse('1847-02-11');
18+
.table("trades")
19+
.symbol("symbol", "ETH-USD")
20+
.symbol("side", "sell")
21+
.floatColumn("price", 2615.54)
22+
.floatColumn("amount", 0.00044)
23+
.at(Date.now(), 'ms')
24+
25+
// add rows to the buffer of the sender
2626
await sender
27-
.table('inventors_nodejs')
28-
.symbol('born', 'USA')
29-
.timestampColumn('birthday', bday, 'ms')
30-
.intColumn('id', 1)
31-
.stringColumn('name', 'Thomas Alva Edison')
32-
.at(Date.now(), 'ms');
27+
.table("trades")
28+
.symbol("symbol", "BTC-USD")
29+
.symbol("side", "sell")
30+
.floatColumn("price", 39269.98)
31+
.floatColumn("amount", 0.001)
32+
.at(Date.now(), 'ms')
3333

3434
// flush the buffer of the sender, sending the data to QuestDB
35-
await sender.flush();
35+
await sender.flush()
3636

3737
// close the connection after all rows ingested
38-
await sender.close();
38+
await sender.close()
3939
}
4040

41-
run().catch(console.error);
41+
run().catch(console.error)

examples/auth_tls.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ const { Sender } = require('@questdb/nodejs-client');
22

33
async function run() {
44
// authentication details
5-
const CLIENT_ID = 'testapp';
6-
const PRIVATE_KEY = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
5+
const CLIENT_ID = 'admin'
6+
const PRIVATE_KEY = 'ZRxmCOQBpZoj2fZ-lEtqzVDkCre_ouF3ePpaQNDwoQk'
77
const AUTH = {
88
keyId: CLIENT_ID,
99
token: PRIVATE_KEY
10-
};
10+
}
1111

1212
// pass the authentication details to the sender
13-
const sender = new Sender({ protocol: 'tcps', host: 'localhost', port: 9009, bufferSize: 4096, auth: AUTH });
14-
await sender.connect();
13+
const sender = new Sender({ protocol: 'tcps', host: '127.0.0.1', port: 9009, bufferSize: 4096, auth: AUTH })
14+
await sender.connect()
1515

16-
// send the data over the authenticated and secure connection
17-
let bday = Date.parse('1856-07-10');
16+
// send the data over the authenticated connection
1817
await sender
19-
.table('inventors_nodejs')
20-
.symbol('born', 'Austrian Empire')
21-
.timestampColumn('birthday', bday, 'ms') // epoch in millis
22-
.intColumn('id', 0)
23-
.stringColumn('name', 'Nicola Tesla')
24-
.at(Date.now(), 'ms'); // epoch in millis
25-
bday = Date.parse('1847-02-11');
18+
.table("trades")
19+
.symbol("symbol", "ETH-USD")
20+
.symbol("side", "sell")
21+
.floatColumn("price", 2615.54)
22+
.floatColumn("amount", 0.00044)
23+
.at(Date.now(), 'ms')
24+
25+
// add rows to the buffer of the sender
2626
await sender
27-
.table('inventors_nodejs')
28-
.symbol('born', 'USA')
29-
.timestampColumn('birthday', bday, 'ms')
30-
.intColumn('id', 1)
31-
.stringColumn('name', 'Thomas Alva Edison')
32-
.at(Date.now(), 'ms');
27+
.table("trades")
28+
.symbol("symbol", "BTC-USD")
29+
.symbol("side", "sell")
30+
.floatColumn("price", 39269.98)
31+
.floatColumn("amount", 0.001)
32+
.at(Date.now(), 'ms')
3333

3434
// flush the buffer of the sender, sending the data to QuestDB
35-
await sender.flush();
35+
await sender.flush()
3636

3737
// close the connection after all rows ingested
38-
await sender.close();
38+
await sender.close()
3939
}
4040

41-
run().catch(console.error);
41+
run().catch(console.error)

0 commit comments

Comments
 (0)