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
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for alchemista-0.1.0.post2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 931218ba53a0c9f50b791e1b39a9152bb5e251a99a7b2c1545959a3aae15201f |
|
MD5 | f6fecedae19ddd2be19c2559d0749e79 |
|
BLAKE2b-256 | b24ec0301e3bbb63b96a01f989ca24558c2e3b7aa9500fbfc6803ba535591fe0 |