简体   繁体   中英

How to store documents in an ArangoDb graph using ArangoJs?

I am using latest version of ArangoDb and ArangoJs from a nodejs application. 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. I have got an edge definition named token_belongs_to connecting tokens to users

How do I store a newly generated token belonging to an existing user using 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.

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

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

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 (eg 12345678 ).

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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