fastapi-sqla 3.2.1__py3-none-any.whl → 3.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fastapi_sqla/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi_sqla.base import setup
1
+ from fastapi_sqla.base import setup, setup_middlewares, startup
2
2
  from fastapi_sqla.models import Collection, Item, Page
3
3
  from fastapi_sqla.pagination import Paginate, PaginateSignature, Pagination
4
4
  from fastapi_sqla.sqla import (
@@ -22,6 +22,8 @@ __all__ = [
22
22
  "SqlaSession",
23
23
  "open_session",
24
24
  "setup",
25
+ "setup_middlewares",
26
+ "startup",
25
27
  ]
26
28
 
27
29
 
fastapi_sqla/base.py CHANGED
@@ -2,6 +2,7 @@ import functools
2
2
  import os
3
3
  import re
4
4
 
5
+ from deprecated import deprecated
5
6
  from fastapi import FastAPI
6
7
  from sqlalchemy.engine import Engine
7
8
 
@@ -20,6 +21,33 @@ except ImportError as err: # pragma: no cover
20
21
  _ENGINE_KEYS_REGEX = re.compile(r"fastapi_sqla__(?!_)(.+)(?<!_)__(?!_).+")
21
22
 
22
23
 
24
+ async def startup():
25
+ engine_keys = _get_engine_keys()
26
+ engines = {key: sqla.new_engine(key) for key in engine_keys}
27
+ for key, engine in engines.items():
28
+ if not _is_async_dialect(engine):
29
+ sqla.startup(key=key)
30
+ else:
31
+ await async_sqla.startup(key=key)
32
+
33
+
34
+ def setup_middlewares(app: FastAPI):
35
+ engine_keys = _get_engine_keys()
36
+ engines = {key: sqla.new_engine(key) for key in engine_keys}
37
+ for key, engine in engines.items():
38
+ if not _is_async_dialect(engine):
39
+ app.middleware("http")(
40
+ functools.partial(sqla.add_session_to_request, key=key)
41
+ )
42
+ else:
43
+ app.middleware("http")(
44
+ functools.partial(async_sqla.add_session_to_request, key=key)
45
+ )
46
+
47
+
48
+ @deprecated(
49
+ reason="FastAPI events are deprecated. This function will be remove in the upcoming major release." # noqa: E501
50
+ )
23
51
  def setup(app: FastAPI):
24
52
  engine_keys = _get_engine_keys()
25
53
  engines = {key: sqla.new_engine(key) for key in engine_keys}
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-sqla
3
- Version: 3.2.1
3
+ Version: 3.3.1
4
4
  Summary: SQLAlchemy extension for FastAPI with support for pagination, asyncio, SQLModel, and pytest, ready for production.
5
5
  Home-page: https://github.com/dialoguemd/fastapi-sqla
6
6
  License: MIT
7
7
  Keywords: FastAPI,SQLAlchemy,asyncio,pytest,alembic
8
8
  Author: Hadrien David
9
9
  Author-email: hadrien.david@dialogue.co
10
- Requires-Python: >=3.9,<4.0
10
+ Requires-Python: >=3.9,<3.13
11
11
  Classifier: Development Status :: 5 - Production/Stable
12
12
  Classifier: Environment :: Web Environment
13
13
  Classifier: Framework :: AsyncIO
@@ -35,28 +35,19 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
35
35
  Classifier: Typing :: Typed
36
36
  Provides-Extra: asyncpg
37
37
  Provides-Extra: aws-rds-iam
38
+ Provides-Extra: psycopg2
39
+ Provides-Extra: pytest-plugin
38
40
  Provides-Extra: sqlmodel
39
- Provides-Extra: tests
40
- Requires-Dist: Faker (>=14.2.0,<15.0.0) ; extra == "tests"
41
- Requires-Dist: alembic (>=1.4.3,<2.0.0) ; extra == "tests"
42
- Requires-Dist: asgi_lifespan (>=1.0.1,<2.0.0) ; extra == "tests"
41
+ Requires-Dist: alembic (>=1.4.3,<2.0.0) ; extra == "pytest-plugin"
43
42
  Requires-Dist: asyncpg (>=0.28.0,<0.29.0) ; extra == "asyncpg"
44
43
  Requires-Dist: boto3 (>=1.24.74,<2.0.0) ; extra == "aws-rds-iam"
45
- Requires-Dist: fastapi (>=0.95.1)
46
- Requires-Dist: greenlet (>=3.0.3,<4.0.0) ; extra == "tests"
47
- Requires-Dist: httpx (>=0.23.0,<0.24.0) ; extra == "tests"
48
- Requires-Dist: mypy[tests] (>=1.0.0,<2.0.0) ; extra == "tests"
49
- Requires-Dist: pdbpp (>=0.10.2,<0.11.0) ; extra == "tests"
50
- Requires-Dist: psycopg2 (>=2.8.6,<3.0.0) ; extra == "tests"
51
- Requires-Dist: pydantic (>=1)
52
- Requires-Dist: pytest (>=7.2.1,<8.0.0) ; extra == "tests"
53
- Requires-Dist: pytest-asyncio (>=0.19.0,<0.20.0) ; extra == "tests"
54
- Requires-Dist: pytest-cov (>=2.10.1,<3.0.0) ; extra == "tests"
55
- Requires-Dist: ruff (>=0.4.5,<0.5.0) ; extra == "tests"
56
- Requires-Dist: sqlalchemy (>=1.3)
44
+ Requires-Dist: deprecated (>=1.2,<2.0)
45
+ Requires-Dist: fastapi (>=0.95.1,<0.112)
46
+ Requires-Dist: psycopg2 (>=2.8.6,<3.0.0) ; extra == "psycopg2"
47
+ Requires-Dist: pydantic (>=1,<3)
48
+ Requires-Dist: sqlalchemy (>=1.3,<3)
57
49
  Requires-Dist: sqlmodel (>=0.0.14,<0.0.15) ; extra == "sqlmodel"
58
- Requires-Dist: structlog (>=20)
59
- Requires-Dist: tox (>=3.26.0,<4.0.0) ; extra == "tests"
50
+ Requires-Dist: structlog (>=20,<25)
60
51
  Project-URL: Repository, https://github.com/dialoguemd/fastapi-sqla
61
52
  Description-Content-Type: text/markdown
62
53
 
@@ -82,6 +73,8 @@ Using [pip](https://pip.pypa.io/):
82
73
  pip install fastapi-sqla
83
74
  ```
84
75
 
76
+ Note that you need a [SQLAlchemy compatible engine](https://docs.sqlalchemy.org/en/20/core/engines.html) adapter. We test with `psycopg2` which you can install using the `psycopg2` extra.
77
+
85
78
  # Quick Example
86
79
 
87
80
  Assuming it runs against a DB with a table `user` with 3 columns, `id`, `name` and
@@ -89,15 +82,22 @@ unique `email`:
89
82
 
90
83
  ```python
91
84
  # main.py
85
+ from contextlib import asynccontextmanager
92
86
  from fastapi import FastAPI, HTTPException
93
- from fastapi_sqla import Base, Item, Page, Paginate, Session, setup
87
+ from fastapi_sqla import Base, Item, Page, Paginate, Session, setup_middlewares, startup
94
88
  from pydantic import BaseModel, EmailStr
95
89
  from sqlalchemy import select
96
90
  from sqlalchemy.exc import IntegrityError
97
91
 
98
- app = FastAPI()
99
92
 
100
- setup(app)
93
+ @asynccontextmanager
94
+ async def lifespan(app: FastAPI):
95
+ await startup()
96
+ yield
97
+
98
+
99
+ app = FastAPI(lifespan=lifespan)
100
+ setup_middlewares(app)
101
101
 
102
102
 
103
103
  class User(Base):
@@ -202,7 +202,23 @@ And define the environment variable `sqlalchemy_url` with `postgres+asyncpg` sch
202
202
  export sqlalchemy_url=postgresql+asyncpg://postgres@localhost
203
203
  ```
204
204
 
205
- ## Setup the app:
205
+ ## Setup the app AsyncContextManager (recommended):
206
+
207
+ ```python
208
+ import fastapi_sqla
209
+ from fastapi import FastAPI
210
+
211
+ @asynccontextmanager
212
+ async def lifespan(app: FastAPI):
213
+ await fastapi_sqla.startup()
214
+ yield
215
+
216
+
217
+ app = FastAPI(lifespan=lifespan)
218
+ fastapi_sqla.setup_middlewares(app)
219
+ ```
220
+
221
+ ## Setup the app using startup/shutdown events (deprecated):
206
222
 
207
223
  ```python
208
224
  import fastapi_sqla
@@ -639,7 +655,7 @@ If your project uses [SQLModel], then `Session` dependency is an SQLModel sessio
639
655
  # Pytest fixtures
640
656
 
641
657
  This library provides a set of utility fixtures, through its PyTest plugin, which is
642
- automatically installed with the library.
658
+ automatically installed with the library. Using the plugin requires the `pytest_plugin` extra.
643
659
 
644
660
  By default, no records are actually written to the database when running tests.
645
661
  There currently is no way to change this behaviour.
@@ -783,7 +799,7 @@ It returns the path of `alembic.ini` configuration file. By default, it returns
783
799
  ## Setup
784
800
 
785
801
  ```bash
786
- $ poetry install --extras tests --extras asyncpg --extras aws_rds_iam
802
+ $ poetry install --all-extras
787
803
  ```
788
804
 
789
805
  ## Running tests
@@ -1,16 +1,16 @@
1
- fastapi_sqla/__init__.py,sha256=G9bKR6L9EnLgzelE_BLnHgc1cOm0Yu0wMzwLAdyA8iE,1153
1
+ fastapi_sqla/__init__.py,sha256=RRkwo9xZzidQ-k3BRXfqT0jtWm8d08w94XuOLteFCdU,1221
2
2
  fastapi_sqla/_pytest_plugin.py,sha256=IQUlj-O874Sfuth254LBrEBGCrwH3rNHST9OMZl2pIk,5411
3
3
  fastapi_sqla/async_pagination.py,sha256=3DHGUjvrpkbWMIc_BEX4GvM-_PTcn62K9z48ucTJlH0,3164
4
4
  fastapi_sqla/async_sqla.py,sha256=7SXRH3DMsQvpjCQdvCBEjQhDZ4-cPAg175MlgNVBnCg,5888
5
5
  fastapi_sqla/aws_aurora_support.py,sha256=4dxLKOqDccgLwFqlz81L6f4HzrOXMZkY7Zuf4t_310U,838
6
6
  fastapi_sqla/aws_rds_iam_support.py,sha256=Uw-XaiwShMMWYKCvlSqXoxvtKMblCAvbCZ1m6BYVpJk,1257
7
- fastapi_sqla/base.py,sha256=ssgt-EU7KrRg3IfvXoEZdCsDE4uQVhOVTFuamMmw-Kk,1546
7
+ fastapi_sqla/base.py,sha256=8XHIKO8sBOmnRvCsOYRWhg5Y-XYqobM5DypeIMvJTFs,2501
8
8
  fastapi_sqla/models.py,sha256=-B1xwINpTc9rEQd3KYHEC1s5s7jdVQkJ6Gy6xpmT13c,1108
9
9
  fastapi_sqla/pagination.py,sha256=1gfIGcmt1OFspbRgtJ8AZOZdFd14DGRc4FkDgyh5bJ8,4517
10
10
  fastapi_sqla/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  fastapi_sqla/sqla.py,sha256=irHuZaun027o9WJrdcZPavCN2vXNHupCWYhU5Dx-AqE,6348
12
- fastapi_sqla-3.2.1.dist-info/LICENSE,sha256=8G0-nWLqi3xRYRrtRlTE8n1mkYJcnCRoZGUhv6ZE29c,1064
13
- fastapi_sqla-3.2.1.dist-info/METADATA,sha256=48-YxU0G8S7GR5zDMrLLgRY5LyMA7v0ku29SiEnqihA,20848
14
- fastapi_sqla-3.2.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
15
- fastapi_sqla-3.2.1.dist-info/entry_points.txt,sha256=haa0EueKcRo8-AlJTpHBMn08wMBiULNGA53nkvaDWj0,53
16
- fastapi_sqla-3.2.1.dist-info/RECORD,,
12
+ fastapi_sqla-3.3.1.dist-info/LICENSE,sha256=8G0-nWLqi3xRYRrtRlTE8n1mkYJcnCRoZGUhv6ZE29c,1064
13
+ fastapi_sqla-3.3.1.dist-info/METADATA,sha256=fBq_qZ1JGhyzpjZGr30nYp4tgFNI4QvPIY26WvPdRi8,20994
14
+ fastapi_sqla-3.3.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
15
+ fastapi_sqla-3.3.1.dist-info/entry_points.txt,sha256=haa0EueKcRo8-AlJTpHBMn08wMBiULNGA53nkvaDWj0,53
16
+ fastapi_sqla-3.3.1.dist-info/RECORD,,