Package: saffier
Saffier🚀 The only Async ORM you need. 🚀Documentation:https://saffier.tarsild.io📚Source Code:https://github.com/tarsil/saffierMotivationAlmost every project, in one way or another uses one (or many) databases. An ORM is simply an mapping
of the top of an existing database. ORM extends for Object Relational Mapping and bridges object-oriented
programs and relational databases.Two of the most well known ORMs are from Django and SQLAlchemy. Both have their own strengths and
weaknesses and specific use cases.This ORM is built on the top of SQLAlchemy core and aims to simplify the way the setup and queries
are done into a more common and familiar interface.Before continuingIf you are looking for something morePyadnticoriented where you can take literally advantage
of everything that Pydantic can offer, then instead of continuing with Saffier, have a look at
itsdata ORM brother,Edgy.Edgyits extremely powerful as well with a key difference that its100% Pydanticwhich means
you can leverage the technology if you already familiar with it.No worries, it is not completely different from Saffier, in fact, it was designed with the same principles and what it changes for you
are essentially theimports.Thinking of moving to Edgy?If you are considering of moving to Edgy but you don't want to be bothered about learning a new
tool and afraid of breaking changes, then fear not!Edgy was designed to also make your migration feel seemless, which means that essentially you would
only need to install it and change the imports in your project fromsaffiertoedgyand it should
work automatically for you.Even the documentation structure its almost the same, intentionally, so what you already know with
Saffier, you will know withEdgy.This discards any custom code done by you, of course.Why this ORMWhen investigating for a project different types of ORMs and compared them to each other, for a lot
of use cases, SQLAlchemyalways took the win but had an issue, the async support (which now there
are a few solutions). While doing the research I came acrossEncode ORM.The team is the same behind of Databases, Django Rest Framework, Starlette,
httpx and a lot more tools used by millions.There was one issue though, although ORM was doing a great familiar interface with SQLAlchemy and
providing the async solution needed, it was, by the time of this writing, incomplete and they
even stated that in the documentation and that is howSaffierwas born.Saffier uses some of the same concepts of ORM from Encode but rewritten inPydanticbut not all.SaffierSaffier is some sort of a fork fromEncode ORMbut rewritten at its
core and with a complete set of tools with a familiar interface to work with.
If you are familiar with Django, then you came for a treat 😄.Saffier leverages the power ofPydanticfor its fields while offering a friendly, familiar and
easy to use interface.This ORM was designed to be flexible and compatible with pretty much every ASGI framework, likeEsmerald, Starlette, FastAPI, Sanic, Quart... With simple pluggable
design thanks to its origins.Special notesSaffier couldn't exist withoutEncode ORMand the continous work
done by the amazing team behind it. For that reason, thank you!FeaturesWhile adopting a familiar interface, it offers some cool and powerful features on the top of
SQLAlchemy core.Key featuresModel inheritance- For those cases where you don't want to repeat yourself while maintaining
intregity of the models.Abstract classes- That's right! Sometimes you simply want a model that holds common fields
that doesn't need to created as a table in the database.Meta classes- If you are familiar with Django, this is not new to you and Saffier offers this
in the same fashion.Managers- Versatility at its core, you can have separate managers for your models to optimise
specific queries and querysets at ease.Filters- Filter by any field you want and need.Model operators- Classic operations such asupdate,get,get_or_none,bulk_create,bulk_updateand a lot more.Relationships made it easy- Support forOneToOneandForeignKeyin the same Django style.Constraints- Unique constraints through meta fields.Native test client- We all know how hard it can be to setup that client for those tests you
need so we give you already one.Multi-tenancy- Saffier supports multi-tenancy and even offers a possible solution to be used
out of the box if you don't want to waste time.And a lot more you can do here.MigrationsSinceSaffier, likeEncode ORM, is built on the top ofSQLAlchemy core, it brings its own native migration
system running on the top ofAlembicbut making it a
lot easier to use and more pleasant for you.Have a look at themigrationsfor more details.InstallationTo install Saffier, simply run:$pipinstallsaffierYou can pickup your favourite database driver by yourself or you can run:Postgres$pipinstallsaffier[postgres]MySQL/MariaDB$pipinstallsaffier[mysql]SQLite$pipinstallsaffier[sqlite]Quick StartThe following is an example how to start withSaffierand more details and examples can be
found throughout the documentation.Useipythonto run the following from the console, since it supportsawait.importsaffierfromsaffierimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")models=Registry(database=database)classUser(saffier.Model):"""The User model to be created in the database as a tableIf no name is provided the in Meta class, it will generatea "users" table for you."""id=saffier.IntegerField(primary_key=True)is_active=saffier.BooleanField(default=False)classMeta:registry=models# Create the db and tables# Don't use this in production! Use Alembic or any tool to manage# The migrations for youawaitmodels.create_all()awaitUser.query.create(is_active=False)user=awaitUser.query.get(id=1)print(user)# User(id=1)As stated in the example, if notablenameis provided in theMetaclass, Saffier automatically
generates the name of the table for you by pluralising the class name.Connect your applicationDo you want to have more complex structures and connect to your favourite framework? Have a look
atconnectionsto understand how to do it properly.Exciting!In the documentation we go deeper in explanations and examples, this was just to warm up. 😁