{"id":31859504,"url":"https://github.com/python-ellar/ellar-sql","last_synced_at":"2026-03-04T01:03:02.051Z","repository":{"id":214795135,"uuid":"723139197","full_name":"python-ellar/ellar-sql","owner":"python-ellar","description":"EllarSQL Module adds support for SQLAlchemy and Alembic package to your Ellar application","archived":false,"fork":false,"pushed_at":"2025-10-04T07:54:32.000Z","size":3297,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-04T08:17:42.859Z","etag":null,"topics":["alembic","ellar","sqlalchemy"],"latest_commit_sha":null,"homepage":"https://python-ellar.github.io/ellar-sql/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/python-ellar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-24T19:42:35.000Z","updated_at":"2025-10-04T07:53:38.000Z","dependencies_parsed_at":"2023-12-30T21:53:50.307Z","dependency_job_id":"a5a8f970-880e-406c-ab4d-5bcbdcfc54b2","html_url":"https://github.com/python-ellar/ellar-sql","commit_stats":null,"previous_names":["python-ellar/ellar-sqlalchemy","python-ellar/ellar-sql"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/python-ellar/ellar-sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-ellar%2Fellar-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-ellar%2Fellar-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-ellar%2Fellar-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-ellar%2Fellar-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/python-ellar","download_url":"https://codeload.github.com/python-ellar/ellar-sql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-ellar%2Fellar-sql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279011850,"owners_count":26085004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alembic","ellar","sqlalchemy"],"created_at":"2025-10-12T15:26:03.790Z","updated_at":"2025-10-12T15:26:08.227Z","avatar_url":"https://github.com/python-ellar.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"#\" target=\"blank\"\u003e\u003cimg src=\"https://python-ellar.github.io/ellar-sql/img/ellar_sql.png\" width=\"200\" alt=\"Ellar Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n![Test](https://github.com/python-ellar/ellar-sql/actions/workflows/test_full.yml/badge.svg)\n![Coverage](https://img.shields.io/codecov/c/github/python-ellar/ellar-sql)\n[![PyPI version](https://badge.fury.io/py/ellar-sql.svg)](https://badge.fury.io/py/ellar-sql)\n[![PyPI version](https://img.shields.io/pypi/v/ellar-sql.svg)](https://pypi.python.org/pypi/ellar-sql)\n[![PyPI version](https://img.shields.io/pypi/pyversions/ellar-sql.svg)](https://pypi.python.org/pypi/ellar-sql)\n\n\n## Introduction\nEllarSQL Module adds support for `SQLAlchemy` and `Alembic` package to your Ellar application\n\n## Installation\n```shell\n$(venv) pip install ellar-sql\n```\n\nThis library was inspired by [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/3.1.x/)\n## Features\n\n- Migration\n- Single/Multiple Database\n- Pagination\n- Compatible with SQLAlchemy tools\n\n\n## **Usage**\nIn your ellar application, create a module called `db` or any name of your choice,\n```shell\nellar create-module db\n```\nThen, in `models/base.py` define your model base as shown below:\n\n```python\n# db/models/base.py\nfrom datetime import datetime\nfrom sqlalchemy import DateTime, func\nfrom sqlalchemy.orm import Mapped, mapped_column\nfrom ellar_sql.model import Model\n\n\nclass Base(Model):\n  __base_config__ = {'as_base': True}\n  __database__ = 'default'\n\n  created_date: Mapped[datetime] = mapped_column(\n      \"created_date\", DateTime, default=datetime.utcnow, nullable=False\n  )\n\n  time_updated: Mapped[datetime] = mapped_column(\n      \"time_updated\", DateTime, nullable=False, default=datetime.utcnow, onupdate=func.now()\n  )\n```\n\nUse `Base` to create other models, like users in `User` in \n```python\n# db/models/users.py\nfrom sqlalchemy import Integer, String\nfrom sqlalchemy.orm import Mapped, mapped_column\nfrom .base import Base\n\n\nclass User(Base):\n    id: Mapped[int] = mapped_column(Integer, primary_key=True)\n    username: Mapped[str] = mapped_column(String, unique=True, nullable=False)\n    email: Mapped[str] = mapped_column(String)\n```\n\n### Configure Module\n```python\n# db/module.py\nfrom ellar.app import App\nfrom ellar.common import Module, IApplicationStartup\nfrom ellar.core import ModuleBase\nfrom ellar.di import Container\nfrom ellar_sql import EllarSQLAlchemyModule, EllarSQLService\n\nfrom .controllers import DbController\n\n@Module(\n    controllers=[DbController],\n    providers=[],\n    routers=[],\n    modules=[\n        EllarSQLAlchemyModule.setup(\n            databases={\n                'default': 'sqlite:///project.db',\n            }, \n            echo=True, \n            migration_options={\n                'directory': 'my_migrations_folder'\n            },\n            models=['db.models.users']\n        )\n    ]\n)\nclass DbModule(ModuleBase, IApplicationStartup):\n    \"\"\"\n    Db Module\n    \"\"\"\n\n    async def on_startup(self, app: App) -\u003e None:\n        db_service = app.injector.get(EllarSQLService)\n        db_service.create_all()\n\n    def register_providers(self, container: Container) -\u003e None:\n        \"\"\"for more complicated provider registrations, use container.register_instance(...) \"\"\"\n```\n\n### Model Usage\nDatabase session exist at model level and can be accessed through `model.get_db_session()` eg, `User.get_db_session()`\n```python\n# db/models/controllers.py\nfrom ellar.common import Controller, ControllerBase, get, post, Body\nfrom pydantic import EmailStr\nfrom sqlalchemy import select\n\nfrom .models.users import User\n\n\n@Controller\nclass DbController(ControllerBase):\n    @post(\"/users\")\n    async def create_user(self, username: Body[str], email: Body[EmailStr]):\n        session = User.get_db_session()\n        user = User(username=username, email=email)\n\n        session.add(user)\n        session.commit()\n        \n        return user.dict()\n\n\n    @get(\"/users/{user_id:int}\")\n    def get_user_by_id(self, user_id: int):\n        session = User.get_db_session()\n        stmt = select(User).filter(User.id==user_id)\n        user = session.execute(stmt).scalar()\n        return user.dict()\n\n    @get(\"/users\")\n    async def get_all_users(self):\n        session = User.get_db_session()\n        stmt = select(User)\n        rows = session.execute(stmt.offset(0).limit(100)).scalars()\n        return [row.dict() for row in rows]\n```\n\n## License\n\nEllar is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-ellar%2Fellar-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpython-ellar%2Fellar-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-ellar%2Fellar-sql/lists"}