Database API Reference
TheDatabase is the main interface for interacting with EngramDB. This document provides a detailed reference for the Database API.
Creating a Database
Database::in_memory()
Creates a new in-memory database with linear vector search (volatile, for testing and development).
Returns:
- A new
Databaseinstance using memory storage and linear vector index
Database::in_memory_with_hnsw()
Creates a new in-memory database with HNSW vector search for faster similarity queries.
Returns:
- A new
Databaseinstance using memory storage and HNSW vector index
Database::file_based(dir)
Creates a file-based database at the specified directory with linear vector search.
Parameters:
dir:&strorPath- Path to the storage directory
- A new
Databaseinstance using file storage, or an error if initialization failed
Database::file_based_with_hnsw(dir)
Creates a file-based database at the specified directory with HNSW vector search.
Parameters:
dir:&strorPath- Path to the storage directory
- A new
Databaseinstance using file storage and HNSW vector index, or an error if initialization failed
Database::new(config)
Creates a new database with the given configuration.
Parameters:
config:DatabaseConfig- Configuration options for the database
- A new
Databaseinstance, or an error if initialization failed
DatabaseConfig
TheDatabaseConfig struct provides configuration options for the database:
storage_type: The type of storage to use (StorageType::Memory,StorageType::MultiFile, orStorageType::SingleFile)storage_path: Directory path for file storage (ignored if using memory storage)cache_size: Size of the query result cache (0 to disable caching)vector_index_config: Configuration for the vector index
StorageType
VectorIndexConfig
algorithm: The vector indexing algorithm to use (VectorAlgorithm::LinearorVectorAlgorithm::HNSW)hnsw: Configuration for the HNSW algorithm (if used)
Basic Operations
initialize()
Initializes the database by loading existing memories into the vector index.
Returns:
Result<()>- Success or an error
save(node)
Saves a memory node to the database.
Parameters:
node:&MemoryNode- The memory node to save
Result<Uuid>- The ID of the saved memory node, or an error
load(id)
Loads a memory node by its ID.
Parameters:
id:Uuid- The ID of the memory node to load
Result<MemoryNode>- The loaded memory node, or an error if not found
delete(id)
Deletes a memory node by its ID.
Parameters:
id:Uuid- The ID of the memory node to delete
Result<()>- Success or an error
list_all()
Lists all memory node IDs in the database.
Returns:
Result<Vec<Uuid>>- A vector of all memory node IDs, or an error
Vector Similarity Search
search_similar(query_vector, limit, threshold)
Searches for memory nodes with similar vector embeddings.
Parameters:
query_vector:&[f32]- The query vectorlimit:usize- Maximum number of results to returnthreshold:f32- Minimum similarity threshold (0.0 to 1.0)
Result<Vec<(Uuid, f32)>>- A vector of (ID, similarity) pairs, sorted by descending similarity
Query Builder
query()
Creates a new query builder for complex queries.
Returns:
- A new
QueryBuilderinstance
Connection Management
get_connections(id)
Gets all connections for a memory node.
Parameters:
id:Uuid- The ID of the memory node
Result<Vec<ConnectionInfo>>- A vector of connection information, or an error
add_connection(source_id, target_id, relationship_type, strength)
Adds a connection between two memory nodes.
Parameters:
source_id:Uuid- The ID of the source memory nodetarget_id:Uuid- The ID of the target memory noderelationship_type:RelationshipType- The type of relationshipstrength:f32- The strength of the connection (0.0 to 1.0)
Result<()>- Success or an error
remove_connection(source_id, target_id)
Removes a connection between two memory nodes.
Parameters:
source_id:Uuid- The ID of the source memory nodetarget_id:Uuid- The ID of the target memory node
Result<bool>- True if a connection was removed, false otherwise, or an error
Background Processing
EngramDB includes a background processing system that can perform operations during idle periods:Enabling Background Processing
Scheduling Background Tasks
Available Task Types
EngramDB supports several types of background tasks:Error Handling
The database operations return aResult type that can contain various error types:

