Skip to main content

Tool to create text files from DBML using Jinja2 templates.

Project description

Dinja

Dinja is a small tool for creating any text file format from a DBML database definition using Jinja2 templates. It can be used to auto-generate e.g. DDL, database access code or schema documentation from a single DBML file, maintaining a single source of truth for your database schema definition.

Its name is made from "DBML" and "Jinja2", but it's also name of Cucumis melo (a melon) 🍈 🍉 in Croatian.

Installation

Dinja is available on PyPI, so you can install it with pip:

pip install dinja-tool

Usage

1. Create your Jinja2 template(s)

The template files are of the destination file format but contain some special markers that Dinja replaces with the real database schema data. Their file names must end with either .jinja or .in. See the Jinja2 template documentation for how to write such a template.

The Python data structures provided to the templates are documented in the dinja.api module.

Dinja can render several template files at once - simply put them all into a single directory.

2. Run Dinja on your DBML file

The following command converts the dbschema.dbml file by rendering all template files inside the jinja_templates directory, storing the resulting files into the generated directory (creating one rendered file for each template).

$ dinja dbschema.dbml jinja_templates generated

The result files have the same names as their corresponding template files, but without the .jinja or .in suffix. Existing files are overwritten without a warning.

dinja -h or dinja --help prints a summary of all possible options and arguments.

Examples

Let's create and render a template for the example tables from the DBML introduction:

Table users {
  id integer
  username varchar
  role varchar
  created_at timestamp
}

Table posts {
  id integer [primary key]
  title varchar
  body text [note: 'Content of the post']
  user_id integer
  status integer
  created_at timestamp
}

Store this into the local file example.dbml.

For example, the following Jinja2 template generates SQLite2 DDL:

{% for table in tables %}
CREATE TABLE {{table.name}} (
    {% for column in table.columns %}
    "{{column.name}}" {{column.type.upper()}}
        {{-" PRIMARY KEY" if column.primary_key}}
        {{-" UNIQUE" if column.unique}}
        {{-" NOT NULL" if not column.nullable}}
        {{-" AUTOINCREMENT" if column.autoincrement}}
        {{-"," if not loop.last or table.primary_key or table.unique_constraints or table.references}}
    {% endfor %}
    {% if table.primary_key %}
    PRIMARY KEY({{table.primary_key|join(", ")}})
    {{-"," if table.unique_constraints or table.references}}
    {%endif %}
    {% for columns in table.unique_constraints %}
    UNIQUE({{columns|join(", ")}})
        {{- "," if not loop.last or table.references}}
    {% endfor %}
    {% for ref in table.references %}
    FOREIGN KEY("{{ref.local_columns|join(", ")}}") REFERENCES "{{ref.table_name}}" ("{{ref.remote_columns|join(", ")}}")
        {{-" ON DELETE {}".format(ref.delete_action) if ref.delete_action}}
        {{-"," if not loop.last}}
    {% endfor %}
);

{% endfor %}

Store it into a local file named schema_creation.sql.in within the templates directory, and run

$ dinja example.dbml templates .

This will create a new file schema_creation.sql with the following SQL content:

CREATE TABLE users (
    "id" INTEGER,
    "username" VARCHAR,
    "role" VARCHAR,
    "created_at" TIMESTAMP
);

CREATE TABLE posts (
    "id" INTEGER PRIMARY KEY,
    "title" VARCHAR,
    "body" TEXT,
    "user_id" INTEGER,
    "status" INTEGER,
    "created_at" TIMESTAMP
);

By creating Jinja2 templates according to your needs, you can generate literally any text-based file format from your DBML definition.

Copyright & License

Copyright 2025 by Thomas Wesenigk.

Licensed under the EUPL 1.2. Visit https://interoperable-europe.ec.europa.eu/collection/eupl/eupl-text-eupl-12 to read it in your favourite language.

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

dinja_tool-0.2.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

dinja_tool-0.2.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file dinja_tool-0.2.0.tar.gz.

File metadata

  • Download URL: dinja_tool-0.2.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.7 Linux/6.8.0-85-generic

File hashes

Hashes for dinja_tool-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dfc62d0abbb89218c20a6192a7ff5c71d28e865d750562fea75f8ae849128412
MD5 64fe66935d74c93d061954fc59d752c2
BLAKE2b-256 20f94f89cbab94e1e39651f0541aa641e2bfb62668afd324d9608842651b6aa1

See more details on using hashes here.

File details

Details for the file dinja_tool-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dinja_tool-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.7 Linux/6.8.0-85-generic

File hashes

Hashes for dinja_tool-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1698d4ae91c989ba07e79bd167b968c6a488930323722da0ec2c25bcedabad99
MD5 16585c34906f3a23532f9d7da1653659
BLAKE2b-256 d776a68dacca6644683ed40885f5e4b67726b915cf580373f0d7a0e1d92292c5

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