Metadata-Version: 2.3
Name: alch-dc
Version: 0.1.0
Summary: Add your description here
Author: minhdenthedev
Author-email: minhdenthedev <hminh100304@gmail.com>
Requires-Dist: sqlalchemy>=2.1.9
Requires-Python: >=3.12
Description-Content-Type: text/markdown

# `Alch-DC`: Converting between SQLAlchemy ORM models and dataclass objects

Tired of manually converting dataclass objects to SQLAlchemy ORM models and backward?
If yes, `alch-dc` (Alchemy - Dataclass) is for you. It validates and maps between the
two paradigms for you.

## Installation

If you're using `uv` then
```commandline
uv add alch-dc
```

or install with `pip`:
```commandline
pip install alch-dc
```


## Quick start

```python
from dataclasses import dataclass
from uuid import UUID, uuid4
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


# --- DATACLASS MODEL --- #
@dataclass
class Person:
    id: UUID
    name: str
    age: int


# --- SQLALCHEMY MODEL --- #
class Base(DeclarativeBase):
    pass


class PersonModel(Base):
    __tablename__ = "people"

    id: Mapped[UUID] = mapped_column(primary_key=True)
    name: Mapped[str]
    age: Mapped[int]


# --- ALCH-DC --- #    
from alch_dc import Converter

# Create a converter to use throughout the project
converter = Converter(Person, PersonModel)

# Map dataclass object to ORM object
p = Person(id=uuid4(), name="minhdenthedev", age=22)
orm_p = converter.to_orm(p)

# Map ORM object to dataclass object
orm_p = PersonModel(id=uuid4(), name="minhdenthedev", age=22)
p = converter.from_orm(orm_p)
```

## Best practices

When instantiating, the `converter` would validate the schema to make sure the two
classes are convertible. This operation is time-consuming and only needs to be checked
once, so it's best to instantiate the `Converter` class once and then use it throughout
your code.
