Skip to content

Commit

Permalink
Merge pull request #2 from milsman2/extend_models
Browse files Browse the repository at this point in the history
test sqlite db
  • Loading branch information
milsman2 authored Feb 13, 2024
2 parents 4848bfe + ba34b72 commit e1fdbe1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
Binary file added scraper/src/disc_golfers.db
Binary file not shown.
69 changes: 57 additions & 12 deletions scraper/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,67 @@
"""

from icecream import ic
from scraper.src.models import DiscGolfer, Base
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.future import select

from scraper.src.models import DiscGolfer


def run():
async def run():
ic()
disc_golfer = DiscGolfer(
id=2,
classification="Amateur",
last_name="Kane",
first_name="Miles",
career_events=25,
career_wins=4,
engine = create_async_engine(
"sqlite+aiosqlite:///scraper/src/disc_golfers.db",
echo=True,
)
ic(disc_golfer)

async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

async_session = async_sessionmaker(engine, expire_on_commit=False)

async with async_session() as session:
async with session.begin():
session.add_all(
[
DiscGolfer(
id=71050,
classification="Professional",
last_name="Barajas",
first_name="Matthew",
career_events=133,
career_wins=16,
rating=999,
),
DiscGolfer(
id=12626,
classification="Professional",
last_name="Feldberg",
first_name="David",
career_events=586,
career_wins=124,
rating=1018,
),
]
)
stmt = select(DiscGolfer)
# AsyncSession.execute() is used for 2.0 style ORM execution
# (same as the synchronous API).
result = await session.scalars(stmt)
# result is a buffered Result object.
for disc_golfer in result:
ic(disc_golfer)
# for streaming ORM results, AsyncSession.stream() may be used.
result = await session.stream(stmt)
# result is a streaming AsyncResult object.
async for disc_golfer in result.scalars():
ic(disc_golfer)
result = await session.scalars(select(DiscGolfer).order_by(DiscGolfer.id))
ic(result)
ic(result.first())
await session.commit()


if __name__ == "__main__":
run()
asyncio.run(run())
5 changes: 3 additions & 2 deletions scraper/src/models/disc_golfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
from __future__ import annotations

from sqlalchemy.orm import Mapped, mapped_column

from typing import Optional
from .base import Base


class DiscGolfer(Base):
__tablename__ = "disc_golfer"

id: Mapped[int] = mapped_column(primary_key=True)
id: Mapped[int] = mapped_column(primary_key=True, unique=True)
classification: Mapped[str]
last_name: Mapped[str]
first_name: Mapped[str]
career_events: Mapped[int]
career_wins: Mapped[int]
rating: Mapped[Optional[int]] = mapped_column(nullable=True)

0 comments on commit e1fdbe1

Please sign in to comment.