Package: edgy
Edgy🔥 The perfect ORM to work with complex databases 🔥Documentation:https://edgy.tarsild.io📚Source Code:https://github.com/tarsil/edgyMotivationFrom the same author ofSaffier, Edgy is also an ORM but different from its predecessor.
Saffier is more of a Generic ORM for probably 99.9% of every single day application and works
perfectly well with it, whereasEdgyis also that and more.Edgy comes with batteries included thanks to Pydantic so that means yourmodelsare 100%
Pydantic which also means you get all the benefits of the technology (like automatic validations...)
out of the box with the need of building independent schemas to validate those fields before injest
them into a database.Was it already mentioned that Edgy is extremely fast? Well, it is!Almost 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 strenghts 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 with the power ofPydantic.EdgyEdgy is some sort of a fork fromSaffierbut rewritten at its core fully in Pydantic 🔥.This was necessary because Saffier although serving 99.9% of the daily use cases, there was still
a great need to add automatic validations and performance, so instead of rewrittingSaffierand risking breaking existing use cases already in place, a brand new shiny ORM
came to be 😁.Edgy leverages the power ofPydanticwhile 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.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
integrity 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 Edgy 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_update,values,values_list,only,deferand a lot more.Relationships made it easy- Support forOneToOne,ForeignKeyandManyToManyin the same Django style.Constraints- Unique constraints through meta fields.Indexes- Unique indexes 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- Edgy 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.MigrationsSinceEdgy, likeSaffier, it is built on the top ofSQLAlchemy coreand 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 Edgy, simply run:$pipinstalledgyYou can pickup your favourite database driver by yourself or you can run:Postgres$pipinstalledgy[postgres]MySQL/MariaDB$pipinstalledgy[mysql]SQLite$pipinstalledgy[sqlite]MSSQL$pipinstalledgy[mssql]Quick StartThe following is an example how to start withEdgyand more details and examples can be
found throughout the documentation.Useipythonto run the following from the console, since it supportsawait.importedgyfromedgyimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")models=Registry(database=database)classUser(edgy.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:int=edgy.IntegerField(primary_key=True)is_active:bool=edgy.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()# noqaawaitUser.query.create(is_active=False)# noqauser=awaitUser.query.get(id=1)# noqaprint(user)# User(id=1)As stated in the example, if notablenameis provided in theMetaclass, Edgy automatically
generates the name of the table for you by pluralising the class name.NoteEdgy model declaration withtypingismerely visual. The validations of the fields
are not done by the typing of the attribute of the models but from theedgy fields.Which means you don't need to worry about thewrongtyping as long as you declare the correct
field type.So does that mean pydantic won't work if you don't declare the type? Absolutely not. Internally
Edgy runs those validations through the declared fields and the Pydantic validations are done
exactly in the same way you do a normal Pydantic model.Nothing to worry about!Let us see an example.With field typingimportedgyfromedgyimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")models=Registry(database=database)classUser(edgy.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:int=edgy.IntegerField(primary_key=True)is_active:bool=edgy.BooleanField(default=False)classMeta:registry=modelsWithout field typingimportedgyfromedgyimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")models=Registry(database=database)classUser(edgy.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=edgy.IntegerField(primary_key=True)is_active=edgy.BooleanField(default=False)classMeta:registry=modelsIt does not matter if you type or not, Edgy knows what and how to validate viaedgy fieldslikeIntegerFieldorBooleanFieldor any other field.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.EsmeraldecosystemThis does not mean that only works with Esmerald! Edgy is also framework agnostic but the author
of Edgy is the same of Saffier and Esmerald which makes it nicer to integrate directly with Esmerald.How could you integrateEdgywith Esmerald (or any other framework)?Let us see an example. Since Edgy is fully Pydantic that means we can perform tasks directly.fromesmeraldimportEsmerald,Gateway,postimportedgyfromedgy.testclientimportDatabaseTestClientasDatabasedatabase=Database("sqlite:///db.sqlite")models=edgy.Registry(database=database)classUser(edgy.Model):id:int=edgy.IntegerField(primary_key=True)name:str=edgy.CharField(max_length=100)email:str=edgy.EmailField(max_length=100)language:str=edgy.CharField(max_length=200,null=True)description:str=edgy.TextField(max_length=5000,null=True)classMeta:registry=models@post("/create")asyncdefcreate_user(data:User)->User:"""You can perform the same directly like thisas the validations for the model (nulls, mandatories, @field_validators)already did all the necessary checks defined by you."""user=awaitdata.save()returnuserapp=Esmerald(routes=[Gateway(handler=create_user)],on_startup=[database.connect],on_shutdown=[database.disconnect],)The response of the API/createshould have a format similar to this (assuming the post with the following payload as well):{"id":1,"name":"Edgy","email":"edgy@esmerald.dev","language":"EN","description":"A description",}All the examples of this documentation will be using field typing but it is up to you if you want to use them or not.Exciting!In the documentation we go deeper in explanations and examples, this was just to warm up. 😁