mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #112 from grillazz/sqlalchemy-2
apply sqlalchemy 2.0 declarative base and mapped columns
This commit is contained in:
commit
2345e06682
@ -19,7 +19,7 @@ depends_on = None
|
|||||||
def upgrade():
|
def upgrade():
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.create_table('user',
|
op.create_table('user',
|
||||||
sa.Column('uuid', sa.UUID(), nullable=False),
|
sa.Column('id', sa.UUID(), nullable=False),
|
||||||
sa.Column('email', sa.String(), nullable=False),
|
sa.Column('email', sa.String(), nullable=False),
|
||||||
sa.Column('first_name', sa.String(), nullable=False),
|
sa.Column('first_name', sa.String(), nullable=False),
|
||||||
sa.Column('last_name', sa.String(), nullable=False),
|
sa.Column('last_name', sa.String(), nullable=False),
|
||||||
|
@ -10,7 +10,7 @@ router = APIRouter(prefix="/v1/nonsense")
|
|||||||
|
|
||||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
||||||
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
||||||
nonsense = Nonsense(**payload.dict())
|
nonsense = Nonsense(**payload.model_dump())
|
||||||
await nonsense.save(db_session)
|
await nonsense.save(db_session)
|
||||||
return nonsense
|
return nonsense
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ router = APIRouter(prefix="/v1/stuff")
|
|||||||
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
|
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
|
||||||
async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)):
|
async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)):
|
||||||
try:
|
try:
|
||||||
stuff_instances = [Stuff(name=stuf.name, description=stuf.description) for stuf in payload]
|
stuff_instances = [Stuff(**stuff.model_dump()) for stuff in payload]
|
||||||
db_session.add_all(stuff_instances)
|
db_session.add_all(stuff_instances)
|
||||||
await db_session.commit()
|
await db_session.commit()
|
||||||
except SQLAlchemyError as ex:
|
except SQLAlchemyError as ex:
|
||||||
@ -28,7 +28,7 @@ async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSessio
|
|||||||
|
|
||||||
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
||||||
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
||||||
stuff = Stuff(name=payload.name, description=payload.description)
|
stuff = Stuff(**payload.model_dump())
|
||||||
await stuff.save(db_session)
|
await stuff.save(db_session)
|
||||||
return stuff
|
return stuff
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy import Column, String, select
|
from sqlalchemy import String, select
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.orm import mapped_column, Mapped
|
||||||
|
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
|
|
||||||
@ -11,9 +12,9 @@ from app.models.base import Base
|
|||||||
class Nonsense(Base):
|
class Nonsense(Base):
|
||||||
__tablename__ = "nonsense"
|
__tablename__ = "nonsense"
|
||||||
__table_args__ = ({"schema": "happy_hog"},)
|
__table_args__ = ({"schema": "happy_hog"},)
|
||||||
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
id: Mapped[uuid:UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||||
name = Column(String, nullable=False, primary_key=True, unique=True)
|
name: Mapped[str] = mapped_column(String, nullable=False, primary_key=True, unique=True)
|
||||||
description = Column(String, nullable=False)
|
description: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
@ -33,3 +34,4 @@ class Nonsense(Base):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from sqlalchemy import (
|
|||||||
select,
|
select,
|
||||||
)
|
)
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
||||||
|
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy import Column, String, select
|
from sqlalchemy import String, select
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.orm import mapped_column, Mapped
|
||||||
|
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
|
|
||||||
@ -11,9 +12,9 @@ from app.models.base import Base
|
|||||||
class Stuff(Base):
|
class Stuff(Base):
|
||||||
__tablename__ = "stuff"
|
__tablename__ = "stuff"
|
||||||
__table_args__ = ({"schema": "happy_hog"},)
|
__table_args__ = ({"schema": "happy_hog"},)
|
||||||
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||||
name = Column(String, nullable=False, primary_key=True, unique=True)
|
name: Mapped[str] = mapped_column(String, nullable=False, primary_key=True, unique=True)
|
||||||
description = Column(String, nullable=False)
|
description: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
|
@ -6,6 +6,7 @@ from passlib.context import CryptContext
|
|||||||
from sqlalchemy import Column, String, LargeBinary, select
|
from sqlalchemy import Column, String, LargeBinary, select
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.orm import mapped_column, Mapped
|
||||||
|
|
||||||
from app import config
|
from app import config
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
@ -15,23 +16,12 @@ global_settings = config.get_settings()
|
|||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
|
|
||||||
|
|
||||||
class User(Base): # type: ignore
|
class User(Base):
|
||||||
uuid = Column(
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
|
||||||
UUID(as_uuid=True),
|
email: Mapped[str] = mapped_column(String, nullable=False, unique=True)
|
||||||
unique=True,
|
first_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
default=uuid.uuid4,
|
last_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
primary_key=True,
|
_password: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||||||
)
|
|
||||||
email = Column(String, nullable=False)
|
|
||||||
first_name = Column(String, nullable=False)
|
|
||||||
last_name = Column(String, nullable=False)
|
|
||||||
_password = Column("password", LargeBinary, nullable=False)
|
|
||||||
|
|
||||||
def __init__(self, email: str, first_name: str, last_name: str, password: str = None):
|
|
||||||
self.email = email
|
|
||||||
self.first_name = first_name
|
|
||||||
self.last_name = last_name
|
|
||||||
self.password = password
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self):
|
||||||
|
@ -15,7 +15,7 @@ class UserSchema(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class UserResponse(BaseModel):
|
class UserResponse(BaseModel):
|
||||||
uuid: UUID = Field(title="User’s id", description="User’s id")
|
id: UUID = Field(title="User’s id", description="User’s id")
|
||||||
email: EmailStr = Field(title="User’s email", description="User’s email")
|
email: EmailStr = Field(title="User’s email", description="User’s email")
|
||||||
first_name: str = Field(title="User’s first name", description="User’s first name")
|
first_name: str = Field(title="User’s first name", description="User’s first name")
|
||||||
last_name: str = Field(title="User’s last name", description="User’s last name")
|
last_name: str = Field(title="User’s last name", description="User’s last name")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user