Search code examples
node.jsuuidmongoose-schema

Trouble generating unique UUIDv4 at model instanciation


I am having an issue related to UUID when trying to create an instance of mongoose model.

Mongoose Schema

export interface IRessourceModel extends IRessource, mongoose.Document { }

export let RessourceSchema: mongoose.Schema = new Schema({
    uuid: {
        type: String,
        default: uuidv4() },
    name: {
        type: String,
        required: true,
        minlength: RessourceValidation.CONSTANTS.NAME_MIN_LENGTH,
        maxlength: RessourceValidation.CONSTANTS.NAME_MAX_LENGTH,
        trim: true,
        validate: {
            validator: RessourceValidation.name,
            message: '{VALUE} is not a valid name'}},
    type: {
        type: String,
        required: true,
        enum: RessourceValidation.ENUM.TYPE,
        validate: {
            validator: RessourceValidation.type,
            message: '{VALUE} is not a valid type'}},
    uploaded: {
        type: Date,
        required: true,
        default: Date.now }
})

export const Ressource: mongoose.Model<IRessourceModel> = mongoose.model<IRessourceModel>('Ressource', RessourceSchema);

Instanciation

const RessourceModel = mongoose.model('Ressource');
// Some code here...

let Ressource: any = new RessourceModel({
    name: req.body.name,
    type: req.body.type,
})

Ressource.save((err, ressource) => {
    if (err)
        return (APIResponse.jsonErrorAfterMongooseQuery(res, 'Cannot create ressource', err));
    APIResponse.json(res, ressource);
})

Issue

When I'm sending a POST request on /ressources/ which is using the piece of instanciation code above, the ressource is created but, if I send another POST request, the second ressource created has the same UUID than the first one...

Results for POST request #1

{
    "type": true,
    "data": {
        "uuid": "1794bbb4-3385-4b0d-909a-e22c60aee608",
        "_id": "5b3a42f5ae71a02af4ed6d11",
        "name": "Name of ressource 1",
        "type": "IMAGE",
        "uploaded": "2018-07-02T15:21:25.866Z",
        "__v": 0
    }
}

Results for POST request #2

{
    "type": true,
    "data": {
        "uuid": "1794bbb4-3385-4b0d-909a-e22c60aee608",
        "_id": "5b3a4530f3ab1f3d40b7ac93",
        "name": "Name of ressource 2",
        "type": "IMAGE",
        "uploaded": "2018-07-02T15:30:56.379Z",
        "__v": 0
    }
}

Am I using the default: uuidv4() wrongly or is it coming from the way I instanciate the mongoose model ? Or anything else ?

I do have tried to set the UUID from the Schema.pre('save') function but without success...

I'm a bit lost, thanks for your help !


Solution

  • It seems that uuidv4() is a string. Of course, it will be same in all models.

    Considering that:

    const uuidv4 = require('uuidv4');
    

    Mongoose default schema option can accept a function to generate a value:

    export let RessourceSchema: mongoose.Schema = new Schema({
        uuid: {
            type: String,
            default: uuidv4
        },
        ...