-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
46 lines (34 loc) · 1.06 KB
/
example.py
File metadata and controls
46 lines (34 loc) · 1.06 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sqlalchemy
from fastapi import APIRouter, FastAPI
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from SimpleFastCrud import SimpleFastCrud
Base = declarative_base()
engine = create_engine('sqlite:///./test.db')
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
app = FastAPI()
api_router = APIRouter()
# Database dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Create Model
class Item(Base):
__tablename__ = 'items'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, index=True)
name = sqlalchemy.Column(sqlalchemy.String)
description = sqlalchemy.Column(sqlalchemy.String)
# Create tables
Base.metadata.create_all(bind=engine)
# Initialize CRUD
crud = SimpleFastCrud(api_router=api_router, get_db=get_db)
crud.add(Item)
# Include router in app
app.include_router(api_router)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)