In-memory key-value store with optional vector search.

A lightweight store implementation using JavaScript Maps. Supports basic key-value operations and vector search when configured with embeddings.

Example

// Basic key-value storage
const store = new InMemoryStore();
await store.put(["users", "123"], "prefs", { theme: "dark" });
const item = await store.get(["users", "123"], "prefs");

// Vector search with embeddings
import { OpenAIEmbeddings } from "@langchain/openai";
const store = new InMemoryStore({
index: {
dims: 1536,
embeddings: new OpenAIEmbeddings({ modelName: "text-embedding-3-small" }),
}
});

// Store documents
await store.put(["docs"], "doc1", { text: "Python tutorial" });
await store.put(["docs"], "doc2", { text: "TypeScript guide" });

// Search by similarity
const results = await store.search(["docs"], { query: "python programming" });

Warning

This store keeps all data in memory. Data is lost when the process exits. For persistence, use a database-backed store.

Hierarchy (view full)

Constructors

Properties

_indexConfig?: any
cosineSimilarity: any
data: any
doesMatch: any
extractTexts: any
filterItems: any
getOperation: any
getVectors: any
insertVectors: any
listNamespacesOperation: any
paginateResults: any
putOperation: any
scoreResults: any
vectors: any

Accessors

  • get indexConfig(): undefined | IndexConfig
  • Returns undefined | IndexConfig

Methods

  • Execute multiple operations in a single batch. This is more efficient than executing operations individually.

    Type Parameters

    Parameters

    • operations: Op

      Array of operations to execute

    Returns Promise<OperationResults<Op>>

    Promise resolving to results matching the operations

  • Delete an item from the store.

    Parameters

    • namespace: string[]

      Hierarchical path for the item

    • key: string

      Unique identifier within the namespace

    Returns Promise<void>

  • Retrieve a single item by its namespace and key.

    Parameters

    • namespace: string[]

      Hierarchical path for the item

    • key: string

      Unique identifier within the namespace

    Returns Promise<null | Item>

    Promise resolving to the item or null if not found

  • List and filter namespaces in the store. Used to explore data organization and navigate the namespace hierarchy.

    Parameters

    • Optional options: {
          limit?: number;
          maxDepth?: number;
          offset?: number;
          prefix?: string[];
          suffix?: string[];
      }

      Options for listing namespaces

      • Optional limit?: number
      • Optional maxDepth?: number
      • Optional offset?: number
      • Optional prefix?: string[]
      • Optional suffix?: string[]

    Returns Promise<string[][]>

    Promise resolving to list of namespace paths

    Example

    // List all namespaces under "documents"
    await store.listNamespaces({
    prefix: ["documents"],
    maxDepth: 2
    });

    // List namespaces ending with "v1"
    await store.listNamespaces({
    suffix: ["v1"],
    limit: 50
    });
  • Store or update an item.

    Parameters

    • namespace: string[]

      Hierarchical path for the item

    • key: string

      Unique identifier within the namespace

    • value: Record<string, any>

      Object containing the item's data

    • Optional index: false | string[]

      Optional indexing configuration

    Returns Promise<void>

    Example

    // Simple storage
    await store.put(["docs"], "report", { title: "Annual Report" });

    // With specific field indexing
    await store.put(
    ["docs"],
    "report",
    {
    title: "Q4 Report",
    chapters: [{ content: "..." }, { content: "..." }]
    },
    ["title", "chapters[*].content"]
    );
  • Search for items within a namespace prefix. Supports both metadata filtering and vector similarity search.

    Parameters

    • namespacePrefix: string[]

      Hierarchical path prefix to search within

    • Optional options: {
          filter?: Record<string, any>;
          limit?: number;
          offset?: number;
          query?: string;
      }

      Search options for filtering and pagination

      • Optional filter?: Record<string, any>
      • Optional limit?: number
      • Optional offset?: number
      • Optional query?: string

    Returns Promise<SearchItem[]>

    Promise resolving to list of matching items with relevance scores

    Example

    // Search with filters
    await store.search(["documents"], {
    filter: { type: "report", status: "active" },
    limit: 5,
    offset: 10
    });

    // Vector similarity search
    await store.search(["users", "content"], {
    query: "technical documentation about APIs",
    limit: 20
    });
  • Start the store. Override if initialization is needed.

    Returns void

  • Stop the store. Override if cleanup is needed.

    Returns void