@mastra/clickhouse 1.5.1-alpha.1 → 1.5.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @mastra/clickhouse
2
2
 
3
+ ## 1.5.2-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Changed ClickHouse background task deletes to use lightweight `DELETE FROM` instead of `ALTER TABLE ... DELETE` mutations with `mutations_sync`. This makes deleted rows immediately invisible to subsequent reads without forcing part rewrites for each delete. ([#15902](https://github.com/mastra-ai/mastra/pull/15902))
8
+
9
+ Improved bulk background task deletion to push filtering into ClickHouse instead of fetching all matching task IDs into Node.js memory first. This avoids unnecessary network transfer and out-of-memory risk when deleting large result sets. As a safety guard, calling `deleteTasks` with no filters is now a no-op — use `dangerouslyClearAll` to wipe the table.
10
+
11
+ - Updated dependencies [[`703a443`](https://github.com/mastra-ai/mastra/commit/703a44390c587d9c0b8ae94ec4edd8afb2a74044), [`808df1b`](https://github.com/mastra-ai/mastra/commit/808df1b39358b5f10b7317107e42b1fda7c87185)]:
12
+ - @mastra/core@1.29.1-alpha.1
13
+
14
+ ## 1.5.1
15
+
16
+ ### Patch Changes
17
+
18
+ - Fixed `mastra dev` repeatedly reporting `MIGRATION REQUIRED` on ClickHouse Cloud after `mastra migrate` had already run successfully. The observability migration check now recognizes the engine-name variants that ClickHouse Cloud and replicated clusters use in place of `ReplacingMergeTree`. ([#15623](https://github.com/mastra-ai/mastra/pull/15623))
19
+
20
+ - Improved ClickHouse v-next observability initialization errors to include the underlying ClickHouse message in the standard error text. This makes init failures actionable in loggers that only print `error.message`. ([#15588](https://github.com/mastra-ai/mastra/pull/15588))
21
+
22
+ - Updated dependencies [[`f112db1`](https://github.com/mastra-ai/mastra/commit/f112db179557ae9b5a0f1d25dc47f928d7d61cd9), [`21d9706`](https://github.com/mastra-ai/mastra/commit/21d970604d89eee970cbf8013d26d7551aff6ea5), [`0a0aa94`](https://github.com/mastra-ai/mastra/commit/0a0aa94729592e99885af2efb90c56aaada62247), [`ed07df3`](https://github.com/mastra-ai/mastra/commit/ed07df32a9d539c8261e892fc1bade783f5b41a6), [`01a7d51`](https://github.com/mastra-ai/mastra/commit/01a7d513493d21562f677f98550f7ceb165ba78c)]:
23
+ - @mastra/core@1.27.0
24
+
3
25
  ## 1.5.1-alpha.1
4
26
 
5
27
  ### Patch Changes
package/README.md CHANGED
@@ -10,7 +10,9 @@ npm install @mastra/clickhouse
10
10
 
11
11
  ## Prerequisites
12
12
 
13
- - Clickhouse server (version 21.12 or higher recommended)
13
+ - Clickhouse server (version 23.3 or higher required for delete operations; earlier versions may work for read/write operations)
14
+ - Lightweight `DELETE FROM` requires ClickHouse 22.8+ with `allow_experimental_lightweight_delete = 1` (for 22.8–23.2), or 23.3+ where it is generally available.
15
+ - The `deleteTask`, `deleteTasks`, and `deleteMessages` methods use `DELETE FROM` — ensure your server supports lightweight delete before using those operations.
14
16
  - Node.js 22.13.0 or later
15
17
 
16
18
  ## Usage
@@ -128,7 +130,6 @@ The store uses different table engines for different types of data:
128
130
 
129
131
  ### Operations Not Currently Supported
130
132
 
131
- - `deleteMessages(messageIds)`: Message deletion is not supported in ClickHouse
132
133
  - AI Observability (traces/spans): Not currently supported
133
134
 
134
135
  ## Data Types
@@ -3,7 +3,7 @@ name: mastra-clickhouse
3
3
  description: Documentation for @mastra/clickhouse. Use when working with @mastra/clickhouse APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/clickhouse"
6
- version: "1.5.1-alpha.1"
6
+ version: "1.5.2-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -16,6 +16,7 @@ Read the individual reference documents for detailed explanations and code examp
16
16
 
17
17
  ### Reference
18
18
 
19
+ - [Reference: ClickHouse storage](references/reference-storage-clickhouse.md) - Documentation for the ClickHouse storage implementation in Mastra, including the vNext observability domain.
19
20
  - [Reference: Composite storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
20
21
 
21
22
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.1-alpha.1",
2
+ "version": "1.5.2-alpha.0",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -0,0 +1,274 @@
1
+ # ClickHouse storage
2
+
3
+ [ClickHouse](https://clickhouse.com/) is a columnar database designed for analytical workloads. The `@mastra/clickhouse` package provides storage adapters for several Mastra storage domains and is the recommended backend for production observability.
4
+
5
+ ClickHouse is most commonly used as the dedicated observability backend in a [composite storage](https://mastra.ai/reference/storage/composite) setup, with another database serving the remaining domains. It can also back the supported domains on its own through `ClickhouseStore`.
6
+
7
+ ## When to use ClickHouse
8
+
9
+ - Production observability for traces, logs, metrics, scores, and feedback.
10
+ - Append-heavy workloads where columnar storage and compression help keep costs down.
11
+ - Deployment platforms with ephemeral filesystems (such as [Railway](#deploying-with-railway-and-similar-platforms), Fly.io, Render, Heroku, or container schedulers) where embedded backends like DuckDB cannot persist data.
12
+
13
+ For local development, [LibSQL](https://mastra.ai/reference/storage/libsql) or `@mastra/duckdb` are usually a better fit because they need no external service.
14
+
15
+ ## Installation
16
+
17
+ **npm**:
18
+
19
+ ```bash
20
+ npm install @mastra/clickhouse@latest
21
+ ```
22
+
23
+ **pnpm**:
24
+
25
+ ```bash
26
+ pnpm add @mastra/clickhouse@latest
27
+ ```
28
+
29
+ **Yarn**:
30
+
31
+ ```bash
32
+ yarn add @mastra/clickhouse@latest
33
+ ```
34
+
35
+ **Bun**:
36
+
37
+ ```bash
38
+ bun add @mastra/clickhouse@latest
39
+ ```
40
+
41
+ You will also need a running ClickHouse server. See [Hosting options](#hosting-options) for managed and self-hosted choices.
42
+
43
+ ## Usage
44
+
45
+ ### Observability with vNext (recommended)
46
+
47
+ `ObservabilityStorageClickhouseVNext` is the current observability domain implementation. It uses an insert-only schema backed by `ReplacingMergeTree` and is optimized for the volume produced by traces, logs, metrics, scores, and feedback.
48
+
49
+ Compose it with another storage adapter so observability writes do not contend with your application data:
50
+
51
+ ```typescript
52
+ import { Mastra } from '@mastra/core'
53
+ import { MastraCompositeStore } from '@mastra/core/storage'
54
+ import { LibSQLStore } from '@mastra/libsql'
55
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
56
+ import { Observability, DefaultExporter } from '@mastra/observability'
57
+
58
+ const observabilityStore = new ObservabilityStorageClickhouseVNext({
59
+ url: process.env.CLICKHOUSE_URL!,
60
+ username: process.env.CLICKHOUSE_USERNAME!,
61
+ password: process.env.CLICKHOUSE_PASSWORD!,
62
+ })
63
+
64
+ export const mastra = new Mastra({
65
+ storage: new MastraCompositeStore({
66
+ id: 'composite-storage',
67
+ default: new LibSQLStore({
68
+ id: 'mastra-storage',
69
+ url: 'file:./mastra.db',
70
+ }),
71
+ domains: {
72
+ observability: observabilityStore,
73
+ },
74
+ }),
75
+ observability: new Observability({
76
+ configs: {
77
+ default: {
78
+ serviceName: 'mastra',
79
+ exporters: [new DefaultExporter()],
80
+ },
81
+ },
82
+ }),
83
+ })
84
+ ```
85
+
86
+ `DefaultExporter` automatically selects the `insert-only` strategy when ClickHouse is the observability backend, which gives the highest write throughput. See [tracing strategies](https://mastra.ai/docs/observability/tracing/exporters/default) for details.
87
+
88
+ ### Observability with the legacy domain
89
+
90
+ `ObservabilityStorageClickhouse` is the original observability adapter and remains supported for projects that have not migrated to the vNext schema. The configuration shape is the same as the vNext class.
91
+
92
+ ```typescript
93
+ import { ObservabilityStorageClickhouse } from '@mastra/clickhouse'
94
+
95
+ const observabilityStore = new ObservabilityStorageClickhouse({
96
+ url: process.env.CLICKHOUSE_URL!,
97
+ username: process.env.CLICKHOUSE_USERNAME!,
98
+ password: process.env.CLICKHOUSE_PASSWORD!,
99
+ })
100
+ ```
101
+
102
+ New projects should use `ObservabilityStorageClickhouseVNext` instead.
103
+
104
+ ### Full storage with `ClickhouseStore`
105
+
106
+ `ClickhouseStore` implements the `memory`, `workflows`, `scores`, and `observability` domains. Use it when you want a single backend for the supported domains. For most observability deployments, the composite setup above is preferable because it isolates observability writes from primary application data.
107
+
108
+ ```typescript
109
+ import { Mastra } from '@mastra/core'
110
+ import { ClickhouseStore } from '@mastra/clickhouse'
111
+
112
+ export const mastra = new Mastra({
113
+ storage: new ClickhouseStore({
114
+ id: 'clickhouse-storage',
115
+ url: process.env.CLICKHOUSE_URL!,
116
+ username: process.env.CLICKHOUSE_USERNAME!,
117
+ password: process.env.CLICKHOUSE_PASSWORD!,
118
+ }),
119
+ })
120
+ ```
121
+
122
+ ### Bring your own ClickHouse client
123
+
124
+ Pass a pre-configured client when you need custom connection settings such as request timeouts, compression, or interceptors:
125
+
126
+ ```typescript
127
+ import { createClient } from '@clickhouse/client'
128
+ import { ClickhouseStore } from '@mastra/clickhouse'
129
+
130
+ const client = createClient({
131
+ url: process.env.CLICKHOUSE_URL!,
132
+ username: process.env.CLICKHOUSE_USERNAME!,
133
+ password: process.env.CLICKHOUSE_PASSWORD!,
134
+ request_timeout: 60_000,
135
+ compression: { request: true, response: true },
136
+ })
137
+
138
+ const storage = new ClickhouseStore({ id: 'clickhouse-storage', client })
139
+ ```
140
+
141
+ The same `client` form is accepted by `ObservabilityStorageClickhouse` and `ObservabilityStorageClickhouseVNext`.
142
+
143
+ ## Configuration
144
+
145
+ ### `ClickhouseStore` options
146
+
147
+ **id** (`string`): Unique identifier for this storage instance.
148
+
149
+ **url** (`string`): ClickHouse server URL (for example, \`https\://your-instance.clickhouse.cloud:8443\` or \`http\://localhost:8123\`). Required when not passing a pre-configured \`client\`.
150
+
151
+ **username** (`string`): ClickHouse username. Required when not passing a pre-configured \`client\`.
152
+
153
+ **password** (`string`): ClickHouse password. Required when not passing a pre-configured \`client\`. Can be an empty string for the default user on a local instance.
154
+
155
+ **client** (`ClickHouseClient`): Pre-configured ClickHouse client from \`@clickhouse/client\`. Use this when you need custom request settings. Mutually exclusive with the credential fields above.
156
+
157
+ **ttl** (`object`): Per-table TTL configuration applied at table creation time. Accepts row-level and column-level TTLs in interval units from \`NANOSECOND\` through \`YEAR\`.
158
+
159
+ **disableInit** (`boolean`): When \`true\`, the store does not run table creation or migrations on first use. Call \`storage.init()\` explicitly from your deployment scripts. (Default: `false`)
160
+
161
+ `ClickhouseStore` also accepts every option from `ClickHouseClientConfigOptions` (such as `database`, `request_timeout`, `compression`, `keep_alive`, and `max_open_connections`).
162
+
163
+ ### Observability domain options
164
+
165
+ `ObservabilityStorageClickhouse` and `ObservabilityStorageClickhouseVNext` accept the same connection options as `ClickhouseStore` (`url`, `username`, `password`, or a pre-configured `client`).
166
+
167
+ ## Hosting options
168
+
169
+ ClickHouse runs anywhere you can reach it over HTTP. Two common choices:
170
+
171
+ - **[ClickHouse Cloud](https://clickhouse.com/cloud)**: Managed service with a free trial tier. Provides connection details directly compatible with `url`, `username`, and `password`.
172
+ - **Self-hosted**: Run the official [`clickhouse/clickhouse-server`](https://hub.docker.com/r/clickhouse/clickhouse-server) container or install from the [official packages](https://clickhouse.com/docs/en/install). Suitable for VPS, dedicated hardware, or Kubernetes.
173
+
174
+ For local development:
175
+
176
+ ```bash
177
+ docker run -d --name mastra-clickhouse \
178
+ -p 8123:8123 -p 9000:9000 \
179
+ -e CLICKHOUSE_USER=default \
180
+ -e CLICKHOUSE_PASSWORD=password \
181
+ clickhouse/clickhouse-server
182
+ ```
183
+
184
+ ```typescript
185
+ new ObservabilityStorageClickhouseVNext({
186
+ url: 'http://localhost:8123',
187
+ username: 'default',
188
+ password: 'password',
189
+ })
190
+ ```
191
+
192
+ ## Deploying with Railway and similar platforms
193
+
194
+ Platforms like [Railway](https://railway.com), [Fly.io](https://fly.io), [Render](https://render.com), and Heroku run application containers on ephemeral filesystems. Embedded observability backends such as DuckDB require a writable, persistent local file, so they either lose data on restart or fail to deploy entirely on these platforms.
195
+
196
+ Use ClickHouse instead. Because ClickHouse is reached over HTTP, the same connection works from any host:
197
+
198
+ ```typescript
199
+ import { Mastra } from '@mastra/core'
200
+ import { MastraCompositeStore } from '@mastra/core/storage'
201
+ import { PostgresStore } from '@mastra/pg'
202
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
203
+ import { Observability, DefaultExporter } from '@mastra/observability'
204
+
205
+ export const mastra = new Mastra({
206
+ storage: new MastraCompositeStore({
207
+ id: 'composite-storage',
208
+ default: new PostgresStore({
209
+ id: 'pg',
210
+ connectionString: process.env.DATABASE_URL!,
211
+ }),
212
+ domains: {
213
+ observability: new ObservabilityStorageClickhouseVNext({
214
+ url: process.env.CLICKHOUSE_URL!,
215
+ username: process.env.CLICKHOUSE_USERNAME!,
216
+ password: process.env.CLICKHOUSE_PASSWORD!,
217
+ }),
218
+ },
219
+ }),
220
+ observability: new Observability({
221
+ configs: {
222
+ default: {
223
+ serviceName: 'mastra',
224
+ exporters: [new DefaultExporter()],
225
+ },
226
+ },
227
+ }),
228
+ })
229
+ ```
230
+
231
+ Two ways to provision the database:
232
+
233
+ - **Managed**: Use ClickHouse Cloud. Set `CLICKHOUSE_URL`, `CLICKHOUSE_USERNAME`, and `CLICKHOUSE_PASSWORD` as environment variables in your hosting platform.
234
+ - **Self-hosted on Railway**: Add a ClickHouse service to your Railway project from the official Docker image, then reference it in the application service through Railway's private networking.
235
+
236
+ The same approach applies to other hosts with ephemeral filesystems. For application data that should also live off-host, pair this setup with a managed PostgreSQL or LibSQL/Turso instance for the `default` storage.
237
+
238
+ > **Warning:** Do not point an embedded backend like DuckDB at a path inside an ephemeral container filesystem. Data written there is lost when the container restarts, and on some platforms the path is read-only.
239
+
240
+ ## Initialization
241
+
242
+ When passed to the `Mastra` class, `ClickhouseStore` calls `init()` automatically to create the schema and run any pending migrations. The same applies to `ObservabilityStorageClickhouseVNext` when used through `MastraCompositeStore`.
243
+
244
+ If you manage storage outside of `Mastra`, call `init()` explicitly:
245
+
246
+ ```typescript
247
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
248
+
249
+ const observability = new ObservabilityStorageClickhouseVNext({
250
+ url: process.env.CLICKHOUSE_URL!,
251
+ username: process.env.CLICKHOUSE_USERNAME!,
252
+ password: process.env.CLICKHOUSE_PASSWORD!,
253
+ })
254
+
255
+ await observability.init()
256
+ ```
257
+
258
+ In CI/CD pipelines, set `disableInit: true` on `ClickhouseStore` and run `init()` from a deployment step that uses elevated credentials. Runtime application credentials can then be limited to read and insert.
259
+
260
+ ## Observability
261
+
262
+ ClickHouse is the recommended backend for production observability:
263
+
264
+ - **Insert-only strategy**: `DefaultExporter` writes completed spans in batches without per-span updates, which is the highest-throughput strategy available.
265
+ - **Columnar compression**: Span attributes and log payloads compress well compared to the same data in row-oriented databases.
266
+
267
+ For the full strategy matrix and production guidance, see the [`DefaultExporter` reference](https://mastra.ai/docs/observability/tracing/exporters/default).
268
+
269
+ ## Related
270
+
271
+ - [Storage overview](https://mastra.ai/reference/storage/overview)
272
+ - [Composite storage](https://mastra.ai/reference/storage/composite)
273
+ - [`DefaultExporter`](https://mastra.ai/docs/observability/tracing/exporters/default)
274
+ - [Observability overview](https://mastra.ai/docs/observability/overview)
@@ -218,12 +218,12 @@ const storage = new MastraCompositeStore({
218
218
 
219
219
  Observability data can quickly overwhelm general-purpose databases in production. A single agent interaction can generate hundreds of spans, and high-traffic applications can produce thousands of traces per day.
220
220
 
221
- **ClickHouse** is recommended for production observability because it's optimized for high-volume, write-heavy analytics workloads. Use composite storage to route observability to ClickHouse while keeping other data in your primary database:
221
+ **[ClickHouse](https://mastra.ai/reference/storage/clickhouse)** is recommended for production observability because it's optimized for high-volume, write-heavy analytics workloads. Use composite storage to route observability to ClickHouse while keeping other data in your primary database:
222
222
 
223
223
  ```typescript
224
224
  import { MastraCompositeStore } from '@mastra/core/storage'
225
225
  import { MemoryPG, WorkflowsPG, ScoresPG } from '@mastra/pg'
226
- import { ObservabilityStorageClickhouse } from '@mastra/clickhouse'
226
+ import { ObservabilityStorageClickhouseVNext } from '@mastra/clickhouse'
227
227
 
228
228
  const storage = new MastraCompositeStore({
229
229
  id: 'composite',
@@ -231,7 +231,7 @@ const storage = new MastraCompositeStore({
231
231
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
232
232
  workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
233
233
  scores: new ScoresPG({ connectionString: process.env.DATABASE_URL }),
234
- observability: new ObservabilityStorageClickhouse({
234
+ observability: new ObservabilityStorageClickhouseVNext({
235
235
  url: process.env.CLICKHOUSE_URL,
236
236
  username: process.env.CLICKHOUSE_USERNAME,
237
237
  password: process.env.CLICKHOUSE_PASSWORD,
@@ -240,4 +240,6 @@ const storage = new MastraCompositeStore({
240
240
  })
241
241
  ```
242
242
 
243
+ > **Note:** `ObservabilityStorageClickhouseVNext` is the current observability domain implementation. The legacy `ObservabilityStorageClickhouse` class is also exported and remains supported for projects that have not migrated. See the [ClickHouse storage reference](https://mastra.ai/reference/storage/clickhouse) for details.
244
+
243
245
  > **Info:** This approach is also required when using storage providers that don't support observability (like Convex, DynamoDB, or Cloudflare). See the [DefaultExporter documentation](https://mastra.ai/docs/observability/tracing/exporters/default) for the full list of supported providers.
package/dist/index.cjs CHANGED
@@ -516,16 +516,11 @@ var ClickhouseDB = class extends base.MastraBase {
516
516
  }
517
517
  async clearTable({ tableName }) {
518
518
  try {
519
- await this.client.query({
520
- query: `TRUNCATE TABLE ${tableName}`,
521
- clickhouse_settings: {
522
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
523
- date_time_input_format: "best_effort",
524
- date_time_output_format: "iso",
525
- use_client_time_zone: 1,
526
- output_format_json_quote_64bit_integers: 0
527
- }
519
+ await this.client.command({ query: `SYSTEM STOP MERGES ${tableName}` });
520
+ await this.client.command({
521
+ query: `TRUNCATE TABLE ${tableName}`
528
522
  });
523
+ await this.client.command({ query: `SYSTEM START MERGES ${tableName}` });
529
524
  } catch (error$1) {
530
525
  throw new error.MastraError(
531
526
  {
@@ -787,7 +782,7 @@ var BackgroundTasksStorageClickhouse = class extends storage.BackgroundTasksStor
787
782
  const rows = await result.json();
788
783
  return rows.length > 0 ? rowToTask(rows[0]) : null;
789
784
  }
790
- async listTasks(filter) {
785
+ buildFilterClause(filter) {
791
786
  const conditions = [];
792
787
  const params = {};
793
788
  if (filter.status) {
@@ -821,6 +816,10 @@ var BackgroundTasksStorageClickhouse = class extends storage.BackgroundTasksStor
821
816
  params.var_to_date = filter.toDate.toISOString();
822
817
  }
823
818
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
819
+ return { where, params };
820
+ }
821
+ async listTasks(filter) {
822
+ const { where, params } = this.buildFilterClause(filter);
824
823
  const countResult = await this.client.query({
825
824
  query: `SELECT count() as count FROM ${storage.TABLE_BACKGROUND_TASKS} FINAL ${where}`,
826
825
  query_params: params,
@@ -850,19 +849,18 @@ var BackgroundTasksStorageClickhouse = class extends storage.BackgroundTasksStor
850
849
  return { tasks, total };
851
850
  }
852
851
  async deleteTask(taskId) {
853
- await this.client.query({
854
- query: `ALTER TABLE ${storage.TABLE_BACKGROUND_TASKS} DELETE WHERE id = {var_id:String}`,
852
+ await this.client.command({
853
+ query: `DELETE FROM ${storage.TABLE_BACKGROUND_TASKS} WHERE id = {var_id:String}`,
855
854
  query_params: { var_id: taskId }
856
855
  });
857
856
  }
858
857
  async deleteTasks(filter) {
859
- const { tasks } = await this.listTasks(filter);
860
- for (const task of tasks) {
861
- await this.client.query({
862
- query: `ALTER TABLE ${storage.TABLE_BACKGROUND_TASKS} DELETE WHERE id = {var_id:String}`,
863
- query_params: { var_id: task.id }
864
- });
865
- }
858
+ const { where, params } = this.buildFilterClause(filter);
859
+ if (!where) return;
860
+ await this.client.command({
861
+ query: `DELETE FROM ${storage.TABLE_BACKGROUND_TASKS} ${where}`,
862
+ query_params: params
863
+ });
866
864
  }
867
865
  async getRunningCount() {
868
866
  const result = await this.client.query({