uuid-mongodb icon indicating copy to clipboard operation
uuid-mongodb copied to clipboard

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

Open louis-vinchon opened this issue 3 years ago • 4 comments

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');

louis-vinchon avatar Oct 22 '21 00:10 louis-vinchon

this is a good idea. we should implement it.

cdimascio avatar Nov 27 '21 16:11 cdimascio

would you be interested in submitting a PR?

cdimascio avatar Jan 02 '22 22:01 cdimascio

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 using MUUID.from("DEol4JenEeqVKusA+dzMMA==") meaning I can't query the db...

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

mattbf avatar Jan 12 '23 22:01 mattbf

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

vnugent avatar Jan 28 '24 01:01 vnugent