New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UUID Schema Type (BSON Buffer SubType 4) #12268
Conversation
May I suggest something regarding UUID. I would actually prefer if you would implement a generic UUID-type. mongo is not differentiating imho between UUID v1 or v4. What we could do then, would be to assign a uuid generator to the default method of the schematype.So if somebody wants for some reason to use uuid v5 or the maybe more interesting v6, than dev can override the generator. |
@Uzlopak thats basically what i had also suggested in though i would like some implementation details, like should the version be required / defaulted to something like 4 or default to none, or also no version support at all, and if version support, what option name to set the version, like |
I apologize @hasezoey I also wanted to implement at one time uuid for mongoose, so i also had once worked on that branch. So it is nice to be on the same page ;). Regarding subtype 3 and 4 is nothing uuid itself specific, but how mongodb was storing uuids internally. See: The thing is, that nodejs has builtin uuid v4. But probably v6 would be better regarding indexing, but v6 is not even implemented in the uuid package. I expect actually no good by setting v4 as default. It could be that actually v6 is better regarding indexing (never benchmarked). But because we would decide that we ues v4 because it is builtin in node, I expect actually bike shedding. So instead to actively decide one of the uuid-versions by the implementers, they assume that v4 is probably the best for a mongo collectoin because mongoose people had decided to use it so they know more than we do. |
thanks for clarifying
i take from this the following:
|
with 4c50837 i have renamed it to |
i think i have now resolved the errors i was encountering in the tests, it came down to using the default connection instead of the one created |
update to the current state of the PR:
|
regarding the unit test. I think you can use directly the mongo driver and check if findOne has a raw options. Then you would get the bson Buffer. bson should be always the same structure, so you can theoretically slice the range of the field and its value and check if it has the right data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of quick suggestions but otherwise looks good!
Any thoughts on keeping this in Mongoose itself versus putting uuid type in a plugin? Right now I'm leaning toward putting this in the Mongoose repo, because it doesn't add too much code weight or complexity, and because I'm starting to like adding more functionality to Mongoose vs asking people to install a bunch of plugins.
assert.strictEqual(doc.x, '09190f70-3d30-11e5-8814-0f4df9a59c41'); | ||
await doc.save(); | ||
|
||
const query = Model.findOne({ x: '09190f70-3d30-11e5-8814-0f4df9a59c41' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a test using $lte
and $nin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will try to add one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just looked into $lte
, though i dont quite understand how that is supposed to work with UUID, because from what i can tell there is not "less than" or "higher than" in UUID (at least in the generic implementation and not having a specific version)
also mongodb does not list anything that $lte
(or $lt
, $gt
, $gte
) would work on UUID, do they work on buffers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hasezoey
Assuming that we store UUIDs as strings in MongoDB, $lt/$gt in mongo generally works with strings by sorting alphabetically. Check this mongo playground, for example.
I fail to see how that can be useful for UUIDs, though. AFAIK, UUIDs don't have any guarantees for alphabetical sorting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that we store UUIDs as strings in MongoDB
they are not, we store them as BSON UUID Buffers (bin-type 4)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does MongoDB support $gt
, $lt
, etc. for buffers? There are times when I want to sort by _id
in tests to ensure consistent sort order, regardless of what that sort order is. So it would be nice to support $gt
and $lt
, but not strictly necessary. If MongoDB doesn't support those with buffers, let's remove support for $gt
and $lt
.
I think it is fine to just support uuidv4 for now. If we want to support different uuid versions, that makes me more inclined to make uuid support a plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a plugin for mongoose 6 and uuid. But uuid is a native bson type. If it was some non-bson type then i would expect to be a plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does MongoDB support $gt, $lt, etc. for buffers?
i dont quite know, but it seems at least mongoose lists it as supported for normal buffers
But uuid is a native bson type. If it was some non-bson type then i would expect to be a plugin
that is also my reasoning
I think it is fine to just support uuidv4 for now
currently we dont check for a specific version, though i can add it as a optional option like enforceVersion: 4
(or something like that)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just set the default field with crypto.randomUUID from node. If you want to have a different uuid version a implementer can just pass something like
const { v4, v1 } = require('uuid')
new Schema({
x: { type: mongoose.Schema.Types.UUID, default: v4 },
y: [{ type: mongoose.Schema.Types.UUID, default: v1 }]
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just set the default field with crypto.randomUUID from node
would it be a good idea to provide a default for a specific version? when i should add it, how is this done in mongoose? (it probably is not as simple as setting .defaultOptions = { default: generateV4 }
, is .auto
the correct function?)
well i just did this because the old PR was just closed without comment (by the author) and it is a BSON type so i though mongoose itself should maybe support it |
because they are not supported on buffers
added with a187b01 |
because it is basically a buffer
Update to the current state of this PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good. I'll do a little more review later, but I'd like to merge this into 6.7.
updated to add tests as "todo"s that still need to be, also merged latest master |
UUID
Merging into 6.7 👍 |
@vkarpov15 Looks like this got merged without types. Created an issue for it. |
Summary
This PR tries to add Schema Type UUIDv1 (and later UUIDv4), but is a Draft (WIP), because i think i need some help with this, because i am not familiar enough with mongoose
continuation of #3242 (which was closed by the author without comment)
fixes #3208
Current Blockages:
doc.save
, and i can figure out whydoc.save
times-out at that pointQuestions / Notes:
3
(UUID (old)) and4
(UUID) is?Notes on the state of the added code:
//
)