Start a new topic

How to obtain/generate id for new entity

If I don't provide an id for new entity it will be created for me (http://devcenter.kinvey.com/html5/guides/datastore#Saving). Is the id unique or just random? Does the save method returns the new id? If not how do I find out the id on newly saved entity?



Anyway such process is asynchronous, I would like to be able to create entities in my front-end scrip synchronously. Is there a way to:

a) fetch list of multiple new ID's per collection at once so that I could use them when I generate multiple new entities before I save them, or

b) generate unique ID's in my front-end script? (Actually I know answer to this is no)





NOTE:

Due to the limitations I am handling the relations of my entities in the front-end script (http://devcenter.kinvey.com/html5/guides/datastore#limitations)

The id generated will be a [MongoID](http://docs.mongodb.org/manual/reference/object-id/) when stored online, or a random one when stored offline first.



There is no way to obtain the id before saving an object. You could, however, set the _id yourself prior to saving. Just make sure you generate a random / unique identifier so that the object doesn’t collide with another already existing object.
Maybe this helps....

The save method returns a response object that has the _id property loaded into it. In addition, you could add some business logic that runs after a save, and pulls the _id from the response object. (response.body._id)



Just be sure to note that _id properties are stored as BSON and therefore require some conversion in your business logic code if you're going to run queries with that value.
Login or Signup to post a comment