Skip to the content.

EdgeCollection API

The EdgeCollection API extends the Collection API with the following methods.

edgeCollection.document

async edgeCollection.document(documentHandle, [opts]): Edge

Alias: edgeCollection.edge.

Retrieves the edge with the given documentHandle from the collection.

Arguments

If a boolean is passed instead of an options object, it will be interpreted as the graceful option.

Examples

const db = new Database();
const collection = db.edgeCollection("edges");

const edge = await collection.document("some-key");
// the edge exists
assert.equal(edge._key, "some-key");
assert.equal(edge._id, "edges/some-key");

// -- or --

const edge = await collection.document("edges/some-key");
// the edge exists
assert.equal(edge._key, "some-key");
assert.equal(edge._id, "edges/some-key");

// -- or --

const edge = await collection.document("some-key", true);
if (edge === null) {
  // the edge does not exist
}

edgeCollection.documentExists

async edgeCollection.documentExists(documentHandle): boolean

Checks whether the edge with the given documentHandle exists.

Arguments

Examples

const db = new Database();
const collection = db.edgeCollection("my-docs");

const exists = await collection.documentExists("some-key");
if (exists === false) {
  // the edge does not exist
}

edgeCollection.save

async edgeCollection.save(data, [fromId, toId], [opts]): Object

Creates a new edge between the documents fromId and toId with the given data and returns an object containing the edge’s metadata.

Arguments

If a boolean is passed instead of an options object, it will be interpreted as the returnNew option.

Examples

const db = new Database();
const collection = db.edgeCollection("edges");
const data = { some: "data" };

const info = await collection.save(
  data,
  "vertices/start-vertex",
  "vertices/end-vertex"
);
assert.equal(info._id, "edges/" + info._key);
const edge = await collection.edge(edge);
assert.equal(edge._key, info._key);
assert.equal(edge._rev, info._rev);
assert.equal(edge.some, data.some);
assert.equal(edge._from, "vertices/start-vertex");
assert.equal(edge._to, "vertices/end-vertex");

// -- or --

const info = await collection.save({
  some: "data",
  _from: "verticies/start-vertex",
  _to: "vertices/end-vertex",
});
// ...

edgeCollection.edges

async edgeCollection.edges(documentHandle): Array<Object>

Retrieves a list of all edges of the document with the given documentHandle.

Arguments

Examples

const db = new Database();
const collection = db.edgeCollection("edges");
await collection.import([
  ["_key", "_from", "_to"],
  ["x", "vertices/a", "vertices/b"],
  ["y", "vertices/a", "vertices/c"],
  ["z", "vertices/d", "vertices/a"],
]);
const edges = await collection.edges("vertices/a");
assert.equal(edges.length, 3);
assert.deepEqual(
  edges.map((edge) => edge._key),
  ["x", "y", "z"]
);

edgeCollection.inEdges

async edgeCollection.inEdges(documentHandle): Array<Object>

Retrieves a list of all incoming edges of the document with the given documentHandle.

Arguments

Examples

const db = new Database();
const collection = db.edgeCollection("edges");
await collection.import([
  ["_key", "_from", "_to"],
  ["x", "vertices/a", "vertices/b"],
  ["y", "vertices/a", "vertices/c"],
  ["z", "vertices/d", "vertices/a"],
]);
const edges = await collection.inEdges("vertices/a");
assert.equal(edges.length, 1);
assert.equal(edges[0]._key, "z");

edgeCollection.outEdges

async edgeCollection.outEdges(documentHandle): Array<Object>

Retrieves a list of all outgoing edges of the document with the given documentHandle.

Arguments

Examples

const db = new Database();
const collection = db.edgeCollection("edges");
await collection.import([
  ["_key", "_from", "_to"],
  ["x", "vertices/a", "vertices/b"],
  ["y", "vertices/a", "vertices/c"],
  ["z", "vertices/d", "vertices/a"],
]);
const edges = await collection.outEdges("vertices/a");
assert.equal(edges.length, 2);
assert.deepEqual(
  edges.map((edge) => edge._key),
  ["x", "y"]
);

edgeCollection.traversal

async edgeCollection.traversal(startVertex, opts): Object

Performs a traversal starting from the given startVertex and following edges contained in this edge collection.

Arguments

Examples

const db = new Database();
const collection = db.edgeCollection("edges");
await collection.import([
  ["_key", "_from", "_to"],
  ["x", "vertices/a", "vertices/b"],
  ["y", "vertices/b", "vertices/c"],
  ["z", "vertices/c", "vertices/d"],
]);
const result = await collection.traversal("vertices/a", {
  direction: "outbound",
  visitor: "result.vertices.push(vertex._key);",
  init: "result.vertices = [];",
});
assert.deepEqual(result.vertices, ["a", "b", "c", "d"]);