pydbml


Namepydbml JSON
Version 1.1.1 PyPI version JSON
download
home_pagehttps://github.com/Vanderhoof/PyDBML
SummaryPython parser and builder for DBML
upload_time2024-08-11 07:21:59
maintainerNone
docs_urlNone
authorDaniil Minukhin
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![](https://img.shields.io/pypi/v/pydbml.svg)](https://pypi.org/project/pydbml/) [![](https://img.shields.io/pypi/dm/pydbml.svg)](https://pypi.org/project/pydbml/)  [![](https://img.shields.io/github/v/tag/Vanderhoof/PyDBML.svg?label=GitHub)](https://github.com/Vanderhoof/PyDBML) ![](coverage.svg)

# DBML parser for Python

*Compliant with DBML **v3.6.1** syntax*

PyDBML is a Python parser and builder for [DBML](https://www.dbml.org) 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/upgrading.md).

**Docs:**

* [Class Reference](docs/classes.md)
* [Creating DBML schema](docs/creating_schema.md)
* [Upgrading to PyDBML 1.0.0](docs/upgrading.md)
* [Arbitrary Properties](docs/properties.md)

> PyDBML requires Python v3.8 or higher

## Installation

You can install PyDBML using pip:

```bash
pip3 install pydbml
```

## Quick start

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

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

```

or with file stream

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

```

or with entire source string

```python
>>> 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:

```python
>>> 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:

```python
>>> 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:

```python
>>> 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:

```python
>>> 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]
}
...

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Vanderhoof/PyDBML",
    "name": "pydbml",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Daniil Minukhin",
    "author_email": "ddddsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/cb/86/82014f65c6081b7f3b158a094e02fdf2efb57f903dd70ffa5b2c9b12e656/pydbml-1.1.1.tar.gz",
    "platform": "any",
    "description": "[![](https://img.shields.io/pypi/v/pydbml.svg)](https://pypi.org/project/pydbml/) [![](https://img.shields.io/pypi/dm/pydbml.svg)](https://pypi.org/project/pydbml/)  [![](https://img.shields.io/github/v/tag/Vanderhoof/PyDBML.svg?label=GitHub)](https://github.com/Vanderhoof/PyDBML) ![](coverage.svg)\n\n# DBML parser for Python\n\n*Compliant with DBML **v3.6.1** syntax*\n\nPyDBML is a Python parser and builder for [DBML](https://www.dbml.org) syntax. \n\n> 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/upgrading.md).\n\n**Docs:**\n\n* [Class Reference](docs/classes.md)\n* [Creating DBML schema](docs/creating_schema.md)\n* [Upgrading to PyDBML 1.0.0](docs/upgrading.md)\n* [Arbitrary Properties](docs/properties.md)\n\n> PyDBML requires Python v3.8 or higher\n\n## Installation\n\nYou can install PyDBML using pip:\n\n```bash\npip3 install pydbml\n```\n\n## Quick start\n\nTo parse a DBML file, import the `PyDBML` class and initialize it with Path object\n\n```python\n>>> from pydbml import PyDBML\n>>> from pathlib import Path\n>>> parsed = PyDBML(Path('test_schema.dbml'))\n\n```\n\nor with file stream\n\n```python\n>>> with open('test_schema.dbml') as f:\n...     parsed = PyDBML(f)\n\n```\n\nor with entire source string\n\n```python\n>>> with open('test_schema.dbml') as f:\n...     source = f.read()\n>>> parsed = PyDBML(source)\n>>> parsed\n<Database>\n\n```\n\nThe parser returns a Database object that is a container for the parsed DBML entities.\n\nYou can access tables inside the `tables` attribute:\n\n```python\n>>> for table in parsed.tables:\n...     print(table.name)\n...\norders\norder_items\nproducts\nusers\nmerchants\ncountries\n\n```\n\nOr just by getting items by index or full table name:\n\n```python\n>>> parsed[1]\n<Table 'public' 'order_items'>\n>>> parsed['public.countries']\n<Table 'public' 'countries'>\n\n```\n\nOther attributes are:\n\n* **refs** \u2014 list of all references,\n* **enums** \u2014 list of all enums,\n* **table_groups** \u2014 list of all table groups,\n* **project** \u2014 the Project object, if was defined.\n\nGenerate SQL for your DBML Database by accessing the `sql` property:\n\n```python\n>>> print(parsed.sql)  # doctest:+ELLIPSIS\nCREATE TYPE \"orders_status\" AS ENUM (\n  'created',\n  'running',\n  'done',\n  'failure',\n);\n<BLANKLINE>\nCREATE TYPE \"product status\" AS ENUM (\n  'Out of Stock',\n  'In Stock',\n);\n<BLANKLINE>\nCREATE TABLE \"orders\" (\n  \"id\" int PRIMARY KEY AUTOINCREMENT,\n  \"user_id\" int UNIQUE NOT NULL,\n  \"status\" \"orders_status\",\n  \"created_at\" varchar\n);\n...\n\n```\n\nGenerate DBML for your Database by accessing the `dbml` property:\n\n```python\n>>> parsed.project.items['author'] = 'John Doe'\n>>> print(parsed.dbml)  # doctest:+ELLIPSIS\nProject \"test_schema\" {\n    author: 'John Doe'\n    Note {\n        'This schema is used for PyDBML doctest'\n    }\n}\n<BLANKLINE>\nEnum \"orders_status\" {\n    \"created\"\n    \"running\"\n    \"done\"\n    \"failure\"\n}\n<BLANKLINE>\nEnum \"product status\" {\n    \"Out of Stock\"\n    \"In Stock\"\n}\n<BLANKLINE>\nTable \"orders\" [headercolor: #fff] {\n    \"id\" int [pk, increment]\n    \"user_id\" int [unique, not null]\n    \"status\" \"orders_status\"\n    \"created_at\" varchar\n}\n<BLANKLINE>\nTable \"order_items\" {\n    \"order_id\" int\n    \"product_id\" int\n    \"quantity\" int [default: 1]\n}\n...\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python parser and builder for DBML",
    "version": "1.1.1",
    "project_urls": {
        "Homepage": "https://github.com/Vanderhoof/PyDBML"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "137c895bb9c2332ed244da7bd1ee9195a5da1d692e8047b0d5818f4494d958d6",
                "md5": "8dbb317cfb47314b0bdd63f3343e8960",
                "sha256": "dbc0832fc9d24527ed004f4c8d8306cf7a06db4928ec7d5b73d864ab609225f5"
            },
            "downloads": -1,
            "filename": "pydbml-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8dbb317cfb47314b0bdd63f3343e8960",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 45327,
            "upload_time": "2024-08-11T07:21:57",
            "upload_time_iso_8601": "2024-08-11T07:21:57.169314Z",
            "url": "https://files.pythonhosted.org/packages/13/7c/895bb9c2332ed244da7bd1ee9195a5da1d692e8047b0d5818f4494d958d6/pydbml-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cb8682014f65c6081b7f3b158a094e02fdf2efb57f903dd70ffa5b2c9b12e656",
                "md5": "4387518a4adfe8583cc4fa7adf1c9da0",
                "sha256": "a9833f93d20a5b48c032bdc8d4f151ca3bd6d8a97ce83301d211810d0b2a16cc"
            },
            "downloads": -1,
            "filename": "pydbml-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "4387518a4adfe8583cc4fa7adf1c9da0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 35167,
            "upload_time": "2024-08-11T07:21:59",
            "upload_time_iso_8601": "2024-08-11T07:21:59.723602Z",
            "url": "https://files.pythonhosted.org/packages/cb/86/82014f65c6081b7f3b158a094e02fdf2efb57f903dd70ffa5b2c9b12e656/pydbml-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-11 07:21:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Vanderhoof",
    "github_project": "PyDBML",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pydbml"
}
        
Elapsed time: 1.94790s