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 = ; const dg = db namespace: 'example' ; await dgready; // A await dg; // ├── X await dg; // ├── Y await dg; // └── Z await dg; // X, Y, Z await dgsize; // 3 await dg; // [['A', 'X'], ['A', 'Y'], ['A', 'Z']] await dg; // ['A'] await dg; // ['X', 'Y', 'Z']} ;
Install
yarn add level-level-directed-graph-map
Usage
const level = ;const LevelDirectedGraphMap = ; const run = async { const db = ; const dgm = db 'A' 'B' namespace: 'example' ; await dgmready; // A // └── B await dgm; // true await dgm; // A // └── B // └── C await dgm; // true await dgm; // new Set(['B']); await dgm; // new Set(['C']); await dgm; // new Set(); await dgm; // new Set(); await dgm; // new Set(['A']); await dgm; // new Set(['B']); await dgm; // B // └── C await dgm; // false await dgm; // new Set(); await dgm; // Empty await dgm; // new Set(); await dgm; // false await dgm; // A // └── B await dgm; // true await dgm; // Empty await dgm; // false} ;
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