{"id":19710421,"url":"https://github.com/blueromans/sqlalchemymixin","last_synced_at":"2025-04-29T17:31:15.114Z","repository":{"id":123850620,"uuid":"559187951","full_name":"blueromans/SqlalchemyMixin","owner":"blueromans","description":"A pack of framework-agnostic, easy-to-integrate for SQLAlchemy ORM.","archived":false,"fork":false,"pushed_at":"2023-08-02T14:15:40.000Z","size":54,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T18:50:33.631Z","etag":null,"topics":["django","fastapi","flask","flask-application","flask-sqlalchemy","mixins","postgresql","pypi","pypi-package","python","python3","sqlalchemy","sqlalchemy-orm","sqlalchemy-python","sqlite3"],"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/blueromans.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":"2022-10-29T10:35:55.000Z","updated_at":"2025-02-12T11:09:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"52314a69-556f-4231-ae08-c9c4ce649a0d","html_url":"https://github.com/blueromans/SqlalchemyMixin","commit_stats":{"total_commits":22,"total_committers":1,"mean_commits":22.0,"dds":0.0,"last_synced_commit":"1fb8ceb5d69ec38abfd4047bf9cee901304845db"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueromans%2FSqlalchemyMixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueromans%2FSqlalchemyMixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueromans%2FSqlalchemyMixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueromans%2FSqlalchemyMixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blueromans","download_url":"https://codeload.github.com/blueromans/SqlalchemyMixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251549187,"owners_count":21607367,"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":["django","fastapi","flask","flask-application","flask-sqlalchemy","mixins","postgresql","pypi","pypi-package","python","python3","sqlalchemy","sqlalchemy-orm","sqlalchemy-python","sqlite3"],"created_at":"2024-11-11T22:07:19.132Z","updated_at":"2025-04-29T17:31:15.088Z","avatar_url":"https://github.com/blueromans.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI version](https://img.shields.io/pypi/v/SqlalchemyMixin.svg)](https://pypi.python.org/pypi/SqlalchemyMixin)\n# SQLAlchemy Mixin\n\nA pack of framework-agnostic, easy-to-integrate for SQLAlchemy ORM.\n\nHeavily inspired by [Django ORM](https://docs.djangoproject.com/en/1.10/topics/db/queries/)\nand [Eloquent ORM](https://laravel.com/docs/5.4/eloquent)\n\neasy integration to your existing project like [FastApi](https://fastapi.tiangolo.com):\n```python\nfrom sqlalchemy_mixins import BaseMixin\n\nclass User(Base, BaseMixin):\n     pass\n```\n\n## Table of Contents\n\n1. [Installation](#installation)\n1. [Quick Start](#quick-start)\n    1. [Framework-agnostic](#framework-agnostic)\n    1. [Usage with Flask-SQLAlchemy](#usage-with-flask-sqlalchemy)\n1. [Features](#features)\n    1. [Active Record](#active-record)\n        1. [CRUD](#crud)\n        1. [Querying](#querying)\n    1. [Eager Load](#eager-load)\n    1. [Django-like queries](#django-like-queries)\n        1. [Filter and sort by relations](#filter-and-sort-by-relations)\n        1. [Automatic eager load relations](#automatic-eager-load-relations)\n    1. [All-in-one: smart_query](#all-in-one-smart_query)\n    1. [Beauty \\_\\_repr\\_\\_](#beauty-__repr__)\n    1. [DateMixin](#timestamps)\n\n## Installation\n\nUse pip\n```\npip install SqlalchemyMixin\n```\n\n## Quick Start\n\n### Framework-agnostic\nHere's a quick demo of what our mixins can do.\n\n```python\nbob = User.create(name='Bob')\npost1 = Post.create(body='Post 1', user=bob, rating=3)\npost2 = Post.create(body='long-long-long-long-long body', rating=2,\n                    user=User.create(name='Bill'),\n                    comments=[Comment.create(body='cool!', user=bob)])\n\n# filter using operators like 'in' and 'contains' and relations like 'user'\n# will output this beauty: \u003cPost #1 body:'Post1' user:'Bill'\u003e\nprint(Post.where(rating__in=[2, 3, 4], user___name__like='%Bi%').all())\n# joinedload post and user\nprint(Comment.with_joined('user', 'post', 'post.comments').first())\n# subqueryload posts and their comments\nprint(User.with_subquery('posts', 'posts.comments').first())\n# sort by rating DESC, user name ASC\nprint(Post.sort('-rating', 'user___name').all())\n# created_at, updated_at timestamps added automatically\nprint(\"Created Bob at \", bob.created_at)   \n# serialize to dict, with relationships\n```\n\n\n\n### Usage with Flask-SQLAlchemy\n\n```python\nimport sqlalchemy as sa\nfrom flask import Flask\nfrom flask_sqlalchemy import SQLAlchemy\nfrom sqlalchemy_mixins import BaseMixin\n\napp = Flask(__name__)\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'\ndb = SQLAlchemy(app)\n\n######### Models ######### \nclass BaseModel(db.Model, BaseMixin):\n    __abstract__ = True\n    pass\n\n\nclass User(BaseModel):\n    name = sa.Column(sa.String)\n\n######## Initialize ########\nBaseModel.set_session(db.session)\n\n######## Create test entity ########\ndb.create_all()\nuser = User.create(name='bob')\nprint(user)\n```\n\n# *** Autocommit ***\nThis library relies on SQLAlchemy's `autocommit` flag. It needs to be set to True when initializing the session i.e:\n```python\nsession = scoped_session(sessionmaker(bind=engine, autocommit=True))\nBaseModel.set_session(session)\n```\nor with `Flask-SQLAlchemy`\n```python\ndb = SQLAlchemy(app, session_options={'autocommit': True})\n```\n\n# Features\n\nMain features are\n * [Active Record](#active-record)\n * [Eager Load](#eager-load)\n * [Django-like queries](#django-like-queries)\n * [Beauty \\_\\_repr\\_\\_](#beauty-__repr__)\n * [Timestamps](#timestamps)\n\n## Active Record\nprovided by [`ActiveRecordMixin`](sqlalchemy_mixins/active_record.py)\n\nSQLAlchemy's [Data Mapper](https://en.wikipedia.org/wiki/Data_mapper_pattern)\npattern is cool, but\n[Active Record](https://en.wikipedia.org/wiki/Active_record_pattern)\npattern is easiest and more [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself).\n\nWell, we implemented it on top of Data Mapper!\nAll we need is to just inject [session](http://docs.sqlalchemy.org/en/latest/orm/session.html) into ORM class while bootstrapping our app:\n\n```python\nBaseModel.set_session(session)\n# now we have access to BaseOrmModel.session property\n```\n\n### CRUD\nWe all love SQLAlchemy, but doing [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete)\nis a bit tricky there.\n\nFor example, creating an object needs 3 lines of code:\n```python\nbob = User(name='Bobby', age=1)\nsession.add(bob)\nsession.flush()\n```\n\nWell, having access to session from model, we can just write\n```python\nbob = User.create(name='Bobby', age=1)\n```\nthat's how it's done in [Django ORM](https://docs.djangoproject.com/en/1.10/ref/models/querysets/#create)\nand [Peewee](http://docs.peewee-orm.com/en/latest/peewee/querying.html#creating-a-new-record)\n\nupdate and delete methods are provided as well\n```python\nbob.update(name='Bob', age=21)\nbob.delete()\n```\n\nAnd, as in [Django](https://docs.djangoproject.com/en/1.10/topics/db/queries/#retrieving-a-single-object-with-get)\nand [Eloquent](https://laravel.com/docs/5.4/eloquent#retrieving-single-models),\nwe can quickly retrieve object by id\n```python\nUser.get(1) # instead of session.query(User).get(1)\n```\n\nand fail if such id doesn't exist\n```python\nUser.get_or_abort(123987) # will raise sqlalchemy_mixins.ModelNotFoundError\n```\n\n\n### Querying\nAs in [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/2.1/queries/#querying-records),\n[Peewee](http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.select)\nand [Django ORM](https://docs.djangoproject.com/en/1.10/topics/db/queries/#retrieving-objects),\nyou can quickly query some class\n```python\nUser.query # instead of session.query(User)\n```\n\nAlso we can quickly retrieve first or all objects:\n```python\nUser.first() # instead of session.query(User).first()\nUser.all() # instead of session.query(User).all()\n```\n\n## Eager load\nprovided by [`EagerLoadMixin`](sqlalchemy_mixins/eager_load.py)\n\n### Nested eager load\nIf you use SQLAlchemy's [eager loading](http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#eager-loading),\nyou may find it not very convenient, especially when we want, say,\nload user, all his posts and comments to every his post in the same query.\n\nWell, now you can easily set what ORM relations you want to eager load\n```python\nUser.with_({\n    'posts': {\n        'comments': {\n            'user': JOINED\n        }\n    }\n}).all()\n```\n\nor we can write class properties instead of strings:\n```python\nUser.with_({\n    User.posts: {\n        Post.comments: {\n            Comment.user: JOINED\n        }\n    }\n}).all()\n```\n\n### Subquery load\nSometimes we want to load relations in separate query, i.e. do [subqueryload](http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#sqlalchemy.orm.subqueryload).\nFor example, we load posts on page like [this](http://www.qopy.me/3V4Tsu_GTpCMJySzvVH1QQ),\nand for each post we want to have user and all comments (and comment authors).\n\nTo speed up query, we load comments in separate query, but, in this separate query, join user\n```python\nfrom sqlalchemy_mixins import JOINED, SUBQUERY\nPost.with_({\n    'user': JOINED, # joinedload user\n    'comments': (SUBQUERY, {  # load comments in separate query\n        'user': JOINED  # but, in this separate query, join user\n    })\n}).all()\n```\n\nHere, posts will be loaded on first query, and comments with users - in second one.\nSee [SQLAlchemy docs](http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html)\nfor explaining relationship loading techniques.\n\n### Quick eager load\nFor simple cases, when you want to just \n[joinedload](http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#sqlalchemy.orm.joinedload)\nor [subqueryload](http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#sqlalchemy.orm.subqueryload) \na few relations, we have easier syntax for you:\n\n```python\nComment.with_joined('user', 'post', 'post.comments').first()\nUser.with_subquery('posts', 'posts.comments').all()\n```\n\n\u003e Note that you can split relations with dot like `post.comments`\n\u003e due to [this SQLAlchemy feature](http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#sqlalchemy.orm.subqueryload_all)\n\n\n## Filter and sort by relations\nprovided by [`SmartQueryMixin`](sqlalchemy_mixins/smart_query.py)\n\n### Django-like queries\nWe implement Django-like\n[field lookups](https://docs.djangoproject.com/en/1.10/topics/db/queries/#field-lookups)\nand\n[automatic relation joins](https://docs.djangoproject.com/en/1.10/topics/db/queries/#lookups-that-span-relationships).\n\nIt means you can **filter and sort dynamically by attributes defined in strings!**\n\nSo, having defined `Post` model with `Post.user` relationship to `User` model,\nyou can write\n```python\nPost.where(rating__gt=2, user___name__like='%Bi%').all() # post rating \u003e 2 and post user name like ...\nPost.sort('-rating', 'user___name').all() # sort by rating DESC, user name ASC\n```\n(`___` splits relation and attribute, `__` splits attribute and operator)\n\n\u003e If you need more flexibility, you can use low-level `filter_expr` method `session.query(Post).filter(*Post.filter_expr(rating__gt=2, body='text'))`, [see example](examples/smartquery.py#L232).\n\u003e\n\u003e It's like [`filter_by` in SQLALchemy](http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.filter_by), but also allows magic operators like `rating__gt`.\n\u003e\n\u003e Note: `filter_expr` method is very low-level and does NOT do magic Django-like joins. Use [`smart_query`](#all-in-one-smart_query) for that.\n\n\u003e **All relations used in filtering/sorting should be _explicitly set_, not just being a backref**\n\u003e\n\u003e In our example, `Post.user` relationship should be defined in `Post` class even if `User.posts` is defined too.\n\u003e\n\u003e So, you can't type\n\u003e ```python\n\u003e class User(BaseModel):\n\u003e     # ...\n\u003e     user = sa.orm.relationship('User', backref='posts')\n\u003e ```\n\u003e and skip defining `Post.user` relationship. You must define it anyway:\n\u003e\n\u003e ```python\n\u003e class Post(BaseModel):\n\u003e     # ...\n\u003e     user = sa.orm.relationship('User') # define it anyway\n\u003e ```\n\nFor DRY-ifying your code and incapsulating business logic, you can use\nSQLAlchemy's [hybrid attributes](http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html)\nand [hybrid_methods](http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html?highlight=hybrid_method#sqlalchemy.ext.hybrid.hybrid_method).\nUsing them in our filtering/sorting is straightforward (see examples and tests).\n\n### Automatic eager load relations\nWell, as [`SmartQueryMixin`](sqlalchemy_mixins/smart_query.py) does auto-joins for filtering/sorting,\nthere's a sense to tell sqlalchemy that we already joined that relation.\n\nSo that relations are automatically set to be joinedload if they were used for filtering/sorting.\n\n\nSo, if we write\n```python\ncomments = Comment.where(post___public=True, post___user___name__like='Bi%').all()\n```\nthen no additional query will be executed if we will access used relations\n```python\ncomments[0].post\ncomments[0].post.user\n```\n\n### All-in-one: smart_query\n#### Filter, sort and eager load in one smartest method.\nprovided by [`SmartQueryMixin`](sqlalchemy_mixins/smart_query.py)\n\nIn real world, we want to filter, sort and also eager load some relations at once.\nWell, if we use the same, say, `User.posts` relation in filtering and sorting,\nit **should not be joined twice**.\n\nThat's why we combined filter, sort and eager load in one smartest method:\n```python\nComment.smart_query(\n    filters={\n        'post___public': True,\n        'user__isnull': False\n    },\n    sort_attrs=['user___name', '-created_at'],\n    schema={\n        'post': {\n            'user': JOINED\n        }\n    }).all()\n```\n\n\n\nAs developers, we need to debug things with convenience.\nWhen we play in REPL, we can see this\n\n```\n\u003e\u003e\u003e session.query(Post).all()\n[\u003cmyapp.models.Post object at 0x04287A50\u003e, \u003cmyapp.models.Post object at 0x04287A90\u003e]\n```\n\nWell, using our mixin, we can have more readable output with post IDs:\n\n```\n\u003e\u003e\u003e session.query(Post).all()\n[\u003cPost #11\u003e, \u003cPost #12\u003e]\n```\n\nEven more, in `Post` model, we can define what else (except id) we want to see:\n\n```python\nclass User(BaseModel):\n    __repr_attrs__ = ['name']\n    # ...\n\n\nclass Post(BaseModel):\n    __repr_attrs__ = ['user', 'body'] # body is just column, user is relationship\n    # ...\n\n```\n\nNow we have\n```\n\u003e\u003e\u003e session.query(Post).all()\n[\u003cPost #11 user:\u003cUser #1 'Bill'\u003e body:'post 11'\u003e,\n \u003cPost #12 user:\u003cUser #2 'Bob'\u003e body:'post 12'\u003e]\n\n```\n\nAnd you can customize max `__repr__` length:\n```\nclass Post(BaseModel):\n    # ...\n    __repr_max_length__ = 25\n    # ...\n    \n\u003e\u003e\u003e long_post\n\u003cPost #2 body:'Post 2 long-long body' user:\u003cUser #1 'Bob'\u003e\u003e   \n```\n\n\n\n## DateMixin\nprovided by [`DateMixin`](sqlalchemy_mixins/date_mixin.py)\n\nYou can view the created and updated timestamps.\n\n```python\nbob = User(name=\"Bob\")\nsession.add(bob)\nsession.flush()\n\nprint(\"Created Bob:    \", bob.created_at)\n# Created Bob:     2019-03-04 03:53:53.606765\n\nprint(\"Pre-update Bob: \", bob.updated_at)\n# Pre-update Bob:  2019-03-04 03:53:53.606769\n\ntime.sleep(2)\n\nbob.name = \"Robert\"\nsession.commit()\n\nprint(\"Updated Bob:    \", bob.updated_at)\n# Updated Bob:     2019-03-04 03:53:58.613044\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblueromans%2Fsqlalchemymixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblueromans%2Fsqlalchemymixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblueromans%2Fsqlalchemymixin/lists"}