MCPcopy Index your code
hub / github.com/Vanderhoof/PyDBML

github.com/Vanderhoof/PyDBML @1.2.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.2.1 ↗ · + Follow
861 symbols 3,439 edges 130 files 75 documented · 9%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

DBML parser for Python

Compliant with DBML v3.9.5 syntax

PyDBML is a Python parser and builder for DBML syntax.

The project was rewritten in May 2022, the new version 1.0.0 is not compatible with versions 0.x.x. See details in Upgrading to PyDBML 1.0.0.

Docs:

PyDBML requires Python v3.8 or higher

Installation

You can install PyDBML using pip:

pip3 install pydbml

Quick start

To parse a DBML file, import the PyDBML class and initialize it with Path object

>>> from pydbml import PyDBML
>>> from pathlib import Path
>>> parsed = PyDBML(Path('test_schema.dbml'))

or with file stream

>>> with open('test_schema.dbml') as f:
...     parsed = PyDBML(f)

or with entire source string

>>> with open('test_schema.dbml') as f:
...     source = f.read()
>>> parsed = PyDBML(source)
>>> parsed
<Database>

The parser returns a Database object that is a container for the parsed DBML entities.

You can access tables inside the tables attribute:

>>> for table in parsed.tables:
...     print(table.name)
...
orders
order_items
products
users
merchants
countries

Or just by getting items by index or full table name:

>>> parsed[1]
<Table 'public' 'order_items'>
>>> parsed['public.countries']
<Table 'public' 'countries'>

Other attributes are:

  • refs — list of all references,
  • enums — list of all enums,
  • table_groups — list of all table groups,
  • project — the Project object, if was defined.

Generate SQL for your DBML Database by accessing the sql property:

>>> print(parsed.sql)  # doctest:+ELLIPSIS
CREATE TYPE "orders_status" AS ENUM (
  'created',
  'running',
  'done',
  'failure'
);
<BLANKLINE>
CREATE TYPE "product status" AS ENUM (
  'Out of Stock',
  'In Stock'
);
<BLANKLINE>
CREATE TABLE "orders" (
  "id" int PRIMARY KEY AUTOINCREMENT,
  "user_id" int UNIQUE NOT NULL,
  "status" "orders_status",
  "created_at" varchar
);
...

Generate DBML for your Database by accessing the dbml property:

>>> parsed.project.items['author'] = 'John Doe'
>>> print(parsed.dbml)  # doctest:+ELLIPSIS
Project "test_schema" {
    author: 'John Doe'
    Note {
        'This schema is used for PyDBML doctest'
    }
}
<BLANKLINE>
Enum "orders_status" {
    "created"
    "running"
    "done"
    "failure"
}
<BLANKLINE>
Enum "product status" {
    "Out of Stock"
    "In Stock"
}
<BLANKLINE>
Table "orders" [headercolor: #fff] {
    "id" int [pk, increment]
    "user_id" int [unique, not null]
    "status" "orders_status"
    "created_at" varchar
}
<BLANKLINE>
Table "order_items" {
    "order_id" int
    "product_id" int
    "quantity" int [default: 1]
}
...

Core symbols most depended-on inside this repo

add_column
called by 78
pydbml/_classes/table.py
add
called by 53
pydbml/database.py
parse_file
called by 26
pydbml/parser/parser.py
add_table
called by 23
pydbml/database.py
indent
called by 18
pydbml/tools.py
build
called by 18
pydbml/parser/blueprints.py
get
called by 18
pydbml/_classes/table.py
render
called by 14
pydbml/renderer/sql/default/renderer.py

Shape

Method 553
Class 163
Function 144
Route 1

Languages

Python100%

Modules by API surface

test/test_definitions/test_index.py38 symbols
test/test_definitions/test_column.py37 symbols
test/test_definitions/test_reference.py31 symbols
test/test_database.py31 symbols
pydbml/parser/blueprints.py26 symbols
test/test_definitions/test_table.py25 symbols
test/test_tools.py24 symbols
test/test_renderer/test_sql/test_default/test_table.py24 symbols
pydbml/database.py22 symbols
test/test_renderer/test_dbml/test_reference.py21 symbols
test/test_renderer/test_dbml/test_column.py20 symbols
test/conftest.py20 symbols

For agents

$ claude mcp add PyDBML \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact