Skip to content

Commit

Permalink
chore: update to bento_lib 10.1 + factor out new shared stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Nov 21, 2023
1 parent 0ddce84 commit 4afb422
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 52 deletions.
3 changes: 3 additions & 0 deletions bento_reference_service/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

from bento_lib.logging import LogLevelLiteral
from fastapi import Depends
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -37,6 +38,8 @@ class Config(BaseSettings):
service_description: str = "Reference data (genomes & annotations) service for the Bento platform."
service_url_base_path: str = "http://127.0.0.1:5000" # Base path to construct URIs from

log_level: LogLevelLiteral = "debug"

database_uri: str = "postgres://localhost:5432"
data_path: Path = Path(__file__).parent / "data"

Expand Down
51 changes: 4 additions & 47 deletions bento_reference_service/db.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
import aiofiles
import asyncpg
import contextlib

from bento_lib.db.pg_async import PgAsyncDatabase
from fastapi import Depends
from functools import lru_cache
from pathlib import Path
from typing import Annotated, AsyncGenerator
from typing import Annotated

from .config import ConfigDependency


SCHEMA_PATH = Path(__file__).parent / "sql" / "schema.sql"


class Database:
def __init__(self, db_uri: str, schema_path: Path):
self._db_uri: str = db_uri
self._schema_path: Path = schema_path

self._pool: asyncpg.Pool | None = None

async def initialize(self, pool_size: int = 10):
conn: asyncpg.Connection

if not self._pool: # Initialize the connection pool if needed
self._pool = await asyncpg.create_pool(self._db_uri, min_size=pool_size, max_size=pool_size)

# Connect to the database and execute the schema script
async with aiofiles.open(self._schema_path, "r") as sf:
async with self.connect() as conn:
async with conn.transaction():
await conn.execute(await sf.read())

async def close(self):
if self._pool:
await self._pool.close()
self._pool = None

@contextlib.asynccontextmanager
async def connect(
self,
existing_conn: asyncpg.Connection | None = None,
) -> AsyncGenerator[asyncpg.Connection, None]:
# TODO: raise raise DatabaseError("Pool is not available") when FastAPI has lifespan dependencies
# + manage pool lifespan in lifespan fn.

if self._pool is None:
await self.initialize() # initialize if this is the first time we're using the pool

if existing_conn is not None:
yield existing_conn
return

conn: asyncpg.Connection
async with self._pool.acquire() as conn:
yield conn
class Database(PgAsyncDatabase):
pass # TODO


@lru_cache()
Expand Down
10 changes: 6 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
python = "^3.10.0"
fastapi = "^0.104.1"
pydantic = "^2.5.1"
bento-lib = {extras = ["fastapi"], version = "^10.0.0"}
bento-lib = {extras = ["asyncpg", "fastapi"], version = "^10.1.0"}
aiofiles = "^23.2.1"
pysam = "^0.22.0"
elasticsearch = {extras = ["async"], version = "^8.6.0"}
Expand Down

0 comments on commit 4afb422

Please sign in to comment.