Skip to main content

Python parser and builder for DBML

Project description

DBML parser for Python

Compliant with DBML v2.4.1 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 the previous ones. See details in Upgrading to PyDBML 1.0.0.

Docs:

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" {
    "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]
}
...

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

pydbml-1.0.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.

pydbml-1.0.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file pydbml-1.0.0.tar.gz.

File metadata

  • Download URL: pydbml-1.0.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for pydbml-1.0.0.tar.gz
Algorithm Hash digest
SHA256 964811e53a023b0f6570c108f741534b89cbd1a75a0290c4d23dd5923c006aab
MD5 f86c9edcd905907686e011507a2a71bb
BLAKE2b-256 0820454a420cf0220ad0d0e37e18efc4755aaaab854577c13e3f9a21c4839516

See more details on using hashes here.

File details

Details for the file pydbml-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pydbml-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for pydbml-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f98af507f23c95124dcb6516be77fbefdf6f854b37018abebe1fa112c8f8766
MD5 722de7462af299339ff10c3e4a5bcf2d
BLAKE2b-256 4ca2241e5bca9b024c0ac317d0bc6f502c9bdcc40be94e33a312a848ad3e17e1

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