dbml-to-sqlalchemy


Namedbml-to-sqlalchemy JSON
Version 0.9.2 PyPI version JSON
download
home_pagehttps://github.com/fraoustin/dbml-to-sqlalchemy.git
Summarydbml-to-sqlalchemy extension for sqlachemy: upload dbml model in orm sqlalchemy
upload_time2023-09-26 15:02:18
maintainer
docs_urlNone
authorFrédéric Aoustin
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dbml-to-sqlalchemy

generate Model Class SqlAlchemy from database model dbml


## Installation


    pip install dbml-to-sqlalchemy
        
Or

    git clone https://github.com/fraoustin/dbml-to-sqlalchemy.git
    cd dbml-to-sqlalchemy
    python setup.py install

You can load test by

    flake8 --ignore E501,E226,E128,F401
    python -m unittest discover -s tests


## Usage

for sqlalchemy only 

    import os
    from re import sub
    import sqlalchemy as db
    from sqlalchemy.orm import Session
    from pydbml import PyDBML

    from dbml_to_sqlalchemy import createModel
    from dbml_to_sqlalchemy import mymodel

    database_file = "sqlite://"
    engine = db.create_engine(database_file, echo=False)
    conn = engine.connect()
    metadata = db.MetaData()


    try:
        from sqlalchemy.orm import DeclarativeBase

        class Base(DeclarativeBase):
            metadata = metadata
    except Exception:
        # for sqlalchemy 1.4
        from sqlalchemy.orm import declarative_base
        Base = declarative_base()

    source = """
    Table user {
        id integer [pk, increment, note:'key of user']
        name string [default: 'me', note:'only name']
        Note: 'Stores user data'
    }

    Table post {
        id integer [pk, increment]
        user_id integer [ref: > user.id]
    }
    """

    parsed = PyDBML(source)


    User = createModel(parsed.tables[0], Base)
    Post = createModel(parsed.tables[1], Base)

    print(User.__doc__)
    print(Post.__doc__)

    metadata.create_all(engine)

    with Session(engine) as session:
        user = User(id=1)
        session.add_all([user, ])
        session.commit()
        post = Post(id=1, user_id=1)
        session.add_all([post, ])
        session.commit()
        mypost = session.scalars(db.select(Post)).all()[0]
        print(mypost.user.name)



for flask-sqlalchemy

    import os
    import logging
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    from pydbml import PyDBML
    from dbml_to_sqlalchemy import createModel, mymodel

    app = Flask(__name__)

    # db SQLAlchemy
    database_file = "sqlite://"
    app.config["SQLALCHEMY_DATABASE_URI"] = database_file
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    db = SQLAlchemy()

    source = """
    Table user {
    id integer [primary key]
    username varchar
    role varchar
    created_at timestamp
    }
    """

    parsed = PyDBML(source)
    createModel(parsed.tables[0], db.Model)


    def model_to_dict(obj):
        return {c.name: getattr(obj, c.name) for c in obj.__table__.columns}


    @app.route('/me')
    def mepath():
        return model_to_dict(mymodel.User.query.filter_by(id=1).first()), 200


    if __name__ == "__main__":
        db.init_app(app)
        with app.app_context():
            db.create_all()
            me = mymodel.User(id=1, username='me')
            db.session.add_all([me, ])
            db.session.commit()
        app.logger.setLevel(logging.DEBUG)
        app.run(host='0.0.0.0', port=5000, debug=True)

## TODO

- manage view from https://github.com/jklukas/sqlalchemy-views (https://stackoverflow.com/questions/9766940/how-to-create-an-sql-view-with-sqlalchemy)
- voir la gestion des defaults

# Feature

- generate documentation
- TODO

# V. 0.9.2

- generate specific class for Enumarate

# V. 0.9.1

- add sample in README

# V. 0.9.0

