[COMPASS-6337] Investigate changes in NODE-4855: Add static fromX bson type creation methods to Binary and ObjectId Created: 30/Nov/22  Updated: 01/Feb/23  Resolved: 01/Feb/23

Status: Closed
Project: Compass
Component/s: None
Affects Version/s: None
Fix Version/s: No version

Type: Investigation Priority: Major - P3
Reporter: PM Bot Assignee: Unassigned
Resolution: Done Votes: 0
Labels: node-driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on NODE-4855 Add static fromX bson type creation m... Closed
Documentation Changes: Not Needed

 Description   

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

Generated at Wed Feb 07 22:42:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.