Skip to main content

Tools to convert SQLAlchemy models to Pydantic models

Project description

Alchemista

Tools to generate Pydantic models from SQLAlchemy models.

Still experimental.

How to use

Quick example:

from alchemista import sqlalchemy_to_pydantic
from sqlalchemy import Column, Integer, String, create_engine, select
from sqlalchemy.orm import declarative_base, sessionmaker


Base = declarative_base()
engine = create_engine("sqlite://", echo=True)


class Person(Base):
    __tablename__ = "people"

    id = Column(Integer, primary_key=True)
    age = Column(Integer)
    name = Column(String, nullable=False)


PersonPydantic = sqlalchemy_to_pydantic(Person)


Base.metadata.create_all(engine)
SessionMaker = sessionmaker(bind=engine)

person = PersonPydantic.construct(name="Someone", age=25)
with SessionMaker.begin() as session:
    session.add(Person(**person.dict()))

with SessionMaker.begin() as session:
    person_db = session.execute(select(Person)).scalar_one()
    person = PersonPydantic.from_orm(person_db)
    print(person)

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alchemista-0.1.0.post2.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

alchemista-0.1.0.post2-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page