Lightweight In-Memory Graph Database

3.1.0 · active · verified Wed Apr 22

Simple-graphdb is an open-source, lightweight, in-memory graph database designed for managing graph data structures directly within a JavaScript/TypeScript application. Currently at version 3.1.0, it offers a stable API for creating, manipulating, and traversing nodes and edges with properties. Unlike persistent graph databases such as Neo4j or Ontotext's GraphDB, simple-graphdb does not offer data persistence or advanced enterprise features like clustering or complex query languages. Its primary differentiators are its simplicity, zero-dependency footprint, and suitability for use cases where graph data needs to be modeled and queried programmatically within a single application process, such as social network simulations, routing algorithms, or in-memory knowledge graphs for smaller datasets. The library typically follows a moderate, stable release cadence, focusing on core graph operations and performance within its in-memory scope.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a graph, add nodes and edges with properties, and perform basic traversal and querying operations to find related data.

import { Graph } from 'simple-graphdb';

// Create a new graph instance
const graph = new Graph();

// Add nodes with types and properties
const userNode = graph.addNode('User', { name: 'Alice', email: 'alice@example.com' });
const productNode = graph.addNode('Product', { name: 'Laptop', price: 1200 });
const orderNode = graph.addNode('Order', { orderId: 'ORD001', date: new Date() });

// Add directed edges to represent relationships
graph.addEdge(userNode.id, orderNode.id, 'PLACED');
graph.addEdge(orderNode.id, productNode.id, 'CONTAINS', { quantity: 1 });

// Query the graph
const aliceOrders = graph.getChildren(userNode.id, { edgeType: 'PLACED' });
console.log('Alice\'s orders:', aliceOrders.map(node => node.properties));

const orderProducts = graph.getChildren(orderNode.id, { edgeType: 'CONTAINS' });
console.log('Products in Order ORD001:', orderProducts.map(node => node.properties));

// Find a path between Alice and the Laptop product
const path = graph.traverse(userNode.id, productNode.id, { method: 'bfs' });
if (path) {
  console.log('Path from Alice to Laptop:', path);
} else {
  console.log('No path found from Alice to Laptop.');
}

view raw JSON →