sqlagen


Namesqlagen JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/maacck/sqlagen
SummaryNone
upload_time2024-06-19 06:53:14
maintainerNone
docs_urlNone
authormaacck
requires_python<4.0,>=3.10
licenseNone
keywords pydantic sqlalchemy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqlagen

Model Generator from projects using SQLAlchemy as their orm.

This package is helpful while building web apps with FastAPI and SQLAlchemy. 

It provides capability which can:
- generate SQLAlchemy from database schema
- generates Pydantic Model from SQLAlchemy Model.

Project was originally forked from https://github.com/agronholm/sqlacodegen

However, we design to modify some of the features and add new features to the project to fit our own project needs, so we decided to create a new project.


Installation
============

To install,

    pip install sqlagen


Quickstart
==========

Please be aware that we are using `pydantic >= 2.0.0` and `sqlalchemy >= 2.0` in this project.

### Generate SQLAlchemy Models

You may use `generate_db_models` function directly to generate Pydantic Model from SQLAlchemy Model. It takes `Generator`'s init arguments and init a Generator class. 

Examples:

```python
from sqlagen import generate_db_models

generate_db_models(db_url="pymysql+mysql://localhost:6306/test")
```

```python
from sqlagen import DeclarativeGenerator
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import MetaData

metadata = MetaData()
engine = create_engine("pymysql+mysql://localhost:6306/test")
generator = DeclarativeGenerator(metadata=metadata, engine=engine, options={})
print(generator.generate())
```


### Generate Pydantic Schema Models

You may use `generate_db_models` function directly to generate Pydantic Model from SQLAlchemy Model. It takes `Generator`'s init arguments and init a Generator class. 

Examples:

```python
from sqlagen import generate_schema_models
    
generate_schema_models(models=[MyModel], base_model=CustomBaseModel)

```

```python
from sqlagen import SchemaModelGenerator

generator = SchemaModelGenerator(base_model=CustomBaseModel)
generator.generate_from_module(models=my_models_module, output_path="schemas.py")
```


Generator Class takes following init arguments
- `split_models`: Whether to split models into Base, Create, Update and Read models. Default is `Fakse`.
- `base_model`: Base model to inherit from. Default is `BaseModel` from `pydantic`.
- `restrict_fields`: Which takes a `set` of fields to restrict. Default is `None`. This is useful when you want to restrict some fields to be readonly such as id, created_at, updated_at.
- `indentation`: Indentation to use in generated code.

## More Examples

Since most of our projects use `sqlalchemy` and `pydantic`, so we often need to generate models from database schema and generate pydantic models from sqlalchemy models.
```python
from sqlagen import generate_db_models, SchemaModelGenerator

generate_db_models(db_url="pymysql+mysql://localhost:6306/test",outfile_path="./models")
generate_schema_models(models=[MyModel], base_model=CustomBaseModel, outfile_path="./schemas")
```

