libSQL 儲存
libSQL 是一個開源、兼容 SQLite 的數據庫,支援本機及遙距部署。它可用於儲存訊息記錄、Workflow 快照、Trace 和 Evals 分數。
如要處理語義回憶或傳統 RAG 等向量用途,請使用 libSQL Vector,當中涵蓋 embedding 和向量搜尋。
安裝安裝 的直接連結
儲存 Provider 必須以獨立依賴套件安裝:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/libsql@latest
pnpm add @mastra/libsql@latest
yarn add @mastra/libsql@latest
bun add @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 數據庫的驗證 token。
受管理的資料表受管理的資料表 的直接連結
儲存實作會自動建立核心儲存資料表,包括用於通知收件箱記錄和傳送 metadata 的 mastra_notifications。
LibSQLStore 透過 getStore('notifications') 提供通知儲存。
初始化初始化 的直接連結
當你將儲存傳入 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 而直接使用儲存,請明確呼叫 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: '...' })
可觀測性可觀測性 的直接連結
libSQL 支援可觀測性,而且非常適合本機開發。偵錯時請使用 realtime 追蹤策略,即時查看結果。
如果生產環境的 Trace 數量較多,可考慮使用 PostgreSQL 或透過複合儲存使用 ClickHouse。