Initial Commit
This commit is contained in:
0
db/__init__.py
Normal file
0
db/__init__.py
Normal file
12
db/base.py
Normal file
12
db/base.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
from config import POSTGRES_URL
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
engine = create_async_engine(POSTGRES_URL, echo=True)
|
||||
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
||||
4
db/models/__init__.py
Normal file
4
db/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .orders import Order, OrderItem, OrderStatus
|
||||
from .products import Product, Category
|
||||
|
||||
__all__ = ["Order", "OrderItem", "OrderStatus", "Product", "Category"]
|
||||
35
db/models/orders.py
Normal file
35
db/models/orders.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import enum
|
||||
from typing import List, Optional
|
||||
from sqlalchemy import ForeignKey, BigInteger
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import ENUM
|
||||
from db.base import Base
|
||||
|
||||
|
||||
class OrderStatus(enum.Enum):
|
||||
CREATED = "CREATED"
|
||||
PAID = "PAID"
|
||||
IN_PROGRESS = "IN_PROGRESS"
|
||||
CANCELLED = "CANCELLED"
|
||||
DRAFT = "DRAFT"
|
||||
|
||||
|
||||
class Order(Base):
|
||||
__tablename__ = "orders"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
customer: Mapped[Optional[int]] = mapped_column(BigInteger)
|
||||
status: Mapped[OrderStatus] = mapped_column(ENUM(OrderStatus, name="order_status"))
|
||||
|
||||
items: Mapped[List["OrderItem"]] = relationship("OrderItem", back_populates="order")
|
||||
|
||||
|
||||
class OrderItem(Base):
|
||||
__tablename__ = "order_items"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
|
||||
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
|
||||
quantity: Mapped[int] = mapped_column(default=1)
|
||||
|
||||
order: Mapped["Order"] = relationship("Order", back_populates="items")
|
||||
30
db/models/products.py
Normal file
30
db/models/products.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy import ForeignKey, Text, Index
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import TSVECTOR
|
||||
from db.base import Base
|
||||
|
||||
|
||||
class Category(Base):
|
||||
__tablename__ = "categories"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
name: Mapped[Optional[str]] = mapped_column(Text)
|
||||
parent_id: Mapped[Optional[int]] = mapped_column(ForeignKey("categories.id"))
|
||||
|
||||
parent: Mapped["Category"] = relationship("Category", remote_side=[id])
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
category_id: Mapped[Optional[int]] = mapped_column(ForeignKey("categories.id"))
|
||||
name: Mapped[Optional[str]] = mapped_column(Text)
|
||||
description: Mapped[Optional[str]] = mapped_column(Text)
|
||||
|
||||
search_vector: Mapped[str] = mapped_column(TSVECTOR)
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_products_search", "search_vector", postgresql_using="gin"),
|
||||
)
|
||||
Reference in New Issue
Block a user