Skip to content
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

What is the proper way to replace _id type from ObjectId to UUID in mongoose 6 #13032

Closed
vkarpov15 opened this issue Feb 14, 2023 Discussed in #13025 · 0 comments
Closed

What is the proper way to replace _id type from ObjectId to UUID in mongoose 6 #13032

vkarpov15 opened this issue Feb 14, 2023 Discussed in #13025 · 0 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Discussed in #13025

Originally posted by Freezystem February 11, 2023
I've read a lot of post in SO or GH where people are trying to implement UUIDs properly instead of ObjectIds. But these posts are a bit outdated for most of them or incomplete. I also wanted to avoid third party librairies like mongoose-uuid2.
The official documentation is not even mentioning the official UUID schema type.
I also have read that storing UUIDs as Buffer instead of String improve overall storage and index performance but it could be false.
Also it is not clear if I should use uuidv1 or uuidv4 generator as mongoose UUID type mention both in its source.

So for the moment I'm doing something like this:

import type { Schema } from "mongoose";
import { randomUUID } from "crypto";  // NodeJS implementation of uuid v4.

interface IUser {
    _id: Schema.Types.UUID;
    name: string;
    organization?: Schema.Types.UUID;
}

const userSchema = new Schema<IUser>({
    _id: {
        type: Schema.Types.UUID,
        default: randomUUID,
        immutable: true,
    },
    name: {
        type: String,
        required: true,
    },
    organization: {
        type: Schema.Types.UUID,
        ref: "Organization",
        index: true,
    }
},
{ _id: false });

But I get some errors, especially with the organization reference when I try to destructure a newly created user and organization isn't provided, example:

const {name, organization} = await User.create({name: "user"});

TypeError: Cannot read properties of undefined (reading 'toString')

at binary2hex (node_modules/mongoose/lib/schema/uuid.js:39:19)
at model.binaryToString (node_modules/mongoose/lib/schema/uuid.js:71:11)
at SchemaUUID.Object.<anonymous>.SchemaType.applyGetters (node_modules/mongoose/lib/schematype.js:1225:20)
at model.Object.<anonymous>.Document.get (node_modules/mongoose/lib/document.js:1886:18)
at model.get [as organization] (node_modules/mongoose/lib/helpers/document/compile.js:191:32)

Seems like the UUID getter is missing an undefined or null test before trying to cast it or maybe I'm missing something.
If I remove organization the error does not occur.

@vkarpov15 vkarpov15 added this to the 6.9.3 milestone Feb 14, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 15, 2023
vkarpov15 added a commit to lpizzinidev/mongoose that referenced this issue Feb 15, 2023
vkarpov15 added a commit that referenced this issue Feb 15, 2023
schematype(UUID): added null check to prevent error on binaryToString conversion
@vkarpov15 vkarpov15 modified the milestones: 6.9.4, 6.9.6 Feb 21, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.10.3, 6.10.4 Mar 3, 2023
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed note to self labels Mar 17, 2023
vkarpov15 added a commit that referenced this issue Mar 21, 2023
docs(guide+schematypes): add UUID to schematypes guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants