๐ ๐ (๐) ๐ฝ¶
๐ ๐ช โ๏ธ encode/databases
โฎ๏ธ FastAPI ๐ ๐ฝ โ๏ธ async
& await
.
โซ๏ธ ๐ โฎ๏ธ:
- โณ
- โณ
- ๐
๐ ๐ผ, ๐ฅ ๐ โ๏ธ ๐, โฉ๏ธ โซ๏ธ โ๏ธ ๐ ๐ & ๐ โ๏ธ ๐ ๏ธ ๐โ๐ฆบ. , ๐ ๐ช ๐ ๐ ๐ผ & ๐ โซ๏ธ.
โช, ๐ ๐ญ ๐ธ, ๐ ๐ช ๐ โ๏ธ ๐ฝ ๐ฝ ๐ โณ.
Tip
๐ ๐ช ๐ ๏ธ ๐ญ โช๏ธโก๏ธ ๐ ๐ ๐ธ๐ฒ ๐ (๐ (๐) ๐ฝ), ๐ โ๏ธ ๐ ๐ข ๐ญ ๐ ๏ธ ๐ฝ, ๐ฌ ๐ FastAPI ๐.
๐ ๐ ๐ซ โ ๐ ๐ญ, ๐ ๐ ๐.
๐ & โ ๐ SQLAlchemy
¶
- ๐
SQLAlchemy
. - โ
metadata
๐. - โ ๐
notes
โ๏ธmetadata
๐.
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}
Tip
๐ ๐ ๐ ๐ ๐ ๐ ๐ธ๐ฒ ๐.
databases
๐ซ ๐จ ๐ณ ๐ฅ.
๐ & โ ๐ databases
¶
- ๐
databases
. - โ
DATABASE_URL
. - โ
database
๐.
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}
Tip
๐ฅ ๐ ๐ ๐ ๐ฝ (โ
โณ), ๐ ๐ ๐ช ๐ DATABASE_URL
.
โ ๐¶
๐ ๐ผ, ๐ฅ ๐ ๐ ๐ ๐ ๐, โ๏ธ ๐ญ, ๐ ๐ ๐ฒ ๐ โ ๐ซ โฎ๏ธ โ, ๐ ๏ธ โฎ๏ธ ๐ ๏ธ, โ๏ธ.
๐ฅ, ๐ ๐ ๐ ๐ ๐, โถ๏ธ๏ธ โญ โถ๏ธ ๐ FastAPI ๐ธ.
- โ
engine
. - โ ๐ ๐ โช๏ธโก๏ธ
metadata
๐.
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}
โ ๐ท¶
โ Pydantic ๐ท:
- ๐ โ (
NoteIn
). - ๐ ๐จ (
Note
).
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}
๐ ๐ซ Pydantic ๐ท, ๐ข ๐ฝ ๐ โ, ๐ป (๐), & โ (๐).
, ๐ ๐ ๐ช ๐ โซ๏ธ ๐ ๐ ๐ ๏ธ ๐ฉบ.
๐ & ๐¶
- โ ๐
FastAPI
๐ธ. - โ ๐ ๐โ๐ฆบ ๐ & ๐ โช๏ธโก๏ธ ๐ฝ.
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}
โ ๐¶
โ โก ๐ ๏ธ ๐ข โ ๐:
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}
Note
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ await
, โก ๐ ๏ธ ๐ข ๐ฃ โฎ๏ธ async
.
๐ response_model=List[Note]
¶
โซ๏ธ โ๏ธ typing.List
.
๐ ๐ (& โ, ๐ป, โฝ) ๐ข ๐ฝ, list
Note
โ.
โ ๐¶
โ โก ๐ ๏ธ ๐ข โ ๐:
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}
Note
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ await
, โก ๐ ๏ธ ๐ข ๐ฃ โฎ๏ธ async
.
๐ {**note.dict(), "id": last_record_id}
¶
note
Pydantic Note
๐.
note.dict()
๐จ dict
โฎ๏ธ ๐ฎ ๐ฝ, ๐ณ ๐:
{
"text": "Some note",
"completed": False,
}
โ๏ธ โซ๏ธ ๐ซ โ๏ธ id
๐.
๐ฅ โ ๐ dict
, ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ note.dict()
โฎ๏ธ:
{**note.dict()}
**note.dict()
"unpacks" the key value pairs directly, so, {**note.dict()}
would be, more or less, a copy of note.dict()
.
& โคด๏ธ, ๐ฅ โ ๐ ๐ dict
, โ โ1๏ธโฃ ๐-๐ฒ ๐ซ: "id": last_record_id
:
{**note.dict(), "id": last_record_id}
, ๐ ๐ ๐จ ๐ ๐ณ ๐:
{
"id": 1,
"text": "Some note",
"completed": False,
}
โ โซ๏ธ¶
๐ ๐ช ๐ ๐ ๐, & ๐ ๐ฉบ http://127.0.0.1:8000/docs.
๐ค ๐ ๐ช ๐ ๐ ๐ ๐ ๏ธ ๐ & ๐ โฎ๏ธ โซ๏ธ:
๐ โน¶
๐ ๐ช โ ๐
๐ encode/databases
๐ฎ ๐ ๐.