DBML syntax parser for Python
Project description
DBML parser for Python
The parser is currently on alpha stage. It doesn't have any tests or docstrings yet.
PyDBML is a Python parser for DBML syntax.
Installation
You can install PyDBML using pip:
pip install pydbml
Quick start
Import the PyDBML
class and initialize it with path to DBML-file:
>>> from pydbml import PyDBML
>>> from pathlib import Path
>>> p = PyDBML(Path('schema.dbml'))
or with file stream:
>>> with open('schema.dbml') as f:
... p = PyDBML(f)
or with entire source string:
>>> with open('schema.dbml') as f:
... source = f.read()
>>> p = PyDBML(source)
You can access tables inside the tables
attribute:
>>> for table in p.tables:
... print(table.name)
...
orders
order_items
products
users
merchants
countries
Or just by getting items directly:
>>> p['countries']
Table('countries', [Column('code', 'int', pk=True), Column('name', 'varchar'), Column('continent_name', 'varchar')])
>>> p[1]
Table('order_items', [Column('order_id', 'int'), Column('product_id', 'int'), Column('quantity', 'int', default=1)])
Other meaningful 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.
Finally, you can get the SQL for your DBML schema by accessing sql
property:
>>> print(p.sql) # doctest:+ELLIPSIS
CREATE TYPE "orders_status" AS ENUM (
'created',
'running',
'done',
'failure',
);
CREATE TYPE "product status" AS ENUM (
'Out of Stock',
'In Stock',
);
CREATE TABLE "orders" (
"id" int PRIMARY KEY AUTOINCREMENT,
"user_id" int UNIQUE NOT NULL,
"status" orders_status,
"created_at" varchar
);
...
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
pydbml-0.1.1.tar.gz
(10.3 kB
view hashes)
Built Distribution
pydbml-0.1.1-py3-none-any.whl
(14.2 kB
view hashes)