Skip to content

Commit 7f4d454

Browse files
committed
TimestampUnit type
1 parent eb0d7ee commit 7f4d454

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

src/buffer/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Buffer } from "node:buffer";
44
import { log, Logger } from "../logging";
55
import { validateColumnName, validateTableName } from "../validation";
66
import { SenderOptions } from "../options";
7-
import { isInteger, timestampToMicros, timestampToNanos } from "../utils";
7+
import { isInteger, timestampToMicros, timestampToNanos, TimestampUnit } from "../utils";
88

99
const DEFAULT_MAX_NAME_LENGTH = 127;
1010

@@ -262,11 +262,7 @@ class SenderBuffer {
262262
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
263263
* @return {Sender} Returns with a reference to this sender.
264264
*/
265-
timestampColumn(
266-
name: string,
267-
value: number | bigint,
268-
unit: "ns" | "us" | "ms" = "us",
269-
): SenderBuffer {
265+
timestampColumn(name: string, value: number | bigint, unit: TimestampUnit = "us"): SenderBuffer {
270266
if (typeof value !== "bigint" && !Number.isInteger(value)) {
271267
throw new Error(`Value must be an integer or BigInt, received ${value}`);
272268
}
@@ -286,7 +282,7 @@ class SenderBuffer {
286282
* @param {number | bigint} timestamp - Designated epoch timestamp, accepts numbers or BigInts.
287283
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
288284
*/
289-
at(timestamp: number | bigint, unit: "ns" | "us" | "ms" = "us") {
285+
at(timestamp: number | bigint, unit: TimestampUnit = "us") {
290286
if (!this.hasSymbols && !this.hasColumns) {
291287
throw new Error("The row must have a symbol or column set before it is closed");
292288
}

src/sender.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { log, Logger } from "./logging";
33
import { SenderOptions, ExtraOptions } from "./options";
44
import { SenderTransport, createTransport } from "./transport";
5-
import { isBoolean, isInteger } from "./utils";
5+
import { isBoolean, isInteger, TimestampUnit } from "./utils";
66
import { SenderBuffer } from "./buffer";
77

88
const DEFAULT_AUTO_FLUSH_INTERVAL = 1000; // 1 sec
@@ -248,11 +248,7 @@ class Sender {
248248
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
249249
* @return {Sender} Returns with a reference to this sender.
250250
*/
251-
timestampColumn(
252-
name: string,
253-
value: number | bigint,
254-
unit: "ns" | "us" | "ms" = "us",
255-
): Sender {
251+
timestampColumn(name: string, value: number | bigint, unit: TimestampUnit = "us"): Sender {
256252
this.buffer.timestampColumn(name, value, unit);
257253
return this;
258254
}
@@ -263,7 +259,7 @@ class Sender {
263259
* @param {number | bigint} timestamp - Designated epoch timestamp, accepts numbers or BigInts.
264260
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
265261
*/
266-
async at(timestamp: number | bigint, unit: "ns" | "us" | "ms" = "us") {
262+
async at(timestamp: number | bigint, unit: TimestampUnit = "us") {
267263
this.buffer.at(timestamp, unit);
268264
this.pendingRowCount++;
269265
this.log("debug", `Pending row count: ${this.pendingRowCount}`);

src/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type TimestampUnit = "ns" | "us" | "ms";
2+
13
function isBoolean(value: unknown): value is boolean {
24
return typeof value === "boolean";
35
}
@@ -8,7 +10,7 @@ function isInteger(value: unknown, lowerBound: number): value is number {
810
);
911
}
1012

11-
function timestampToMicros(timestamp: bigint, unit: "ns" | "us" | "ms") {
13+
function timestampToMicros(timestamp: bigint, unit: TimestampUnit) {
1214
switch (unit) {
1315
case "ns":
1416
return timestamp / 1000n;
@@ -17,11 +19,11 @@ function timestampToMicros(timestamp: bigint, unit: "ns" | "us" | "ms") {
1719
case "ms":
1820
return timestamp * 1000n;
1921
default:
20-
throw new Error("Unknown timestamp unit: " + unit);
22+
throw new Error(`Unknown timestamp unit: ${unit}`);
2123
}
2224
}
2325

24-
function timestampToNanos(timestamp: bigint, unit: "ns" | "us" | "ms") {
26+
function timestampToNanos(timestamp: bigint, unit: TimestampUnit) {
2527
switch (unit) {
2628
case "ns":
2729
return timestamp;
@@ -30,8 +32,8 @@ function timestampToNanos(timestamp: bigint, unit: "ns" | "us" | "ms") {
3032
case "ms":
3133
return timestamp * 1000_000n;
3234
default:
33-
throw new Error("Unknown timestamp unit: " + unit);
35+
throw new Error(`Unknown timestamp unit: ${unit}`);
3436
}
3537
}
3638

37-
export { isBoolean, isInteger, timestampToMicros, timestampToNanos };
39+
export { isBoolean, isInteger, timestampToMicros, timestampToNanos, TimestampUnit };

0 commit comments

Comments
 (0)