Skip to content

Commit 4ba7774

Browse files
puzpuzpuzglasstiger
andauthoredSep 18, 2023
feat(nodejs): support BigInt type for timestamp values (#20)
* feat(nodejs): support BigInt type for timestamp values * Fix typo * fix doc and version bump --------- Co-authored-by: glasstiger <imre@questdb.io>
1 parent 82ced72 commit 4ba7774

16 files changed

+106
-121
lines changed
 

‎docs/Sender.html

+10-4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ <h5>Parameters:</h5>
281281

282282

283283
<span class="param-type">string</span>
284+
|
285+
286+
<span class="param-type">bigint</span>
284287

285288

286289

@@ -290,7 +293,7 @@ <h5>Parameters:</h5>
290293

291294

292295

293-
<td class="description last">A string represents the designated timestamp in nanoseconds.</td>
296+
<td class="description last">A string or BigInt that represents the designated timestamp in epoch nanoseconds.</td>
294297
</tr>
295298

296299

@@ -420,7 +423,7 @@ <h4 class="name" id="atNow"><span class="type-signature"></span>atNow<span class
420423

421424
<dt class="tag-source">Source:</dt>
422425
<dd class="tag-source"><ul class="dummy"><li>
423-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line380">line 380</a>
426+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line381">line 381</a>
424427
</li></ul></dd>
425428

426429

@@ -2266,6 +2269,9 @@ <h5>Parameters:</h5>
22662269

22672270

22682271
<span class="param-type">number</span>
2272+
|
2273+
2274+
<span class="param-type">bigint</span>
22692275

22702276

22712277

@@ -2275,7 +2281,7 @@ <h5>Parameters:</h5>
22752281

22762282

22772283

2278-
<td class="description last">Column value, accepts only number objects.</td>
2284+
<td class="description last">Epoch timestamp in microseconds, accepts only numbers or BigInts.</td>
22792285
</tr>
22802286

22812287

@@ -2390,7 +2396,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
23902396
<br class="clear">
23912397

23922398
<footer>
2393-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri May 26 2023 10:30:37 GMT+0100 (British Summer Time)
2399+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Mon Sep 18 2023 15:34:51 GMT+0100 (British Summer Time)
23942400
</footer>
23952401

23962402
<script> prettyPrint(); </script>

‎docs/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
248248
<br class="clear">
249249

250250
<footer>
251-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri May 26 2023 10:30:37 GMT+0100 (British Summer Time)
251+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Mon Sep 18 2023 15:34:51 GMT+0100 (British Summer Time)
252252
</footer>
253253

254254
<script> prettyPrint(); </script>

‎docs/index.js.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
5353
<br class="clear">
5454

5555
<footer>
56-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri May 26 2023 10:30:37 GMT+0100 (British Summer Time)
56+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Mon Sep 18 2023 15:34:51 GMT+0100 (British Summer Time)
5757
</footer>
5858

5959
<script> prettyPrint(); </script>

‎docs/module-@questdb_nodejs-client.html

+2-38
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ <h1 class="page-title">Module: @questdb/nodejs-client</h1>
2727
<section>
2828

2929
<header>
30-
31-
32-
33-
34-
30+
3531
</header>
3632

3733
<article>
@@ -42,20 +38,6 @@ <h1 class="page-title">Module: @questdb/nodejs-client</h1>
4238

4339

4440

45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
5941

6042

6143
<dl class="details">
@@ -98,24 +80,6 @@ <h1 class="page-title">Module: @questdb/nodejs-client</h1>
9880

9981
</dl>
10082

101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
115-
116-
117-
118-
11983

12084

12185

@@ -156,7 +120,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
156120
<br class="clear">
157121

158122
<footer>
159-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri May 26 2023 10:30:37 GMT+0100 (British Summer Time)
123+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Mon Sep 18 2023 15:34:51 GMT+0100 (British Summer Time)
160124
</footer>
161125

162126
<script> prettyPrint(); </script>

‎docs/src_sender.js.html

+14-13
Original file line numberDiff line numberDiff line change
@@ -357,46 +357,47 @@ <h1 class="page-title">Source: src/sender.js</h1>
357357
checkCapacity(this, [valueStr], 1 + valueStr.length);
358358
write(this, valueStr);
359359
write(this, 'i');
360-
}, "number");
360+
});
361361
return this;
362362
}
363363

