# Model your data (/docs/quick-start)

<LanguageFromHash />

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](/docs/quick-start/api-and-dashboard) | An HTTP endpoint with OpenAPI, and a React chart over it |
| [Connect via MCP](/docs/mcp/quickstart) | MCP tools an agent queries instead of writing SQL |
| [Add AI chat to your app](/docs/agents/quickstart) | 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.

<Component type="info" title="No ClickHouse to hand?">
TypeScript and Python both run on [chDB](/docs/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](/docs/python/cli#hypequery-demo).
</Component>

## Define and query a dataset [#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.

<Component>
<Component>
### Install [#install]

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```bash
npm install @hypequery/datasets @hypequery/clickhouse
npm install -D @hypequery/cli
```
</CodeBlock>

With pnpm, yarn or bun, swap `npm install` for `pnpm add`, `yarn add` or `bun add`.
</Tab>

<Tab value="Python">
<CodeBlock>
```bash
pip install "hypequery[clickhouse]==0.1.0b4"
```
</CodeBlock>

With uv: `uv add "hypequery[clickhouse]==0.1.0b4"`. The Python SDK is in beta, so pin the version you deploy.
</Tab>
</Tabs>
</Component>

<Component>
### Connect to ClickHouse [#connect-to-clickhouse]

Create `.env` in your project folder. The `hypequery` CLI reads it from the folder it runs in.

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```bash
CLICKHOUSE_URL=https://your-host.clickhouse.cloud:8443
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=your-password
```
</CodeBlock>
</Tab>

<Tab value="Python">
<CodeBlock>
```bash
CLICKHOUSE_HOST=your-host.clickhouse.cloud
CLICKHOUSE_PORT=8443
CLICKHOUSE_SECURE=true
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=your-password
```
</CodeBlock>

`CLICKHOUSE_URL` (`https://user:password@host:8443/database`) can replace all six, but not be combined with them.
</Tab>
</Tabs>
</Component>

<Component>
### Generate datasets from your schema [#generate-datasets-from-your-schema]

The CLI reads your tables and writes a starting dataset for each one.

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```bash
npx hypequery generate:datasets
```
</CodeBlock>

This writes `analytics/datasets.ts`, with exports named like `OrdersDataset`.
</Tab>

<Tab value="Python">
<CodeBlock>
```bash
hypequery generate datasets
```
</CodeBlock>

This writes `datasets.py`, with one dataset per table in a `datasets` mapping. `--diff` shows schema drift
before you re-run it.
</Tab>
</Tabs>

Generated datasets are a starting point. The next step curates one so its names match your product's language.
</Component>

<Component>
### Curate the dataset [#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:

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```typescript
// 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'),
  },
});
```
</CodeBlock>
</Tab>

<Tab value="Python">
<CodeBlock>
```python
# datasets.py
from hypequery.datasets import dataset, dimension, measure

orders = dataset(
    "orders",
    source="orders",
    tenant_key="tenant_id",
    time_key="created_at",
    dimensions={
        "id": dimension.number(),
        "status": dimension.string(),
        "country": dimension.string(),
        "createdAt": dimension.timestamp(column="created_at"),
    },
    measures={
        "revenue": measure.sum("amount"),
        "orderCount": measure.count("id"),
    },
)

datasets = {"orders": orders}
```
</CodeBlock>
</Tab>
</Tabs>

`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.
</Component>

<Component>
### Create a client and query it [#create-a-client-and-query-it]

The client validates a query against the dataset, generates the SQL and runs it on ClickHouse.

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```typescript
// 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 });
```
</CodeBlock>

<CodeBlock>
```typescript
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);
```
</CodeBlock>
</Tab>

<Tab value="Python">
<CodeBlock>
```python
# client.py
from pathlib import Path

from hypequery.cli import load_env_files
from hypequery.datasets import create_dataset_client
from hypequery.execution import ClickHouseConnection, create_clickhouse_executor

# Fill unset variables from .env.local and .env, as the hypequery CLI does.
load_env_files(Path.cwd())

executor = create_clickhouse_executor(ClickHouseConnection.from_env())
analytics = create_dataset_client(executor=executor)
```
</CodeBlock>

<CodeBlock>
```python
from hypequery.datasets import DatasetQuery, ExecutionContext, filter, order, tenant

from client import analytics
from datasets import orders

by_country = analytics.execute(
    orders,
    DatasetQuery(
        dimensions=("country",),
        measures=("revenue", "orderCount"),
        filters=(filter.eq("status", "completed"),),
        order_by=(order.desc("revenue"),),
        limit=10,
    ),
    context=ExecutionContext(tenant=tenant("org_123")),
)

print(by_country.data)
```
</CodeBlock>

In production, set the variables in the environment instead of calling `load_env_files`.
</Tab>
</Tabs>

The tenant comes from your code (in a real app, from the signed-in user's session), never from the query itself.
</Component>

<Component>
### Add a calculated measure [#add-a-calculated-measure]

When a number is calculated from other measures, declare it once as a derived measure and select it by name.

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```typescript
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)),
}),
```
</CodeBlock>
</Tab>

<Tab value="Python">
<CodeBlock>
```python
from hypequery.datasets import formula, measure

# in orders' measures:
"averageOrderValue": measure.derived(
    formula.divide("revenue", formula.null_if_zero("orderCount")),
),
```
</CodeBlock>
</Tab>
</Tabs>

Query it like any other measure: `measures: ['averageOrderValue']`. Every API, dashboard and agent that uses the
dataset now gets the same definition.
</Component>

<Component>
### Describe your business (optional, for agents) [#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.

<Tabs groupId="language" items={["TypeScript","Python"]} persist>
<Tab value="TypeScript">
<CodeBlock>
```typescript
// 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.'],
});
```
</CodeBlock>

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.
</Tab>

<Tab value="Python">
<CodeBlock>
```python
# business_context.py
from hypequery.datasets import define_business_context

business_context = define_business_context(
    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."],
)
```
</CodeBlock>

Every argument is optional. `define_business_context` raises `ProtocolBusinessContextError`, naming the field,
on a glossary term or KPI defined twice, or a value over the limits.
</Tab>
</Tabs>

Pass it to the executor in [Add AI chat to your app](/docs/agents/quickstart), 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](/docs/agents/business-context) for every field.
</Component>
</Component>

## Next [#next]

<Component>
<Component title="Serve an API and a dashboard" href="/docs/quick-start/api-and-dashboard">
Turn the dataset into an authenticated HTTP endpoint with OpenAPI, then chart it in React.
</Component>

<Component title="Connect via MCP" href="/docs/mcp/quickstart">
Give an MCP client tools over the dataset instead of a SQL console.
</Component>

<Component title="Add AI chat to your app" href="/docs/agents/quickstart">
AI SDK tools for a chat in your product, scoped to the signed-in customer.
</Component>

<Component title="Only need typed SQL?" href="/docs/query-building/setup">
Skip datasets and write type-safe ClickHouse queries with the TypeScript query builder.
</Component>
</Component>

To go deeper on modelling, read [dimensions](/docs/datasets/dimensions), [measures](/docs/datasets/measures) and
[relationships](/docs/datasets/relationships) ([Python](/docs/python/datasets/overview)).

ClickHouse lists hypequery as a community integration in its docs:
[hypequery and ClickHouse](https://clickhouse.com/docs/integrations/connectors/data-visualization/community-integrations/hypequery-and-clickhouse).