
Loading...
Customize how DataChonk generates dbt code to match your team's standards and conventions.
DataChonk uses a template system for code generation. You can customize templates to match your team's coding standards, naming conventions, and patterns. Templates use Jinja-like syntax and can be stored locally or in a shared registry.
.datachonk/templates/~/.datachonk/templates/List all available templates with the CLI:
# List all templates
datachonk templates
# Filter by category
datachonk templates --category staging
# Output:
# Staging Templates:
# staging-model - Standard staging model that cleans and renames columns
# staging-snapshot - Staging model with SCD Type 2 tracking
#
# Mart Templates:
# dimension-model - Dimension table following Kimball methodology
# fact-model - Fact table with foreign key joins
# ...Templates are JSON files with metadata and Jinja-like content:
{
"name": "my-staging-template",
"version": "1.0.0",
"description": "Custom staging model with audit columns",
"category": "staging",
"author": "Data Team",
"tags": ["staging", "audit", "custom"],
"variables": [
{
"name": "source_name",
"type": "string",
"required": true,
"description": "Name of the source"
},
{
"name": "source_table",
"type": "string",
"required": true,
"description": "Source table name"
},
{
"name": "columns",
"type": "array",
"required": false,
"description": "Columns to select"
},
{
"name": "add_audit",
"type": "boolean",
"required": false,
"default": true,
"description": "Add audit columns"
}
],
"content": "... (template content)"
}Templates use Jinja-like syntax for variable substitution and control flow:
-- Basic variable
select * from {{ source('{{ source_name }}', '{{ source_table }}') }}
-- With default value
{{ model_name | default: 'stg_default' }}
-- From config
{{ dbt_utils.generate_surrogate_key(['{{ primary_key }}']) }}{%- if add_audit %}
-- Audit columns
, current_timestamp as _loaded_at
, '{{ source_name }}' as _source
, md5(row_to_json(source.*)::text) as _row_hash
{%- endif %}
{%- if scd_type == '2' %}
-- SCD Type 2 columns
, valid_from
, valid_to
, is_current
{%- endif %}select
{%- for col in columns %}
{{ col }}{% if not loop.last %},{% endif %}
{%- endfor %}
from source
-- With rename mapping
{%- for col in columns %}
{%- if rename_columns and col in rename_columns %}
{{ col }} as {{ rename_columns[col] }}
{%- else %}
{{ col }}
{%- endif %}
{%- endfor %}A complete custom staging template with audit columns and renaming:
{
"name": "staging-with-audit",
"version": "1.0.0",
"description": "Staging model with audit columns and column renaming",
"category": "staging",
"variables": [
{ "name": "source_name", "type": "string", "required": true },
{ "name": "source_table", "type": "string", "required": true },
{ "name": "columns", "type": "array", "required": false },
{ "name": "rename_map", "type": "object", "required": false },
{ "name": "add_audit", "type": "boolean", "default": true }
],
"content": "{{/*\n Staging: stg_{{ source_name }}__{{ source_table }}\n Generated by DataChonk\n*/}}\n\nwith source as (\n select * from {{ source('{{ source_name }}', '{{ source_table }}') }}\n),\n\nrenamed as (\n select\n {%- if columns %}\n {%- for col in columns %}\n {%- if rename_map and col in rename_map %}\n {{ col }} as {{ rename_map[col] }}{% if not loop.last %},{% endif %}\n {%- else %}\n {{ col }}{% if not loop.last %},{% endif %}\n {%- endif %}\n {%- endfor %}\n {%- else %}\n *\n {%- endif %}\n \n {%- if add_audit %}\n -- Audit columns\n , current_timestamp as _dbt_loaded_at\n , '{{ source_name }}.{{ source_table }}' as _dbt_source\n {%- endif %}\n from source\n)\n\nselect * from renamed"
}Generate code with your custom template:
# Use a custom template by name
datachonk generate staging \
--template staging-with-audit \
--source raw.stripe.payments \
--name payments
# Or in the web app, select template from dropdown
# when creating a new ChonkStore templates in your dbt project repo. Changes are tracked and reviewed like code.
Add clear descriptions to all variables. Your team will thank you later.
Customize the built-in templates rather than starting from scratch. They encode dbt best practices.