Model your data
Install hypequery, connect ClickHouse, and define your first dataset of dimensions and measures in TypeScript or Python. The API, dashboard, MCP and AI SDK quick starts all build on it.
Every hypequery feature starts from a dataset: one ClickHouse table described as the dimensions people group and filter by and the measures they ask for. You define it once, in code, and the same definition serves your API, your React dashboards, MCP clients like Claude, and the agent in your app.
By the end of this page you'll have an orders dataset you can query by name. The other quick starts start from it:
| Quick start | What you build |
|---|---|
| Model your data (this page) | A dataset you query from your own code |
| Serve an API and a dashboard | An HTTP endpoint with OpenAPI, and a React chart over it |
| Connect via MCP | MCP tools an agent queries instead of writing SQL |
| Add AI chat to your app | AI SDK tools for a chat in your product, scoped to each customer |
Pick your language once: every code block on these pages follows it.
No ClickHouse to hand?
TypeScript and Python both run on chDB, ClickHouse embedded in your process, with no server or
credentials. In Python, hypequery demo also serves a sample dataset to an AI agent in one command: see the
Python CLI.
Define and query a dataset
You need a ClickHouse server you can query and Node.js 20+ or Python 3.11+. The examples use an orders table with
id, status, country, amount, tenant_id and created_at columns; swap in your own table.
Install
npm install @hypequery/datasets @hypequery/clickhouse
npm install -D @hypequery/cliWith pnpm, yarn or bun, swap npm install for pnpm add, yarn add or bun add.
Connect to ClickHouse
Create .env in your project folder. The hypequery CLI reads it from the folder it runs in.
CLICKHOUSE_URL=https://your-host.clickhouse.cloud:8443
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=your-passwordGenerate datasets from your schema
The CLI reads your tables and writes a starting dataset for each one.
npx hypequery generate:datasetsThis writes analytics/datasets.ts, with exports named like OrdersDataset.
Generated datasets are a starting point. The next step curates one so its names match your product's language.
Curate the dataset
A dataset maps a table to typed dimensions and measures, with optional tenant and time keys. This is the orders
dataset the other quick starts use:
// analytics/datasets.ts
import { dataset, dimension, measure } from '@hypequery/datasets';
export const Orders = dataset('orders', {
source: 'orders',
description: 'One row per order.',
tenantKey: 'tenant_id',
timeKey: 'created_at',
dimensions: {
id: dimension.number(),
status: dimension.string({ description: 'completed, refunded or pending' }),
country: dimension.string(),
createdAt: dimension.timestamp({ column: 'created_at' }),
},
measures: {
revenue: measure.sum('amount', { description: 'Total order amount' }),
orderCount: measure.count('id'),
},
});tenantKey makes the dataset fail closed: it won't run without a tenant, and every query is filtered to it. Leave
it out if your table isn't multi-tenant.
Create a client and query it
The client validates a query against the dataset, generates the SQL and runs it on ClickHouse.
// analytics/client.ts
import { createQueryBuilder } from '@hypequery/clickhouse';
import { createDatasetClient } from '@hypequery/datasets';
export const db = createQueryBuilder({
url: process.env.CLICKHOUSE_URL!,
username: process.env.CLICKHOUSE_USERNAME!,
password: process.env.CLICKHOUSE_PASSWORD!,
database: process.env.CLICKHOUSE_DATABASE!,
});
export const analytics = createDatasetClient({ queryBuilder: db });import { eq } from '@hypequery/datasets';
import { analytics } from './analytics/client.js';
import { Orders } from './analytics/datasets.js';
const byCountry = await analytics.execute(
Orders,
{
dimensions: ['country'],
measures: ['revenue', 'orderCount'],
filters: [eq('status', 'completed')],
orderBy: [{ field: 'revenue', direction: 'desc' }],
limit: 10,
},
{ runtime: { tenant: 'org_123' } },
);
console.log(byCountry.data);The tenant comes from your code (in a real app, from the signed-in user's session), never from the query itself.
Add a calculated measure
When a number is calculated from other measures, declare it once as a derived measure and select it by name.
import { dataset, dimension, divide, measure, nullIfZero } from '@hypequery/datasets';
// in Orders' measures:
averageOrderValue: measure.derived({
uses: { revenue: 'revenue', orders: 'orderCount' },
formula: ({ revenue, orders }) => divide(revenue, nullIfZero(orders)),
}),Query it like any other measure: measures: ['averageOrderValue']. Every API, dashboard and agent that uses the
dataset now gets the same definition.
Describe your business (optional, for agents)
Datasets say what can be queried. Business context says what the numbers mean here: KPI definitions, a glossary of your terms, reporting conventions and known data caveats. AI agents read it before they choose measures; API and dashboard callers never see it, and it never changes a query result.
// analytics/business-context.ts
import { defineBusinessContext } from '@hypequery/datasets';
export const businessContext = defineBusinessContext({
summary: 'An online store selling to businesses in the UK, US and Germany.',
kpis: [
{ name: 'Revenue', definition: 'The revenue measure sums every order amount, in USD. Filter status to completed to leave out refunded and pending orders.' },
{ name: 'Average order value', definition: 'Revenue divided by the order count, over the same orders. Filter status to completed for completed orders only.' },
],
glossary: [
{ term: 'Order', meaning: 'One checkout. An order with several items is still one order.' },
{ term: 'Customer', meaning: 'The business that placed the order (the tenant), not the person.' },
],
conventions: 'Amounts in USD. Times in UTC. Weeks start on Monday.',
caveats: ['Orders before March 2024 have no country.'],
});Every field is optional. defineBusinessContext throws, naming the field, on an unknown field, a glossary term
or KPI defined twice, or a value over the limits.
Pass it to the executor in Add AI chat to your app, or to an MCP server you build
(createMCPServer in TypeScript, create_mcp_server or create_mcp_asgi_app in Python), and agents read it from
list_datasets and get_business_context. The hypequery mcp command doesn't read it yet. You'll also be able to edit it, glossary included, in hypequery Cloud (coming soon). See
Business context for every field.
Next
Serve an API and a dashboard
Turn the dataset into an authenticated HTTP endpoint with OpenAPI, then chart it in React.
Connect via MCP
Give an MCP client tools over the dataset instead of a SQL console.
Add AI chat to your app
AI SDK tools for a chat in your product, scoped to the signed-in customer.
Only need typed SQL?
Skip datasets and write type-safe ClickHouse queries with the TypeScript query builder.
To go deeper on modelling, read dimensions, measures and relationships (Python).