level-directed-graph-map v1.1.1
LevelDB Directed Graph Map
Directed graph data structure implemented using LevelDB. Similiar to multi-key maps or bidirectional maps.
See directed-graph-map for a synchronous, in-memory version.
const run = async () => {
const db = level('./optional-db-path');
const dg = new LevelDirectedGraphMap(db, [], { namespace: 'example' });
await dg.ready;
// A
await dg.addEdge('A', 'X'); // ├── X
await dg.addEdge('A', 'Y'); // ├── Y
await dg.addEdge('A', 'Z'); // └── Z
await dg.getTargets('A'); // X, Y, Z
await dg.size(); // 3
await dg.edges(); // [['A', 'X'], ['A', 'Y'], ['A', 'Z']]
await dg.sources(); // ['A']
await dg.targets(); // ['X', 'Y', 'Z']
}
run();
Install
yarn add level-level-directed-graph-map
Usage
const level = require('level');
const LevelDirectedGraphMap = require('level-directed-graph-map');
const run = async () => {
const db = level('./optional-db-path');
const dgm = new LevelDirectedGraphMap(db, [['A', 'B']], { namespace: 'example' });
await dgm.ready;
// A
// └── B
await dgm.hasEdge('A', 'B'); // true
await dgm.addEdge('B', 'C');
// A
// └── B
// └── C
await dgm.hasEdge('B', 'C'); // true
await dgm.getTargets('A'); // new Set(['B']);
await dgm.getTargets('B'); // new Set(['C']);
await dgm.getTargets('C'); // new Set();
await dgm.getSources('A'); // new Set();
await dgm.getSources('B'); // new Set(['A']);
await dgm.getSources('C'); // new Set(['B']);
await dgm.removeSource('A');
// B
// └── C
await dgm.hasEdge('A', 'B'); // false
await dgm.getTargets('A'); // new Set();
await dgm.removeTarget('C');
// Empty
await dgm.getTargets('B'); // new Set();
await dgm.hasEdge('B', 'C'); // false
await dgm.addEdge('A', 'B');
// A
// └── B
await dgm.hasEdge('A', 'B'); // true
await dgm.removeEdge('A', 'B');
// Empty
await dgm.hasEdge('A', 'B'); // false
}
run();
API
Table of Contents
LevelDirectedGraphMap
Class representing a Level Directed Graph Map
Parameters
db
Object? Object implementing the LevelUp interfaceedges
Iterable<[string, string]> Iterable containing source -> target pairs (optional, default[]
)options
Object Options object (optional, default{}
)
addEdge
Add an edge to the graph map.
Parameters
Returns Promise<void>
removeEdge
Remove an edge from the graph map.
Parameters
Returns Promise<void>
hasEdge
Test if a edge exists in the graph map.
Parameters
Returns Promise<boolean> Whether the edge exists in the graph map.
removeSource
Remove all edges from a source.
Parameters
source
string Source of the edge
Returns Promise<void>
removeTarget
Remove all edges to a target.
Parameters
target
string Target of the edge
Returns Promise<void>
hasSource
Test if a source exists in the graph map.
Parameters
source
string Source of the edge
Returns Promise<boolean> Whether the source exists in the graph map.
hasTarget
Test if a target exists in the graph map.
Parameters
target
string Target of the edge
Returns Promise<boolean> Whether the target exists in the graph map.
getSources
Get all sources with edges to a target.
Parameters
target
string Target of the edge
Returns Promise<Set<string>> Set of sources
getTargets
Get all targets with edges from a source.
Parameters
source
string Source of the edge
Returns Promise<Set<string>> Set of targets
edges
Array of edges
Returns Promise<Array<[string, string]>>
size
Edge count. Costly operation, use sparingly.
sources
Set of sources
targets
Set of targets
LevelDirectedGraphMap#ready
Resolves when the map is initialized and ready for use