← Glossary
Glossary

dbt model

A dbt model is a SQL select statement saved as a file in a dbt project. dbt runs it in the warehouse and saves the result as a view or a table named after the file. A model can read other models with ref(), so dbt builds them in order and can test each one.

In BlockJuice

BlockJuice’s dbt project lives in transformation/. Every join, sign rule, unit and price lives in its models, not in the sources, and the models run in three layers.

Staging models, named stg_<name>__<table>, are views that cast fields out of one raw table. They join nothing and apply no rules. Intermediate models, in models/intermediate/<pipeline>/, hold the joins and rules, one model per chain or protocol version. Marts, in models/marts/<pipeline>/, build the tables questions read, in the analytics schema.

Dagster loads every dbt model as an asset, next to the TypeScript sources that feed it, so one job runs a pipeline from the chain to the mart. On a pull request, CI plants bugs in each model the branch adds or changes, and fails when no unit test catches them.

Example

An excerpt of int_ethereum_uniswap_v3_trades. It reads the staging model with ref() and applies the v3 sign rule. A pool’s amount0 and amount1 are its own balance changes, so the swapper’s change is their negative. analytics.dex_trades then stacks it with the v2 and v4 models.

select
    swaps.id,
    swaps.block_timestamp,
    -swaps.amount0 as change0,
    -swaps.amount1 as change1
from {{ ref('stg_ethereum__uniswap_v3') }} as swaps
inner join pools on pools.pool = swaps.contract
where swaps.event = 'Swap'