{"id":29246682,"url":"https://github.com/aivanf/sqlalchemy-vieworm","last_synced_at":"2025-07-03T23:06:10.816Z","repository":{"id":302093740,"uuid":"998714448","full_name":"AivanF/SQLAlchemy-ViewORM","owner":"AivanF","description":"Define and manage SQL views, including materialized views, in SQLAlchemy ORM","archived":false,"fork":false,"pushed_at":"2025-06-09T12:34:27.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-30T14:51:07.600Z","etag":null,"topics":["materialized-view","orm","python","sqlalchemy"],"latest_commit_sha":null,"homepage":"","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/AivanF.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","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,"zenodo":null}},"created_at":"2025-06-09T06:12:23.000Z","updated_at":"2025-06-30T14:26:09.000Z","dependencies_parsed_at":"2025-06-30T14:51:10.573Z","dependency_job_id":"ac500b20-c0f0-4108-bd3c-2c3dcd7890cd","html_url":"https://github.com/AivanF/SQLAlchemy-ViewORM","commit_stats":null,"previous_names":["aivanf/sqlalchemy-vieworm"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AivanF/SQLAlchemy-ViewORM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AivanF%2FSQLAlchemy-ViewORM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AivanF%2FSQLAlchemy-ViewORM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AivanF%2FSQLAlchemy-ViewORM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AivanF%2FSQLAlchemy-ViewORM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AivanF","download_url":"https://codeload.github.com/AivanF/SQLAlchemy-ViewORM/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AivanF%2FSQLAlchemy-ViewORM/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263416472,"owners_count":23463144,"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","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":["materialized-view","orm","python","sqlalchemy"],"created_at":"2025-07-03T23:06:09.693Z","updated_at":"2025-07-03T23:06:10.716Z","avatar_url":"https://github.com/AivanF.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLAlchemy-ViewORM\n\nA flexible library for defining and managing SQL views, including materialized views, in SQLAlchemy ORM.\n\n[![PyPI version](https://badge.fury.io/py/SQLAlchemy-ViewORM.svg)](https://badge.fury.io/py/SQLAlchemy-ViewORM)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Python Versions](https://img.shields.io/pypi/pyversions/SQLAlchemy-ViewORM.svg)](https://pypi.org/project/SQLAlchemy-ViewORM/)\n[![Tests](https://github.com/AivanF/SQLAlchemy-ViewORM/actions/workflows/python-package.yml/badge.svg)](https://github.com/AivanF/SQLAlchemy-ViewORM/actions/workflows/python-package.yml)\n\n## Overview\n\nSQLAlchemy ViewORM extends SQLAlchemy's ORM to provide a clean, Pythonic interface for creating and managing database views. It supports:\n\n- **Standard views**: Traditional simple SQL views that execute their query on each access\n- **Materialized views**: Views that store their results physically for faster access\n- **Table-simulated views**: For databases that don't support views or materialized views\n- **Cross-database compatibility**: Works with PostgreSQL, MySQL, SQLite, and more\n- **Materialized view emulation**: for DBMSs without materialized views support like SQLite,\n  you can choose what method to use for each model:\n  treat as a simple view or mock by a regular table – useful for tests.\n- **Dialect-aware features**: Allows views' queries customisation for each database\n- **Type annotations**: Fully typed with mypy support.\n\nWell, I developed the lib for my own needs, because lots of other implementations that I found look too weak, and I strive for flexibility with comprehensive features.\n\n## Installation\n\n```bash\npip install SQLAlchemy-ViewORM\n```\n\n## Quick Example\n\n```python\nfrom sqlalchemy import Column, Integer, String, Boolean, select\nfrom sqlalchemy.orm import DeclarativeBase\nfrom sqlalchemy_view_orm import ViewBase, ViewConfig\n\n# Regular SQLAlchemy model\nclass Base(DeclarativeBase):\n    pass\n\nclass User(Base):\n    __tablename__ = \"users\"\n\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    email = Column(String)\n    active = Column(Boolean, default=True)\n\n# Define a view based on the User model\nclass ActiveUserView(ViewBase):\n    __tablename__ = \"active_users_view\"\n\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    email = Column(String)\n\n    # Define view configuration\n    __view_config__ = ViewConfig(\n\n        # Define the view's query\n        definition=select(\n            User.id, User.name, User.email\n        ).where(User.active == True),\n\n        # Create as materialized view for better performance\n        materialized=True,\n\n        # Enable concurrent refresh (PostgreSQL)\n        concurrently=True\n    )\n\n# Create the view in the database\nengine = create_engine(\"postgresql://user:pass@localhost/dbname\")\nActiveUserView.metadata.create_all(engine)\n\n# Refresh materialized view data\nwith engine.begin() as conn:\n    for cmd in ActiveUserView.get_refresh_cmds(engine):\n        conn.execute(cmd)\n```\n\n## Features\n\n### View Types\n\n- **Simple Views**: Standard non-materialized views.\n  ```python\n  __view_config__ = ViewConfig(\n      definition=my_select_query,\n      materialized=False  # Default\n  )\n  ```\n\n- **Materialized Views**: Physically stored query results, in DBMSs that supported materialized views (e.g. PostgreSQL and Oracle), and simple views are used in other cases.\n  ```python\n  __view_config__ = ViewConfig(\n      definition=my_select_query,\n      materialized=True\n  )\n  ```\n\n- **Table Views**: For databases without native materialized view support (like SQLite, MySQL), you easily can emulate them with tables.\n  ```python\n  __view_config__ = ViewConfig(\n      definition=my_select_query,\n      materialized=True,\n      materialized_as_table=True  # Use tables to simulate materialized views\n  )\n  ```\n\nWhich is pretty helpful when developing apps for Postgres while testing with SQLite. Frankly speaking, this is why I developed the lib 🙂\n\n### Advanced Usage\n\nDefine views with dynamic queries to adjust by considering database dialect:\n\n```python\ndef build_query(dialect):\n    # Adapt the query based on the database dialect\n    if dialect == 'postgresql':\n        return select(User.id, func.lower(User.email).label('email'))\n    else:\n        # Simpler version for other databases\n        return select(User.id, User.email)\n\nclass UserEmailView(ViewBase):\n    __tablename__ = \"user_email_view\"\n\n    id = Column(Integer, primary_key=True)\n    email = Column(String)\n\n    __view_config__ = ViewConfig(\n        definer=build_query,  # Pass a function instead of a fixed query\n        materialized=True\n    )\n```\n\n## Why Use SQLAlchemy ViewORM?\n\nDatabase views offer numerous advantages:\n\n1. **Abstraction**: Hide complex queries behind simple interfaces\n2. **Performance**: Materialized views improve query speed for complex calculations\n3. **Consistency**: Ensure the same ORM-based query logic is used across your application\n4. **Security**: Restrict access to sensitive data\n\nThis library makes it easy to leverage these benefits within your SQLAlchemy applications.\n\n## Project Status\n\nThis project is in passive development. We welcome contributions, bug reports, and feature requests, especially with suggested solutions. See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Documentation\n\nFull documentation is available on the [ReadTheDocs](https://sqlalchemy-vieworm.readthedocs.io/en/latest/), and its sources located in the [docs](https://github.com/AivanF/SQLAlchemy-ViewORM/tree/main/docs) directory.\n\n## Examples\n\nCheck out the [examples](https://github.com/AivanF/SQLAlchemy-ViewORM/tree/main/examples) directory for complete working code:\n\n- `basic_example.py`: Simple view usage with SQLite\n- `advanced_example.py`: Complex views with dialect-specific features\n- `flask_example.py`: Integration with Flask web applications\n- `FastAPI-example/`: Deeper example with async FastAPI web applications and updates\n\n## Running Tests\n\n```bash\n# Install development dependencies\npip install -e \".[dev]\"\n\n# Run tests\npytest\n\n# With coverage\npytest --cov=sqlalchemy_view_orm\n```\n\n## Author\n\n- **AivanF** - [GitHub Profile](https://github.com/AivanF)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faivanf%2Fsqlalchemy-vieworm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faivanf%2Fsqlalchemy-vieworm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faivanf%2Fsqlalchemy-vieworm/lists"}