【问题标题】:How to store documents in an ArangoDb graph using ArangoJs?如何使用 ArangoJs 将文档存储在 ArangoDb 图中?
【发布时间】:2015-11-29 20:13:29
【问题描述】:

我正在使用来自 nodejs 应用程序的最新版本的 ArangoDb 和 ArangoJs。我有以下两个顶点

  1. 用户
  2. 令牌

tokens 顶点包含向users 顶点中的一位用户发出的安全令牌。我有一个名为 token_belongs_to 的边缘定义,将 tokens 连接到 users

如何使用 ArangoJs 存储属于现有用户的新生成令牌?

【问题讨论】:

    标签: arangodb arangojs


    【解决方案1】:

    我假设您使用 ArangoDB 2.7 和最新版本的 arangojs(撰写本文时为 4.1),因为自 3.x 驱动程序发布以来 API 发生了一些变化。

    正如您没有提到使用Graph API 一样,最简单的方法是直接使用集合。但是,使用 Graph API 会带来一些好处,例如在删除任何顶点时会自动删除孤立边。

    首先,您需要获取要使用的每个集合的引用:

    var users = db.collection('users');
    var tokens = db.collection('tokens');
    var edges = db.edgeCollection('token_belongs_to');
    

    或者,如果您使用的是 Graph API:

    var graph = db.graph('my_graph');
    var users = graph.vertexCollection('users');
    var tokens = graph.vertexCollection('tokens');
    var edges = graph.edgeCollection('token_belongs_to');
    

    要为现有用户创建令牌,您需要知道用户的_id。文档的_id 由集合名称(users)和文档的_key(例如12345678)组成。

    如果您没有_id_key,您还可以通过其他一些唯一属性来查找文档。例如,如果您有一个知道其值的唯一属性 email,您可以这样做:

    users.firstExample({email: 'admin@example.com'})
    .then(function (doc) {
      var userId = doc._id;
      // more code goes here
    });
    

    接下来您要创建令牌:

    tokens.save(tokenData)
    .then(function (meta) {
      var tokenId = meta._id;
      // more code goes here
    });
    

    获得 userId 和 tokenId 后,您可以创建边缘来定义两者之间的关系:

    edges.save(edgeData, userId, tokenId)
    .then(function (meta) {
      var edgeId = meta._id;
      // more code goes here
    });
    

    如果您不想在边缘存储任何数据,您可以用一个空对象替换edgeData,或者直接写成:

    edges.save({_from: userId, _to: tokenId})
    .then(...);
    

    所以完整的例子应该是这样的:

    var graph = db.graph('my_graph');
    var users = graph.vertexCollection('users');
    var tokens = graph.vertexCollection('tokens');
    var edges = graph.edgeCollection('token_belongs_to');
    
    Promise.all([
      users.firstExample({email: 'admin@example.com'}),
      tokens.save(tokenData)
    ])
    .then(function (args) {
      var userId = args[0]._id; // result from first promise
      var tokenId = args[1]._id; // result from second promise
      return edges.save({_from: userId, _to: tokenId});
    })
    .then(function (meta) {
      var edgeId = meta._id;
      // Edge has been created
    })
    .catch(function (err) {
      console.error('Something went wrong:', err.stack);
    });
    

    【讨论】:

      【解决方案2】:

      注意 - 语法变化:

      边缘创建:

      const { Database, CollectionType } = require('arangojs');
      
      const db = new Database();
      const collection = db.collection("collection_name");
      
      if (!(await collection.exists())
        await collection.create({ type: CollectionType.EDGE_COLLECTION });
      
      await collection.save({_from: 'from_id', _to: 'to_id'});
      

      https://arangodb.github.io/arangojs/7.1.0/interfaces/_collection_.edgecollection.html#create

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多