/ justapi / examples / 09_db_integration.py
swadhinbiswas docs(examples): add 10 progressive tutorial examples and test project · 7/9/2026
b48e461
32 lines · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import asyncio
from justapi import JustAPIApp
from pydantic import BaseModel

app = JustAPIApp()

# Mock database
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]

class Item(BaseModel):
    item_name: str

@app.post("/items/")
async def create_item(item: Item):
    """
    Simulates inserting a record into a database asynchronously.
    """
    await asyncio.sleep(0.5) # Simulate DB latency
    fake_items_db.append(item.model_dump())
    return item

@app.get("/items/")
async def read_items(skip: int = 0, limit: int = 10):
    """
    Simulates querying a database.
    """
    await asyncio.sleep(0.1) # Simulate DB latency
    return fake_items_db[skip : skip + limit]

if __name__ == "__main__":
    app.run("127.0.0.1:8000")