Skip to content

In-Memory Storage

The @graphrag-sdk/in-memory-storage package provides in-memory and file-based storage for development, testing, and small-scale production use.

Installation

bash
pnpm add @graphrag-sdk/in-memory-storage

Features

  • Zero external dependencies — no database required
  • Fast performance — sub-millisecond operations
  • File persistence — optional JSON file storage
  • Simple setup — perfect for development and testing
  • Full feature support — implements all storage interfaces

Storage Options

In-Memory Storage

All data stored in RAM. Fast but not persistent across restarts.

typescript
import { createGraph } from 'graphrag-sdk';
import { lexicalGraph, chunkEntityGeneration } from '@graphrag-sdk/lexical';
import { inMemoryGraph, inMemoryVector, inMemoryKV } from '@graphrag-sdk/in-memory-storage';
import { openai } from '@ai-sdk/openai';

const graph = createGraph({
  graph: lexicalGraph({
    graphStoreFactory: inMemoryGraph,
    vectorStoreFactory: inMemoryVector,
    kvStoreFactory: inMemoryKV,
    pipeline: [chunkEntityGeneration()],
  }),
  model: openai('gpt-4o-mini'),
  embedding: openai.embedding('text-embedding-3-small'),
});

File-Based Storage (Persistent)

Data persists to JSON files on disk.

typescript
import { jsonGraph, jsonKV, inMemoryVector } from '@graphrag-sdk/in-memory-storage';

const graph = createGraph({
  graph: lexicalGraph({
    graphStoreFactory: (ns) => jsonGraph(`./data/${ns}-graph.json`),
    vectorStoreFactory: inMemoryVector,
    kvStoreFactory: (ns) => jsonKV(`./data/${ns}-kv.json`),
    pipeline: [chunkEntityGeneration()],
  }),
  model: openai('gpt-4o-mini'),
  embedding: openai.embedding('text-embedding-3-small'),
});

API Reference

inMemoryGraph(namespace)

Creates an in-memory graph store using Cytoscape.js.

typescript
import { inMemoryGraph } from '@graphrag-sdk/in-memory-storage';

const graphStore = inMemoryGraph('my-namespace');

Features:

  • Node and edge CRUD operations
  • Leiden clustering via Cytoscape.js
  • Community schema extraction
  • Knowledge graph queries

Implementation: Uses Cytoscape.js headless mode.

inMemoryVector(namespace)

Creates an in-memory vector store with cosine similarity search.

typescript
import { inMemoryVector } from '@graphrag-sdk/in-memory-storage';

const vectorStore = inMemoryVector('my-namespace');

Features:

  • Multiple named indexes (chunks, entities, questions)
  • Multiple distance metrics (cosine, euclidean, dotproduct)
  • Metadata filtering
  • Index management

inMemoryKV(namespace)

Creates an in-memory key-value store using JavaScript Map.

typescript
import { inMemoryKV } from '@graphrag-sdk/in-memory-storage';

const kvStore = inMemoryKV('my-namespace');

Features:

  • Fast lookups (O(1))
  • Field filtering
  • Batch operations

jsonGraph(filepath)

Creates a file-persisted graph store.

typescript
import { jsonGraph } from '@graphrag-sdk/in-memory-storage';

const graphStore = jsonGraph('./data/graph.json');

jsonKV(filepath)

Creates a file-persisted key-value store.

typescript
import { jsonKV } from '@graphrag-sdk/in-memory-storage';

const kvStore = jsonKV('./data/kv.json');

Usage with Factory Pattern

Storage functions are designed to work as factory functions — pass them directly to lexicalGraph():

typescript
import { inMemoryGraph, inMemoryVector, inMemoryKV } from '@graphrag-sdk/in-memory-storage';

// These ARE factory functions: (namespace: string) => Store
const graph = createGraph({
  graph: lexicalGraph({
    graphStoreFactory: inMemoryGraph,     // called with namespace automatically
    vectorStoreFactory: inMemoryVector,
    kvStoreFactory: inMemoryKV,
    pipeline: [chunkEntityGeneration()],
  }),
  model: openai('gpt-4o-mini'),
  embedding: openai.embedding('text-embedding-3-small'),
  namespace: 'my-project',  // passed to each factory
});

Performance

Benchmarks

Operations on 10,000 nodes/vectors (M1 Mac):

OperationinMemoryGraphinMemoryVectorinMemoryKV
Insert0.1ms0.05ms0.01ms
Lookup0.05ms-0.01ms
Vector Search (k=10)-2-5ms-
Graph Traversal (depth=2)1-3ms--

Memory Usage

Approximate memory usage:

  • Graph Node: ~1KB per node
  • Vector (1536-dim): ~6KB per vector
  • KV Entry: ~0.5-2KB per entry

Example: 10,000 documents with entities:

  • ~50,000 nodes x 1KB = 50MB
  • ~10,000 vectors x 6KB = 60MB
  • ~10,000 chunks x 1.5KB = 15MB
  • Total: ~125MB

Limitations

Good for:

  • Development and testing
  • Prototyping and demos
  • Small datasets (< 100K documents)
  • Single-server deployments

Not recommended for:

  • Large-scale production (> 100K documents)
  • Distributed systems
  • High-availability requirements

Next Steps

Released under the Elastic License 2.0. Made with ❤️ by Narek.