stores/clickhouse/README.md
Clickhouse implementation for Mastra, providing efficient storage capabilities with support for threads, messages, and workflow snapshots.
npm install @mastra/clickhouse
import { ClickhouseStore } from '@mastra/clickhouse';
const store = new ClickhouseStore({
url: 'http://localhost:8123',
username: 'default',
password: 'password',
});
// Create a thread
await store.saveThread({
thread: {
id: 'thread-123',
resourceId: 'resource-456',
title: 'My Thread',
metadata: { key: 'value' },
createdAt: new Date(),
},
});
// Add messages to thread
await store.saveMessages({
messages: [
{
id: 'msg-789',
threadId: 'thread-123',
role: 'user',
content: { content: 'Hello' },
resourceId: 'resource-456',
createdAt: new Date(),
},
],
});
// Query threads and messages
const savedThread = await store.getThreadById({ threadId: 'thread-123' });
const { messages } = await store.listMessages({ threadId: 'thread-123' });
// Clean up
await store.close();
The Clickhouse store can be initialized with the following configuration:
type ClickhouseConfig = {
url: string; // Clickhouse HTTP interface URL
username: string; // Database username
password: string; // Database password
};
The store uses different table engines for different types of data:
MergeTree(): Used for messages, traces, and evalsReplacingMergeTree(): Used for threads and workflow snapshotssaveThread({ thread }): Create or update a threadgetThreadById({ threadId }): Get a thread by IDlistThreadsByResourceId({ resourceId, offset, limit, orderBy? }): List paginated threads for a resourceupdateThread({ id, title, metadata }): Update thread title and metadatadeleteThread({ threadId }): Delete a thread and its messagessaveMessages({ messages }): Save multiple messageslistMessages({ threadId, perPage?, page? }): Get messages for a thread with paginationupdateMessages({ messages }): Update existing messagesgetResourceById({ resourceId }): Get a resource by IDsaveResource({ resource }): Create or save a resourceupdateResource({ resourceId, workingMemory }): Update resource working memorypersistWorkflowSnapshot({ workflowName, runId, snapshot }): Save workflow stateloadWorkflowSnapshot({ workflowName, runId }): Load workflow statelistWorkflowRuns({ workflowName, pagination }): List workflow runs with paginationgetWorkflowRunById({ workflowName, runId }): Get a specific workflow rungetScoreById({ id }): Get a score by IDsaveScore(score): Save an evaluation scorelistScoresByScorerId({ scorerId, pagination }): List scores by scorer with paginationlistScoresByRunId({ runId, pagination }): List scores by run with paginationlistScoresByEntityId({ entityId, entityType, pagination }): List scores by entity with paginationlistScoresBySpan({ traceId, spanId, pagination }): List scores by span with paginationdeleteMessages(messageIds): Message deletion is not supported in ClickHouseThe store supports the following data types:
text: Stringtimestamp: DateTime64(3)uuid: Stringjsonb: String (JSON serialized)integer: Int64bigint: Int64float: Float64boolean: Bool