02: Changed setup for alembic and sqlAlchemy
This commit is contained in:
0
app/db/__init__.py
Normal file
0
app/db/__init__.py
Normal file
13
app/db/database.py
Normal file
13
app/db/database.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||
|
||||
engine = create_async_engine(
|
||||
DATABASE_URL,
|
||||
echo=True,
|
||||
future= True
|
||||
)
|
||||
5
app/db/models/base.py
Normal file
5
app/db/models/base.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
46
app/db/models/location.py
Normal file
46
app/db/models/location.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from sqlalchemy import String, Integer, Float, BigInteger
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from app.db.models.base import Base
|
||||
|
||||
|
||||
class Location(Base):
|
||||
|
||||
__tablename__ = "locations"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
||||
|
||||
# OSM Identifiers
|
||||
place_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
|
||||
osm_id: Mapped[int] = mapped_column(BigInteger)
|
||||
osm_type: Mapped[str] = mapped_column(String(20))
|
||||
|
||||
# Coordinates
|
||||
latitude: Mapped[float] = mapped_column(Float)
|
||||
longitude: Mapped[float] = mapped_column(Float)
|
||||
|
||||
# Address Info
|
||||
house_number: Mapped[str] = mapped_column(String(20), nullable=True)
|
||||
road: Mapped[str] = mapped_column(String(150))
|
||||
city: Mapped[str] = mapped_column(String(100))
|
||||
county: Mapped[str] = mapped_column(String(100))
|
||||
state: Mapped[str] = mapped_column(String(100))
|
||||
postcode: Mapped[str] = mapped_column(String(20))
|
||||
country: Mapped[str] = mapped_column(String(100))
|
||||
country_code: Mapped[str] = mapped_column(String(10))
|
||||
|
||||
# Display name
|
||||
display_name: Mapped[str] = mapped_column(String(300))
|
||||
|
||||
# Bounding Box
|
||||
bbox_lat_min: Mapped[float] = mapped_column(Float)
|
||||
bbox_lat_max: Mapped[float] = mapped_column(Float)
|
||||
bbox_lon_min: Mapped[float] = mapped_column(Float)
|
||||
bbox_lon_max: Mapped[float] = mapped_column(Float)
|
||||
|
||||
property = relationship(
|
||||
"Property",
|
||||
back_populates="location",
|
||||
uselist=False,
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
31
app/db/models/owner.py
Normal file
31
app/db/models/owner.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from sqlalchemy import String, Integer
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.db.models.base import Base
|
||||
|
||||
|
||||
class Owner(Base):
|
||||
|
||||
__tablename__ = "owners"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
|
||||
full_name: Mapped[str] = mapped_column(String(150))
|
||||
|
||||
phone_number: Mapped[str] = mapped_column(String(20), unique=True)
|
||||
email: Mapped[str] = mapped_column(String(150), unique=True)
|
||||
|
||||
occupation: Mapped[str] = mapped_column(String(100))
|
||||
annual_income_range: Mapped[str] = mapped_column(String(50))
|
||||
|
||||
willing_to_rent: Mapped[bool] = mapped_column(default=False)
|
||||
desired_rent_price: Mapped[int] = mapped_column(nullable=True)
|
||||
|
||||
willing_to_sell: Mapped[bool] = mapped_column(default=False)
|
||||
desired_sell_price: Mapped[int] = mapped_column(nullable=True)
|
||||
|
||||
# Relationship
|
||||
properties = relationship(
|
||||
"Property",
|
||||
back_populates="owner",
|
||||
cascade="all, delete"
|
||||
)
|
||||
65
app/db/models/property.py
Normal file
65
app/db/models/property.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from sqlalchemy import String, Integer, Float, Date, Boolean, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.db.models.base import Base
|
||||
from datetime import date
|
||||
|
||||
|
||||
class Property(Base):
|
||||
|
||||
__tablename__ = "properties"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
|
||||
# Basic Info
|
||||
property_type: Mapped[str] = mapped_column(String(50))
|
||||
built_up_area: Mapped[float] = mapped_column(Float)
|
||||
|
||||
bedrooms: Mapped[int] = mapped_column(Integer)
|
||||
bathrooms: Mapped[int] = mapped_column(Integer)
|
||||
|
||||
# Transaction Info
|
||||
purchase_date: Mapped[date] = mapped_column(Date)
|
||||
purchase_price: Mapped[float]
|
||||
|
||||
sale_listing_date: Mapped[date] = mapped_column(Date,nullable=True)
|
||||
sale_asking_price: Mapped[float] = mapped_column(nullable=True)
|
||||
|
||||
# Maintenance
|
||||
last_renovation_date: Mapped[date] = mapped_column(Date,nullable=True)
|
||||
renovation_description: Mapped[str] = mapped_column(String(255), nullable=True)
|
||||
|
||||
roof_repair_date: Mapped[date] = mapped_column(Date,nullable=True)
|
||||
roof_condition: Mapped[str] = mapped_column(String(50))
|
||||
|
||||
# Rental Status
|
||||
available_for_rent: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
expected_rent: Mapped[float] = mapped_column(nullable=True)
|
||||
rental_available_date: Mapped[date] = mapped_column(Date, nullable=True)
|
||||
|
||||
# -------------------------
|
||||
# Foreign Keys
|
||||
# -------------------------
|
||||
|
||||
owner_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("owners.id", ondelete="CASCADE")
|
||||
)
|
||||
|
||||
location_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("locations.id", ondelete="CASCADE"),
|
||||
unique=True # ONE property = ONE location
|
||||
)
|
||||
|
||||
# -------------------------
|
||||
# Relationships
|
||||
# -------------------------
|
||||
|
||||
owner = relationship(
|
||||
"Owner",
|
||||
back_populates="properties"
|
||||
)
|
||||
|
||||
location = relationship (
|
||||
"Location",
|
||||
back_populates="property",
|
||||
uselist=False,
|
||||
)
|
||||
13
app/db/session.py
Normal file
13
app/db/session.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
from app.db.database import engine
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
bind=engine,
|
||||
expire_on_commit=False,
|
||||
autoflush=False
|
||||
)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user