Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge dataclasses with SQLModel, pydantic and sqlalchemy #4

Merged
merged 10 commits into from
Jan 8, 2025

Conversation

adsharma
Copy link
Owner

@adsharma adsharma commented Jan 7, 2025

In the last few years, dataclasses have become the standard way of creating Python objects.
They're convenient and fast because they don't validate at runtime and rely on static checkers.

On the other hand, pydantic and sqlalchemy are popular because they validate at runtime, do extensive meta programming.

This is an effort to have the cake and eat it too. You can use your dataclasses as usual. But with the new decorator, you get:

user.sqlmodel() # to generate a SQLModel object on the fly
user.__sqlmodel__ # the class corresponding to the object 

These provide the same functionality as SQLModel. Note that this doesn't include support for more advanced features like controlling the type used for the column in the database, foreign keys.

But hopefully it's a good start and we can keep adding support for more metadata in the future.

Benchmarks:

Creating 100000 User objects took 0.019142 seconds
Average time per object creation: 191.42 nanoseconds
Creating 100000 User SQL Model objects took 2.121499 seconds
Average time per object creation: 21214.99 nanoseconds

@adsharma adsharma merged commit c2a6c05 into main Jan 8, 2025
2 checks passed
@adsharma adsharma deleted the sqlmodel_bridge branch January 8, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant