1
1
import { Long } from './long' ;
2
+ import { isObjectLike } from './parser/utils' ;
2
3
3
4
/** @public */
4
5
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect' ;
5
6
/** @public */
6
- export type LongWithoutOverrides = new ( low : number | Long , high ?: number , unsigned ?: boolean ) => {
7
+ export type LongWithoutOverrides = new ( low : unknown , high ?: number , unsigned ?: boolean ) => {
7
8
[ P in Exclude < keyof Long , TimestampOverrides > ] : Long [ P ] ;
8
9
} ;
9
10
/** @public */
@@ -27,20 +28,26 @@ export class Timestamp extends LongWithoutOverridesClass {
27
28
/**
28
29
* @param low - A 64-bit Long representing the Timestamp.
29
30
*/
30
- constructor ( low : Long ) ;
31
+ constructor ( long : Long ) ;
32
+ /**
33
+ * @param value - A pair of two values indicating timestamp and increment.
34
+ */
35
+ constructor ( value : { t : number ; i : number } ) ;
31
36
/**
32
37
* @param low - the low (signed) 32 bits of the Timestamp.
33
38
* @param high - the high (signed) 32 bits of the Timestamp.
39
+ * @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
34
40
*/
35
- constructor ( low : Long ) ;
36
41
constructor ( low : number , high : number ) ;
37
- constructor ( low : number | Long , high ?: number ) {
42
+ constructor ( low : number | Long | { t : number ; i : number } , high ?: number ) {
38
43
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
39
44
///@ts -expect-error
40
45
if ( ! ( this instanceof Timestamp ) ) return new Timestamp ( low , high ) ;
41
46
42
47
if ( Long . isLong ( low ) ) {
43
48
super ( low . low , low . high , true ) ;
49
+ } else if ( isObjectLike ( low ) && typeof low . t !== 'undefined' && typeof low . i !== 'undefined' ) {
50
+ super ( low . i , low . t , true ) ;
44
51
} else {
45
52
super ( low , high , true ) ;
46
53
}
@@ -95,7 +102,7 @@ export class Timestamp extends LongWithoutOverridesClass {
95
102
96
103
/** @internal */
97
104
static fromExtendedJSON ( doc : TimestampExtended ) : Timestamp {
98
- return new Timestamp ( doc . $timestamp . i , doc . $timestamp . t ) ;
105
+ return new Timestamp ( doc . $timestamp ) ;
99
106
}
100
107
101
108
/** @internal */
@@ -104,6 +111,6 @@ export class Timestamp extends LongWithoutOverridesClass {
104
111
}
105
112
106
113
inspect ( ) : string {
107
- return `new Timestamp(${ this . getLowBits ( ) . toString ( ) } , ${ this . getHighBits ( ) . toString ( ) } )` ;
114
+ return `new Timestamp({ t: ${ this . getHighBits ( ) } , i: ${ this . getLowBits ( ) } })` ;
108
115
}
109
116
}
0 commit comments