跳至主要內容

Storage

Mastra 會透過 src/mastra/ 正下方的 storage.ts 檔案設定專案的預設 Storage。此檔案會 default export 一個 store,取代 Memory、Workflow、Observability 與其他 Storage domain 所使用的內建記憶體內 store。

本頁說明檔案式慣例。若要了解後端選擇、Storage domain、保留政策與 Provider 詳細資訊,請參閱 Storage 概觀

快速開始
「快速開始」的直接連結

使用 LibSQLStore 建立以本機檔案為基礎的 store:

src/mastra/storage.ts
import { LibSQLStore } from '@mastra/libsql'

export default new LibSQLStore({
id: 'mastra-storage',
url: 'file:./mastra.db',
})

Mastra 會先註冊此 store,再註冊檔案式 Agent 與 Workflow,因此依賴 Storage 的 primitive 會繫結至此 store,而不是預設的記憶體內 store。

正式環境後端
「正式環境後端」的直接連結

storage.ts 可以 export 任何 Mastra Storage adapter,例如 LibSQL、PostgreSQL 或 MongoDB。若要了解設定模式、Provider 支援情況與 schema 詳細資訊,請參閱 Storage 概觀Observability signal 支援情況Storage 參考文件

與程式碼設定的優先順序
「與程式碼設定的優先順序」的直接連結

以程式碼註冊的 Storage 優先於 storage.ts。當整個專案使用一個 store 即可時,請使用 storage.ts;若設定取決於 src/mastra/index.ts 中的執行階段接線,請改用程式碼註冊。