LanceDB 存储
🌐 LanceDB Storage
LanceDB 存储实现提供了一种高性能存储解决方案,使用 LanceDB 数据库系统,能够出色地处理传统数据存储和向量操作。
🌐 The LanceDB storage implementation provides a high-performance storage solution using the LanceDB database system, which excels at handling both traditional data storage and vector operations.
LanceDB storage does not support the observability domain. Traces from the DefaultExporter cannot be persisted to LanceDB, and Mastra Studio's observability features won't work with LanceDB as your only storage provider. To enable observability, use composite storage to route observability data to a supported provider like ClickHouse or PostgreSQL.
安装Direct link to 安装
🌐 Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/lance@latest
pnpm add @mastra/lance@latest
yarn add @mastra/lance@latest
bun add @mastra/lance@latest
用法Direct link to 用法
🌐 Usage
基本存储使用Direct link to 基本存储使用
🌐 Basic Storage Usage
import { LanceStorage } from "@mastra/lance";
// Connect to a local database
const storage = await LanceStorage.create("my-storage", "/path/to/db");
// Connect to a LanceDB cloud database
const storage = await LanceStorage.create("my-storage", "db://host:port");
// Connect to a cloud database with custom options
const storage = await LanceStorage.create("my-storage", "s3://bucket/db", {
storageOptions: { timeout: "60s" },
});
参数Direct link to 参数
🌐 Parameters
LanceStorage.create()Direct link to LanceStorage.create()
name:
uri:
options?:
附加说明Direct link to 附加说明
🌐 Additional Notes
模式管理Direct link to 模式管理
🌐 Schema Management
LanceStorage 实现会自动处理模式的创建和更新。它将 Mastra 的模式类型映射到 Apache Arrow 数据类型,这些数据类型在 LanceDB 内部使用:
🌐 The LanceStorage implementation automatically handles schema creation and updates. It maps Mastra's schema types to Apache Arrow data types, which are used by LanceDB internally:
text,uuid→ Utf8int,integer→ Int32float→ 浮点32jsonb,json→ Utf8(序列化)binary→ 二进制
初始化Direct link to 初始化
🌐 Initialization
当你将存储传递给 Mastra 类时,init() 会在任何存储操作之前自动调用:
🌐 When you pass storage to the Mastra class, init() is called automatically before any storage operation:
import { Mastra } from "@mastra/core";
import { LanceStorage } from "@mastra/lance";
const storage = await LanceStorage.create("my-storage", "/path/to/db");
const mastra = new Mastra({
storage, // init() is called automatically
});
如果你直接使用存储而不使用 Mastra,必须显式调用 init() 来创建表格:
🌐 If you're using storage directly without Mastra, you must call init() explicitly to create the tables:
import { LanceStorage } from "@mastra/lance";
const storage = await LanceStorage.create("my-storage", "/path/to/db");
// Required when using storage directly
await storage.init();
// Access domain-specific stores via getStore()
const memoryStore = await storage.getStore('memory');
const thread = await memoryStore?.getThreadById({ threadId: "..." });
如果未调用 init(),表将不会被创建,存储操作可能会静默失败或抛出错误。
部署选项Direct link to 部署选项
🌐 Deployment Options
LanceDB 存储可以根据不同的部署场景进行配置:
🌐 LanceDB storage can be configured for different deployment scenarios:
-
本地开发:在开发和测试时使用本地文件路径
/path/to/db -
云部署:连接到托管的 LanceDB 实例
db://host:port -
S3 存储:使用 Amazon S3 进行可扩展的云存储
s3://bucket/db
桌面管理Direct link to 桌面管理
🌐 Table Management
LanceStorage 提供了管理表的方法:
🌐 LanceStorage provides methods for managing tables:
- 创建自定义模式的表
- 删除表
- 清空表格(删除所有记录)
- 按键加载记录
- 插入单条和批量记录