Skip to main content

Define your dbt models in yaml

Project description

dbt-metamodels

Define your dbt models in YAML using metamodels - a powerful way to generate SQL models from macro calls defined in your schema files.

Overview

dbt-metamodels extends dbt to allow you to define models directly in your schema.yml files using a metamodel field. Instead of writing separate SQL files, you can reference macros that generate your SQL code, making your dbt project more maintainable and DRY.

Installation

pip install dbt-metamodels

Or using uv:

uv add dbt-metamodels

Usage

1. Define a Macro

First, create a macro that generates your SQL. For example, in macros/demo_model.sql:

{% macro demo_model(v) %}
select {{ v }} as id
{% endmacro %}

2. Define Models in schema.yml

Instead of creating separate .sql files, define your models in schema.yml using the metamodel field:

version: 2

models:
  - name: my_first_dbt_model
    description: "A starter dbt model"
    metamodel: demo_model(1)
    columns:
      - name: id
        description: "The primary key for this table"

  - name: my_second_dbt_model
    description: "A starter dbt model"
    metamodel: demo_model(2)
    columns:
      - name: id
        description: "The primary key for this table"

3. Use Metamodels Alongside Regular Models

You can mix metamodels with regular SQL models. For example, my_final_dbt_model.sql can reference metamodel-generated models:

{{ config(materialized='table') }}

with source_data as (

    select * from {{ ref('my_first_dbt_model') }}
    union all
    select * from {{ ref('my_second_dbt_model') }}

)

select * from source_data

How It Works

When dbt reads your project files:

  1. The plugin intercepts the file reading process
  2. It scans schema.yml files for models with a metamodel field
  3. For each metamodel definition, it automatically generates a corresponding .sql file
  4. The generated SQL wraps your macro call in {{ }} syntax
  5. dbt then processes these generated files as if they were regular SQL models

Example Project Structure

metamodels_demo/
├── dbt_project.yml
├── macros/
│   └── demo_model.sql          # Macro definition
├── models/
│   └── example/
│       ├── schema.yml          # Model definitions with metamodels
│       └── my_final_dbt_model.sql  # Regular SQL model
└── profiles.yml

Benefits

  • DRY Principle: Reuse macro logic across multiple models
  • YAML-First: Define models alongside their documentation
  • Flexibility: Mix metamodels with regular SQL models
  • Maintainability: Update model logic in one place (the macro)

Requirements

  • Python >= 3.8
  • dbt-core >= 1.5.0

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_metamodels-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_metamodels-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file dbt_metamodels-0.1.0.tar.gz.

File metadata

  • Download URL: dbt_metamodels-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dbt_metamodels-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d7de85bc57261870b6c866e6b48146eab3c557753c9e22e14bc44a6924fb1574
MD5 793391bb01c04301d5701506e331cc3e
BLAKE2b-256 ae4f818b5f68fdeadb65d272acb34017e4b0e2ecf7f5972e92fdeb33abb68038

See more details on using hashes here.

File details

Details for the file dbt_metamodels-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dbt_metamodels-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dbt_metamodels-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ede8dfff8503e74c23e5c9fc1a83f7c6b24ca7f2d1c0c31e80d24e7346485f5a
MD5 8509aa9f80c819008772166d92601e4f
BLAKE2b-256 217a7142a5a7c331655d48af05ad1a1d608416b1ed290f60c642be6cc4665df6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page