跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

DuckDB

DuckDB is an embedded, in-process analytical database. The @mastra/duckdb package provides an OLAP-backed observability store for local development, suitable for traces, logs, metrics, scores, and feedback without running an external service.

For vector search, see the DuckDB vector store reference, which is a separate API in the same package.

When to use DuckDB
When to use DuckDB的直接链接

Local development of observability features. DuckDB is embedded and file-based, so it doesn't require a server and starts instantly. It supports the same observability signals as ClickHouse, which makes it useful for testing dashboards and trace exploration before deploying to a production backend.

DuckDB currently implements only the observability domain. Pair it with another storage adapter (such as LibSQL) for memory and workflows in a composite storage setup.

注意

DuckDB is for development and not recommended for production. It runs in-process, persists to a single local file, and doesn't work on platforms with ephemeral filesystems (such as Railway, Fly.io, Render, Heroku, or serverless containers). For production observability, use ClickHouse.

Installation
Installation的直接链接

npm install @mastra/duckdb@latest

Usage
Usage的直接链接

As the observability domain in a composite store
As the observability domain in a composite store的直接链接

This is the standard local-development setup. LibSQL handles the other domains, and DuckDB handles observability.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { MastraCompositeStore } from '@mastra/core/storage'
import { LibSQLStore } from '@mastra/libsql'
import { DuckDBStore } from '@mastra/duckdb'
import { Observability, MastraStorageExporter } from '@mastra/observability'

export const mastra = new Mastra({
storage: new MastraCompositeStore({
id: 'composite-storage',
default: new LibSQLStore({
id: 'mastra-storage',
url: 'file:./mastra.db',
}),
domains: {
observability: new DuckDBStore().observability,
},
}),
observability: new Observability({
configs: {
default: {
serviceName: 'mastra',
exporters: [new MastraStorageExporter()],
},
},
}),
})

The .observability accessor returns the observability domain store directly. The equivalent generic form uses getStore(), which works for any composite-style storage adapter:

const observability = await new DuckDBStore().getStore('observability')

Standalone
Standalone的直接链接

When you need only observability storage outside the Mastra composite, instantiate DuckDBStore directly and access the observability domain:

import { DuckDBStore } from '@mastra/duckdb'

const duckdb = new DuckDBStore({ path: './traces.duckdb' })
const observability = duckdb.observability

await observability.init()

In-memory database
In-memory database的直接链接

Pass :memory: to use an ephemeral DuckDB instance. Data is lost when the process exits, which is appropriate for unit tests and short-lived scripts.

const duckdb = new DuckDBStore({ path: ':memory:' })

Configuration
Configuration的直接链接

DuckDBStore options
duckdbstore-options的直接链接

id?:

string
= 'duckdb'
Unique identifier for this storage instance.

path?:

string
= 'mastra.duckdb'
Path to the DuckDB database file. Use :memory: for an ephemeral in-memory database.

memoryLimit?:

string
= '2GB'
Maximum memory DuckDB may use, such as '2GB' or '512MB'. Larger-than-memory operations spill to disk for file-backed databases. Raise this for dedicated analytical workloads.

threads?:

number
= one per CPU core
Number of threads DuckDB may use. Lower this to keep queries from monopolizing all cores of a shared application server.

Lower-level types
Lower-level types的直接链接

@mastra/duckdb also exports DuckDBConnection for sharing a single underlying database across multiple Mastra storage instances, and the corresponding DuckDBStorageConfig type. Most applications won't need these directly.

Supported domains
Supported domains的直接链接

DuckDB currently implements one storage domain:

DomainSupported
observabilityYes
memoryNo
workflowsNo
scoresNo
agentsNo

For a full storage solution, compose DuckDBStore with an adapter that covers the missing domains (most commonly LibSQL for local development).

Initialization
Initialization的直接链接

When passed to Mastra through MastraCompositeStore, the observability domain initializes itself on first use. To run initialization explicitly outside of Mastra, call init() on the observability store:

import { DuckDBStore } from '@mastra/duckdb'

const duckdb = new DuckDBStore({ path: './traces.duckdb' })
await duckdb.observability.init()

Observability strategy
Observability strategy的直接链接

DuckDB supports the event-sourced strategy used by MastraStorageExporter, which buffers spans in memory and writes completed events in batches. This is appropriate for development-scale traffic. For high-volume production workloads, see MastraStorageExporter storage provider support.