Skip to content

Commit

Permalink
feat(nodejs): HTTP transport, configuration string, new config options (
Browse files Browse the repository at this point in the history
#26)

* feat(nodejs): HTTP transport, configuration string, new config options

* feat(nodejs): HTTP transport, configuration string, new config options

* Update src/sender.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/sender.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update types/src/sender.d.ts

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* Update src/options.js

Co-authored-by: Nick Woolmer <[email protected]>

* use sender's logger

* destroy request before retry

* fix test to work with different node versions

* custom http agent option

* custom http agent options

* destroy default http agents when last sender is closed

* buffer size comment + jsdoc fix

* tsc + jsdoc generated

* fixing tests

* fix test depends on node version

* Update src/options.js

Co-authored-by: Andrei Pechkurov <[email protected]>

* separate counters for http and tcp

* examples

* examples

* examples

* make protocol option mandatory

* remove sender instance counters

* improve docs

---------

Co-authored-by: Nick Woolmer <[email protected]>
Co-authored-by: Andrei Pechkurov <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent f8c221e commit 97a5f9e
Show file tree
Hide file tree
Showing 32 changed files with 6,409 additions and 826 deletions.
65 changes: 22 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,45 @@ The client requires Node.js v16 or newer version.
npm i -s @questdb/nodejs-client
```

## Configuration options

Detailed description of the client's configuration options can be found in
the <a href="SenderOptions.html">SenderOptions</a> documentation.

## Examples

The examples below demonstrate how to use the client. <br>
For more details, please, check the <a href="Sender.html">Sender</a>'s documentation.

### Basic API usage

```javascript
const { Sender } = require('@questdb/nodejs-client');

async function run() {
const sender = new Sender();

// connect to QuestDB
// host and port are required in connect options
await sender.connect({port: 9009, host: 'localhost'});
// create a sender using HTTP protocol
const sender = Sender.fromConfig('http::addr=localhost:9000');

// add rows to the buffer of the sender
sender.table('prices').symbol('instrument', 'EURUSD')
await sender.table('prices').symbol('instrument', 'EURUSD')
.floatColumn('bid', 1.0195).floatColumn('ask', 1.0221)
.at(Date.now(), 'ms');
sender.table('prices').symbol('instrument', 'GBPUSD')
await sender.table('prices').symbol('instrument', 'GBPUSD')
.floatColumn('bid', 1.2076).floatColumn('ask', 1.2082)
.at(Date.now(), 'ms');

// flush the buffer of the sender, sending the data to QuestDB
// the buffer is cleared after the data is sent and the sender is ready to accept new data
// the buffer is cleared after the data is sent, and the sender is ready to accept new data
await sender.flush();

// add rows to the buffer again and send it to the server
sender.table('prices').symbol('instrument', 'EURUSD')
// add rows to the buffer again, and send it to the server
await sender.table('prices').symbol('instrument', 'EURUSD')
.floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
.at(Date.now(), 'ms');
await sender.flush();

// close the connection after all rows ingested
await sender.close();
return new Promise(resolve => resolve(0));
}

run()
Expand All @@ -57,23 +61,11 @@ run()
const { Sender } = require('@questdb/nodejs-client');

async function run() {
// authentication details
const CLIENT_ID = 'testapp';
const PRIVATE_KEY = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
const AUTH = {
keyId: CLIENT_ID,
token: PRIVATE_KEY
};

// pass the authentication details to the sender
const sender = new Sender({auth: AUTH});

// connect() takes an optional second argument
// if 'true' passed the connection is secured with TLS encryption
await sender.connect({port: 9009, host: 'localhost'}, true);
// create a sender using HTTPS protocol with username and password authentication
const sender = Sender.fromConfig('https::addr=localhost:9000;username=user1;password=pwd');

// send the data over the authenticated and secure connection
sender.table('prices').symbol('instrument', 'EURUSD')
await sender.table('prices').symbol('instrument', 'EURUSD')
.floatColumn('bid', 1.0197).floatColumn('ask', 1.0224)
.at(Date.now(), 'ms');
await sender.flush();
Expand All @@ -91,20 +83,8 @@ run().catch(console.error);
import { Sender } from '@questdb/nodejs-client';

async function run(): Promise<number> {
// authentication details
const CLIENT_ID: string = 'testapp';
const PRIVATE_KEY: string = '9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8';
const AUTH: { keyId: string, token: string } = {
keyId: CLIENT_ID,
token: PRIVATE_KEY
};

// pass the authentication details to the sender
const sender: Sender = new Sender({auth: AUTH});

// connect() takes an optional second argument
// if 'true' passed the connection is secured with TLS encryption
await sender.connect({port: 9009, host: 'localhost'}, true);
// create a sender using HTTPS protocol with bearer token authentication
const sender: Sender = Sender.fromConfig('https::addr=localhost:9000;token=Xyvd3er6GF87ysaHk');

// send the data over the authenticated and secure connection
sender.table('prices').symbol('instrument', 'EURUSD')
Expand Down Expand Up @@ -160,14 +140,13 @@ async function run() {
} else {
// it is important that each worker has a dedicated sender object
// threads cannot share the sender because they would write into the same buffer
const sender = new Sender();
await sender.connect({ port: 9009, host: 'localhost' });
const sender = Sender.fromConfig('http::addr=localhost:9000');

// subscribe for the market data of the ticker assigned to the worker
// ingest each price update into the database using the sender
let count = 0;
await subscribe(workerData.ticker, async (tick) => {
sender
await sender
.table('prices')
.symbol('ticker', tick.ticker)
.floatColumn('price', tick.price)
Expand Down
Loading

0 comments on commit 97a5f9e

Please sign in to comment.