def create_app(): app, engine, Base, session = _setup_base_app() class Child(Base): __tablename__ = "child" id = Column(Integer, primary_key=True, index=True) parent_id = Column(Integer, ForeignKey("parent.id")) class Parent(Base): __tablename__ = "parent" id = Column(Integer, primary_key=True, index=True) children = relationship(Child, backref="parent", lazy="joined") Base.metadata.create_all(bind=engine) parent_router = SQLAlchemyCRUDRouter( schema=ParentSchema, create_schema=ParentCreate, db_model=Parent, db=session, prefix=PARENT_URL, ) child_router = SQLAlchemyCRUDRouter(schema=ChildSchema, db_model=Child, db=session, prefix=CHILD_URL) app.include_router(parent_router) app.include_router(child_router) return app
def sqlalchemy_implementation(): app, engine, Base, session = _setup_base_app() class PotatoModel(Base): __tablename__ = 'potatoes' id = Column(Integer, primary_key=True, index=True) thickness = Column(Float) mass = Column(Float) color = Column(String) type = Column(String) class CarrotModel(Base): __tablename__ = 'carrots' id = Column(Integer, primary_key=True, index=True) length = Column(Float) color = Column(String) Base.metadata.create_all(bind=engine) app.include_router( SQLAlchemyCRUDRouter(schema=Potato, db_model=PotatoModel, db=session, prefix='potato')) app.include_router( SQLAlchemyCRUDRouter(schema=Carrot, db_model=CarrotModel, db=session, create_schema=CarrotCreate, prefix='carrot')) return app
def create_crud_routes(self, engine): app = FastAPI() SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def get_db(): session = SessionLocal() try: yield session session.commit() finally: session.close() for name in self.pydantic_models.keys(): pydantic_model = self.pydantic_models[name] sqla_orm_model = sqla_core_to_orm(name, self.sqla_models[name]) router = SQLAlchemyCRUDRouter(schema=pydantic_model, db_model=sqla_orm_model, db=get_db, prefix=name) app.include_router(router) return app
def sqlalchemy_implementation(): app, engine, Base, session = _setup_base_app() class PotatoModel(Base): __tablename__ = "potatoes" id = Column(Integer, primary_key=True, index=True) thickness = Column(Float) mass = Column(Float) color = Column(String) type = Column(String) class CarrotModel(Base): __tablename__ = "carrots" id = Column(Integer, primary_key=True, index=True) length = Column(Float) color = Column(String) Base.metadata.create_all(bind=engine) app.include_router( SQLAlchemyCRUDRouter( schema=Potato, db_model=PotatoModel, db=session, prefix="potato", paginate=PAGINATION_SIZE, ) ) app.include_router( SQLAlchemyCRUDRouter( schema=Carrot, db_model=CarrotModel, db=session, create_schema=CarrotCreate, update_schema=CarrotUpdate, prefix="carrot", tags=CUSTOM_TAGS, ) ) return app
def sqlalchemy_implementation_string_pk(): app, engine, Base, session = _setup_base_app() class PotatoTypeModel(Base): __tablename__ = 'potato_type' name = Column(String, primary_key=True, index=True) origin = Column(String) Base.metadata.create_all(bind=engine) app.include_router( SQLAlchemyCRUDRouter(schema=PotatoType, create_schema=PotatoType, db_model=PotatoTypeModel, db=session, prefix='potato_type')) return app
def sqlalchemy_implementation_custom_ids(): app, engine, Base, session = _setup_base_app() class PotatoModel(Base): __tablename__ = "potatoes" potato_id = Column(Integer, primary_key=True, index=True) thickness = Column(Float) mass = Column(Float) color = Column(String) type = Column(String) Base.metadata.create_all(bind=engine) app.include_router( SQLAlchemyCRUDRouter(schema=CustomPotato, db_model=PotatoModel, db=session) ) return app
get_byname = crud.get_byName(db, name) return get_byname @app.get("/AllVisitsProfession/{profession}") async def get_byProfession(profession: str, db: Session = Depends(get_db)): get_byprofession = crud.get_byProfession(db, profession) return get_byprofession Base = declarative_base() Base.metadata.create_all(bind=engine) persons = SQLAlchemyCRUDRouter(schema=schemas.Person, create_schema=schemas.PersonCreate, db_model=models.PersonModel, db=get_db, prefix='Person') Tests = SQLAlchemyCRUDRouter(schema=schemas.Test, create_schema=schemas.Test, db_model=models.TestModel, db=get_db, prefix='Test') Visits = SQLAlchemyCRUDRouter(schema=schemas.VisitAll, create_schema=schemas.VisitCreate, db_model=models.VisitModel, db=get_db, prefix='Visit')
else: from datetime import timedelta access_token_expires = timedelta( minutes=ACCESS_TOKEN_EXPIRE_MINUTES) from sql_app.app_utils import create_access_token access_token = create_access_token( data={"sub": user.username}, expires_delta=access_token_expires) return {"access_token": access_token, "token_type": "Bearer"} @app.get("/getID/{username}") async def getID(username: str, db: Session = Depends(get_db)): db_user = crud.get_user_ID(db, username) return db_user Base = declarative_base() Base.metadata.create_all(bind=engine) persons = SQLAlchemyCRUDRouter(schema=schemas.Person, create_schema=schemas.PersonCreate, db_model=models.PersonModel, db=get_db, prefix='Person') app.include_router(persons) if __name__ == "__main__": uvicorn.run(app, host="127.0.0.1", port=8081)