pydbml


Namepydbml JSON
Version 1.0.11 PyPI version JSON
download
home_pagehttps://github.com/Vanderhoof/PyDBML
SummaryPython parser and builder for DBML
upload_time2024-04-23 16:13:16
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.2.0** 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)

> 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/f8/dc/1f4b1040b037df49dc046472675a880c5520e68d7861e6c510ded3a26e5e/pydbml-1.0.11.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.2.0** 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\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.0.11",
    "project_urls": {
        "Homepage": "https://github.com/Vanderhoof/PyDBML"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c87865848b89d1e9fe0130213da0376adcebfcbbbd5286e2ef01e744d76f1b3",
                "md5": "c34518006ed2f2c392bd511922d71acf",
                "sha256": "54d8d5cef7155996b55c4e2d338d6958db130f76d14190528b8cb696509501ff"
            },
            "downloads": -1,
            "filename": "pydbml-1.0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c34518006ed2f2c392bd511922d71acf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 33928,
            "upload_time": "2024-04-23T16:13:14",
            "upload_time_iso_8601": "2024-04-23T16:13:14.574211Z",
            "url": "https://files.pythonhosted.org/packages/7c/87/865848b89d1e9fe0130213da0376adcebfcbbbd5286e2ef01e744d76f1b3/pydbml-1.0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f8dc1f4b1040b037df49dc046472675a880c5520e68d7861e6c510ded3a26e5e",
                "md5": "133760eaea1f532ae66a971e6d6fd926",
                "sha256": "74ef0b64416a7cf71539c8340bbc175dd318a2e76c72f46546097938dad547ac"
            },
            "downloads": -1,
            "filename": "pydbml-1.0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "133760eaea1f532ae66a971e6d6fd926",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 30980,
            "upload_time": "2024-04-23T16:13:16",
            "upload_time_iso_8601": "2024-04-23T16:13:16.963208Z",
            "url": "https://files.pythonhosted.org/packages/f8/dc/1f4b1040b037df49dc046472675a880c5520e68d7861e6c510ded3a26e5e/pydbml-1.0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-23 16:13:16",
    "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: 0.22680s