跳到主要内容

libSQL 存储

libSQL 是一款开源且兼容 SQLite 的数据库,支持本地和远程部署。它可用于存储消息历史、Workflow 快照、Trace 和 eval 分数。

对于 semantic recall 或传统 RAG 等向量功能,请使用涵盖 embeddings 和向量搜索的 libSQL Vector

安装
安装的直接链接

Storage Provider 必须作为独立包安装:

npm install @mastra/libsql@latest

使用方法
使用方法的直接链接

import { LibSQLStore } from '@mastra/libsql'
import { Mastra } from '@mastra/core'

const mastra = new Mastra({
storage: new LibSQLStore({
id: 'libsql-storage',
url: 'file:./storage.db',
}),
})

Agent 级别的文件存储:

import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { LibSQLStore } from '@mastra/libsql'

export const agent = new Agent({
id: 'example-agent',
memory: new Memory({
storage: new LibSQLStore({
id: 'libsql-storage',
url: 'file:./agent.db',
}),
}),
})
注意

文件存储无法在具有临时文件系统的 serverless 平台上运行。对于 serverless 部署,请使用 Turso 或其他数据库引擎。

使用远程数据库进行生产:

storage: new LibSQLStore({
id: 'libsql-storage',
url: 'libsql://your-db-name.aws-ap-northeast-1.turso.io',
authToken: process.env.TURSO_AUTH_TOKEN,
})

对于本地开发和测试,可以将数据存储在内存中:

storage: new LibSQLStore({
id: 'libsql-storage',
url: ':memory:',
})
注意

进程变更时,内存存储会重置。仅适用于开发。

选项
选项的直接链接

url:

string
数据库 URL。内存数据库使用 :memory:,文件数据库使用 file:filename.db,远程存储使用 libSQL 连接字符串(例如 libsql://your-database.turso.io)。

authToken?:

string
远程 libSQL 数据库的身份验证令牌。

托管表
托管表的直接链接

该存储实现会自动创建核心存储表,其中包括用于通知收件箱记录和投递元数据的 mastra_notifications

LibSQLStore 通过 getStore('notifications') 暴露通知存储。

初始化
初始化的直接链接

将 storage 传递给 Mastra class 时,会自动调用 init() 来创建核心 schema

import { Mastra } from '@mastra/core'
import { LibSQLStore } from '@mastra/libsql'

const storage = new LibSQLStore({
id: 'libsql-storage',
url: 'file:./storage.db',
})

const mastra = new Mastra({
storage, // init() called automatically
})

如果不通过 Mastra 而直接使用 storage,请显式调用 init()

import { LibSQLStore } from '@mastra/libsql'

const storage = new LibSQLStore({
id: 'libsql-storage',
url: 'file:./storage.db',
})

await storage.init()

// Access domain-specific stores via getStore()
const memoryStore = await storage.getStore('memory')
const thread = await memoryStore?.getThreadById({ threadId: '...' })

Observability
Observability的直接链接

libSQL 支持 observability,非常适合本地开发。调试期间若要立即看到数据,请使用 realtime tracing strategy

对于 Trace 量更高的生产环境,请考虑使用 PostgreSQL通过组合存储使用 ClickHouse