EdgeCollection
  • Updated on 11 Oct 2019
  • 5 minutes to read
  • Contributors
  • Print
  • Share
  • Dark
    Light

EdgeCollection

  • Print
  • Share
  • Dark
    Light

GraphEdgeCollection

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

graphEdgeCollection.remove

async graphEdgeCollection.remove(documentHandle): Object

Deletes the edge with the given documentHandle from the collection.

Arguments

  • documentHandle: string

    The handle of the edge to retrieve. This can be either the _id or the _key of an edge in the collection, or an edge (i.e. an object with an _id or _key property).

Examples

const graph = fabric.graph('some-graph');
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const collection = graph.edgeCollection('edges');

await collection.remove('some-key')
// document 'edges/some-key' no longer exists

// -- or --

await collection.remove('edges/some-key')
// document 'edges/some-key' no longer exists

graphEdgeCollection.documentExists

async graphEdgeCollection.documentExists(documentHandle): boolean

Checks whether the edge with the given documentHandle exists.

Arguments

  • documentHandle: string

    The handle of the edge to retrieve. This can be either the _id or the
    _key of a edge in the collection, or an edge (i.e. an object with an
    _id or _key property).

Examples

const graph = fabric.graph('some-graph');
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const collection = graph.edgeCollection('edges');

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

graphEdgeCollection.document

async graphEdgeCollection.document(documentHandle, [graceful]): Object

Alias: graphEdgeCollection.edge.

Retrieves the edge with the given documentHandle from the collection.

Arguments

  • documentHandle: string

    The handle of the edge to retrieve. This can be either the _id or the _key of an edge in the collection, or an edge (i.e. an object with an _id or _key property).

  • graceful: boolean (Default: false)

    If set to true, the method will return null instead of throwing an error if the edge does not exist.

Examples

const graph = fabric.graph('some-graph');
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const collection = graph.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
}

graphEdgeCollection.save

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

Creates a new edge between the vertices fromId and toId with the given data.

Arguments

  • data: Object

    The data of the new edge. If fromId and toId are not specified, the data needs to contain the properties **from_ and **to_.

  • fromId: string (optional)

    The handle of the start vertex of this edge. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

  • toId: string (optional)

    The handle of the end vertex of this edge. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

Examples

const fabric = new Fabric();
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const graph = fabric.graph('some-graph');
const collection = graph.edgeCollection('edges');
const edge = await collection.save(
  {some: 'data'},
  'vertices/start-vertex',
  'vertices/end-vertex'
);
assert.equal(edge._id, 'edges/' + edge._key);
assert.equal(edge.some, 'data');
assert.equal(edge._from, 'vertices/start-vertex');
assert.equal(edge._to, 'vertices/end-vertex');

graphEdgeCollection.edges

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

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

Arguments

  • documentHandle: string

    The handle of the document to retrieve the edges of. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

Examples

const fabric = new Fabric();
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const graph = fabric.graph('some-graph');
const collection = graph.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']);

graphEdgeCollection.inEdges

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

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

Arguments

  • documentHandle: string

    The handle of the document to retrieve the edges of. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

Examples

const fabric = new Fabric();
const graph = fabric.graph('some-graph');
const collection = graph.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');

graphEdgeCollection.outEdges

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

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

Arguments

  • documentHandle: string

    The handle of the document to retrieve the edges of. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

Examples

const fabric = new Fabric();
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const graph = fabric.graph('some-graph');
const collection = graph.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']);

graphEdgeCollection.traversal

async graphEdgeCollection.traversal(startVertex, opts): Object

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

Arguments

  • startVertex: string

    The handle of the start vertex. This can be either the _id of a document in the fabric, the _key of an edge in the collection, or a document (i.e. an object with an _id or _key property).

  • opts: Object

Note:-
Please note that while opts.filter, opts.visitor, opts.init, opts.expander and opts.sort should be strings evaluating to well-formed JavaScript code, it's not possible to pass in JavaScript functions directly because the code needs to be evaluated on the server and will be transmitted in plain text.

Examples

const fabric = new Fabric();
await fabric.login(tenant-mail, password);
fabric.useTenant(tenant-name);
const graph = fabric.graph('some-graph');
const collection = graph.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']);
Was this article helpful?