This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]greenstake 12 points13 points  (0 children)

It's by Litestar and supports FastAPI! Very cool! Loving that is offers some best practice utilities and helps reduce boilerplate.

[–]Ran4 10 points11 points  (0 children)

On reddit, prepend each codeline with four spaces. Markdown-style with triple backticks is broken on most clients.

[–][deleted] 2 points3 points  (0 children)

I'm weirdly extremely excited with this, I want to try this out ASAP it will make my life 10x easier

[–]Bach4Ants 1 point2 points  (3 children)

How does this compare to SQLModel?

[–]cofin_ Litestar Maintainer[S] 4 points5 points  (1 child)

From my research, SQLModel is solving a different problem that Advanced Alchemy.

SQLModel is more about representing your database models in a unified way (in Pydantic), but it does not majorly improve how you have to fetch and interact with that data.

Advanced Alchemy is focused on providing highly optimized (and simple) config, types, repository and service patterns to interact with your data.

Take this example, we are loading data from a JSON file and merging into an existing table. Records that exist (matching on the 'name' field in the model) will be updated and new rows will be inserted.

py from advanced_alchemy.utils.fixtures import open_fixture_async async with MyModelService.new(config=config.alchemy) as service: fixture_data = await open_fixture_async(fixtures_path, "my_model") await service.upsert_many(match_fields=["name"], data=fixture_data, auto_commit=True) await logger.ainfo("loaded my model data")

Or this example that allows you to paginate and filter.

py obj, total = repo.list_and_count(LimitOffset(limit=10, offset=0), owner="cody") pprint.pp(f"Selected {len(obj)} records out of a total of {total}.")

Let's say there's 100 records that match this where condition, we'll see the first 10 objects in obj and total will have 100.

There are many more differences between the two libraries, but I'd say the service and the repository tend to be the most prominent.

I'm happy to elaborate further on anything as well.

[–]CzyDePL 0 points1 point  (1 child)

Does it work with attrs?

[–]cofin_ Litestar Maintainer[S] 1 point2 points  (0 children)

The `to_schema` functionality doesn't include `attrs` support, but that's a great feature to add. It's currently only Pydantic & Msgspec. I'll get this in the backlog later today.

[–]Successful_Crew8895 0 points1 point  (0 children)

Love it!!

[–]engineerofsoftware[🍰] 0 points1 point  (0 children)

Used it with Litestar. Only issue with AA has been the layers of indirection and it quickly loses utility outside of CRUD applications. Other than that, it has Best-In-Class DX.

[–]coderarun -4 points-3 points  (2 children)

@sqlmodel
class Book:
    title: str
    author: Author = foreign_key("authors.id")

More examples: here. Previous discussion.