mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-29 01:00:51 -05:00
* 🌐 Refactor file structure to support internationalization * ✅ Update tests changed after i18n * 🔀 Merge Typer style from master * 🔧 Update MkConfig with Typer-styles * 🎨 Format mkdocs.yml with cannonical form * 🎨 Format mkdocs.yml * 🔧 Update MkDocs config * ➕ Add docs translation scripts dependencies * ✨ Add Typer scripts to handle translations * ✨ Add missing translation snippet to include * ✨ Update contributing docs, add docs for translations * 🙈 Add docs_build to gitignore * 🔧 Update scripts with new locations and docs scripts * 👷 Update docs deploy action with translations * 📝 Add note about languages not supported in the theme * ✨ Add first translation, for Spanish
66 lines
1.4 KiB
Python
66 lines
1.4 KiB
Python
from typing import List
|
|
|
|
import databases
|
|
import sqlalchemy
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
|
|
# SQLAlchemy specific code, as with any other app
|
|
DATABASE_URL = "sqlite:///./test.db"
|
|
# DATABASE_URL = "postgresql://user:password@postgresserver/db"
|
|
|
|
database = databases.Database(DATABASE_URL)
|
|
|
|
metadata = sqlalchemy.MetaData()
|
|
|
|
notes = sqlalchemy.Table(
|
|
"notes",
|
|
metadata,
|
|
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
|
|
sqlalchemy.Column("text", sqlalchemy.String),
|
|
sqlalchemy.Column("completed", sqlalchemy.Boolean),
|
|
)
|
|
|
|
|
|
engine = sqlalchemy.create_engine(
|
|
DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
metadata.create_all(engine)
|
|
|
|
|
|
class NoteIn(BaseModel):
|
|
text: str
|
|
completed: bool
|
|
|
|
|
|
class Note(BaseModel):
|
|
id: int
|
|
text: str
|
|
completed: bool
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
await database.connect()
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown():
|
|
await database.disconnect()
|
|
|
|
|
|
@app.get("/notes/", response_model=List[Note])
|
|
async def read_notes():
|
|
query = notes.select()
|
|
return await database.fetch_all(query)
|
|
|
|
|
|
@app.post("/notes/", response_model=Note)
|
|
async def create_note(note: NoteIn):
|
|
query = notes.insert().values(text=note.text, completed=note.completed)
|
|
last_record_id = await database.execute(query)
|
|
return {**note.dict(), "id": last_record_id}
|