Helpers for using the cx_Oracle_async library with the FastAPI framework.
Project description
fastapi-oracle
Helpers for using the cx_Oracle_async
library with the FastAPI framework.
Getting started
- Install a recent Python 3.x version (if you don't already have one).
- Install the Oracle client libraries (if you don't already have them), they are available for most systems on the official Oracle Instant Client Downloads page.
- Create a project that uses
cx_Oracle_async
and FastAPI (if you don't already have one). - Install
fastapi-oracle
as a dependency using Poetry, pip, or similar:poetry add fastapi-oracle
- Set DB config in environment variables:
DB_HOST=foodbhost.aintitfootastic.com DB_PORT=1521 DB_USER=foouser DB_PASSWORD=foopass DB_SERVICE_NAME=foodb1.aintitfootastic.com
- Use the utils:
from collections.abc import AsyncIterable, Mapping from typing import Any, AsyncGenerator from fastapi import APIRouter, Depends, FastAPI from fastapi_oracle import ( DbPoolConnAndCursor, close_db_pools, cursor_rows_as_dicts, cursor_rows_as_gen, get_db_cursor, result_keys_to_lower, ) from pydantic import BaseModel router = APIRouter() class Foo(BaseModel): id: int name: str async def map_list_foos_result_to_foos( result: AsyncIterable[Mapping[str, Any]] ) -> AsyncGenerator[Foo, None]: """Map a list foos DB result to a list of foos.""" async for row in result: yield Foo(**row) async def list_foos_query( db: DbPoolConnAndCursor ) -> AsyncGenerator[dict[str, Any], None]: """List all foos.""" await db.cursor.execute("SELECT id, name FROM foo") cursor_rows_as_dicts(db.cursor) return ( row2 async for row2 in result_keys_to_lower( row1 async for row1 in cursor_rows_as_gen(db.cursor) ) ) @router.get("/", response_model=list[Foo]) async def list_foos(db: DbPoolConnAndCursor = Depends(get_db_cursor)): result = await list_foos_query(db) foos = [x async for x in map_list_foos_result_to_foos(result)] return foos def get_app() -> FastAPI: """Create a FastAPI app instance.""" app = FastAPI(on_shutdown=[close_db_pools]) app.include_router(router) return app
Developing
To clone the repo:
git clone git@github.com:Jaza/fastapi-oracle.git
To automatically fix code style issues:
./scripts/format.sh
To verify code style and static typing:
./scripts/verify.sh
To run tests:
./scripts/test.sh
To run tests that need to connect to a real database:
./scripts/testdb.sh
Building
To build the library:
poetry build
Built by Seertech.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for fastapi_oracle-0.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1affed682b7db091dc8bb748e35ab5f8c8017e5255830123b8b3cbef3aadf14f |
|
MD5 | 02361767ab45dc58ee6cb6ca5068e1a5 |
|
BLAKE2b-256 | f930658f10100c44e5a8210bf2f588abfb3f89df6adefddf36938e34c914de93 |