cdimascio / uuid-mongodb

📇 Generates and parses MongoDB BSON UUIDs
MIT License
100 stars 17 forks source link

Add support for formatted UUIDs to the "from()" method. #60

Open louis-vinchon opened 2 years ago

louis-vinchon commented 2 years ago

The library has several formating options, which is nice, however it only understands one of them when you "parse" a string with the from() method, which is weird.

Steps to reproduce:

const MUUID = require('uuid-mongodb');

const uuid1 = MUUID.v4();
console.log(uuid1); // Binary { ... }
const string = uuid1.toString('N');
console.log(string); // 9cea27b4d0e44781826ede1b6f378d6f

const uuid2 = MUUID.from(string); // Error: Invalid UUID

An optional parsing parameter like we have in the toString method would make a lot of sense, e.g. MUUID.from(string, 'N');

cdimascio commented 2 years ago

this is a good idea. we should implement it.

cdimascio commented 2 years ago

would you be interested in submitting a PR?

mattbf commented 1 year ago

This is needed. I have set my 'mode' on the schema creation to 'relaxed' and everywhere else I use MUUID const mUUID = MUUID.mode('relaxed');

and in the schema: _id: { type: 'object', value: { type: 'Buffer' }, default: () => mUUID.v4() },

except when stringified, the documents return the 'canonical' version of the string. I am unable to convert this canonical version to a uuid usingMUUID.from("DEol4JenEeqVKusA+dzMMA==") meaning I can't query the db...

@cdimascio does relaxed work with v4? What am I doing wrong here?

vnugent commented 7 months ago

I'm looking for the same solution, how to parse the canonical version hJVV+w11SZqk+vK9BSCpeA== or convert it to "relaxed" version?