Skip to content

Commit 23dcc15

Browse files
committed
feat(NODE-3504): add unambiguous Timestamp() ctor overload
Add a `Timestamp({ t: <number>, i: <number> })` overload, and mark the `Timestamp(low, high)` overload as deprecated.
1 parent 6ceaa05 commit 23dcc15

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/timestamp.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Long } from './long';
2+
import { isObjectLike } from './parser/utils';
23

34
/** @public */
45
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
56
/** @public */
6-
export type LongWithoutOverrides = new (low: number | Long, high?: number, unsigned?: boolean) => {
7+
export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
78
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
89
};
910
/** @public */
@@ -26,20 +27,26 @@ export class Timestamp extends LongWithoutOverridesClass {
2627
/**
2728
* @param low - A 64-bit Long representing the Timestamp.
2829
*/
29-
constructor(low: Long);
30+
constructor(long: Long);
31+
/**
32+
* @param value - A pair of two values indicating timestamp and increment.
33+
*/
34+
constructor(value: { t: number; i: number });
3035
/**
3136
* @param low - the low (signed) 32 bits of the Timestamp.
3237
* @param high - the high (signed) 32 bits of the Timestamp.
38+
* @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
3339
*/
34-
constructor(low: Long);
3540
constructor(low: number, high: number);
36-
constructor(low: number | Long, high?: number) {
41+
constructor(low: number | Long | { t: number; i: number }, high?: number) {
3742
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3843
///@ts-expect-error
3944
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
4045

4146
if (Long.isLong(low)) {
4247
super(low.low, low.high, true);
48+
} else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
49+
super(low.i, low.t, true);
4350
} else {
4451
super(low, high, true);
4552
}
@@ -94,7 +101,7 @@ export class Timestamp extends LongWithoutOverridesClass {
94101

95102
/** @internal */
96103
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
97-
return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
104+
return new Timestamp(doc.$timestamp);
98105
}
99106

100107
/** @internal */

test/node/timestamp_tests.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe('Timestamp', function () {
1515
new BSON.Timestamp(-1, -1),
1616
new BSON.Timestamp(new BSON.Timestamp(0xffffffff, 0xffffffff)),
1717
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, false)),
18-
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, true))
18+
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, true)),
19+
new BSON.Timestamp({ t: 0xffffffff, i: 0xfffffffff }),
20+
new BSON.Timestamp({ t: -1, i: -1 })
1921
].forEach(timestamp => {
2022
expect(timestamp).to.have.property('unsigned', true);
2123
});
@@ -29,4 +31,10 @@ describe('Timestamp', function () {
2931
$timestamp: { t: 4294967295, i: 4294967295 }
3032
});
3133
});
34+
35+
it('should accept a { t, i } object as constructor input', function () {
36+
const input = { t: 89, i: 144 };
37+
const timestamp = new BSON.Timestamp(input);
38+
expect(timestamp.toExtendedJSON()).to.deep.equal({ $timestamp: input });
39+
});
3240
});

0 commit comments

Comments
 (0)