diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | python-fastapi-async-sqlalchemy.spec | 326 | ||||
-rw-r--r-- | sources | 1 |
3 files changed, 328 insertions, 0 deletions
@@ -0,0 +1 @@ +/fastapi-async-sqlalchemy-0.4.1.tar.gz diff --git a/python-fastapi-async-sqlalchemy.spec b/python-fastapi-async-sqlalchemy.spec new file mode 100644 index 0000000..15c0e7f --- /dev/null +++ b/python-fastapi-async-sqlalchemy.spec @@ -0,0 +1,326 @@ +%global _empty_manifest_terminate_build 0 +Name: python-fastapi-async-sqlalchemy +Version: 0.4.1 +Release: 1 +Summary: SQLAlchemy middleware for FastAPI +License: MIT +URL: https://github.com/h0rn3t/fastapi-async-sqlalchemy.git +Source0: https://mirrors.aliyun.com/pypi/web/packages/7e/59/3273c6fb5b12c73635c2b000b751147aad23c853b3b4db68047a00214d81/fastapi-async-sqlalchemy-0.4.1.tar.gz +BuildArch: noarch + +Requires: python3-starlette +Requires: python3-SQLAlchemy + +%description +# SQLAlchemy FastAPI middleware + +[](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB) +[](https://github.com/h0rn3t/fastapi-async-sqlalchemy/workflows/ci/badge.svg) +[](https://codecov.io/gh/h0rn3t/fastapi-async-sqlalchemy) +[](https://opensource.org/licenses/MIT) +[](https://pypi.org/project/fastapi-async-sqlalchemy/) +[](https://pepy.tech/project/fastapi-async-sqlalchemy) +[](https://pyup.io/repos/github/h0rn3t/fastapi-async-sqlalchemy/) + +### Description + +Provides SQLAlchemy middleware for FastAPI using AsyncSession and async engine. + +### Install + +```bash + pip install fastapi-async-sqlalchemy +``` + + +### Examples + +Note that the session object provided by ``db.session`` is based on the Python3.7+ ``ContextVar``. This means that +each session is linked to the individual request context in which it was created. + +```python + +from fastapi import FastAPI +from fastapi_async_sqlalchemy import SQLAlchemyMiddleware +from fastapi_async_sqlalchemy import db # provide access to a database session +from sqlalchemy import column +from sqlalchemy import table + +app = FastAPI() +app.add_middleware( + SQLAlchemyMiddleware, + db_url="postgresql+asyncpg://user:user@192.168.88.200:5432/primary_db", + engine_args={ # engine arguments example + "echo": True, # print all SQL statements + "pool_pre_ping": True, # feature will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool + "pool_size": 5, # number of connections to keep open at a time + "max_overflow": 10, # number of connections to allow to be opened above pool_size + "connect_args": { + "prepared_statement_cache_size": 0, # disable prepared statement cache + "statement_cache_size": 0, # disable statement cache + }, + }, +) +# once the middleware is applied, any route can then access the database session +# from the global ``db`` + +foo = table("ms_files", column("id")) + +# Usage inside of a route +@app.get("/") +async def get_files(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +async def get_db_fetch(): + # It uses the same ``db`` object and use it as a context manager: + async with db(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +# Usage inside of a route using a db context +@app.get("/db_context") +async def db_context(): + return await get_db_fetch() + +# Usage outside of a route using a db context +@app.on_event("startup") +async def on_startup(): + # We are outside of a request context, therefore we cannot rely on ``SQLAlchemyMiddleware`` + # to create a database session for us. + result = await get_db_fetch() + + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8002) + +``` + + +%package -n python3-fastapi-async-sqlalchemy +Summary: SQLAlchemy middleware for FastAPI +Provides: python-fastapi-async-sqlalchemy +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-fastapi-async-sqlalchemy +# SQLAlchemy FastAPI middleware + +[](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB) +[](https://github.com/h0rn3t/fastapi-async-sqlalchemy/workflows/ci/badge.svg) +[](https://codecov.io/gh/h0rn3t/fastapi-async-sqlalchemy) +[](https://opensource.org/licenses/MIT) +[](https://pypi.org/project/fastapi-async-sqlalchemy/) +[](https://pepy.tech/project/fastapi-async-sqlalchemy) +[](https://pyup.io/repos/github/h0rn3t/fastapi-async-sqlalchemy/) + +### Description + +Provides SQLAlchemy middleware for FastAPI using AsyncSession and async engine. + +### Install + +```bash + pip install fastapi-async-sqlalchemy +``` + + +### Examples + +Note that the session object provided by ``db.session`` is based on the Python3.7+ ``ContextVar``. This means that +each session is linked to the individual request context in which it was created. + +```python + +from fastapi import FastAPI +from fastapi_async_sqlalchemy import SQLAlchemyMiddleware +from fastapi_async_sqlalchemy import db # provide access to a database session +from sqlalchemy import column +from sqlalchemy import table + +app = FastAPI() +app.add_middleware( + SQLAlchemyMiddleware, + db_url="postgresql+asyncpg://user:user@192.168.88.200:5432/primary_db", + engine_args={ # engine arguments example + "echo": True, # print all SQL statements + "pool_pre_ping": True, # feature will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool + "pool_size": 5, # number of connections to keep open at a time + "max_overflow": 10, # number of connections to allow to be opened above pool_size + "connect_args": { + "prepared_statement_cache_size": 0, # disable prepared statement cache + "statement_cache_size": 0, # disable statement cache + }, + }, +) +# once the middleware is applied, any route can then access the database session +# from the global ``db`` + +foo = table("ms_files", column("id")) + +# Usage inside of a route +@app.get("/") +async def get_files(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +async def get_db_fetch(): + # It uses the same ``db`` object and use it as a context manager: + async with db(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +# Usage inside of a route using a db context +@app.get("/db_context") +async def db_context(): + return await get_db_fetch() + +# Usage outside of a route using a db context +@app.on_event("startup") +async def on_startup(): + # We are outside of a request context, therefore we cannot rely on ``SQLAlchemyMiddleware`` + # to create a database session for us. + result = await get_db_fetch() + + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8002) + +``` + + +%package help +Summary: Development documents and examples for fastapi-async-sqlalchemy +Provides: python3-fastapi-async-sqlalchemy-doc +%description help +# SQLAlchemy FastAPI middleware + +[](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB) +[](https://github.com/h0rn3t/fastapi-async-sqlalchemy/workflows/ci/badge.svg) +[](https://codecov.io/gh/h0rn3t/fastapi-async-sqlalchemy) +[](https://opensource.org/licenses/MIT) +[](https://pypi.org/project/fastapi-async-sqlalchemy/) +[](https://pepy.tech/project/fastapi-async-sqlalchemy) +[](https://pyup.io/repos/github/h0rn3t/fastapi-async-sqlalchemy/) + +### Description + +Provides SQLAlchemy middleware for FastAPI using AsyncSession and async engine. + +### Install + +```bash + pip install fastapi-async-sqlalchemy +``` + + +### Examples + +Note that the session object provided by ``db.session`` is based on the Python3.7+ ``ContextVar``. This means that +each session is linked to the individual request context in which it was created. + +```python + +from fastapi import FastAPI +from fastapi_async_sqlalchemy import SQLAlchemyMiddleware +from fastapi_async_sqlalchemy import db # provide access to a database session +from sqlalchemy import column +from sqlalchemy import table + +app = FastAPI() +app.add_middleware( + SQLAlchemyMiddleware, + db_url="postgresql+asyncpg://user:user@192.168.88.200:5432/primary_db", + engine_args={ # engine arguments example + "echo": True, # print all SQL statements + "pool_pre_ping": True, # feature will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool + "pool_size": 5, # number of connections to keep open at a time + "max_overflow": 10, # number of connections to allow to be opened above pool_size + "connect_args": { + "prepared_statement_cache_size": 0, # disable prepared statement cache + "statement_cache_size": 0, # disable statement cache + }, + }, +) +# once the middleware is applied, any route can then access the database session +# from the global ``db`` + +foo = table("ms_files", column("id")) + +# Usage inside of a route +@app.get("/") +async def get_files(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +async def get_db_fetch(): + # It uses the same ``db`` object and use it as a context manager: + async with db(): + result = await db.session.execute(foo.select()) + return result.fetchall() + +# Usage inside of a route using a db context +@app.get("/db_context") +async def db_context(): + return await get_db_fetch() + +# Usage outside of a route using a db context +@app.on_event("startup") +async def on_startup(): + # We are outside of a request context, therefore we cannot rely on ``SQLAlchemyMiddleware`` + # to create a database session for us. + result = await get_db_fetch() + + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8002) + +``` + + +%prep +%autosetup -n fastapi-async-sqlalchemy-0.4.1 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-fastapi-async-sqlalchemy -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 0.4.1-1 +- Package Spec generated @@ -0,0 +1 @@ +8230f4a492e3506d3fe55e0696ea50d6 fastapi-async-sqlalchemy-0.4.1.tar.gz |