4

I am trying to set up a Azure DocumentDB using Javascript SDK. So far I have a master key, a endpoint url, as well as a database set up and a collection. What I cannot figure is how to substitute what I have with the values they require and I cannot find anything to refrence in the documentation or otherwise.

below is the code I am trying to fill the values in for. My thought was that collectionurl = my endpoint url, resourceToken = my key, collectionId = collection name. but then what is hostedendpoint and how should I set up the resourceToken? I know its not much to go on but if anyone has used DocumentDB I would really appreciate the help.

var host = (hostendpoint);                        // Add your host
var resourceToken = {};
resourceTokens[(collectionId)] = (resourceToken); // Add the collectionId and resourceToken for read/write on the collection
var collectionUrl = (collectionUrl);              // Add the collection self-link
var client = DocumentDB.createClient(host, {resourceTokens: resourceTokens});
var documentDefinition = {id: "Hello world document", content: "Hello World!"};
client.createDocument(collectionUrl, documentDefinition, function(err, createdDocument) {
    if (err) {
        throw err;
    }

    console.log('result', createdDocument.content);
})

http://dl.windowsazure.com/documentDB/jsclientdocs/

3 Answers 3

4

The steps for getting the JS SDK code sample above to work are as follows:

  • Place your DocumentDB endpoint in to (hostendpoint).
  • Place the collection resource id (this is the _rid, not the id, property in the collection JSON document) as the value for (collectionId).
  • Place the permissions token (you will need to create a user and permission for the collection) as the value for (resourceToken).
  • Place the _self link for the collection in (collectionUrl)

The completed code sample should resemble something like this:

var host = "https://bloopbloop.documents.azure.com:443"; // Add your host

var resourceTokens = {};
resourceTokens["Pa0wAKPRZQA="] = "type=resource&ver=1&sig=WaOXNCJaZ7Z7obf74i48Yg==;Dbb5bXDnm5ou0rpAUyifsFR5VNIsfSTeuad81P7zC7ytJtSwLCLnw9ne99vuIH8/giBsYIrqtXE5PYDs2idLfdJ4+K3bfT8BJgWqdgIuIEE/nvVpdEQ85y1azPXO7F+wXwBzK4eH2wQ0yMudy+petUdnN1GR3VJNsuNTZ1j+mnLLT/FLpFjWLVyI2dTLe7KHM0FvnczVZmT9wGJV8rUMjgjV9oG552DAev9exPGnj4E=;"; // Add the collectionId and resourceToken for read/write on the collection

var collectionUrl = "dbs/Pa0wAA==/colls/Pa0wAKPRZQA=/"; // Add the collection self-link

var client = DocumentDB.createClient(host, {
  resourceTokens: resourceTokens
});

var documentDefinition = {
  id: "Hello world document",
  content: "Hello World!"
};

client.createDocument(collectionUrl, documentDefinition, function(err, createdDocument) {
  if (err) {
    throw err;
  }

  console.log('result', createdDocument);
});

In case you are building a Node.js application - I'd highly recommend checking out the Node.js client: https://github.com/Azure/azure-documentdb-node/

The DocumentDB JS client is not the most intuitive SDK to use. The dev experience surrounding this SDK is something we're trying to improve. If you are open to discussing your use-case/scenario (or even would like some general help) - please reach out to me at andrl {at} microsoft.com!

Sign up to request clarification or add additional context in comments.

1 Comment

I couldnt find anything that explained this or gave an example, Thank you so much aliuy. This was an incredible help!
1

Not sure if you still need help, but since aliuy didn't touch too much on how to get a resource token, I want to supplement his answer.

Put it simply, if you are using the Javascript SDK for "Client Side" development, you won't be able to generate a resource token using the SDK because you need to use the master key to get the resource token, but the Client Side Javascript SDK does not support authentication through the master key.

Therefore, you will need to use the other SDK (ex, Node.js, .NET, etc) to create an user, then use that user to create permission for a specific resource.

Unfortunately, with this approach, even if your app is strictly client side (ex, a Metro HTML5/JS app), you will need a middle layer to grant access to your app.

However, IF (a BIG IF) your app will be used primarily by people whom you are trusted with the master key, you can add authentication by master key to the Client Side Javascript SDK easily. I won't encourage people doing that, so I won't post working codes here unless you need it. But here's a pointer -- you will need a library to create the signature, such as Crypto.JS.

1 Comment

This is helpful, thanks. I'd be interested in your method for using the master key if you can post it please.
0

You should take a look on DoQmentDB module.
it's a wrapper on top of DocumentDB but more intuitive(mongodb style/flow), supports hooks, schema, atomic-transactions and queries/operations.

Example:

// Pass connection and database-name, if `test` is not exist it will create one.
var db = new DoQmentDB(connection, 'test');
// Create a CollectionManager, if `users` is not exist it will create one.
var users = db.use('users');

// Each http function returns a `Promise` with two specific methods: success and error.
users.create({ name: 'Ariel M.' })
  .then(console.log);

users.update({ name: 'Ariel', admin: true }, { admin: false })
  .then(console.log);

users.findAndRemove({ isAdmin: false })
  .then(console.log);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.