tmpl-fastapi/app/sql/crud.py
2023-02-27 19:05:04 +04:00

42 lines
822 B
Python

from typing import Optional, List
from sqlalchemy.orm import Session
from . import models
from . import schemas
# SELECT * from users LIMIT 1
def get_user(
db: Session,
user_id: int) -> Optional[models.User]:
return db \
.query(models.User) \
.filter(models.User.id == user_id) \
.first()
# SELECT * from users
def get_users(
db: Session,
skip: int = 0,
limit: int = 100) -> List[models.User]:
return db \
.query(models.User) \
.offset(skip) \
.limit(limit) \
.all()
# INSERT INTO users
def create_user(
db: Session,
user: schemas.UserCreate) -> models.User:
user_model = models.User(**user.dict())
db.add(user_model)
db.commit()
db.refresh(user_model)
return user_model