stores/duckdb/README.md
DuckDB vector store implementation for Mastra, providing high-performance embedded vector similarity search with HNSW indexing. No external server required - runs entirely in-process.
npm install @mastra/duckdb
import { DuckDBVector } from '@mastra/duckdb';
// Create a vector store with in-memory database
const vectorStore = new DuckDBVector({
id: 'my-vector-store',
path: ':memory:', // or './vectors.duckdb' for persistence
});
// Create a new index with vector support
await vectorStore.createIndex({
indexName: 'my_vectors',
dimension: 1536,
metric: 'cosine',
});
// Add vectors with metadata
const ids = await vectorStore.upsert({
indexName: 'my_vectors',
vectors: [
[0.1, 0.2, 0.3],
[0.3, 0.4, 0.5],
], // truncated - use actual 1536-dim vectors
metadata: [
{ text: 'doc1', category: 'A' },
{ text: 'doc2', category: 'B' },
],
});
// Query similar vectors
const results = await vectorStore.query({
indexName: 'my_vectors',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual 1536-dim vector
topK: 10,
filter: { category: 'A' },
includeVector: false,
});
// Clean up
await vectorStore.close();
import { Mastra } from '@mastra/core';
import { DuckDBVector } from '@mastra/duckdb';
const vectorStore = new DuckDBVector({
id: 'rag-store',
path: './rag-vectors.duckdb',
});
// Use with Mastra's RAG system
const mastra = new Mastra({
vectors: {
ragStore: vectorStore,
},
});
| Option | Type | Default | Description |
|---|---|---|---|
id | string | required | Unique identifier for the vector store instance |
path | string | ':memory:' | Database file path. Use :memory: for in-memory database |
dimensions | number | 1536 | Default dimension for vector embeddings |
metric | 'cosine' | 'euclidean' | 'dotproduct' | 'cosine' | Default distance metric for similarity search |
// In-memory (fast, non-persistent)
const memoryStore = new DuckDBVector({
id: 'memory-store',
path: ':memory:',
});
// File-based (persistent)
const fileStore = new DuckDBVector({
id: 'file-store',
path: './data/vectors.duckdb',
dimensions: 768,
metric: 'cosine',
});
// With euclidean distance
const euclideanStore = new DuckDBVector({
id: 'euclidean-store',
path: ':memory:',
metric: 'euclidean',
});
The following filter operators are supported for metadata queries:
$eq, $ne, $gt, $gte, $lt, $lte$and, $or, $not, $nor$in, $nin$exists$contains// Simple equality
const results = await vectorStore.query({
indexName: 'docs',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual embedding vector
filter: { category: 'technology' },
});
// Comparison operators
const results = await vectorStore.query({
indexName: 'docs',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual embedding vector
filter: { price: { $gt: 100, $lte: 500 } },
});
// Logical operators
const results = await vectorStore.query({
indexName: 'docs',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual embedding vector
filter: {
$and: [{ category: 'electronics' }, { $or: [{ brand: 'Apple' }, { brand: 'Samsung' }] }],
},
});
// Array operators
const results = await vectorStore.query({
indexName: 'docs',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual embedding vector
filter: { tags: { $in: ['featured', 'sale'] } },
});
// Nested field access
const results = await vectorStore.query({
indexName: 'docs',
queryVector: [0.1, 0.2, 0.3], // truncated - use actual embedding vector
filter: { 'user.profile.tier': 'premium' },
});
createIndex({ indexName, dimension, metric? }): Create a new table with vector support and optional HNSW indexlistIndexes(): List all vector-enabled tablesdescribeIndex({ indexName }): Get table statistics (dimension, count, metric)deleteIndex({ indexName }): Delete a table and its dataupsert({ indexName, vectors, metadata?, ids? }): Add or update vectorsquery({ indexName, queryVector, topK?, filter?, includeVector? }): Search for similar vectorsupdateVector({ indexName, id?, filter?, update }): Update a vector by ID or metadata filterdeleteVector({ indexName, id }): Delete a single vector by IDdeleteVectors({ indexName, ids?, filter? }): Delete multiple vectors by IDs or metadata filterclose(): Close the database connection| Metric | Description | Score Range | Best For |
|---|---|---|---|
cosine | Cosine similarity | 0-1 (1 = identical) | Text embeddings, normalized vectors |
euclidean | L2 distance | 0-∞ (0 = identical) | Image embeddings, spatial data |
dotproduct | Inner product | -∞ to ∞ | When magnitude matters |
Build offline-capable AI applications with semantic search that runs entirely in-process without external dependencies.
const vectorStore = new DuckDBVector({
id: 'semantic-search',
path: './search.duckdb',
});
Process sensitive documents locally without sending data to cloud vector databases.
const vectorStore = new DuckDBVector({
id: 'local-rag',
path: './private-docs.duckdb',
dimensions: 1536, // OpenAI embeddings
});
Rapidly prototype vector search features with zero infrastructure setup.
const vectorStore = new DuckDBVector({
id: 'dev-store',
path: ':memory:', // Fast in-memory for testing
});