- init repos


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fraoustin/dbml-to-sqlalchemy.git",
    "name": "dbml-to-sqlalchemy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Fr\u00e9d\u00e9ric Aoustin",
    "author_email": "fraoustin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/48/0e/5b82ed1fb662888fb49ac720e179be31132a3f70d19db58e3ec83351c930/dbml-to-sqlalchemy-0.9.2.tar.gz",
    "platform": "any",
    "description": "# dbml-to-sqlalchemy\n\ngenerate Model Class SqlAlchemy from database model dbml\n\n\n## Installation\n\n\n    pip install dbml-to-sqlalchemy\n        \nOr\n\n    git clone https://github.com/fraoustin/dbml-to-sqlalchemy.git\n    cd dbml-to-sqlalchemy\n    python setup.py install\n\nYou can load test by\n\n    flake8 --ignore E501,E226,E128,F401\n    python -m unittest discover -s tests\n\n\n## Usage\n\nfor sqlalchemy only \n\n    import os\n    from re import sub\n    import sqlalchemy as db\n    from sqlalchemy.orm import Session\n    from pydbml import PyDBML\n\n    from dbml_to_sqlalchemy import createModel\n    from dbml_to_sqlalchemy import mymodel\n\n    database_file = \"sqlite://\"\n    engine = db.create_engine(database_file, echo=False)\n    conn = engine.connect()\n    metadata = db.MetaData()\n\n\n    try:\n        from sqlalchemy.orm import DeclarativeBase\n\n        class Base(DeclarativeBase):\n            metadata = metadata\n    except Exception:\n        # for sqlalchemy 1.4\n        from sqlalchemy.orm import declarative_base\n        Base = declarative_base()\n\n    source = \"\"\"\n    Table user {\n        id integer [pk, increment, note:'key of user']\n        name string [default: 'me', note:'only name']\n        Note: 'Stores user data'\n    }\n\n    Table post {\n        id integer [pk, increment]\n        user_id integer [ref: > user.id]\n    }\n    \"\"\"\n\n    parsed = PyDBML(source)\n\n\n    User = createModel(parsed.tables[0], Base)\n    Post = createModel(parsed.tables[1], Base)\n\n    print(User.__doc__)\n    print(Post.__doc__)\n\n    metadata.create_all(engine)\n\n    with Session(engine) as session:\n        user = User(id=1)\n        session.add_all([user, ])\n        session.commit()\n        post = Post(id=1, user_id=1)\n        session.add_all([post, ])\n        session.commit()\n        mypost = session.scalars(db.select(Post)).all()[0]\n        print(mypost.user.name)\n\n\n\nfor flask-sqlalchemy\n\n    import os\n    import logging\n    from flask import Flask\n    from flask_sqlalchemy import SQLAlchemy\n    from pydbml import PyDBML\n    from dbml_to_sqlalchemy import createModel, mymodel\n\n    app = Flask(__name__)\n\n    # db SQLAlchemy\n    database_file = \"sqlite://\"\n    app.config[\"SQLALCHEMY_DATABASE_URI\"] = database_file\n    app.config[\"SQLALCHEMY_TRACK_MODIFICATIONS\"] = False\n    db = SQLAlchemy()\n\n    source = \"\"\"\n    Table user {\n    id integer [primary key]\n    username varchar\n    role varchar\n    created_at timestamp\n    }\n    \"\"\"\n\n    parsed = PyDBML(source)\n    createModel(parsed.tables[0], db.Model)\n\n\n    def model_to_dict(obj):\n        return {c.name: getattr(obj, c.name) for c in obj.__table__.columns}\n\n\n    @app.route('/me')\n    def mepath():\n        return model_to_dict(mymodel.User.query.filter_by(id=1).first()), 200\n\n\n    if __name__ == \"__main__\":\n        db.init_app(app)\n        with app.app_context():\n            db.create_all()\n            me = mymodel.User(id=1, username='me')\n            db.session.add_all([me, ])\n            db.session.commit()\n        app.logger.setLevel(logging.DEBUG)\n        app.run(host='0.0.0.0', port=5000, debug=True)\n\n## TODO\n\n- manage view from https://github.com/jklukas/sqlalchemy-views (https://stackoverflow.com/questions/9766940/how-to-create-an-sql-view-with-sqlalchemy)\n- voir la gestion des defaults\n\n# Feature\n\n- generate documentation\n- TODO\n\n# V. 0.9.2\n\n- generate specific class for Enumarate\n\n# V. 0.9.1\n\n- add sample in README\n\n# V. 0.9.0\n\n- init repos\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "dbml-to-sqlalchemy extension for sqlachemy: upload dbml model in orm sqlalchemy",
    "version": "0.9.2",
    "project_urls": {
        "Homepage": "https://github.com/fraoustin/dbml-to-sqlalchemy.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "480e5b82ed1fb662888fb49ac720e179be31132a3f70d19db58e3ec83351c930",
                "md5": "9773606b66b5e8078a6067b3823ea087",
                "sha256": "aa0075b367b23f93b826fea4e104613cd2d1de4eead74508aa2af37812ab6d0b"
            },
            "downloads": -1,
            "filename": "dbml-to-sqlalchemy-0.9.2.tar.gz",
            "has_sig": false,
            "md5_digest": "9773606b66b5e8078a6067b3823ea087",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5665,
            "upload_time": "2023-09-26T15:02:18",
            "upload_time_iso_8601": "2023-09-26T15:02:18.727417Z",
            "url": "https://files.pythonhosted.org/packages/48/0e/5b82ed1fb662888fb49ac720e179be31132a3f70d19db58e3ec83351c930/dbml-to-sqlalchemy-0.9.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-26 15:02:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fraoustin",
    "github_project": "dbml-to-sqlalchemy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "dbml-to-sqlalchemy"
}
        
Elapsed time: 0.33247s