skinneejoe
skinneejoe

Reputation: 3991

ArangoDB Request Unique Key in Advance

I'm developing a real-time web app. The back end is NodeJS and ArangoDB. I would like to generate and send a unique _key to the web app in advance of creation of a document. That way the document can be created quickly on the fly without having to wait for a response from the server with the newly generated ID. To clarify:

  1. User logs in and Web app receives unique key from ArangoDB (via NodeJS)
  2. User creates a new document and Web App assigns it the unique key it was given in step 1
  3. Web app immediately requests a new unique key for use now that the previous one is being used
  4. User saves the newly created document to one of many collections in the database using the unique key from step 1
  5. Web app updates it's local cache of documents without needing to wait for auto generated key from database
  6. Begin again at step 2

This is just a rough overview of the intended workflow. I believe CouchDB has a function that will provide a user with unique ID's: http://docs.couchdb.org/en/1.6.1/api/server/common.html?highlight=id#get--_uuids

I did find this code in the ArangoDB documentation:

FOR i IN 1..10
  INSERT { value: i } IN test
  RETURN { _key: NEW._key, value: i }

It creates a batch of unique keys in a collection. However, I was unsure if the keys generated here would be unique for ALL collections or, what seems more likely, just the collection they are generated for. My app needs a key that could be used for any collection.

Is there a way to fetch one or more unique keys that could be used on multiple collections from ArangoDB? Is there a different way to approach this issue?

Upvotes: 2

Views: 1823

Answers (1)

Alan Plum
Alan Plum

Reputation: 10892

Keys are always relative to the collection. The full document _id always consists of the collection name and the document key.

If you want to define keys yourself, you can simply provide a _key property as part of the document when saving it to the w collection. Of course this means it becomes your application's responsibility to pick unique keys and resolve any conflicts (i.e. failed saves due to duplicate keys). UUIDs are a good option if you don't mind the format.

Upvotes: 7

Related Questions