如何使用ArangoJs将文档存储在ArangoDb图中? [英] How to store documents in an ArangoDb graph using ArangoJs?

查看:114
本文介绍了如何使用ArangoJs将文档存储在ArangoDb图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using latest version of ArangoDb and ArangoJs from a nodejs application. I have got following two vertexes

  1. 用户
  2. 令牌

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

tokens vertex contain the security tokens issues to one of the user in the users vertex. I have got an edge definition named token_belongs_to connecting tokens to users

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

How do I store a newly generated token belonging to an existing user using ArangoJs?

推荐答案

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

I am going to assume you are using ArangoDB 2.7 with the latest version of arangojs (4.1 at the time of this writing) as the API has changed a bit since the 3.x release of the driver.

正如您未提及的那样,使用图形API 最简单的方法是直接使用集合.但是,使用Graph API会带来很多好处,例如孤立的边缘在删除任何顶点时会自动删除.

As you don't mention using the Graph API the easiest way is to simply use the collections directly. Using the Graph API however adds benefits like orphaned edges automatically being deleted when any of their vertices are deleted.

首先,您需要获得要使用的每个集合的参考:

First you need to get a reference to each collection you want to work with:

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

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

Or if you are using the 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)组成.

In order to create a token for an existing user, you need to know the _id of the user. The _id of a document is made up of the collection name (users) and the _key of the document (e.g. 12345678).

如果没有_id_key,则还可以通过其他一些唯一属性来查找文档.例如,如果您有一个唯一的属性email知道其值,则可以执行以下操作:

If you don't have the _id or _key you can also look up the document by some other unique attribute. For example, if you have a unique attribute email that you know the value of, you could do this:

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

接下来,您将要创建令牌:

Next you'll want to create the token:

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

一旦有了userId和tokenId,就可以创建边缘来定义两者之间的关系:

Once you have the userId and tokenId you can create the edge to define the relation between the two:

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

如果您不想在边缘存储任何数据,则可以用空对象代替edgeData或简单地将其写为:

If you don't want to store any data on the edge you can substitute an empty object for edgeData or simply write it as:

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

因此,完整示例将如下所示:

So the full example would go something like this:

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

这篇关于如何使用ArangoJs将文档存储在ArangoDb图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