首页 文章

设置Azure DocumentDB Javascript SDK

提问于
浏览
4

我正在尝试使用Javascript SDK设置Azure DocumentDB . 到目前为止,我有一个主密钥,一个 endpoints URL,以及一个数据库设置和一个集合 . 我无法想象的是如何用他们需要的值替换我所拥有的东西,我在文档或其他方面找不到任何东西 .

下面是我试图填写的值的代码 . 我的想法是collectionurl =我的 endpoints url,resourceToken =我的密钥,collectionId =集合名称 . 但是什么是hostedendpoint,我应该如何设置resourceToken?我知道它不会太多,但如果有人使用过DocumentDB,我会非常感谢帮助 .

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 回答

  • 0

    上面的JS SDK代码示例的工作步骤如下:

    • 将DocumentDB endpoints 放入 (hostendpoint) .

    • 将集合资源ID(这是集合JSON文档中的 _rid ,而不是 id ,属性)放置为 (collectionId) 的值 .

    • 放置权限令牌(您需要为集合创建用户和权限)作为 (resourceToken) 的值 .

    • (collectionUrl) 中放置集合的 _self 链接

    完成的代码示例应类似于以下内容:

    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);
    });
    

    如果您正在构建Node.js应用程序 - 我强烈建议您检查Node.js客户端:https://github.com/Azure/azure-documentdb-node/

    DocumentDB JS客户端不是最直观的SDK . 围绕此SDK的开发体验是我们正在努力改进的 . 如果您愿意讨论您的用例/场景(或者甚至想要一些常规帮助) - 请致电 andrl {at} microsoft.com 与我联系!

  • 4

    不确定你是否还需要帮助,但由于aliuy没有太多关于如何获取资源令牌,我想补充他的答案 .

    简而言之,如果您使用Javascript SDK进行"Client Side"开发,您将无法使用SDK生成资源令牌,因为您需要使用 master key 来获取资源令牌,但客户端Javascript SDK不会通过 master key 支持身份验证 .

    因此,您需要使用其他SDK(例如,Node.js,.NET等)来创建用户,然后使用该用户为特定资源创建权限 .

    不幸的是,使用这种方法,即使您的应用程序是严格的客户端(例如,Metro HTML5 / JS应用程序),您将需要一个中间层来授予对您的应用程序的访问权限 .

    但是, IF (BIG IF)您的应用程序主要由您信任主密钥的人使用,您可以轻松地通过主密钥将身份验证添加到客户端Javascript SDK . 除非你需要,否则我在这里发布了't encourage people doing that, so I won' t发布工作代码 . 但这是一个指针 - 你需要一个库来创建签名,比如Crypto.JS .

  • 1

    你应该看看DoQmentDB模块 .
    它是 DocumentDB 之上的包装器,但更直观(mongodb样式/流程),支持钩子,模式,原子事务和查询/操作 .

    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);
    

相关问题