
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
@mastra/lance
Advanced tools
Affected versions:
Lance provider for Mastra - includes both vector and db storage capabilities
This guide explains how to use LanceDB as both a storage backend and vector database with Mastra. LanceDB provides a high-performance, open-source, and embeddable vector database built on Lance file format.
pnpm add @mastra/lance @lancedb/lancedb apache-arrow
import { LanceStorage } from '@mastra/lance';
import { Mastra } from '@mastra/core/mastra';
// Initialize LanceStorage
const storage = await LanceStorage.create(
'myApp', // Name for your storage instance
'path/to/db', // Path to database directory
);
// Configure Mastra with LanceStorage
const mastra = new Mastra({
storage: storage,
});
LanceStorage supports multiple connection configurations:
// Local database
const localStore = await LanceStorage.create('myApp', '/path/to/db');
// LanceDB Cloud
const cloudStore = await LanceStorage.create('myApp', 'db://host:port');
// S3 bucket
const s3Store = await LanceStorage.create('myApp', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
import { TABLE_MESSAGES } from '@mastra/core/storage';
import type { StorageColumn } from '@mastra/core/storage';
// Define schema with appropriate types
const schema: Record<string, StorageColumn> = {
id: { type: 'uuid', nullable: false },
threadId: { type: 'uuid', nullable: false },
content: { type: 'text', nullable: true },
createdAt: { type: 'timestamp', nullable: false },
metadata: { type: 'jsonb', nullable: true },
};
// Create table
await storage.createTable({
tableName: TABLE_MESSAGES,
schema,
});
// Insert a single record
await storage.insert({
tableName: TABLE_MESSAGES,
record: {
id: '123e4567-e89b-12d3-a456-426614174000',
threadId: '123e4567-e89b-12d3-a456-426614174001',
content: 'Hello, world!',
createdAt: new Date(),
metadata: { tags: ['important', 'greeting'] },
},
});
// Batch insert multiple records
await storage.batchInsert({
tableName: TABLE_MESSAGES,
records: [
{
id: '123e4567-e89b-12d3-a456-426614174002',
threadId: '123e4567-e89b-12d3-a456-426614174001',
content: 'First message',
createdAt: new Date(),
metadata: { priority: 'high' },
},
{
id: '123e4567-e89b-12d3-a456-426614174003',
threadId: '123e4567-e89b-12d3-a456-426614174001',
content: 'Second message',
createdAt: new Date(),
metadata: { priority: 'low' },
},
],
});
// Load a record by id
const message = await storage.load({
tableName: TABLE_MESSAGES,
keys: { id: '123e4567-e89b-12d3-a456-426614174000' },
});
// Load messages from a thread
const messages = await storage.listMessages({
threadId: '123e4567-e89b-12d3-a456-426614174001',
});
import type { StorageThreadType } from '@mastra/core/storage';
// Create a new thread
const thread: StorageThreadType = {
id: '123e4567-e89b-12d3-a456-426614174010',
resourceId: 'resource-123',
title: 'New Discussion',
createdAt: new Date(),
metadata: { topic: 'technical-support' },
};
// Save the thread
const savedThread = await storage.saveThread({ thread });
import type { MessageType } from '@mastra/core/memory';
// Create messages
const messages: MessageType[] = [
{
id: 'msg-001',
threadId: '123e4567-e89b-12d3-a456-426614174010',
resourceId: 'resource-123',
role: 'user',
content: 'How can I use LanceDB with Mastra?',
createdAt: new Date(),
type: 'text',
toolCallIds: [],
toolCallArgs: [],
toolNames: [],
},
{
id: 'msg-002',
threadId: '123e4567-e89b-12d3-a456-426614174010',
resourceId: 'resource-123',
role: 'assistant',
content: 'You can use LanceDB with Mastra by installing @mastra/lance package.',
createdAt: new Date(),
type: 'text',
toolCallIds: [],
toolCallArgs: [],
toolNames: [],
},
];
// Save messages
await storage.saveMessages({ messages });
// Retrieve messages with pagination and context
const retrievedMessages = await storage.listMessages({
threadId: '123e4567-e89b-12d3-a456-426614174010',
perPage: 10,
page: 0,
include: [
{
id: 'msg-001',
withPreviousMessages: 5, // Include up to 5 messages before this one
withNextMessages: 5, // Include up to 5 messages after this one
},
],
});
Mastra's workflow system uses LanceDB to persist workflow state for continuity across runs:
import type { WorkflowRunState } from '@mastra/core/storage';
// Persist a workflow snapshot
await storage.persistWorkflowSnapshot({
workflowName: 'documentProcessing',
runId: 'run-123',
snapshot: {
context: {
steps: {
step1: { status: 'success', payload: { data: 'processed' } },
step2: { status: 'waiting' },
},
triggerData: { documentId: 'doc-123' },
attempts: { step1: 1, step2: 0 },
},
} as WorkflowRunState,
});
// Load a workflow snapshot
const workflowState = await storage.loadWorkflowSnapshot({
workflowName: 'documentProcessing',
runId: 'run-123',
});
The LanceDB integration in Mastra can be used for both traditional storage and vector search:
// Create a schema with vector field
const vectorSchema: Record<string, StorageColumn> = {
id: { type: 'uuid', nullable: false },
content: { type: 'text', nullable: true },
embedding: { type: 'binary', nullable: false }, // Vector embedding
metadata: { type: 'jsonb', nullable: true },
};
// Create a vector table
await storage.createTable({
tableName: 'vector_store',
schema: vectorSchema,
});
// Insert a vector with content and metadata
await storage.insert({
tableName: 'vector_store',
record: {
id: 'vec-001',
content: 'This is a document about LanceDB and Mastra integration',
embedding: new Float32Array([0.1, 0.2, 0.3, 0.4]), // Your embedding vector
metadata: { source: 'documentation', category: 'integration' },
},
});
// Drop a table
await storage.dropTable(TABLE_MESSAGES);
// Clear all records from a table
await storage.clearTable({ tableName: TABLE_MESSAGES });
// Get table schema
const schema = await storage.getTableSchema(TABLE_MESSAGES);
saveThread({ thread }): Create or update a threadgetThreadById({ threadId }): Get a thread by IDlistThreadsByResourceId({ resourceId, offset, limit, orderBy? }): List paginated threads for a resourceupdateThread({ id, title, metadata }): Update thread title and/or metadatadeleteThread({ threadId }): Delete a thread and its messagessaveMessages({ messages }): Save multiple messages in a transactionlistMessages({ threadId, resourceId?, perPage?, page?, orderBy?, filter?, include? }): Get messages for a thread with pagination and optional context inclusionlistMessagesById({ messageIds }): Get specific messages by their IDsupdateMessages({ messages }): Update existing messagesgetResourceById({ resourceId }): Get a resource by IDsaveResource({ resource }): Create or save a resourceupdateResource({ resourceId, workingMemory }): Update resource working memorypersistWorkflowSnapshot({ workflowName, runId, snapshot }): Save workflow stateloadWorkflowSnapshot({ workflowName, runId }): Load workflow statelistWorkflowRuns({ workflowName?, pagination? }): List workflow runs with paginationgetWorkflowRunById({ runId, workflowName? }): Get a specific workflow runupdateWorkflowState({ workflowName, runId, state }): Update workflow stateupdateWorkflowResults({ workflowName, runId, results }): Update workflow resultsgetScoreById({ id }): Get a score by IDsaveScore(score): Save an evaluation scorelistScoresByScorerId({ scorerId, pagination }): List scores by scorer with paginationlistScoresByRunId({ runId, pagination }): List scores by run with paginationlistScoresByEntityId({ entityId, entityType, pagination }): List scores by entity with paginationlistScoresBySpan({ traceId, spanId, pagination }): List scores by span with paginationcreateTable({ tableName, schema }): Create a new table with schemadropTable({ tableName }): Drop a tableclearTable({ tableName }): Clear all records from a tableinsert({ tableName, record }): Insert a single recordbatchInsert({ tableName, records }): Insert multiple recordsload({ tableName, keys }): Load a record by keysdeleteMessages(messageIds): Message deletion is not currently supportedFAQs
Lance provider for Mastra - includes both vector and db storage capabilities
The npm package @mastra/lance receives a total of 1,269 weekly downloads. As such, @mastra/lance popularity was classified as popular.
We found that @mastra/lance demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.