Skip to content

Commit 5b26f8e

Browse files
authored
feat: adds updated examples
feat: adds updated examples
2 parents 7a75400 + a94e0d4 commit 5b26f8e

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

examples/__init__.py

Whitespace-only changes.

examples/basic.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import msgspec
6+
from litestar import Controller, Litestar, get
7+
from litestar.exceptions import InternalServerException
8+
9+
from litestar_asyncpg import AsyncpgConfig, AsyncpgPlugin, PoolConfig
10+
11+
if TYPE_CHECKING:
12+
from asyncpg import Connection
13+
14+
15+
class PostgresHealthCheck(msgspec.Struct):
16+
"""A new type describing a User"""
17+
18+
version: str
19+
uptime: float
20+
21+
22+
class SampleController(Controller):
23+
@get(path="/sample")
24+
async def sample_route(self, db_connection: Connection) -> PostgresHealthCheck:
25+
"""Check database available and returns app config info."""
26+
result = await db_connection.fetchrow(
27+
"select version() as version, extract(epoch from current_timestamp - pg_postmaster_start_time()) as uptime",
28+
)
29+
if result:
30+
return PostgresHealthCheck(version=result["version"], uptime=result["uptime"])
31+
raise InternalServerException
32+
33+
34+
asyncpg = AsyncpgPlugin(config=AsyncpgConfig(pool_config=PoolConfig(dsn="postgresql://app:app@localhost:5423/app")))
35+
app = Litestar(plugins=[asyncpg], route_handlers=[SampleController])

tests/docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ services:
66
ports:
77
- "5423:5432" # use a non-standard port here
88
environment:
9-
POSTGRES_PASSWORD: super-secret
9+
POSTGRES_PASSWORD: "app"
10+
POSTGRES_USER: "app"
11+
POSTGRES_DB: "app"

0 commit comments

Comments
 (0)