Python parser and builder for DBML
Project description
DBML parser for Python
Compliant with DBML v3.6.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 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]
}
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydbml-1.1.1.tar.gz
.
File metadata
- Download URL: pydbml-1.1.1.tar.gz
- Upload date:
- Size: 35.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9833f93d20a5b48c032bdc8d4f151ca3bd6d8a97ce83301d211810d0b2a16cc |
|
MD5 | 4387518a4adfe8583cc4fa7adf1c9da0 |
|
BLAKE2b-256 | cb8682014f65c6081b7f3b158a094e02fdf2efb57f903dd70ffa5b2c9b12e656 |
File details
Details for the file pydbml-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: pydbml-1.1.1-py3-none-any.whl
- Upload date:
- Size: 45.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbc0832fc9d24527ed004f4c8d8306cf7a06db4928ec7d5b73d864ab609225f5 |
|
MD5 | 8dbb317cfb47314b0bdd63f3343e8960 |
|
BLAKE2b-256 | 137c895bb9c2332ed244da7bd1ee9195a5da1d692e8047b0d5818f4494d958d6 |