Generating unique Id using Ottoman node js

I am using Ottoman to interact with Couch base in my application for Curd operations.

I have an document model like
User:{
username:‘string’,
_id:{type:‘string’, auto:‘uuid’, readonly:true},
phone:[{
phone_id:{type:‘string’, auto:‘uuid’, readonly:true},
phone_number:‘string’,
phone_type:‘string’
}]
}

When i try to create a document with this model Ottoman generates a UUID for the _id field but is not generating one for the internal phone_id field.

Two things I need inputs for.

  1. How can I generate an Id for this field.
  2. I will be deploying my node application on a cluster will this UUID remain unique around all instances on the cluster or there is a chance of two applications generating similar UUID for two insert request in exponentially heavy load scenario?

Thanks in advance for the inputs and guidance.

Hi,
1./ I’ve looked a bit into the source , I think ottoman currently does not support default value/ auto UUID generation for nested object
2./ UUID generation process is kind of guarantee to be unique every time it generated a new one . you can read more about this here
[https://en.wikipedia.org/wiki/Universally_unique_identifier]

Hey @ktn122,

You should be able to set the default value to anything, including something such as a UUID. For instance:

const uuid = require('uuid')
// ...
ottoman.model('Something', {
  uid: {type: 'string', default: uuid.v4()},
  // ...
});

Cheers, Brett

Hi, @brett19

What I’m talking about is default value for nested object . Check this test case .

it('uid created for nested object', function() {

        var Something = ottoman.model('Something', {

            uid: {type: 'string', default: uuid.v4()},

            nestedObj:{

                uid:{type: 'string', default: uuid.v4()},

            }

        });

        

        var p = new Something();

        assert.ok('uid' in p); //ok 

        assert.ok('uid' in p.nestedObj); //failed

    });