Skip to content

Commit

Permalink
feat(sqlalchemy): use lambda_stmt in the repository (#2179)
Browse files Browse the repository at this point in the history
* feat: use `lambda_stmt` in the repository

* fix: additional tests are now passing

* fix: non-mocked tests are now passing

* fix: fix updated remaining tests for lambda_stmt

* feat: use `lambda_stmt` in the repository (Sourcery refactored) (#2180)

'Refactored by Sourcery'

Co-authored-by: Sourcery AI <>

* docs: update

* fix: updated syntax

* fix: remove debug print

* fix: cleanup error handling and remove 23c for now

* fix: correctly call method

* chore: pre-commit linting

* docs: typo correction

* docs: adjusted information on SQL Alchemy field types

* Update litestar/contrib/sqlalchemy/repository/_async.py

Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>

* fix: updated fx call

* feat: make delete_many a lambda_stmt

* feat: lambdafiy update many

* fix: remove `self` reference from lambda

* fix: re-add `update` statement

* fix: revert signature change

* chore: linting

* feat(sqlalchemy): adds override for forcing basic paginated query mode (#2200)

feat: adds override for forcing basic paginated query mode

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>
  • Loading branch information
3 people authored Aug 23, 2023
1 parent 8494f33 commit 43d052f
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 185 deletions.
8 changes: 6 additions & 2 deletions docs/usage/databases/sqlalchemy/models_and_repository.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Features
a `sentinel column <https://docs.sqlalchemy.org/en/20/core/connections.html#configuring-sentinel-columns>`_, and
an optional version with audit columns.
* Generic synchronous and asynchronous repositories for select, insert, update, and delete operations on SQLAlchemy models
* Implements optimized methods for bulk inserts, updates, and deletes.
* Implements optimized methods for bulk inserts, updates, and deletes and uses `lambda_stmt <https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.lambda_stmt>`_ when possible.
* Integrated counts, pagination, sorting, filtering with ``LIKE``, ``IN``, and dates before and/or after.
* Tested support for multiple database backends including:

Expand All @@ -37,6 +37,9 @@ implementations:
Both include a ``UUID`` based primary key
and ``UUIDAuditBase`` includes an ``updated`` and ``created`` timestamp column.

The ``UUID`` will be a native ``UUID``/``GUID`` type on databases that support it such as Postgres. For other engines without
a native UUID data type, the UUID is stored as a 16-byte ``BYTES`` or ``RAW`` field.

* :class:`BigIntBase <litestar.contrib.sqlalchemy.base.BigIntBase>`
* :class:`BigIntAuditBase <litestar.contrib.sqlalchemy.base.BigIntAuditBase>`

Expand All @@ -47,7 +50,8 @@ Models using these bases also include the following enhancements:

* Auto-generated snake-case table name from class name
* Pydantic BaseModel and Dict classes map to an optimized JSON type that is
:class:`JSONB <sqlalchemy.dialects.postgresql.JSONB>` for the Postgres and
:class:`JSONB <sqlalchemy.dialects.postgresql.JSONB>` for Postgres,
`VARCHAR` or `BYTES` with JSON check constraint for Oracle, and
:class:`JSON <sqlalchemy.types.JSON>` for other dialects.

.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py
Expand Down
5 changes: 3 additions & 2 deletions litestar/contrib/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

if TYPE_CHECKING:
from sqlalchemy.sql import FromClause
from sqlalchemy.sql.schema import _NamingSchemaParameter as NamingSchemaParameter

__all__ = (
"AuditColumns",
Expand All @@ -42,7 +43,7 @@
UUIDBaseT = TypeVar("UUIDBaseT", bound="UUIDBase")
BigIntBaseT = TypeVar("BigIntBaseT", bound="BigIntBase")

convention = {
convention: NamingSchemaParameter = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
Expand Down Expand Up @@ -151,7 +152,7 @@ def to_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:

def create_registry() -> registry:
"""Create a new SQLAlchemy registry."""
meta = MetaData(naming_convention=convention) # type: ignore[arg-type]
meta = MetaData(naming_convention=convention)
return registry(
metadata=meta,
type_annotation_map={
Expand Down
Loading

0 comments on commit 43d052f

Please sign in to comment.