## RoadMap
-  Strict typing, such as using `conint` for limiting `Integer` size and `constr` for `String` length.
-  Probably, generate relationships as well.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/maacck/sqlagen",
    "name": "sqlagen",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "pydantic, sqlalchemy",
    "author": "maacck",
    "author_email": "c.mai@madainchina.com",
    "download_url": "https://files.pythonhosted.org/packages/bc/5c/cef35ddf5509849954d377da96cda10b4f022b751cf7bfd5f325280a25ba/sqlagen-0.1.3.tar.gz",
    "platform": null,
    "description": "# sqlagen\n\nModel Generator from projects using SQLAlchemy as their orm.\n\nThis package is helpful while building web apps with FastAPI and SQLAlchemy. \n\nIt provides capability which can:\n- generate SQLAlchemy from database schema\n- generates Pydantic Model from SQLAlchemy Model.\n\nProject was originally forked from https://github.com/agronholm/sqlacodegen\n\nHowever, we design to modify some of the features and add new features to the project to fit our own project needs, so we decided to create a new project.\n\n\nInstallation\n============\n\nTo install,\n\n    pip install sqlagen\n\n\nQuickstart\n==========\n\nPlease be aware that we are using `pydantic >= 2.0.0` and `sqlalchemy >= 2.0` in this project.\n\n### Generate SQLAlchemy Models\n\nYou may use `generate_db_models` function directly to generate Pydantic Model from SQLAlchemy Model. It takes `Generator`'s init arguments and init a Generator class. \n\nExamples:\n\n```python\nfrom sqlagen import generate_db_models\n\ngenerate_db_models(db_url=\"pymysql+mysql://localhost:6306/test\")\n```\n\n```python\nfrom sqlagen import DeclarativeGenerator\nfrom sqlalchemy.engine import create_engine\nfrom sqlalchemy.schema import MetaData\n\nmetadata = MetaData()\nengine = create_engine(\"pymysql+mysql://localhost:6306/test\")\ngenerator = DeclarativeGenerator(metadata=metadata, engine=engine, options={})\nprint(generator.generate())\n```\n\n\n### Generate Pydantic Schema Models\n\nYou may use `generate_db_models` function directly to generate Pydantic Model from SQLAlchemy Model. It takes `Generator`'s init arguments and init a Generator class. \n\nExamples:\n\n```python\nfrom sqlagen import generate_schema_models\n    \ngenerate_schema_models(models=[MyModel], base_model=CustomBaseModel)\n\n```\n\n```python\nfrom sqlagen import SchemaModelGenerator\n\ngenerator = SchemaModelGenerator(base_model=CustomBaseModel)\ngenerator.generate_from_module(models=my_models_module, output_path=\"schemas.py\")\n```\n\n\nGenerator Class takes following init arguments\n- `split_models`: Whether to split models into Base, Create, Update and Read models. Default is `Fakse`.\n- `base_model`: Base model to inherit from. Default is `BaseModel` from `pydantic`.\n- `restrict_fields`: Which takes a `set` of fields to restrict. Default is `None`. This is useful when you want to restrict some fields to be readonly such as id, created_at, updated_at.\n- `indentation`: Indentation to use in generated code.\n\n## More Examples\n\nSince most of our projects use `sqlalchemy` and `pydantic`, so we often need to generate models from database schema and generate pydantic models from sqlalchemy models.\n```python\nfrom sqlagen import generate_db_models, SchemaModelGenerator\n\ngenerate_db_models(db_url=\"pymysql+mysql://localhost:6306/test\",outfile_path=\"./models\")\ngenerate_schema_models(models=[MyModel], base_model=CustomBaseModel, outfile_path=\"./schemas\")\n```\n\n## RoadMap\n-  Strict typing, such as using `conint` for limiting `Integer` size and `constr` for `String` length.\n-  Probably, generate relationships as well.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/maacck/sqlagen",
        "Repository": "https://github.com/maacck/sqlagen"
    },
    "split_keywords": [
        "pydantic",
        " sqlalchemy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2de685cd6826825a42866a8eee23901ed82001380c92d34857413c65218f1415",
                "md5": "095945e54b2120b6be0afcd8767f85ff",
                "sha256": "bf105e7f784dd2a1710f99d0fbf1a2358f968911855d7223c02b9f302fec1ffc"
            },
            "downloads": -1,
            "filename": "sqlagen-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "095945e54b2120b6be0afcd8767f85ff",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 26262,
            "upload_time": "2024-06-19T06:53:09",
            "upload_time_iso_8601": "2024-06-19T06:53:09.988333Z",
            "url": "https://files.pythonhosted.org/packages/2d/e6/85cd6826825a42866a8eee23901ed82001380c92d34857413c65218f1415/sqlagen-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc5ccef35ddf5509849954d377da96cda10b4f022b751cf7bfd5f325280a25ba",
                "md5": "3c2dca389dd68ff4d3f2fe28b096c2bc",
                "sha256": "5d3e46e72c793c18737aab46a74751f94ed3ee66932fe9c0706317df1faa4e18"
            },
            "downloads": -1,
            "filename": "sqlagen-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "3c2dca389dd68ff4d3f2fe28b096c2bc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 21709,
            "upload_time": "2024-06-19T06:53:14",
            "upload_time_iso_8601": "2024-06-19T06:53:14.029377Z",
            "url": "https://files.pythonhosted.org/packages/bc/5c/cef35ddf5509849954d377da96cda10b4f022b751cf7bfd5f325280a25ba/sqlagen-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-19 06:53:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "maacck",
    "github_project": "sqlagen",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "sqlagen"
}
        
Elapsed time: 0.98985s