Skip to content

Commit

Permalink
feat: adds get_connection methods (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin authored Aug 30, 2024
1 parent b94a318 commit 0620dbb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ license = { text = "MIT" }
name = "litestar-oracledb"
readme = "README.md"
requires-python = ">=3.8"
version = "0.1.2"
version = "0.1.3"

[project.urls]
Changelog = "https://litestar-org.github.io/litesatr-oracledb/latest/changelog"
Expand Down
13 changes: 13 additions & 0 deletions src/litestar_oracledb/config/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,16 @@ async def provide_connection(
async with pool.acquire() as connection:
set_scope_state(scope, self.connection_scope_key, connection)
yield connection

@asynccontextmanager
async def get_connection(
self,
) -> AsyncGenerator[AsyncConnection, None]:
"""Create a connection instance.
Returns:
A connection instance.
"""
pool = await self.create_pool()
async with pool.acquire() as connection:
yield connection
17 changes: 15 additions & 2 deletions src/litestar_oracledb/config/_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from contextlib import asynccontextmanager
from contextlib import asynccontextmanager, contextmanager
from dataclasses import dataclass
from typing import TYPE_CHECKING, Generator, Optional, cast

Expand Down Expand Up @@ -193,7 +193,7 @@ async def lifespan(
try:
yield
finally:
db_pool.close(force=True)
db_pool.close()

def provide_connection(
self,
Expand All @@ -219,3 +219,16 @@ def provide_connection(
with pool.acquire() as connection:
set_scope_state(scope, self.connection_scope_key, connection)
yield connection

@contextmanager
def get_connection(
self,
) -> Generator[Connection, None, None]:
"""Create a connection instance.
Returns:
A connection instance.
"""
pool = self.create_pool()
with pool.acquire() as connection:
yield connection

0 comments on commit 0620dbb

Please sign in to comment.