Edgedb DTO is a library that generates DTO pydantic dataclasses from edgeql files.
Project description
Introduction
EdgeDB DTO is a CLI tool for Python that generates pydantic dataclasses for edgeql queries (EdgeDB query language).
Table of Contents
- Use Case Example
- Features
- Installation
- Full Simple Use Case Example
- CLI Options
- Dependencies
- Limitations
- Contributors
- License
Use Case Example
# main.py
from simple.queries.dto import InsertPerson
from edgedb import create_client
CLIENT = create_client()
def insert_three_people():
rob = InsertPerson(name="Rob")
alice = InsertPerson(name="Alice", best_freind=rob)
tom = InsertPerson(name="Tom", best_freind=alice)
tom.run(executor=CLIENT, transaction=True)
insert_three_people()
Features
- Mix and Match multiple Classes: Missed ORMs while using EdgeDB in Python? Look no further! edgedb-dto supports embedding DTOs in each other.
- Easy, intuitive and in 3 steps:
- Write .edgeql queries.
- run
edgedb-py && edgedb-dto
in the terminal - Import generated classes
- Validation using pydantic:
edgedb-dto
generates pydantic dataclasses that you can modify as you wish to validate your input data. - Asynchronous and Synchronous Support: Supports both asynchronous and synchronous database calls.
Installation
To install EdgeDB DTO from PyPI, run:
pip install edgedb-dto
Full Simple Use Case Example
Project Tree
Schema
We have the following simple schema:
# default.esdl
module default {
type Person{
required name: str{
constraint exclusive;
}
best_friend: Person;
}
}
EdgeQL Query
We also have the following simple .edgeql query:
# insert_person.edgeql
insert Person{
name := $name,
best_friend := $best_friend
} unless conflict on .name else (
select Person filter .name = $name
)
Generate DTO Classes
Let's do some Magic ✨
edgedb-py --no-skip-pydantic --target=blocking && edgedb-dto
PS :
To do async calls just remove --target option (run edgedb-py --help
for more context)
New Project Tree
Generated DTO Class
# insert_person_edgeql_dto.py
from ..insert_person_edgeql import InsertPersonResult, insert_person
@dataclass
class InsertPerson(DTO):
name: str
best_friend: DTO | uuid.UUID | None = None
def _query(self, **kwargs):
return insert_person(**kwargs)
def run(self, executor: Client, transaction: bool = False
) -> InsertPersonResult | None:
return self._run(executor, transaction)
DTO Classes Usecase
Now let's populate our database with ease:
# main.py
from simple.queries.dto import InsertPerson
from edgedb import create_client
CLIENT = create_client()
def insert_three_people():
rob = InsertPerson(name="Rob")
alice = InsertPerson(name="Alice", best_freind=rob)
tom = InsertPerson(name="Tom", best_freind=alice)
tom.run(executor=CLIENT, transaction=True)
insert_three_people()
By calling run
method on the tom
DTO class, we managed to insert all of our data into the database in a single transaction.
CLI Options
--source-directory
(-s
) [Optional argument]: Source directory containing EdgeQL files.--output-directory
(-o
) [Optional argument]: Output directory for generated DTO classes.
PS:
source-directory
is optional. Use it only if you want specific queries to be targeted otherwise, edgedb-dto will scan for the generated files.- Please note that it's better for type safety to not specify the
output-directory
and in that case, the files will be generated in a folder nameddto
in the parent folder of the EdgeQL Python files.
Dependencies
python
: version=^3.11 is required to run this library.jinja2
: For templating the DTO classes.pydantic
: For data validation and settings management.edgedb
: For interacting with the EdgeDB database.
Limitations
N + 1 Problem
edgedb-dto does not act like a query builder. So the more DTOs you link, the more back and forth calls are made to the database. In the last example, 3 queries to the database are made to complete the transaction.
Contributors
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for edgedb_dto-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac085324a879d12bc6b7ace3711e39b8fc6b239aa5d4484d3a7e0c30ac1235c3 |
|
MD5 | bdda5db6d240086a2a817a7db9eeef1d |
|
BLAKE2b-256 | e8a4df058f978d7eec4559bae96e482f0ecf4001c21097cb6e18a74658330a1e |