Clickhouse
This guide walks you through setting up a Clickhouse database using Alchemy.
-
Install and setup Alchemy
Terminal window bun add alchemy @clickhouse/client-webTerminal window npm install alchemy @clickhouse/client-webTerminal window pnpm add alchemy @clickhouse/client-webTerminal window yarn add alchemy @clickhouse/client-web -
Configure environment variables
Add the required environment variables to your
.env
file:Terminal window CLICKHOUSE_KEY_ID=your_clickhouse_key_idCLICKHOUSE_KEY_SECRET=your_clickhouse_key_secretCLICKHOUSE_ORG=your_clickhouse_organization -
Create your infrastructure
Create
alchemy.run.ts
with your Clickhouse Service and bind it to your Worker:alchemy.run.ts import alchemy from "alchemy";import { Service } from "alchemy/clickhouse";import { Worker } from "alchemy/cloudflare";const app = await alchemy("my-clickhouse-app");const service = await Service("clickhouse", {organization: process.env.CLICKHOUSE_ORG,provider: "aws",region: "us-east-1",minReplicaMemoryGb: 8,maxReplicaMemoryGb: 356,numReplicas: 3,});export const worker = await Worker("worker", {entrypoint: "./src/worker.ts",bindings: {CLICKHOUSE_URL: `https://${service.mysqlEndpoint.host}:${service.mysqlEndpoint.port}`,CLICKHOUSE_PASSWORD: service.password,},});await app.finalize(); -
Implement your worker
Create
src/worker.ts
to interact with Clickhouse:src/worker.ts import type { worker } from "../alchemy.run.ts";import { createClient } from "@clickhouse/client-web";import workers from "cloudflare:workers";// initialize clickhouse clientconst env = workers.env as typeof worker.Env;const clickhouseClient = createClient({url: env.CLICKHOUSE_URL,password: env.CLICKHOUSE_PASSWORD,});export default {async fetch(req: Request, env: typeof worker.Env): Promise<Response> {await clickhouseClient.insert({table: "worker_log",values: [{ id: crypto.randomUUID(), time: new Date().toISOString() }],format: "JSONEachRow",});return Response.json({ success: true });},}; -
Deploy your service
Use the Alchemy CLI to deploy your Clickhouse service:
Terminal window bun alchemy deployTerminal window npx alchemy deployTerminal window pnpm alchemy deployTerminal window yarn alchemy deployThis will deploy your Clickhouse service and worker. Visit the worker URL to insert data into Clickhouse.
-
(Optional) Tear down
Use the Alchemy CLI to delete all resources:
Terminal window bun alchemy destroyTerminal window npx alchemy destroyTerminal window pnpm alchemy destroyTerminal window yarn alchemy destroy