-
Type: New Feature
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: BSON
Request
Add fromX methods to the Binary and ObjectId classes that provide bespoke translations from common serialized formats into the BSON types. These would be an alternative to the constructors which have some limitations or specific requirements for the myriad of inputs they support and remove some redundant translations required to use them.
Motivations
- Shell/Compass can print out easy to copy code that creates a Binary from one of these formats (currently users need to use Buffer to make bytes from base64)
- Both BSON types are commonly used as primary keys on documents, ease of use with creation/copy+paste/translation to/from helps in many scenarios for query building or document creation
AC
(It may be desirable to break up the following into subtasks)
Implementation requirements
- ObjectId and Binary (and by inheritance UUID)
- add static fromHex(hex: string) method that accepts a hex digits string to be turned into bytes before constructing the bson type
- add static fromBase64(base64: string) method that accepts a base64 string to be turned into bytes before constructing the bson type
add static fromBytes(bytes: Uint8Array | AnyArrayBuffer, byteOffset?, byteLength?) that accepts a Uint8Array (or subclass, ex. Node.js buffer) or a (Shared)ArrayBuffer and applies offset and length before constructing the bson typeFollow up in comments.
- Binary versions of the above methods must accept an optional subType: number argument.
-
- add UUID overloads that return UUID instance
Testing requirements
- Test various inputs within and out of bounds of the what the methods support
- Make sure uuids constructs as expected - pay attention to uuid v4 restriction that is planned to be removed
- is depended on by
-
NODE-5040 Add color to BSON inspect functions
- Closed
-
COMPASS-6477 Expose and test static fromX bson type creation methods to Binary and ObjectId
- Open
-
MONGOSH-1369 Expose and test static fromX bson type creation methods to Binary and ObjectId
- Closed
-
COMPASS-6337 Investigate changes in NODE-4855: Add static fromX bson type creation methods to Binary and ObjectId
- Closed
- is related to
-
NODE-5497 BSON Binary toString method returns more data than is serialized to BSON
- Closed
- related to
-
NODE-3725 Constrain new ObjectId constructor and drop support for non-relevant cases
- Closed