Skip to content

Commit 1e1b619

Browse files
authored
feat(NODE-7255): add well-known Symbol alias for _bsontype (#829)
1 parent 883f238 commit 1e1b619

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

src/bson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export {
5050
BSONRegExp,
5151
Decimal128
5252
};
53-
export { BSONValue } from './bson_value';
53+
export { BSONValue, bsonType, type BSONTypeTag } from './bson_value';
5454
export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error';
5555
export { BSONType } from './constants';
5656
export { EJSON } from './extended_json';

src/bson_value.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@ import { BSON_MAJOR_VERSION } from './constants';
22
import { type InspectFn } from './parser/utils';
33
import { BSON_VERSION_SYMBOL } from './constants';
44

5+
/** @public */
6+
export type BSONTypeTag =
7+
| 'BSONRegExp'
8+
| 'BSONSymbol'
9+
| 'ObjectId'
10+
| 'Binary'
11+
| 'Decimal128'
12+
| 'Double'
13+
| 'Int32'
14+
| 'Long'
15+
| 'MaxKey'
16+
| 'MinKey'
17+
| 'Timestamp'
18+
| 'Code'
19+
| 'DBRef';
20+
21+
/** @public */
22+
export const bsonType = Symbol.for('@@mdb.bson.type');
23+
524
/** @public */
625
export abstract class BSONValue {
726
/** @public */
8-
public abstract get _bsontype(): string;
27+
public abstract get _bsontype(): BSONTypeTag;
28+
29+
public get [bsonType](): this['_bsontype'] {
30+
return this._bsontype;
31+
}
932

1033
/** @internal */
1134
get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION {

src/timestamp.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { bsonType } from './bson_value';
12
import { BSONError } from './error';
23
import type { Int32 } from './int_32';
34
import { Long } from './long';
45
import { type InspectFn, defaultInspect } from './parser/utils';
56

67
/** @public */
7-
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
8+
export type TimestampOverrides =
9+
| '_bsontype'
10+
| 'toExtendedJSON'
11+
| 'fromExtendedJSON'
12+
| 'inspect'
13+
| typeof bsonType;
814
/** @public */
915
export type LongWithoutOverrides = new (
1016
low: unknown,
@@ -35,6 +41,9 @@ export class Timestamp extends LongWithoutOverridesClass {
3541
get _bsontype(): 'Timestamp' {
3642
return 'Timestamp';
3743
}
44+
get [bsonType](): 'Timestamp' {
45+
return 'Timestamp';
46+
}
3847

3948
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
4049

test/node/exports.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const EXPECTED_EXPORTS = [
3737
'deserialize',
3838
'calculateObjectSize',
3939
'deserializeStream',
40-
'BSON'
40+
'BSON',
41+
'bsonType'
4142
];
4243

4344
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];

test/node/type_identifier_tests.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,72 @@ const {
1414
BSONRegExp,
1515
BSONSymbol,
1616
Timestamp,
17-
UUID
17+
UUID,
18+
bsonType
1819
} = require('../register-bson');
1920

2021
describe('_bsontype identifier', () => {
2122
it('should be equal to ObjectId for ObjectId', () => {
2223
expect(ObjectId.prototype._bsontype).to.equal('ObjectId');
24+
expect(ObjectId.prototype[bsonType]).to.equal('ObjectId');
2325
});
2426
it('should be equal to BSONSymbol for BSONSymbol', () => {
2527
expect(BSONSymbol.prototype._bsontype).to.equal('BSONSymbol');
28+
expect(BSONSymbol.prototype[bsonType]).to.equal('BSONSymbol');
2629
});
2730
it('should be equal to Timestamp for Timestamp', () => {
2831
// TODO(NODE-2624): Make Timestamp hold its long value on a property rather than be a subclass
2932
// Timestamp overrides the value in its constructor
3033
const timestamp = new Timestamp({ i: 0, t: 0 });
3134
expect(timestamp._bsontype).to.equal('Timestamp');
3235
expect(Object.getPrototypeOf(Object.getPrototypeOf(timestamp))._bsontype).to.equal('Long');
36+
expect(timestamp[bsonType]).to.equal('Timestamp');
37+
expect(Object.getPrototypeOf(Object.getPrototypeOf(timestamp))[bsonType]).to.equal('Long');
3338
});
3439

3540
// All equal to their constructor names
3641
it('should be equal to Binary for Binary', () => {
3742
expect(Binary.prototype._bsontype).to.equal('Binary');
43+
expect(Binary.prototype[bsonType]).to.equal('Binary');
3844
});
3945
it('should be equal to Code for Code', () => {
4046
expect(Code.prototype._bsontype).to.equal('Code');
47+
expect(Code.prototype[bsonType]).to.equal('Code');
4148
});
4249
it('should be equal to DBRef for DBRef', () => {
4350
expect(DBRef.prototype._bsontype).to.equal('DBRef');
51+
expect(DBRef.prototype[bsonType]).to.equal('DBRef');
4452
});
4553
it('should be equal to Decimal128 for Decimal128', () => {
4654
expect(Decimal128.prototype._bsontype).to.equal('Decimal128');
55+
expect(Decimal128.prototype[bsonType]).to.equal('Decimal128');
4756
});
4857
it('should be equal to Double for Double', () => {
4958
expect(Double.prototype._bsontype).to.equal('Double');
59+
expect(Double.prototype[bsonType]).to.equal('Double');
5060
});
5161
it('should be equal to Int32 for Int32', () => {
5262
expect(Int32.prototype._bsontype).to.equal('Int32');
63+
expect(Int32.prototype[bsonType]).to.equal('Int32');
5364
});
5465
it('should be equal to Long for Long', () => {
5566
expect(Long.prototype._bsontype).to.equal('Long');
67+
expect(Long.prototype[bsonType]).to.equal('Long');
5668
});
5769
it('should be equal to MaxKey for MaxKey', () => {
5870
expect(MaxKey.prototype._bsontype).to.equal('MaxKey');
71+
expect(MaxKey.prototype[bsonType]).to.equal('MaxKey');
5972
});
6073
it('should be equal to MinKey for MinKey', () => {
6174
expect(MinKey.prototype._bsontype).to.equal('MinKey');
75+
expect(MinKey.prototype[bsonType]).to.equal('MinKey');
6276
});
6377
it('should be equal to BSONRegExp for BSONRegExp', () => {
6478
expect(BSONRegExp.prototype._bsontype).to.equal('BSONRegExp');
79+
expect(BSONRegExp.prototype[bsonType]).to.equal('BSONRegExp');
6580
});
6681
it('should be equal to Binary for UUID', () => {
6782
expect(UUID.prototype._bsontype).to.equal('Binary');
83+
expect(UUID.prototype[bsonType]).to.equal('Binary');
6884
});
6985
});

test/types/bson.test-d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import {
1818
DBRefLike,
1919
Document,
2020
Decimal128Extended,
21-
BSONValue
21+
BSONValue,
22+
bsonType,
23+
BSONTypeTag
2224
} from '../../bson'; // import from generated bson.d.ts
2325
import type { InspectFn } from '../../src/parser/utils';
2426

@@ -74,9 +76,25 @@ expectType<'MinKey'>(MinKey.prototype._bsontype)
7476
expectType<'BSONRegExp'>(BSONRegExp.prototype._bsontype)
7577
expectType<'Binary'>(UUID.prototype._bsontype)
7678

79+
expectType<'Timestamp'>(Timestamp.prototype[bsonType])
80+
expectType<'ObjectId'>(ObjectId.prototype[bsonType])
81+
expectType<'BSONSymbol'>(BSONSymbol.prototype[bsonType])
82+
expectType<'Binary'>(Binary.prototype[bsonType])
83+
expectType<'Code'>(Code.prototype[bsonType])
84+
expectType<'DBRef'>(DBRef.prototype[bsonType])
85+
expectType<'Decimal128'>(Decimal128.prototype[bsonType])
86+
expectType<'Double'>(Double.prototype[bsonType])
87+
expectType<'Int32'>(Int32.prototype[bsonType])
88+
expectType<'Long'>(Long.prototype[bsonType])
89+
expectType<'MaxKey'>(MaxKey.prototype[bsonType])
90+
expectType<'MinKey'>(MinKey.prototype[bsonType])
91+
expectType<'BSONRegExp'>(BSONRegExp.prototype[bsonType])
92+
expectType<'Binary'>(UUID.prototype[bsonType])
93+
7794
// Common BSONValue interface
7895
declare const bsonValue: BSONValue;
79-
expectType<string>(bsonValue._bsontype);
96+
expectType<BSONTypeTag>(bsonValue._bsontype);
97+
expectType<BSONTypeTag>(bsonValue[bsonType]);
8098
expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect);
8199

82100
expectNotDeprecated(new ObjectId('foo'));

0 commit comments

Comments
 (0)