FileGraph is a versatile library designed for storing and querying graphs in files. It simplifies graph operations, allowing you to create, manipulate, and traverse graphs effortlessly. Whether you're managing complex networks or simple datasets, FileGraph provides a robust API for seamless integration into your applications.
The file-graph class provides an abstract implementation for graph operations. It supports graph manipulation, including creating, updating, and deleting vertices, creating arcs and edges, and traversing or querying the graph structure.
To install file-graph from npm, run the following command:
npm install file-graph
Alternatively, you can install it from the GitHub registry.
const graph = FileGraph('graph.txt');
FileGraph accepts a path where the graph data will be stored.The returned object graph is an instance of the FileGraphAbstract class, which provides methods for working with the graph.
After initializing the graph at the specified path, a file will be created where each line represents a vertex. It is recommended not to modify the file contents manually.
Example of a vertex record: graph.txt
{
"id": "33ef29be-adaa-4509-b306-62a32db7310e", // UUID - unique identifier of the vertex
"data": { ... }, // The data associated with the vertex
"links": [] // List of UUIDs of vertices that this vertex is connected to
}
Below is an overview of the methods, their parameters, return values, and examples of how to use them.
Creates a vertex (node) in the graph with the provided data.
data: T - An object representing the vertex data.{ id, data, links }.const data = { name: 'Alice', age: 30 };
const createdVertex = await graph.createVertex(data);
console.log(createdVertex);
// Output: { id: 'some-unique-id', data: { name: 'Alice', age: 30 }, links: [] }
Creates multiple vertices at once.
data: T[] - An array of objects representing the data for each vertex.const data = [{ name: 'Alice' }, { name: 'Bob' }];
const createdVertices = await graph.createVertices(data);
console.log(createdVertices);
// Output: [{ id: 'id1', data: { name: 'Alice' }, links: [] }, { id: 'id2', data: { name: 'Bob' }, links: [] }]
Updates a vertex that matches the given condition. The updater function defines the logic for finding and modifying the vertex.
updater: IUpdater<T> - A function that takes a vertex and returns an updated vertex if it matches the condition.true if the update was successful, otherwise false.const isUpdated = await graph.updateVertex(
vertex =>
vertex.id === 'some-unique-id' && { data: { name: 'Alice Updated' } },
);
console.log(isUpdated); // true
Deletes a vertex that matches the given condition.
predicate: IPredicate<T> - A function that returns true if the vertex should be deleted.true if the deletion was successful, otherwise false.const isDeleted = await graph.deleteVertex(
vertex => vertex.id === 'some-unique-id',
);
console.log(isDeleted); // true
Finds a single vertex that matches the given condition.
predicate: IPredicate<T> - A function that returns true if the vertex matches the search condition.null if no match is found.const foundVertex = await graph.findOne(vertex => vertex.data.name === 'Alice');
console.log(foundVertex);
// Output: { id: 'some-unique-id', data: { name: 'Alice', age: 30 }, links: [] }
Finds all vertices that match the given condition.
predicate: IPredicate<T> - A function that returns true for each vertex that matches the search condition.const foundVertices = await graph.findAll(
vertex => vertex.data.name === 'Alice',
);
console.log(foundVertices);
// Output: [{ id: 'id1', data: { name: 'Alice', age: 30 }, links: [] }, { id: 'id2', data: { name: 'Alice', age: 25 }, links: [] }]
Iterates over each vertex in the graph and applies the provided callback function.
callbackVertex: ICallbackVertex<T> - A callback function that is invoked for each vertex. If it returns true, the iteration stops.await graph.forEachVertex(vertex => {
console.log(vertex);
// Stop iteration if the vertex's name is 'Alice'
return vertex.data.name === 'Alice';
});
Creates edges (links) between the specified vertices.
ids: IUuidArray - An array of vertex IDs between which to create edges.true if the edge creation was successful.const isEdgeCreated = await graph.createEdge(['id1', 'id2', 'id3']);
console.log(isEdgeCreated); // true
Removes the edges (links) between the specified vertices in the graph.
Parameters
ids: IUuidArray - An array of vertex IDs. The edges between the vertices specified by these IDs will be removed.Returns
true if the edge removal was successful.const isEdgeRemoved = await graph.removeEdge(['id1', 'id2', 'id3']);
console.log(isEdgeRemoved); // true
Creates an arc (directed edge) between two vertices.
sourceVertexId: uuidType - The ID of the source vertex.targetVertexId: uuidType - The ID of the target vertex.true if the arc creation was successful.const isArcCreated = await graph.createArc('id1', 'id2');
console.log(isArcCreated); // true
Creates arcs (directed edges) between multiple vertices in the specified order.
ids: IUuidArray - An array of vertex IDs.true if the arcs creation was successful.const isArcsCreated = await graph.createArcs(['id1', 'id2', 'id3']);
console.log(isArcsCreated); // true
Removes an arc (edge) between two vertices.
sourceVertexId: uuidType - The ID of the source vertex.targetVertexId: uuidType - The ID of the target vertex.true if the arc was successfully removed.const isArcRemoved = await graph.removeArc('id1', 'id2');
console.log(isArcRemoved); // true
Checks if an arc exists between two vertices.
sourceVertexId: uuidType - The ID of the source vertex.targetVertexId: uuidType - The ID of the target vertex.true if the arc exists, otherwise false.const hasArc = await graph.hasArc('id1', 'id2');
console.log(hasArc); // true or false
Retrieves vertices up to a specified depth level from a starting vertex.
vertexId: uuidType - The ID of the starting vertex.maxLevel?: number - (Optional) The depth level to limit the search.const graphTree = await graph.findUpToLevel('id1', 2);
console.log(graphTree);
/* Output: [
{ id: 'id1',data:{...},links:[...],level: 0 },
{ id: 'id2',data:{...},links:[...], level: 1 },
{ id: 'id3', data:{...},links:[...],level: 2 }
]; */
Performs a search starting from the given vertex and returns vertices that match the predicate.
vertexId: uuidType - The ID of the starting vertex.predicate: IPredicate<T> - A function used to evaluate each vertex. Only vertices that satisfy the predicate will be returned.const matchingVertices = await graph.searchVerticesFrom(
'id1',
vertex => vertex.data.age > 25,
);
console.log(matchingVertices);
// Output: [{ id: 'id2', data: { name: 'Alice', age: 30 }, links: [] }]
Checks if a path exists between two vertices using Depth-First Search (DFS).
sourceVertexId: uuidType - The ID of the source vertex.targetVertexId: uuidType - The ID of the target vertex.true if a path exists between the vertices, otherwise false.const pathExists = await graph.hasPath('id1', 'id3');
console.log(pathExists); // true or false
No comments yet.
Sign in to be the first to comment.