[yeonpil] fastapi study

‍서산·2022년 11월 18일
0

SQLAlchemy : 데이터베이스(MySQL,PostgreSQL,SQLite..)를 다루는 툴

SQLite : 파이썬에 내장된 데이터베이스 (간단하지만 가볍다)

ORM : 객체(클래스)를 데이터베이스 테이블과 매칭시켜주는 툴이다 (파이썬으로 데이터베이스를 다룰 수 있다는 뜻) Pydantic과 SQLAlchemy로 연동한다.

MODEL : 객체이다.

class User(Base):
    __tablename__ = "user" #테이블 이름이다.

    id = Column(Integer, primary_key=True, index=True)
    user_name = Column(String(32))
    user_email = Column(String(32))

schema : 프로그래밍 세계에서 보통 데이터의 구조와 명세이다. Pydantic을 사용해 API(검은 화면에 JSON형식의 데이터 나오는 것)의 출력 스키마를 생성한다.

class User(BaseModel):

    user_name : str
    user_email : str
    class Config:
        orm_mode = True #ORM JSONEncoder에 의해 자동으로 json으로 변환됨.

db.close : db를 닫지 않으면 계속해서 메모리를 차지한다. 그래서 잠시 사용했다가 중지하는 방식으로 사용한다. 이때 Session 개념이 등장.

def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close() 

Session : 쿠키와 비슷. 대신 쿠키는 로컬(클라이언트)에 저장되고 세션은 서버에 저장된다.

SessionLocal : sessionmaker를 통해 만든 로컬 세션.

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

0개의 댓글