364364
/**
365365
* Write a timestamp column with its value into the buffer of the sender.
366366
*
367367
* @param {string} name - Column name.
368-
* @param {number} value - Column value, accepts only number objects.
368+
* @param {number | bigint} value - Epoch timestamp in microseconds, accepts only numbers or BigInts.
369369
* @return {Sender} Returns with a reference to this sender.
370370
*/
371371
timestampColumn(name, value) {
372-
if (!Number.isInteger(value)) {
373-
throw new Error(`Value must be an integer, received ${value}`);
372+
if (typeof value !== "bigint" &amp;&amp; !Number.isInteger(value)) {
373+
throw new Error(`Value must be an integer or BigInt, received ${value}`);
374374
}
375375
writeColumn(this, name, value, () => {
376376
const valueStr = value.toString();
377377
checkCapacity(this, [valueStr], 1 + valueStr.length);
378378
write(this, valueStr);
379379
write(this, 't');
380-
}, "number");
380+
});
381381
return this;
382382
}
383383

384384
/**
385385
* Closing the row after writing the designated timestamp into the buffer of the sender.
386386
*
387-
* @param {string} timestamp - A string represents the designated timestamp in nanoseconds.
387+
* @param {string | bigint} timestamp - A string or BigInt that represents the designated timestamp in epoch nanoseconds.
388388
*/
389389
at(timestamp) {
390390
if (!this.hasSymbols &amp;&amp; !this.hasColumns) {
391391
throw new Error("The row must have a symbol or column set before it is closed");
392392
}
393-
if (typeof timestamp !== "string") {
394-
throw new Error(`The designated timestamp must be of type string, received ${typeof timestamp}`);
393+
if (typeof timestamp !== "string" &amp;&amp; typeof timestamp !== "bigint") {
394+
throw new Error(`The designated timestamp must be of type string or BigInt, received ${typeof timestamp}`);
395395
}
396396
validateDesignatedTimestamp(timestamp);
397-
checkCapacity(this, [], 2 + timestamp.length);
397+
const timestampStr = timestamp.toString();
398+
checkCapacity(this, [], 2 + timestampStr.length);
398399
write(this, ' ');
399-
write(this, timestamp);
400+
write(this, timestampStr);
400401
write(this, '\n');
401402
startNewRow(this);
402403
}
@@ -469,7 +470,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
469470
if (typeof name !== "string") {
470471
throw new Error(`Column name must be a string, received ${typeof name}`);
471472
}
472-
if (typeof value !== valueType) {
473+
if (valueType != null &amp;&amp; typeof value !== valueType) {
473474
throw new Error(`Column value must be of type ${valueType}, received ${typeof value}`);
474475
}
475476
if (!sender.hasTable) {
@@ -529,7 +530,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
529530
}
530531

531532
exports.Sender = Sender;
532-
exports.DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE
533+
exports.DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
533534
</code></pre>
534535
</article>
535536
</section>
@@ -546,7 +547,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
546547
<br class="clear">
547548

548549
<footer>
549-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Fri May 26 2023 10:30:37 GMT+0100 (British Summer Time)
550+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Mon Sep 18 2023 15:34:51 GMT+0100 (British Summer Time)
550551
</footer>
551552

552553
<script> prettyPrint(); </script>

‎examples/basic.js

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
11
const { Sender } = require("@questdb/nodejs-client");
22

33
async function run() {
4-
// create a sender with a 4k buffer
4+
// create a sender with a 4KB buffer
55
const sender = new Sender({ bufferSize: 4096 });
66

77
// connect to QuestDB
88
// host and port are required in connect options
99
await sender.connect({ port: 9009, host: "localhost" });
1010

1111
// add rows to the buffer of the sender
12+
let bday = Date.parse("1856-07-10");
1213
sender
13-
.table("prices")
14-
.symbol("instrument", "EURUSD")
15-
.floatColumn("bid", 1.0195)
16-
.floatColumn("ask", 1.0221)
17-
.atNow();
18-
14+
.table("inventors")
15+
.symbol("born", "Austrian Empire")
16+
.timestampColumn("birthday", BigInt(bday) * 1000n) // epoch in micros (BigInt)
17+
.intColumn("id", 0)
18+
.stringColumn("name", "Nicola Tesla")
19+
.at(BigInt(Date.now()) * 1000_000n); // epoch in nanos (BigInt)
20+
bday = Date.parse("1847-02-11");
1921
sender
20-
.table("prices")
21-
.symbol("instrument", "GBPUSD")
22-
.floatColumn("bid", 1.2076)
23-
.floatColumn("ask", 1.2082)
22+
.table("inventors")
23+
.symbol("born", "USA")
24+
.timestampColumn("birthday", BigInt(bday) * 1000n)
25+
.intColumn("id", 1)
26+
.stringColumn("name", "Thomas Alva Edison")
2427
.atNow();
2528

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

30-
// add rows to the buffer again and send it to the server
31-
sender
32-
.table("prices")
33-
.symbol("instrument", "EURUSD")
34-
.floatColumn("bid", 1.0197)
35-
.floatColumn("ask", 1.0224)
36-
.atNow();
37-
38-
await sender.flush();
39-
40-
// close the connection after all rows ingested
33+
// close the connection after all rows were sent
4134
await sender.close();
4235
return 0;
4336
}
4437

4538
run()
46-
.then((value) => console.log(value))
47-
.catch((err) => console.log(err));
39+
.then(console.log)
40+
.catch(console.error);

‎notes.md

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
### Certs used in tests generated by running:
22
> ./scripts/generateCerts.sh . questdbPwd123
3-
4-
### TODO:
5-
- Would be nice to accept alternative logger implementations to be used instead of console
6-
- If the buffer had to be extended, shrink it back to original size on a subsequent flush() call?

‎package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@questdb/nodejs-client",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "QuestDB Node.js Client",
55
"main": "index.js",
66
"types": "types/index.d.ts",

‎src/sender.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -329,46 +329,47 @@ class Sender {
329329
checkCapacity(this, [valueStr], 1 + valueStr.length);
330330
write(this, valueStr);
331331
write(this, 'i');
332-
}, "number");
332+
});
333333
return this;
334334
}
335335

