DefaultVectorDB Store
The DefaultVectorDB class provides vector search using LibSQL, a fork of SQLite with vector extensions, and Turso with vector extensions, offering a lightweight and efficient vector database solution.
It’s part of the @mastra/core
package and offers efficient vector similarity search with metadata filtering.
Installation
Default vector store is included in the core package:
npm install @mastra/core
Usage
import { DefaultVectorDB } from '@mastra/core/storage';
// Create a new vector store instance
const store = new DefaultVectorDB({
connectionUrl: process.env.DATABASE_URL,
// Optional: for Turso cloud databases
authToken: process.env.DATABASE_AUTH_TOKEN,
});
// Create an index
await store.createIndex("my-collection", 1536);
// Add vectors with metadata
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
const metadata = [
{ text: "first document", category: "A" },
{ text: "second document", category: "B" }
];
await store.upsert("my-collection", vectors, metadata);
// Query similar vectors
const queryVector = [0.1, 0.2, ...];
const results = await store.query(
"my-collection",
queryVector,
10, // top K results
{ category: "A" } // optional metadata filter
);
Constructor Options
connectionUrl:
authToken?:
syncUrl?:
syncInterval?:
Methods
createIndex()
Creates a new vector collection. The index name must start with a letter or underscore and can only contain letters, numbers, and underscores. The dimension must be a positive integer.
indexName:
dimension:
metric?:
upsert()
Adds or updates vectors and their metadata in the index. Uses a transaction to ensure all vectors are inserted atomically - if any insert fails, the entire operation is rolled back.
indexName:
vectors:
metadata?:
ids?:
query()
Searches for similar vectors with optional metadata filtering.
indexName:
queryVector:
topK?:
filter?:
includeVector?:
minScore?:
describeIndex()
Gets information about an index.
indexName:
Returns:
interface IndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
}
deleteIndex()
Deletes an index and all its data.
indexName:
listIndexes()
Lists all vector indexes in the database.
Returns: Promise<string[]>
truncateIndex()
Removes all vectors from an index while keeping the index structure.
indexName:
Response Types
Query results are returned in this format:
interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
}
Error Handling
The store throws specific errors for different failure cases:
try {
await store.query("my-collection", queryVector);
} catch (error) {
// Handle specific error cases
if (error.message.includes("Invalid index name format")) {
console.error(
"Index name must start with a letter/underscore and contain only alphanumeric characters",
);
} else if (error.message.includes("Table not found")) {
console.error("The specified index does not exist");
} else {
console.error("Vector store error:", error.message);
}
}
Common error cases include:
- Invalid index name format
- Invalid vector dimensions
- Table/index not found
- Database connection issues
- Transaction failures during upsert