Anatomy of a dbt Project
When you initialize a new dbt project, you’ll see a directory structure like this:Core Components
Models Directory
Themodels/ directory contains SQL files that define your transformations. Each SQL file typically becomes a table or view in your data warehouse.
Configuration Files
Configuration files define project-wide settings and metadata: dbt_project.yml This is the central configuration file for your dbt project. It defines:- Project name and version
- Profile to use for database connections
- Model materialization settings
- Directory configurations
Source Definitions
Sources represent the raw data tables in your warehouse. They’re defined in YAML files (typically namedsources.yml) within the models directory:
Model Organization Patterns
There’s no single “right way” to organize your dbt project, but here are common patterns that work well:Layered Approach
This approach organizes models by their purpose in the transformation pipeline:Domain-Based Organization
For larger projects, you might organize by business domain first, then by layer:Working with Tests
dbt supports two types of tests:Schema Tests
These are defined in YAML files alongside your models:Singular Tests
These are custom SQL queries in thetests/ directory that should return zero rows when the test passes:
Real-World Project Organization Example
Here’s how a complete e-commerce dbt project might be organized:Best Practices
- Be consistent with naming conventions: Use prefixes like
stg_,int_,dim_andfct_to indicate model purpose - Document as you go: Add descriptions in your YAML files for models and columns
- Start simple: Begin with a staging/marts approach and add complexity as needed
- Group related models: Keep related transformations close together
- Limit cross-schema references: Staging should only reference sources, intermediate should only reference staging, etc.
- Use packages: Don’t reinvent common patterns when packages can help