-
Type: Investigation
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
Not Needed
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 type
Testing requirements
- Test various inputs within and out of bounds of the what the methods support
- TBD
- depends on
-
NODE-4855 Add static fromX bson type creation methods to Binary and ObjectId
- Closed