336336
/**
337337
* Write a timestamp column with its value into the buffer of the sender.
338338
*
339339
* @param {string} name - Column name.
340-
* @param {number} value - Column value, accepts only number objects.
340+
* @param {number | bigint} value - Epoch timestamp in microseconds, accepts only numbers or BigInts.
341341
* @return {Sender} Returns with a reference to this sender.
342342
*/
343343
timestampColumn(name, value) {
344-
if (!Number.isInteger(value)) {
345-
throw new Error(`Value must be an integer, received ${value}`);
344+
if (typeof value !== "bigint" && !Number.isInteger(value)) {
345+
throw new Error(`Value must be an integer or BigInt, received ${value}`);
346346
}
347347
writeColumn(this, name, value, () => {
348348
const valueStr = value.toString();
349349
checkCapacity(this, [valueStr], 1 + valueStr.length);
350350
write(this, valueStr);
351351
write(this, 't');
352-
}, "number");
352+
});
353353
return this;
354354
}
355355

356356
/**
357357
* Closing the row after writing the designated timestamp into the buffer of the sender.
358358
*
359-
* @param {string} timestamp - A string represents the designated timestamp in nanoseconds.
359+
* @param {string | bigint} timestamp - A string or BigInt that represents the designated timestamp in epoch nanoseconds.
360360
*/
361361
at(timestamp) {
362362
if (!this.hasSymbols && !this.hasColumns) {
363363
throw new Error("The row must have a symbol or column set before it is closed");
364364
}
365-
if (typeof timestamp !== "string") {
366-
throw new Error(`The designated timestamp must be of type string, received ${typeof timestamp}`);
365+
if (typeof timestamp !== "string" && typeof timestamp !== "bigint") {
366+
throw new Error(`The designated timestamp must be of type string or BigInt, received ${typeof timestamp}`);
367367
}
368368
validateDesignatedTimestamp(timestamp);
369-
checkCapacity(this, [], 2 + timestamp.length);
369+
const timestampStr = timestamp.toString();
370+
checkCapacity(this, [], 2 + timestampStr.length);
370371
write(this, ' ');
371-
write(this, timestamp);
372+
write(this, timestampStr);
372373
write(this, '\n');
373374
startNewRow(this);
374375
}
@@ -441,7 +442,7 @@ function writeColumn(sender, name, value, writeValue, valueType) {
441442
if (typeof name !== "string") {
442443
throw new Error(`Column name must be a string, received ${typeof name}`);
443444
}
444-
if (typeof value !== valueType) {
445+
if (valueType != null && typeof value !== valueType) {
445446
throw new Error(`Column value must be of type ${valueType}, received ${typeof value}`);
446447
}
447448
if (!sender.hasTable) {

0 commit comments

Comments
 (0)
Failed to load comments.