简体   繁体   English

如何使用 ArangoJs 在 ArangoDb 图中存储文档?

[英]How to store documents in an ArangoDb graph using ArangoJs?

I am using latest version of ArangoDb and ArangoJs from a nodejs application.我正在使用 nodejs 应用程序中最新版本的 ArangoDb 和 ArangoJs。 I have got following two vertexes我有以下两个顶点

  1. users用户
  2. tokens令牌

tokens vertex contain the security tokens issues to one of the user in the users vertex. tokens顶点包含向users顶点中的users之一发行的安全令牌。 I have got an edge definition named token_belongs_to connecting tokens to users我有一个名为边缘清晰度token_belongs_to连接tokensusers

How do I store a newly generated token belonging to an existing user using ArangoJs?如何使用 ArangoJs 存储属于现有用户的新生成的令牌?

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.我将假设您将 ArangoDB 2.7 与最新版本的 arangojs(在撰写本文时为 4.1)一起使用,因为自驱动程序的 3.x 版本以来 API 发生了一些变化。

As you don't mention using the Graph API the easiest way is to simply use the collections directly.由于您没有提到使用Graph API ,最简单的方法是直接使用集合。 Using the Graph API however adds benefits like orphaned edges automatically being deleted when any of their vertices are deleted.但是,使用 Graph API 会增加一些好处,例如在删除任何顶点时会自动删除孤立边。

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

Or if you are using the Graph API:或者,如果您使用的是图形 API:

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

In order to create a token for an existing user, you need to know the _id of the user.为了为现有用户创建令牌,您需要知道用户的_id The _id of a document is made up of the collection name ( users ) and the _key of the document (eg 12345678 ).文档的_id由集合名称( users )和文档的_key (例如12345678 )组成。

If you don't have the _id or _key you can also look up the document by some other unique attribute.如果您没有_id_key您还可以通过其他一些唯一属性查找文档。 For example, if you have a unique attribute email that you know the value of, you could do this:例如,如果您有一个知道其值的唯一属性email ,则可以执行以下操作:

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

Once you have the userId and tokenId you can create the edge to define the relation between the two:获得 userId 和 tokenId 后,您可以创建边来定义两者之间的关系:

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

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:如果您不想在边缘上存储任何数据,您可以将一个空对象替换为edgeData或简单地将其写为:

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

Attention - syntax changes:注意 - 语法更改:

Edge creation:边缘创建:

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 https://arangodb.github.io/arangojs/7.1.0/interfaces/_collection_.edgecollection.html#create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM