Lightweight In-Memory Graph Database
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
-
TypeError: Graph is not a constructor
cause Attempting to import the Graph class incorrectly, often using a default import or CommonJS `require()` pattern instead of a named import for an ESM-style module.fixEnsure you are using a named import: `import { Graph } from 'simple-graphdb';` in an ES module environment or with appropriate transpilation. -
Error: Node with ID 'xyz123' not found.
cause Attempting to access, modify, or traverse from a node ID that does not exist in the current graph instance.fixVerify the node ID exists using `graph.hasNode(id)` before attempting operations, or ensure that the ID is correctly obtained from a previously added node or a deserialized graph. -
Cannot find module 'simple-graphdb'
cause Node.js (or bundler) cannot resolve the package, often due to an ES module/CommonJS mismatch or incorrect `tsconfig.json`/`package.json` configuration.fixEnsure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`, targetting `esnext` in `tsconfig.json`), or verify that your bundler (Webpack, Rollup, etc.) is correctly configured to handle ES Modules. If using CommonJS, ensure you're using `const { Graph } = require('simple-graphdb');` (though this might not work if the package is strictly ESM).
Warnings
- gotcha simple-graphdb is an in-memory database. All data is lost when the application process terminates unless explicitly serialized and deserialized by the user. There are no built-in persistence mechanisms.
- gotcha Performance may degrade significantly with very large graphs (tens of thousands of nodes/edges or more) due to its in-memory nature and single-threaded JavaScript execution. It is not designed for petabyte-scale graph data.
- gotcha Node and edge IDs are internally generated strings. While unique within a single graph instance, they are not guaranteed to be stable across application restarts or different graph instances unless you manage them explicitly during serialization/deserialization.
- gotcha The library does not provide built-in concurrency control or transaction management. If multiple asynchronous operations modify the same graph instance concurrently, data corruption or unexpected behavior may occur.
Install
-
npm install simple-graphdb -
yarn add simple-graphdb -
pnpm add simple-graphdb
Imports
- Graph
const Graph = require('simple-graphdb');import { Graph } from 'simple-graphdb'; - Node
import { Node } from 'simple-graphdb'; - Edge
import { Edge } from 'simple-graphdb';
Quickstart
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.');
}