Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bson_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export abstract class BSONValue {
public get [bsonType](): this['_bsontype'] {
return this._bsontype;
}
public get [Symbol.toStringTag](): string {
return this._bsontype;
}

/** @internal */
get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION {
Expand Down
11 changes: 11 additions & 0 deletions test/node/bson_type_classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ describe('BSON Type classes common interfaces', () => {
));
}

if (TypeClass.name !== 'UUID') {
it(`defines Symbol.toStringTag equal to its name`, () =>
expect(TypeClass.prototype).to.have.property(Symbol.toStringTag, TypeClass.name));
} else {
it(`UUID inherits Symbol.toStringTag from Binary`, () =>
expect(Object.getPrototypeOf(TypeClass.prototype)).to.have.property(
Symbol.toStringTag,
'Binary'
));
Comment on lines +105 to +109
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach is right - matches the existing bsonType getter pattern on BSONValue. One suggestion: override Symbol.toStringTag on UUID to return 'UUID'. _bsontype is a wire-protocol key and must stay 'Binary' (serializer dispatches on it), but Symbol.toStringTag is a developer-facing identity hint with no wire impact - making it reflect the JS class gives nicer debug output ([object UUID]) without breaking anything. The test would then assert toStringTag === TypeClass.name uniformly across all types.

Copy link
Copy Markdown
Contributor

@nbbeeken nbbeeken Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> Object.prototype.toString.call(Buffer.alloc(0))
'[object Uint8Array]'

It is not always the case that the string tag matches the class name, because UUID is a trivial subclass of Binary and Binary is the relevant BSON type it may make sense to keep these matching the _bsontype field. It also is not only a developer hint here's an example where it's a load bearing type check.

An additional benefit to adding this field is it adds the same functionality we added when we introduced the @@mdb.bson.type symbol but without the need to import/create something from the library.

Just my opinion here :) not strongly held

}

it(`defines a Symbol.for('@@mdb.bson.version') property equal to 7`, () =>
expect(TypeClass.prototype).to.have.property(Symbol.for('@@mdb.bson.version'), 7));

Expand Down